|
9 | 9 | use rustc_ast::ast::*; |
10 | 10 | use rustc_ast::attr; |
11 | 11 | use rustc_ast::expand::is_proc_macro_attr; |
| 12 | +use rustc_ast::ptr::P; |
12 | 13 | use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; |
13 | 14 | use rustc_ast::walk_list; |
14 | 15 | use rustc_ast_pretty::pprust; |
@@ -594,6 +595,54 @@ impl<'a> AstValidator<'a> { |
594 | 595 | .span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind)) |
595 | 596 | .emit(); |
596 | 597 | } |
| 598 | + |
| 599 | + fn deny_generic_params(&self, generics: &Generics, ident_span: Span) { |
| 600 | + if !generics.params.is_empty() { |
| 601 | + struct_span_err!( |
| 602 | + self.session, |
| 603 | + generics.span, |
| 604 | + E0567, |
| 605 | + "auto traits cannot have generic parameters" |
| 606 | + ) |
| 607 | + .span_label(ident_span, "auto trait cannot have generic parameters") |
| 608 | + .span_suggestion( |
| 609 | + generics.span, |
| 610 | + "remove the parameters", |
| 611 | + String::new(), |
| 612 | + Applicability::MachineApplicable, |
| 613 | + ) |
| 614 | + .emit(); |
| 615 | + } |
| 616 | + } |
| 617 | + |
| 618 | + fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) { |
| 619 | + if let [first @ last] | [first, .., last] = &bounds[..] { |
| 620 | + let span = first.span().to(last.span()); |
| 621 | + struct_span_err!(self.session, span, E0568, "auto traits cannot have super traits") |
| 622 | + .span_label(ident_span, "auto trait cannot have super traits") |
| 623 | + .span_suggestion( |
| 624 | + span, |
| 625 | + "remove the super traits", |
| 626 | + String::new(), |
| 627 | + Applicability::MachineApplicable, |
| 628 | + ) |
| 629 | + .emit(); |
| 630 | + } |
| 631 | + } |
| 632 | + |
| 633 | + fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) { |
| 634 | + if !trait_items.is_empty() { |
| 635 | + let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect(); |
| 636 | + struct_span_err!( |
| 637 | + self.session, |
| 638 | + spans, |
| 639 | + E0380, |
| 640 | + "auto traits cannot have methods or associated items" |
| 641 | + ) |
| 642 | + .span_label(ident_span, "auto trait cannot have items") |
| 643 | + .emit(); |
| 644 | + } |
| 645 | + } |
597 | 646 | } |
598 | 647 |
|
599 | 648 | fn validate_generic_param_order<'a>( |
@@ -881,57 +930,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> { |
881 | 930 | ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => { |
882 | 931 | if is_auto == IsAuto::Yes { |
883 | 932 | // Auto traits cannot have generics, super traits nor contain items. |
884 | | - if !generics.params.is_empty() { |
885 | | - let mut err = struct_span_err!( |
886 | | - self.session, |
887 | | - generics.span, |
888 | | - E0567, |
889 | | - "auto traits cannot have generic parameters" |
890 | | - ); |
891 | | - err.span_label( |
892 | | - item.ident.span, |
893 | | - "auto trait cannot have generic parameters", |
894 | | - ); |
895 | | - err.span_suggestion( |
896 | | - generics.span, |
897 | | - "remove the parameters", |
898 | | - String::new(), |
899 | | - Applicability::MachineApplicable, |
900 | | - ); |
901 | | - err.emit(); |
902 | | - } |
903 | | - if !bounds.is_empty() { |
904 | | - let span = match &bounds[..] { |
905 | | - [] => unreachable!(), |
906 | | - [single] => single.span(), |
907 | | - [first, .., last] => first.span().to(last.span()), |
908 | | - }; |
909 | | - let mut err = struct_span_err!( |
910 | | - self.session, |
911 | | - span, |
912 | | - E0568, |
913 | | - "auto traits cannot have super traits" |
914 | | - ); |
915 | | - err.span_label(item.ident.span, "auto trait cannot have super traits"); |
916 | | - err.span_suggestion( |
917 | | - span, |
918 | | - "remove the super traits", |
919 | | - String::new(), |
920 | | - Applicability::MachineApplicable, |
921 | | - ); |
922 | | - err.emit(); |
923 | | - } |
924 | | - if !trait_items.is_empty() { |
925 | | - let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect(); |
926 | | - struct_span_err!( |
927 | | - self.session, |
928 | | - spans, |
929 | | - E0380, |
930 | | - "auto traits cannot have methods or associated items" |
931 | | - ) |
932 | | - .span_label(item.ident.span, "auto trait cannot have items") |
933 | | - .emit(); |
934 | | - } |
| 933 | + self.deny_generic_params(generics, item.ident.span); |
| 934 | + self.deny_super_traits(bounds, item.ident.span); |
| 935 | + self.deny_items(trait_items, item.ident.span); |
935 | 936 | } |
936 | 937 | self.no_questions_in_bounds(bounds, "supertraits", true); |
937 | 938 |
|
|
0 commit comments