@@ -16,7 +16,7 @@ use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, Vari
16
16
use rustc_ast:: { FnHeader , ForeignItem , Path , PathSegment , Visibility , VisibilityKind } ;
17
17
use rustc_ast:: { MacCall , MacDelimiter } ;
18
18
use rustc_ast_pretty:: pprust;
19
- use rustc_errors:: { struct_span_err, Applicability , IntoDiagnostic , PResult , StashKey } ;
19
+ use rustc_errors:: { error_code , struct_span_err, Applicability , IntoDiagnostic , PResult , StashKey } ;
20
20
use rustc_span:: edition:: Edition ;
21
21
use rustc_span:: lev_distance:: lev_distance;
22
22
use rustc_span:: source_map:: { self , Span } ;
@@ -870,14 +870,39 @@ impl<'a> Parser<'a> {
870
870
} else {
871
871
// It's a normal trait.
872
872
generics. where_clause = self . parse_where_clause ( ) ?;
873
- let items = self . parse_item_list ( attrs, |p| p. parse_trait_item ( ForceCollect :: No ) ) ?;
873
+ let mut items =
874
+ self . parse_item_list ( attrs, |p| p. parse_trait_item ( ForceCollect :: No ) ) ?;
875
+ if items. len ( ) > 0 && is_auto == IsAuto :: Yes {
876
+ self . deny_items ( & items, ident. span ) ;
877
+ items = vec ! [ ] ;
878
+ }
874
879
Ok ( (
875
880
ident,
876
881
ItemKind :: Trait ( Box :: new ( Trait { is_auto, unsafety, generics, bounds, items } ) ) ,
877
882
) )
878
883
}
879
884
}
880
885
886
+ fn deny_items ( & self , items : & Vec < P < Item < AssocItemKind > > > , ident_span : Span ) {
887
+ let spans: Vec < _ > = items. iter ( ) . map ( |i| i. ident . span ) . collect ( ) ;
888
+ let total_span = items. first ( ) . unwrap ( ) . span . to ( items. last ( ) . unwrap ( ) . span ) ;
889
+ self . sess
890
+ . span_diagnostic
891
+ . struct_span_err_with_code (
892
+ spans,
893
+ "auto traits cannot have associated items" ,
894
+ error_code ! ( E0753 ) ,
895
+ )
896
+ . span_suggestion (
897
+ total_span,
898
+ "remove these associated items" ,
899
+ "" ,
900
+ Applicability :: MachineApplicable ,
901
+ )
902
+ . span_label ( ident_span, "auto trait cannot have associated items" )
903
+ . emit ( ) ;
904
+ }
905
+
881
906
pub fn parse_impl_item (
882
907
& mut self ,
883
908
force_collect : ForceCollect ,
0 commit comments