@@ -4,7 +4,7 @@ use crate::maybe_whole;
4
4
use crate :: ptr:: P ;
5
5
use crate :: ast:: {
6
6
self , DUMMY_NODE_ID , Ident , Attribute , AttrStyle ,
7
- Item , ItemKind , ImplItem , TraitItem , TraitItemKind ,
7
+ Item , ItemKind , ImplItem , ImplItemKind , TraitItem , TraitItemKind ,
8
8
UseTree , UseTreeKind , PathSegment ,
9
9
IsAuto , Constness , IsAsync , Unsafety , Defaultness ,
10
10
Visibility , VisibilityKind , Mutability , FnDecl , FnHeader , MethodSig , Block ,
@@ -727,16 +727,7 @@ impl<'a> Parser<'a> {
727
727
} ;
728
728
( name, kind, generics)
729
729
} 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 ( ) ?
740
731
} else {
741
732
let ( name, inner_attrs, generics, kind) = self . parse_impl_method ( & vis, at_end) ?;
742
733
attrs. extend ( inner_attrs) ;
@@ -785,12 +776,25 @@ impl<'a> Parser<'a> {
785
776
!self . is_keyword_ahead ( 1 , & [ kw:: Fn , kw:: Unsafe ] )
786
777
}
787
778
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
+
788
792
/// Parses a method or a macro invocation in a trait impl.
789
793
fn parse_impl_method (
790
794
& mut self ,
791
795
vis : & Visibility ,
792
796
at_end : & mut bool
793
- ) -> PResult < ' a , ( Ident , Vec < Attribute > , Generics , ast :: ImplItemKind ) > {
797
+ ) -> PResult < ' a , ( Ident , Vec < Attribute > , Generics , ImplItemKind ) > {
794
798
// FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction!
795
799
if let Some ( mac) = self . parse_assoc_macro_invoc ( "impl" , Some ( vis) , at_end) ? {
796
800
// method macro
@@ -935,38 +939,28 @@ impl<'a> Parser<'a> {
935
939
Ok ( item)
936
940
}
937
941
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 > {
941
947
let lo = self . token . span ;
942
948
self . eat_bad_pub ( ) ;
943
949
let ( name, kind, generics) = if self . eat_keyword ( kw:: Type ) {
944
950
self . parse_trait_item_assoc_ty ( ) ?
945
951
} 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 ( ) ?
959
953
} else if let Some ( mac) = self . parse_assoc_macro_invoc ( "trait" , None , & mut false ) ? {
960
954
// trait item macro.
961
- ( Ident :: invalid ( ) , ast :: TraitItemKind :: Macro ( mac) , Generics :: default ( ) )
955
+ ( Ident :: invalid ( ) , TraitItemKind :: Macro ( mac) , Generics :: default ( ) )
962
956
} else {
963
957
// This is somewhat dubious; We don't want to allow
964
958
// argument names to be left off if there is a definition...
965
959
//
966
960
// We don't allow argument names to be left off in edition 2018.
967
961
let ( ident, sig, generics) = self . parse_method_sig ( |t| t. span . rust_2018 ( ) ) ?;
968
962
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)
970
964
} ;
971
965
972
966
Ok ( TraitItem {
@@ -980,6 +974,20 @@ impl<'a> Parser<'a> {
980
974
} )
981
975
}
982
976
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
+
983
991
/// Parse the "body" of a method in a trait item definition.
984
992
/// This can either be `;` when there's no body,
985
993
/// or e.g. a block when the method is a provided one.
@@ -1020,8 +1028,7 @@ impl<'a> Parser<'a> {
1020
1028
/// Parses the following grammar:
1021
1029
///
1022
1030
/// 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 ) > {
1025
1032
let ident = self . parse_ident ( ) ?;
1026
1033
let mut generics = self . parse_generics ( ) ?;
1027
1034
0 commit comments