@@ -1715,8 +1715,9 @@ 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 ,
1718+ /// Is `self` is *semantically* allowed as the first parameter?
1719+ /// This is only used for diagnostics.
1720+ pub in_assoc_item : bool ,
17201721 /// `is_name_required` decides if, per-parameter,
17211722 /// the parameter must have a pattern or just a type.
17221723 pub is_name_required : fn ( & token:: Token ) -> bool ,
@@ -1732,8 +1733,8 @@ impl<'a> Parser<'a> {
17321733 attrs : Vec < Attribute > ,
17331734 header : FnHeader ,
17341735 ) -> PResult < ' a , Option < P < Item > > > {
1735- let ( ident , decl , generics ) =
1736- self . parse_fn_sig ( ParamCfg { is_self_allowed : false , is_name_required : |_| true } ) ?;
1736+ let cfg = ParamCfg { in_assoc_item : false , is_name_required : |_| true } ;
1737+ let ( ident , decl , generics ) = self . parse_fn_sig ( & cfg ) ?;
17371738 let ( inner_attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
17381739 let kind = ItemKind :: Fn ( FnSig { decl, header } , generics, body) ;
17391740 self . mk_item_with_info ( attrs, lo, vis, ( ident, kind, Some ( inner_attrs) ) )
@@ -1747,20 +1748,13 @@ impl<'a> Parser<'a> {
17471748 attrs : Vec < Attribute > ,
17481749 extern_sp : Span ,
17491750 ) -> PResult < ' a , P < ForeignItem > > {
1751+ let cfg = ParamCfg { in_assoc_item : false , is_name_required : |_| true } ;
17501752 self . expect_keyword ( kw:: Fn ) ?;
1751- let ( ident, decl, generics) =
1752- self . parse_fn_sig ( ParamCfg { is_self_allowed : false , is_name_required : |_| true } ) ?;
1753+ let ( ident, decl, generics) = self . parse_fn_sig ( & cfg) ?;
17531754 let span = lo. to ( self . token . span ) ;
17541755 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- } ) )
1756+ let kind = ForeignItemKind :: Fn ( decl, generics) ;
1757+ Ok ( P ( ast:: ForeignItem { ident, attrs, kind, id : DUMMY_NODE_ID , span, vis, tokens : None } ) )
17641758 }
17651759
17661760 fn parse_assoc_fn (
@@ -1769,9 +1763,9 @@ impl<'a> Parser<'a> {
17691763 attrs : & mut Vec < Attribute > ,
17701764 is_name_required : fn ( & token:: Token ) -> bool ,
17711765 ) -> PResult < ' a , ( Ident , AssocItemKind , Generics ) > {
1766+ let cfg = ParamCfg { in_assoc_item : true , is_name_required } ;
17721767 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 } ) ?;
1768+ let ( ident, decl, generics) = self . parse_fn_sig ( & cfg) ?;
17751769 let sig = FnSig { header, decl } ;
17761770 let body = self . parse_assoc_fn_body ( at_end, attrs) ?;
17771771 Ok ( ( ident, AssocItemKind :: Fn ( sig, body) , generics) )
@@ -1847,7 +1841,7 @@ impl<'a> Parser<'a> {
18471841 }
18481842
18491843 /// 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 ) > {
1844+ fn parse_fn_sig ( & mut self , cfg : & ParamCfg ) -> PResult < ' a , ( Ident , P < FnDecl > , Generics ) > {
18511845 let ident = self . parse_ident ( ) ?;
18521846 let mut generics = self . parse_generics ( ) ?;
18531847 let decl = self . parse_fn_decl ( cfg, true ) ?;
@@ -1858,7 +1852,7 @@ impl<'a> Parser<'a> {
18581852 /// Parses the parameter list and result type of a function declaration.
18591853 pub ( super ) fn parse_fn_decl (
18601854 & mut self ,
1861- cfg : ParamCfg ,
1855+ cfg : & ParamCfg ,
18621856 ret_allow_plus : bool ,
18631857 ) -> PResult < ' a , P < FnDecl > > {
18641858 Ok ( P ( FnDecl {
@@ -1868,11 +1862,11 @@ impl<'a> Parser<'a> {
18681862 }
18691863
18701864 /// 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...
1865+ fn parse_fn_params ( & mut self , cfg : & ParamCfg ) -> PResult < ' a , Vec < Param > > {
1866+ let mut first_param = true ;
1867+ // Parse the arguments, starting out with `self` being allowed...
18741868 let ( mut params, _) = self . parse_paren_comma_seq ( |p| {
1875- let param = p. parse_param_general ( & cfg, is_trait_item ) . or_else ( |mut e| {
1869+ let param = p. parse_param_general ( & cfg, first_param ) . or_else ( |mut e| {
18761870 e. emit ( ) ;
18771871 let lo = p. prev_span ;
18781872 // Skip every token until next possible arg or end.
@@ -1881,28 +1875,28 @@ impl<'a> Parser<'a> {
18811875 Ok ( dummy_arg ( Ident :: new ( kw:: Invalid , lo. to ( p. prev_span ) ) ) )
18821876 } ) ;
18831877 // ...now that we've parsed the first argument, `self` is no longer allowed.
1884- cfg . is_self_allowed = false ;
1878+ first_param = false ;
18851879 param
18861880 } ) ?;
18871881 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
18881882 self . deduplicate_recovered_params_names ( & mut params) ;
18891883 Ok ( params)
18901884 }
18911885
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 > {
1886+ /// Parses a single function parameter.
1887+ ///
1888+ /// - `self` is syntactically allowed when `first_param` holds .
1889+ fn parse_param_general ( & mut self , cfg : & ParamCfg , first_param : bool ) -> PResult < ' a , Param > {
18961890 let lo = self . token . span ;
18971891 let attrs = self . parse_outer_attributes ( ) ?;
18981892
18991893 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
19001894 if let Some ( mut param) = self . parse_self_param ( ) ? {
19011895 param. attrs = attrs. into ( ) ;
1902- return if cfg . is_self_allowed {
1896+ return if first_param {
19031897 Ok ( param)
19041898 } else {
1905- self . recover_bad_self_param ( param, is_trait_item )
1899+ self . recover_bad_self_param ( param, cfg . in_assoc_item )
19061900 } ;
19071901 }
19081902
@@ -1919,8 +1913,8 @@ impl<'a> Parser<'a> {
19191913 & mut err,
19201914 pat,
19211915 is_name_required,
1922- cfg. is_self_allowed ,
1923- is_trait_item ,
1916+ first_param && cfg. in_assoc_item ,
1917+ cfg . in_assoc_item ,
19241918 ) {
19251919 err. emit ( ) ;
19261920 Ok ( dummy_arg ( ident) )
@@ -1975,8 +1969,6 @@ impl<'a> Parser<'a> {
19751969 }
19761970
19771971 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1978- ///
1979- /// See `parse_self_param_with_attrs` to collect attributes.
19801972 fn parse_self_param ( & mut self ) -> PResult < ' a , Option < Param > > {
19811973 // Extract an identifier *after* having confirmed that the token is one.
19821974 let expect_self_ident = |this : & mut Self | {
0 commit comments