@@ -6,8 +6,7 @@ use crate::maybe_whole;
6
6
use rustc_errors:: { PResult , Applicability , DiagnosticBuilder , StashKey } ;
7
7
use rustc_error_codes:: * ;
8
8
use syntax:: ast:: { self , DUMMY_NODE_ID , Ident , Attribute , AttrKind , AttrStyle , AnonConst , Item } ;
9
- use syntax:: ast:: { ItemKind , ImplItem , TraitItem , TraitItemKind , UseTree , UseTreeKind } ;
10
- use syntax:: ast:: { AssocItemKind } ;
9
+ use syntax:: ast:: { AssocItem , AssocItemKind , ItemKind , UseTree , UseTreeKind } ;
11
10
use syntax:: ast:: { PathSegment , IsAuto , Constness , IsAsync , Unsafety , Defaultness , Extern , StrLit } ;
12
11
use syntax:: ast:: { Visibility , VisibilityKind , Mutability , FnHeader , ForeignItem , ForeignItemKind } ;
13
12
use syntax:: ast:: { Ty , TyKind , Generics , TraitRef , EnumDef , Variant , VariantData , StructField } ;
@@ -649,7 +648,7 @@ impl<'a> Parser<'a> {
649
648
Ok ( ( Ident :: invalid ( ) , item_kind, Some ( attrs) ) )
650
649
}
651
650
652
- fn parse_impl_body ( & mut self ) -> PResult < ' a , ( Vec < ImplItem > , Vec < Attribute > ) > {
651
+ fn parse_impl_body ( & mut self ) -> PResult < ' a , ( Vec < AssocItem > , Vec < Attribute > ) > {
653
652
self . expect ( & token:: OpenDelim ( token:: Brace ) ) ?;
654
653
let attrs = self . parse_inner_attributes ( ) ?;
655
654
@@ -671,12 +670,12 @@ impl<'a> Parser<'a> {
671
670
}
672
671
673
672
/// Parses an impl item.
674
- pub fn parse_impl_item ( & mut self , at_end : & mut bool ) -> PResult < ' a , ImplItem > {
673
+ pub fn parse_impl_item ( & mut self , at_end : & mut bool ) -> PResult < ' a , AssocItem > {
675
674
maybe_whole ! ( self , NtImplItem , |x| x) ;
676
675
let attrs = self . parse_outer_attributes ( ) ?;
677
676
let mut unclosed_delims = vec ! [ ] ;
678
677
let ( mut item, tokens) = self . collect_tokens ( |this| {
679
- let item = this. parse_impl_item_ ( at_end, attrs) ;
678
+ let item = this. parse_assoc_item ( at_end, attrs, |_| true ) ;
680
679
unclosed_delims. append ( & mut this. unclosed_delims ) ;
681
680
item
682
681
} ) ?;
@@ -689,38 +688,6 @@ impl<'a> Parser<'a> {
689
688
Ok ( item)
690
689
}
691
690
692
- fn parse_impl_item_ (
693
- & mut self ,
694
- at_end : & mut bool ,
695
- mut attrs : Vec < Attribute > ,
696
- ) -> PResult < ' a , ImplItem > {
697
- let lo = self . token . span ;
698
- let vis = self . parse_visibility ( FollowedByType :: No ) ?;
699
- let defaultness = self . parse_defaultness ( ) ;
700
- let ( name, kind, generics) = if self . eat_keyword ( kw:: Type ) {
701
- self . parse_assoc_ty ( ) ?
702
- } else if self . is_const_item ( ) {
703
- self . parse_assoc_const ( ) ?
704
- } else if let Some ( mac) = self . parse_assoc_macro_invoc ( "impl" , Some ( & vis) , at_end) ? {
705
- // FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction!
706
- ( Ident :: invalid ( ) , ast:: ImplItemKind :: Macro ( mac) , Generics :: default ( ) )
707
- } else {
708
- self . parse_assoc_fn ( at_end, & mut attrs, |_| true ) ?
709
- } ;
710
-
711
- Ok ( ImplItem {
712
- id : DUMMY_NODE_ID ,
713
- span : lo. to ( self . prev_span ) ,
714
- ident : name,
715
- attrs,
716
- vis,
717
- defaultness,
718
- generics,
719
- kind,
720
- tokens : None ,
721
- } )
722
- }
723
-
724
691
/// Parses defaultness (i.e., `default` or nothing).
725
692
fn parse_defaultness ( & mut self ) -> Defaultness {
726
693
// `pub` is included for better error messages
@@ -843,12 +810,19 @@ impl<'a> Parser<'a> {
843
810
}
844
811
845
812
/// Parses the items in a trait declaration.
846
- pub fn parse_trait_item ( & mut self , at_end : & mut bool ) -> PResult < ' a , TraitItem > {
813
+ pub fn parse_trait_item ( & mut self , at_end : & mut bool ) -> PResult < ' a , AssocItem > {
847
814
maybe_whole ! ( self , NtTraitItem , |x| x) ;
848
815
let attrs = self . parse_outer_attributes ( ) ?;
849
816
let mut unclosed_delims = vec ! [ ] ;
850
817
let ( mut item, tokens) = self . collect_tokens ( |this| {
851
- let item = this. parse_trait_item_ ( at_end, attrs) ;
818
+ // This is somewhat dubious; We don't want to allow
819
+ // param names to be left off if there is a definition...
820
+ //
821
+ // We don't allow param names to be left off in edition 2018.
822
+ //
823
+ // FIXME(Centril): bake closure into param parsing.
824
+ // Also add semantic restrictions and add tests.
825
+ let item = this. parse_assoc_item ( at_end, attrs, |t| t. span . rust_2018 ( ) ) ;
852
826
unclosed_delims. append ( & mut this. unclosed_delims ) ;
853
827
item
854
828
} ) ?;
@@ -860,30 +834,26 @@ impl<'a> Parser<'a> {
860
834
Ok ( item)
861
835
}
862
836
863
- fn parse_trait_item_ (
837
+ fn parse_assoc_item (
864
838
& mut self ,
865
839
at_end : & mut bool ,
866
840
mut attrs : Vec < Attribute > ,
867
- ) -> PResult < ' a , TraitItem > {
841
+ is_name_required : fn ( & token:: Token ) -> bool ,
842
+ ) -> PResult < ' a , AssocItem > {
868
843
let lo = self . token . span ;
869
844
let vis = self . parse_visibility ( FollowedByType :: No ) ?;
870
845
let defaultness = self . parse_defaultness ( ) ;
871
846
let ( name, kind, generics) = if self . eat_keyword ( kw:: Type ) {
872
847
self . parse_assoc_ty ( ) ?
873
848
} else if self . is_const_item ( ) {
874
849
self . parse_assoc_const ( ) ?
875
- } else if let Some ( mac) = self . parse_assoc_macro_invoc ( "trait" , None , & mut false ) ? {
876
- // trait item macro.
877
- ( Ident :: invalid ( ) , TraitItemKind :: Macro ( mac) , Generics :: default ( ) )
850
+ } else if let Some ( mac) = self . parse_assoc_macro_invoc ( "associated" , Some ( & vis) , at_end) ? {
851
+ ( Ident :: invalid ( ) , AssocItemKind :: Macro ( mac) , Generics :: default ( ) )
878
852
} else {
879
- // This is somewhat dubious; We don't want to allow
880
- // param names to be left off if there is a definition...
881
- //
882
- // We don't allow param names to be left off in edition 2018.
883
- self . parse_assoc_fn ( at_end, & mut attrs, |t| t. span . rust_2018 ( ) ) ?
853
+ self . parse_assoc_fn ( at_end, & mut attrs, is_name_required) ?
884
854
} ;
885
855
886
- Ok ( TraitItem {
856
+ Ok ( AssocItem {
887
857
id : DUMMY_NODE_ID ,
888
858
span : lo. to ( self . prev_span ) ,
889
859
ident : name,
0 commit comments