@@ -1715,8 +1715,6 @@ impl<'a> Parser<'a> {
1715
1715
1716
1716
/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1717
1717
pub ( super ) struct ParamCfg {
1718
- /// Is `self` is allowed as the first parameter?
1719
- pub is_self_allowed : bool ,
1720
1718
/// `is_name_required` decides if, per-parameter,
1721
1719
/// the parameter must have a pattern or just a type.
1722
1720
pub is_name_required : fn ( & token:: Token ) -> bool ,
@@ -1732,8 +1730,8 @@ impl<'a> Parser<'a> {
1732
1730
attrs : Vec < Attribute > ,
1733
1731
header : FnHeader ,
1734
1732
) -> 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 ) ?;
1737
1735
let ( inner_attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
1738
1736
let kind = ItemKind :: Fn ( FnSig { decl, header } , generics, body) ;
1739
1737
self . mk_item_with_info ( attrs, lo, vis, ( ident, kind, Some ( inner_attrs) ) )
@@ -1747,20 +1745,13 @@ impl<'a> Parser<'a> {
1747
1745
attrs : Vec < Attribute > ,
1748
1746
extern_sp : Span ,
1749
1747
) -> PResult < ' a , P < ForeignItem > > {
1748
+ let cfg = ParamCfg { is_name_required : |_| true } ;
1750
1749
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) ?;
1753
1751
let span = lo. to ( self . token . span ) ;
1754
1752
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 } ) )
1764
1755
}
1765
1756
1766
1757
fn parse_assoc_fn (
@@ -1770,8 +1761,7 @@ impl<'a> Parser<'a> {
1770
1761
is_name_required : fn ( & token:: Token ) -> bool ,
1771
1762
) -> PResult < ' a , ( Ident , AssocItemKind , Generics ) > {
1772
1763
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 } ) ?;
1775
1765
let sig = FnSig { header, decl } ;
1776
1766
let body = self . parse_assoc_fn_body ( at_end, attrs) ?;
1777
1767
Ok ( ( ident, AssocItemKind :: Fn ( sig, body) , generics) )
@@ -1847,7 +1837,7 @@ impl<'a> Parser<'a> {
1847
1837
}
1848
1838
1849
1839
/// 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 ) > {
1851
1841
let ident = self . parse_ident ( ) ?;
1852
1842
let mut generics = self . parse_generics ( ) ?;
1853
1843
let decl = self . parse_fn_decl ( cfg, true ) ?;
@@ -1858,7 +1848,7 @@ impl<'a> Parser<'a> {
1858
1848
/// Parses the parameter list and result type of a function declaration.
1859
1849
pub ( super ) fn parse_fn_decl (
1860
1850
& mut self ,
1861
- cfg : ParamCfg ,
1851
+ cfg : & ParamCfg ,
1862
1852
ret_allow_plus : bool ,
1863
1853
) -> PResult < ' a , P < FnDecl > > {
1864
1854
Ok ( P ( FnDecl {
@@ -1868,11 +1858,11 @@ impl<'a> Parser<'a> {
1868
1858
}
1869
1859
1870
1860
/// 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...
1874
1864
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| {
1876
1866
e. emit ( ) ;
1877
1867
let lo = p. prev_span ;
1878
1868
// Skip every token until next possible arg or end.
@@ -1881,29 +1871,25 @@ impl<'a> Parser<'a> {
1881
1871
Ok ( dummy_arg ( Ident :: new ( kw:: Invalid , lo. to ( p. prev_span ) ) ) )
1882
1872
} ) ;
1883
1873
// ...now that we've parsed the first argument, `self` is no longer allowed.
1884
- cfg . is_self_allowed = false ;
1874
+ first_param = false ;
1885
1875
param
1886
1876
} ) ?;
1887
1877
// Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1888
1878
self . deduplicate_recovered_params_names ( & mut params) ;
1889
1879
Ok ( params)
1890
1880
}
1891
1881
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 > {
1896
1886
let lo = self . token . span ;
1897
1887
let attrs = self . parse_outer_attributes ( ) ?;
1898
1888
1899
1889
// Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1900
1890
if let Some ( mut param) = self . parse_self_param ( ) ? {
1901
1891
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) } ;
1907
1893
}
1908
1894
1909
1895
let is_name_required = match self . token . kind {
@@ -1915,13 +1901,9 @@ impl<'a> Parser<'a> {
1915
1901
1916
1902
let pat = self . parse_fn_param_pat ( ) ?;
1917
1903
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
+ {
1925
1907
err. emit ( ) ;
1926
1908
Ok ( dummy_arg ( ident) )
1927
1909
} else {
@@ -1975,8 +1957,6 @@ impl<'a> Parser<'a> {
1975
1957
}
1976
1958
1977
1959
/// Returns the parsed optional self parameter and whether a self shortcut was used.
1978
- ///
1979
- /// See `parse_self_param_with_attrs` to collect attributes.
1980
1960
fn parse_self_param ( & mut self ) -> PResult < ' a , Option < Param > > {
1981
1961
// Extract an identifier *after* having confirmed that the token is one.
1982
1962
let expect_self_ident = |this : & mut Self | {
0 commit comments