@@ -70,16 +70,15 @@ impl<'a> Parser<'a> {
7070 /// Parses one of the items allowed by the flags.
7171 fn parse_item_implementation (
7272 & mut self ,
73- attrs : Vec < Attribute > ,
73+ mut attrs : Vec < Attribute > ,
7474 macros_allowed : bool ,
7575 attributes_allowed : bool ,
7676 ) -> PResult < ' a , Option < P < Item > > > {
7777 maybe_whole ! ( self , NtItem , |item| {
78- let mut item = item. into_inner( ) ;
79- let mut attrs = attrs;
78+ let mut item = item;
8079 mem:: swap( & mut item. attrs, & mut attrs) ;
8180 item. attrs. extend( attrs) ;
82- Some ( P ( item) )
81+ Some ( item)
8382 } ) ;
8483
8584 let lo = self . token . span ;
@@ -1715,8 +1714,6 @@ impl<'a> Parser<'a> {
17151714
17161715/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
17171716pub ( super ) struct ParamCfg {
1718- /// Is `self` is allowed as the first parameter?
1719- pub is_self_allowed : bool ,
17201717 /// `is_name_required` decides if, per-parameter,
17211718 /// the parameter must have a pattern or just a type.
17221719 pub is_name_required : fn ( & token:: Token ) -> bool ,
@@ -1732,8 +1729,8 @@ impl<'a> Parser<'a> {
17321729 attrs : Vec < Attribute > ,
17331730 header : FnHeader ,
17341731 ) -> PResult < ' a , Option < P < Item > > > {
1735- let ( ident , decl , generics ) =
1736- self . parse_fn_sig ( ParamCfg { is_self_allowed : false , is_name_required : |_| true } ) ?;
1732+ let cfg = ParamCfg { is_name_required : |_| true } ;
1733+ let ( ident , decl , generics ) = self . parse_fn_sig ( & cfg ) ?;
17371734 let ( inner_attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
17381735 let kind = ItemKind :: Fn ( FnSig { decl, header } , generics, body) ;
17391736 self . mk_item_with_info ( attrs, lo, vis, ( ident, kind, Some ( inner_attrs) ) )
@@ -1747,20 +1744,13 @@ impl<'a> Parser<'a> {
17471744 attrs : Vec < Attribute > ,
17481745 extern_sp : Span ,
17491746 ) -> PResult < ' a , P < ForeignItem > > {
1747+ let cfg = ParamCfg { is_name_required : |_| true } ;
17501748 self . expect_keyword ( kw:: Fn ) ?;
1751- let ( ident, decl, generics) =
1752- self . parse_fn_sig ( ParamCfg { is_self_allowed : false , is_name_required : |_| true } ) ?;
1749+ let ( ident, decl, generics) = self . parse_fn_sig ( & cfg) ?;
17531750 let span = lo. to ( self . token . span ) ;
17541751 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- } ) )
1752+ let kind = ForeignItemKind :: Fn ( decl, generics) ;
1753+ Ok ( P ( ast:: ForeignItem { ident, attrs, kind, id : DUMMY_NODE_ID , span, vis, tokens : None } ) )
17641754 }
17651755
17661756 fn parse_assoc_fn (
@@ -1770,8 +1760,7 @@ impl<'a> Parser<'a> {
17701760 is_name_required : fn ( & token:: Token ) -> bool ,
17711761 ) -> PResult < ' a , ( Ident , AssocItemKind , Generics ) > {
17721762 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 } ) ?;
1763+ let ( ident, decl, generics) = self . parse_fn_sig ( & ParamCfg { is_name_required } ) ?;
17751764 let sig = FnSig { header, decl } ;
17761765 let body = self . parse_assoc_fn_body ( at_end, attrs) ?;
17771766 Ok ( ( ident, AssocItemKind :: Fn ( sig, body) , generics) )
@@ -1847,7 +1836,7 @@ impl<'a> Parser<'a> {
18471836 }
18481837
18491838 /// 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 ) > {
1839+ fn parse_fn_sig ( & mut self , cfg : & ParamCfg ) -> PResult < ' a , ( Ident , P < FnDecl > , Generics ) > {
18511840 let ident = self . parse_ident ( ) ?;
18521841 let mut generics = self . parse_generics ( ) ?;
18531842 let decl = self . parse_fn_decl ( cfg, true ) ?;
@@ -1858,7 +1847,7 @@ impl<'a> Parser<'a> {
18581847 /// Parses the parameter list and result type of a function declaration.
18591848 pub ( super ) fn parse_fn_decl (
18601849 & mut self ,
1861- cfg : ParamCfg ,
1850+ cfg : & ParamCfg ,
18621851 ret_allow_plus : bool ,
18631852 ) -> PResult < ' a , P < FnDecl > > {
18641853 Ok ( P ( FnDecl {
@@ -1868,11 +1857,11 @@ impl<'a> Parser<'a> {
18681857 }
18691858
18701859 /// 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...
1860+ fn parse_fn_params ( & mut self , cfg : & ParamCfg ) -> PResult < ' a , Vec < Param > > {
1861+ let mut first_param = true ;
1862+ // Parse the arguments, starting out with `self` being allowed...
18741863 let ( mut params, _) = self . parse_paren_comma_seq ( |p| {
1875- let param = p. parse_param_general ( & cfg, is_trait_item ) . or_else ( |mut e| {
1864+ let param = p. parse_param_general ( & cfg, first_param ) . or_else ( |mut e| {
18761865 e. emit ( ) ;
18771866 let lo = p. prev_span ;
18781867 // Skip every token until next possible arg or end.
@@ -1881,29 +1870,25 @@ impl<'a> Parser<'a> {
18811870 Ok ( dummy_arg ( Ident :: new ( kw:: Invalid , lo. to ( p. prev_span ) ) ) )
18821871 } ) ;
18831872 // ...now that we've parsed the first argument, `self` is no longer allowed.
1884- cfg . is_self_allowed = false ;
1873+ first_param = false ;
18851874 param
18861875 } ) ?;
18871876 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
18881877 self . deduplicate_recovered_params_names ( & mut params) ;
18891878 Ok ( params)
18901879 }
18911880
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 > {
1881+ /// Parses a single function parameter.
1882+ ///
1883+ /// - `self` is syntactically allowed when `first_param` holds .
1884+ fn parse_param_general ( & mut self , cfg : & ParamCfg , first_param : bool ) -> PResult < ' a , Param > {
18961885 let lo = self . token . span ;
18971886 let attrs = self . parse_outer_attributes ( ) ?;
18981887
18991888 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
19001889 if let Some ( mut param) = self . parse_self_param ( ) ? {
19011890 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- } ;
1891+ return if first_param { Ok ( param) } else { self . recover_bad_self_param ( param) } ;
19071892 }
19081893
19091894 let is_name_required = match self . token . kind {
@@ -1915,13 +1900,9 @@ impl<'a> Parser<'a> {
19151900
19161901 let pat = self . parse_fn_param_pat ( ) ?;
19171902 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- ) {
1903+ return if let Some ( ident) =
1904+ self . parameter_without_type ( & mut err, pat, is_name_required, first_param)
1905+ {
19251906 err. emit ( ) ;
19261907 Ok ( dummy_arg ( ident) )
19271908 } else {
@@ -1975,8 +1956,6 @@ impl<'a> Parser<'a> {
19751956 }
19761957
19771958 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1978- ///
1979- /// See `parse_self_param_with_attrs` to collect attributes.
19801959 fn parse_self_param ( & mut self ) -> PResult < ' a , Option < Param > > {
19811960 // Extract an identifier *after* having confirmed that the token is one.
19821961 let expect_self_ident = |this : & mut Self | {
0 commit comments