@@ -6,7 +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 , ImplItemKind , TraitItem , TraitItemKind , UseTree , UseTreeKind } ;
9
+ use syntax:: ast:: { ItemKind , ImplItem , TraitItem , TraitItemKind , UseTree , UseTreeKind } ;
10
10
use syntax:: ast:: { AssocItemKind } ;
11
11
use syntax:: ast:: { PathSegment , IsAuto , Constness , IsAsync , Unsafety , Defaultness , Extern , StrLit } ;
12
12
use syntax:: ast:: { Visibility , VisibilityKind , Mutability , FnHeader , ForeignItem , ForeignItemKind } ;
@@ -705,7 +705,7 @@ impl<'a> Parser<'a> {
705
705
// FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction!
706
706
( Ident :: invalid ( ) , ast:: ImplItemKind :: Macro ( mac) , Generics :: default ( ) )
707
707
} else {
708
- self . parse_impl_method ( at_end, & mut attrs) ?
708
+ self . parse_assoc_fn ( at_end, & mut attrs, |_| true ) ?
709
709
} ;
710
710
711
711
Ok ( ImplItem {
@@ -876,7 +876,11 @@ impl<'a> Parser<'a> {
876
876
// trait item macro.
877
877
( Ident :: invalid ( ) , TraitItemKind :: Macro ( mac) , Generics :: default ( ) )
878
878
} else {
879
- self . parse_trait_item_method ( at_end, & mut attrs) ?
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 ( ) ) ?
880
884
} ;
881
885
882
886
Ok ( TraitItem {
@@ -1823,48 +1827,40 @@ impl<'a> Parser<'a> {
1823
1827
} )
1824
1828
}
1825
1829
1826
- /// Parses a method or a macro invocation in a trait impl.
1827
- fn parse_impl_method (
1828
- & mut self ,
1829
- at_end : & mut bool ,
1830
- attrs : & mut Vec < Attribute > ,
1831
- ) -> PResult < ' a , ( Ident , ImplItemKind , Generics ) > {
1832
- let ( ident, sig, generics) = self . parse_method_sig ( |_| true ) ?;
1833
- let body = self . parse_trait_method_body ( at_end, attrs) ?;
1834
- Ok ( ( ident, ast:: ImplItemKind :: Method ( sig, body) , generics) )
1835
- }
1836
-
1837
- fn parse_trait_item_method (
1830
+ fn parse_assoc_fn (
1838
1831
& mut self ,
1839
1832
at_end : & mut bool ,
1840
1833
attrs : & mut Vec < Attribute > ,
1841
- ) -> PResult < ' a , ( Ident , TraitItemKind , Generics ) > {
1842
- // This is somewhat dubious; We don't want to allow
1843
- // argument names to be left off if there is a definition...
1844
- //
1845
- // We don't allow argument names to be left off in edition 2018.
1846
- let ( ident, sig, generics) = self . parse_method_sig ( |t| t. span . rust_2018 ( ) ) ?;
1847
- let body = self . parse_trait_method_body ( at_end, attrs) ?;
1848
- Ok ( ( ident, TraitItemKind :: Method ( sig, body) , generics) )
1834
+ is_name_required : fn ( & token:: Token ) -> bool ,
1835
+ ) -> PResult < ' a , ( Ident , AssocItemKind , Generics ) > {
1836
+ let header = self . parse_fn_front_matter ( ) ?;
1837
+ let ( ident, decl, generics) = self . parse_fn_sig ( ParamCfg {
1838
+ is_self_allowed : true ,
1839
+ allow_c_variadic : false ,
1840
+ is_name_required,
1841
+ } ) ?;
1842
+ let sig = FnSig { header, decl } ;
1843
+ let body = self . parse_assoc_fn_body ( at_end, attrs) ?;
1844
+ Ok ( ( ident, AssocItemKind :: Method ( sig, body) , generics) )
1849
1845
}
1850
1846
1851
- /// Parse the "body" of a method in a trait item definition.
1847
+ /// Parse the "body" of a method in an associated item definition.
1852
1848
/// This can either be `;` when there's no body,
1853
1849
/// or e.g. a block when the method is a provided one.
1854
- fn parse_trait_method_body (
1850
+ fn parse_assoc_fn_body (
1855
1851
& mut self ,
1856
1852
at_end : & mut bool ,
1857
1853
attrs : & mut Vec < Attribute > ,
1858
1854
) -> PResult < ' a , Option < P < Block > > > {
1859
1855
Ok ( match self . token . kind {
1860
1856
token:: Semi => {
1861
- debug ! ( "parse_trait_method_body (): parsing required method" ) ;
1857
+ debug ! ( "parse_assoc_fn_body (): parsing required method" ) ;
1862
1858
self . bump ( ) ;
1863
1859
* at_end = true ;
1864
1860
None
1865
1861
}
1866
1862
token:: OpenDelim ( token:: Brace ) => {
1867
- debug ! ( "parse_trait_method_body (): parsing provided method" ) ;
1863
+ debug ! ( "parse_assoc_fn_body (): parsing provided method" ) ;
1868
1864
* at_end = true ;
1869
1865
let ( inner_attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
1870
1866
attrs. extend ( inner_attrs. iter ( ) . cloned ( ) ) ;
@@ -1885,21 +1881,6 @@ impl<'a> Parser<'a> {
1885
1881
} )
1886
1882
}
1887
1883
1888
- /// Parse the "signature", including the identifier, parameters, and generics
1889
- /// of a method. The body is not parsed as that differs between `trait`s and `impl`s.
1890
- fn parse_method_sig (
1891
- & mut self ,
1892
- is_name_required : fn ( & token:: Token ) -> bool ,
1893
- ) -> PResult < ' a , ( Ident , FnSig , Generics ) > {
1894
- let header = self . parse_fn_front_matter ( ) ?;
1895
- let ( ident, decl, generics) = self . parse_fn_sig ( ParamCfg {
1896
- is_self_allowed : true ,
1897
- allow_c_variadic : false ,
1898
- is_name_required,
1899
- } ) ?;
1900
- Ok ( ( ident, FnSig { header, decl } , generics) )
1901
- }
1902
-
1903
1884
/// Parses all the "front matter" for a `fn` declaration, up to
1904
1885
/// and including the `fn` keyword:
1905
1886
///
0 commit comments