@@ -1715,8 +1715,6 @@ impl<'a> Parser<'a> {
17151715
17161716/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
17171717pub ( super ) struct ParamCfg {
1718- /// Is `self` is allowed as the first parameter?
1719- pub is_self_allowed : bool ,
17201718 /// `is_name_required` decides if, per-parameter,
17211719 /// the parameter must have a pattern or just a type.
17221720 pub is_name_required : fn ( & token:: Token ) -> bool ,
@@ -1732,8 +1730,8 @@ impl<'a> Parser<'a> {
17321730 attrs : Vec < Attribute > ,
17331731 header : FnHeader ,
17341732 ) -> PResult < ' a , Option < P < Item > > > {
1735- let ( ident , decl , generics ) =
1736- self . parse_fn_sig ( ParamCfg { is_self_allowed : false , is_name_required : |_| true } ) ?;
1733+ let cfg = ParamCfg { is_name_required : |_| true } ;
1734+ let ( ident , decl , generics ) = self . parse_fn_sig ( & cfg ) ?;
17371735 let ( inner_attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
17381736 let kind = ItemKind :: Fn ( FnSig { decl, header } , generics, body) ;
17391737 self . mk_item_with_info ( attrs, lo, vis, ( ident, kind, Some ( inner_attrs) ) )
@@ -1747,20 +1745,13 @@ impl<'a> Parser<'a> {
17471745 attrs : Vec < Attribute > ,
17481746 extern_sp : Span ,
17491747 ) -> PResult < ' a , P < ForeignItem > > {
1748+ let cfg = ParamCfg { is_name_required : |_| true } ;
17501749 self . expect_keyword ( kw:: Fn ) ?;
1751- let ( ident, decl, generics) =
1752- self . parse_fn_sig ( ParamCfg { is_self_allowed : false , is_name_required : |_| true } ) ?;
1750+ let ( ident, decl, generics) = self . parse_fn_sig ( & cfg) ?;
17531751 let span = lo. to ( self . token . span ) ;
17541752 self . parse_semi_or_incorrect_foreign_fn_body ( & ident, extern_sp) ?;
1755- Ok ( P ( ast:: ForeignItem {
1756- ident,
1757- attrs,
1758- kind : ForeignItemKind :: Fn ( decl, generics) ,
1759- id : DUMMY_NODE_ID ,
1760- span,
1761- vis,
1762- tokens : None ,
1763- } ) )
1753+ let kind = ForeignItemKind :: Fn ( decl, generics) ;
1754+ Ok ( P ( ast:: ForeignItem { ident, attrs, kind, id : DUMMY_NODE_ID , span, vis, tokens : None } ) )
17641755 }
17651756
17661757 fn parse_assoc_fn (
@@ -1770,8 +1761,7 @@ impl<'a> Parser<'a> {
17701761 is_name_required : fn ( & token:: Token ) -> bool ,
17711762 ) -> PResult < ' a , ( Ident , AssocItemKind , Generics ) > {
17721763 let header = self . parse_fn_front_matter ( ) ?;
1773- let ( ident, decl, generics) =
1774- self . parse_fn_sig ( ParamCfg { is_self_allowed : true , is_name_required } ) ?;
1764+ let ( ident, decl, generics) = self . parse_fn_sig ( & ParamCfg { is_name_required } ) ?;
17751765 let sig = FnSig { header, decl } ;
17761766 let body = self . parse_assoc_fn_body ( at_end, attrs) ?;
17771767 Ok ( ( ident, AssocItemKind :: Fn ( sig, body) , generics) )
@@ -1847,7 +1837,7 @@ impl<'a> Parser<'a> {
18471837 }
18481838
18491839 /// Parse the "signature", including the identifier, parameters, and generics of a function.
1850- fn parse_fn_sig ( & mut self , cfg : ParamCfg ) -> PResult < ' a , ( Ident , P < FnDecl > , Generics ) > {
1840+ fn parse_fn_sig ( & mut self , cfg : & ParamCfg ) -> PResult < ' a , ( Ident , P < FnDecl > , Generics ) > {
18511841 let ident = self . parse_ident ( ) ?;
18521842 let mut generics = self . parse_generics ( ) ?;
18531843 let decl = self . parse_fn_decl ( cfg, true ) ?;
@@ -1858,7 +1848,7 @@ impl<'a> Parser<'a> {
18581848 /// Parses the parameter list and result type of a function declaration.
18591849 pub ( super ) fn parse_fn_decl (
18601850 & mut self ,
1861- cfg : ParamCfg ,
1851+ cfg : & ParamCfg ,
18621852 ret_allow_plus : bool ,
18631853 ) -> PResult < ' a , P < FnDecl > > {
18641854 Ok ( P ( FnDecl {
@@ -1868,11 +1858,11 @@ impl<'a> Parser<'a> {
18681858 }
18691859
18701860 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1871- fn parse_fn_params ( & mut self , mut cfg : ParamCfg ) -> PResult < ' a , Vec < Param > > {
1872- let is_trait_item = cfg . is_self_allowed ;
1873- // Parse the arguments, starting out with `self` being possibly allowed...
1861+ fn parse_fn_params ( & mut self , cfg : & ParamCfg ) -> PResult < ' a , Vec < Param > > {
1862+ let mut first_param = true ;
1863+ // Parse the arguments, starting out with `self` being allowed...
18741864 let ( mut params, _) = self . parse_paren_comma_seq ( |p| {
1875- let param = p. parse_param_general ( & cfg, is_trait_item ) . or_else ( |mut e| {
1865+ let param = p. parse_param_general ( & cfg, first_param ) . or_else ( |mut e| {
18761866 e. emit ( ) ;
18771867 let lo = p. prev_span ;
18781868 // Skip every token until next possible arg or end.
@@ -1881,29 +1871,25 @@ impl<'a> Parser<'a> {
18811871 Ok ( dummy_arg ( Ident :: new ( kw:: Invalid , lo. to ( p. prev_span ) ) ) )
18821872 } ) ;
18831873 // ...now that we've parsed the first argument, `self` is no longer allowed.
1884- cfg . is_self_allowed = false ;
1874+ first_param = false ;
18851875 param
18861876 } ) ?;
18871877 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
18881878 self . deduplicate_recovered_params_names ( & mut params) ;
18891879 Ok ( params)
18901880 }
18911881
1892- /// Skips unexpected attributes and doc comments in this position and emits an appropriate
1893- /// error.
1894- /// This version of parse param doesn't necessarily require identifier names .
1895- fn parse_param_general ( & mut self , cfg : & ParamCfg , is_trait_item : bool ) -> PResult < ' a , Param > {
1882+ /// Parses a single function parameter.
1883+ ///
1884+ /// - `self` is syntactically allowed when `first_param` holds .
1885+ fn parse_param_general ( & mut self , cfg : & ParamCfg , first_param : bool ) -> PResult < ' a , Param > {
18961886 let lo = self . token . span ;
18971887 let attrs = self . parse_outer_attributes ( ) ?;
18981888
18991889 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
19001890 if let Some ( mut param) = self . parse_self_param ( ) ? {
19011891 param. attrs = attrs. into ( ) ;
1902- return if cfg. is_self_allowed {
1903- Ok ( param)
1904- } else {
1905- self . recover_bad_self_param ( param, is_trait_item)
1906- } ;
1892+ return if first_param { Ok ( param) } else { self . recover_bad_self_param ( param) } ;
19071893 }
19081894
19091895 let is_name_required = match self . token . kind {
@@ -1915,13 +1901,9 @@ impl<'a> Parser<'a> {
19151901
19161902 let pat = self . parse_fn_param_pat ( ) ?;
19171903 if let Err ( mut err) = self . expect ( & token:: Colon ) {
1918- return if let Some ( ident) = self . parameter_without_type (
1919- & mut err,
1920- pat,
1921- is_name_required,
1922- cfg. is_self_allowed ,
1923- is_trait_item,
1924- ) {
1904+ return if let Some ( ident) =
1905+ self . parameter_without_type ( & mut err, pat, is_name_required, first_param)
1906+ {
19251907 err. emit ( ) ;
19261908 Ok ( dummy_arg ( ident) )
19271909 } else {
@@ -1975,8 +1957,6 @@ impl<'a> Parser<'a> {
19751957 }
19761958
19771959 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1978- ///
1979- /// See `parse_self_param_with_attrs` to collect attributes.
19801960 fn parse_self_param ( & mut self ) -> PResult < ' a , Option < Param > > {
19811961 // Extract an identifier *after* having confirmed that the token is one.
19821962 let expect_self_ident = |this : & mut Self | {
0 commit comments