@@ -4,7 +4,7 @@ use crate::maybe_whole;
44use crate :: ptr:: P ;
55use crate :: ast:: {
66 self , DUMMY_NODE_ID , Ident , Attribute , AttrStyle ,
7- Item , ItemKind , ImplItem , TraitItem , TraitItemKind ,
7+ Item , ItemKind , ImplItem , ImplItemKind , TraitItem , TraitItemKind ,
88 UseTree , UseTreeKind , PathSegment ,
99 IsAuto , Constness , IsAsync , Unsafety , Defaultness ,
1010 Visibility , VisibilityKind , Mutability , FnDecl , FnHeader , MethodSig , Block ,
@@ -727,16 +727,7 @@ impl<'a> Parser<'a> {
727727 } ;
728728 ( name, kind, generics)
729729 } else if self . is_const_item ( ) {
730- // This parses the grammar:
731- // ImplItemConst = "const" Ident ":" Ty "=" Expr ";"
732- self . expect_keyword ( kw:: Const ) ?;
733- let name = self . parse_ident ( ) ?;
734- self . expect ( & token:: Colon ) ?;
735- let typ = self . parse_ty ( ) ?;
736- self . expect ( & token:: Eq ) ?;
737- let expr = self . parse_expr ( ) ?;
738- self . expect ( & token:: Semi ) ?;
739- ( name, ast:: ImplItemKind :: Const ( typ, expr) , Generics :: default ( ) )
730+ self . parse_impl_const ( ) ?
740731 } else {
741732 let ( name, inner_attrs, generics, kind) = self . parse_impl_method ( & vis, at_end) ?;
742733 attrs. extend ( inner_attrs) ;
@@ -785,12 +776,25 @@ impl<'a> Parser<'a> {
785776 !self . is_keyword_ahead ( 1 , & [ kw:: Fn , kw:: Unsafe ] )
786777 }
787778
779+ /// This parses the grammar:
780+ /// ImplItemConst = "const" Ident ":" Ty "=" Expr ";"
781+ fn parse_impl_const ( & mut self ) -> PResult < ' a , ( Ident , ImplItemKind , Generics ) > {
782+ self . expect_keyword ( kw:: Const ) ?;
783+ let name = self . parse_ident ( ) ?;
784+ self . expect ( & token:: Colon ) ?;
785+ let typ = self . parse_ty ( ) ?;
786+ self . expect ( & token:: Eq ) ?;
787+ let expr = self . parse_expr ( ) ?;
788+ self . expect ( & token:: Semi ) ?;
789+ Ok ( ( name, ImplItemKind :: Const ( typ, expr) , Generics :: default ( ) ) )
790+ }
791+
788792 /// Parses a method or a macro invocation in a trait impl.
789793 fn parse_impl_method (
790794 & mut self ,
791795 vis : & Visibility ,
792796 at_end : & mut bool
793- ) -> PResult < ' a , ( Ident , Vec < Attribute > , Generics , ast :: ImplItemKind ) > {
797+ ) -> PResult < ' a , ( Ident , Vec < Attribute > , Generics , ImplItemKind ) > {
794798 // FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction!
795799 if let Some ( mac) = self . parse_assoc_macro_invoc ( "impl" , Some ( vis) , at_end) ? {
796800 // method macro
@@ -935,38 +939,28 @@ impl<'a> Parser<'a> {
935939 Ok ( item)
936940 }
937941
938- fn parse_trait_item_ ( & mut self ,
939- at_end : & mut bool ,
940- mut attrs : Vec < Attribute > ) -> PResult < ' a , TraitItem > {
942+ fn parse_trait_item_ (
943+ & mut self ,
944+ at_end : & mut bool ,
945+ mut attrs : Vec < Attribute > ,
946+ ) -> PResult < ' a , TraitItem > {
941947 let lo = self . token . span ;
942948 self . eat_bad_pub ( ) ;
943949 let ( name, kind, generics) = if self . eat_keyword ( kw:: Type ) {
944950 self . parse_trait_item_assoc_ty ( ) ?
945951 } else if self . is_const_item ( ) {
946- self . expect_keyword ( kw:: Const ) ?;
947- let ident = self . parse_ident ( ) ?;
948- self . expect ( & token:: Colon ) ?;
949- let ty = self . parse_ty ( ) ?;
950- let default = if self . eat ( & token:: Eq ) {
951- let expr = self . parse_expr ( ) ?;
952- self . expect ( & token:: Semi ) ?;
953- Some ( expr)
954- } else {
955- self . expect ( & token:: Semi ) ?;
956- None
957- } ;
958- ( ident, TraitItemKind :: Const ( ty, default) , Generics :: default ( ) )
952+ self . parse_trait_item_const ( ) ?
959953 } else if let Some ( mac) = self . parse_assoc_macro_invoc ( "trait" , None , & mut false ) ? {
960954 // trait item macro.
961- ( Ident :: invalid ( ) , ast :: TraitItemKind :: Macro ( mac) , Generics :: default ( ) )
955+ ( Ident :: invalid ( ) , TraitItemKind :: Macro ( mac) , Generics :: default ( ) )
962956 } else {
963957 // This is somewhat dubious; We don't want to allow
964958 // argument names to be left off if there is a definition...
965959 //
966960 // We don't allow argument names to be left off in edition 2018.
967961 let ( ident, sig, generics) = self . parse_method_sig ( |t| t. span . rust_2018 ( ) ) ?;
968962 let body = self . parse_trait_method_body ( at_end, & mut attrs) ?;
969- ( ident, ast :: TraitItemKind :: Method ( sig, body) , generics)
963+ ( ident, TraitItemKind :: Method ( sig, body) , generics)
970964 } ;
971965
972966 Ok ( TraitItem {
@@ -980,6 +974,20 @@ impl<'a> Parser<'a> {
980974 } )
981975 }
982976
977+ fn parse_trait_item_const ( & mut self ) -> PResult < ' a , ( Ident , TraitItemKind , Generics ) > {
978+ self . expect_keyword ( kw:: Const ) ?;
979+ let ident = self . parse_ident ( ) ?;
980+ self . expect ( & token:: Colon ) ?;
981+ let ty = self . parse_ty ( ) ?;
982+ let default = if self . eat ( & token:: Eq ) {
983+ Some ( self . parse_expr ( ) ?)
984+ } else {
985+ None
986+ } ;
987+ self . expect ( & token:: Semi ) ?;
988+ Ok ( ( ident, TraitItemKind :: Const ( ty, default) , Generics :: default ( ) ) )
989+ }
990+
983991 /// Parse the "body" of a method in a trait item definition.
984992 /// This can either be `;` when there's no body,
985993 /// or e.g. a block when the method is a provided one.
@@ -1020,8 +1028,7 @@ impl<'a> Parser<'a> {
10201028 /// Parses the following grammar:
10211029 ///
10221030 /// TraitItemAssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
1023- fn parse_trait_item_assoc_ty ( & mut self )
1024- -> PResult < ' a , ( Ident , TraitItemKind , Generics ) > {
1031+ fn parse_trait_item_assoc_ty ( & mut self ) -> PResult < ' a , ( Ident , TraitItemKind , Generics ) > {
10251032 let ident = self . parse_ident ( ) ?;
10261033 let mut generics = self . parse_generics ( ) ?;
10271034
0 commit comments