@@ -1715,8 +1715,9 @@ 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 ,
1718
+ /// Is `self` is *semantically* allowed as the first parameter?
1719
+ /// This is only used for diagnostics.
1720
+ pub in_assoc_item : bool ,
1720
1721
/// `is_name_required` decides if, per-parameter,
1721
1722
/// the parameter must have a pattern or just a type.
1722
1723
pub is_name_required : fn ( & token:: Token ) -> bool ,
@@ -1732,8 +1733,8 @@ impl<'a> Parser<'a> {
1732
1733
attrs : Vec < Attribute > ,
1733
1734
header : FnHeader ,
1734
1735
) -> 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 ) ?;
1737
1738
let ( inner_attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
1738
1739
let kind = ItemKind :: Fn ( FnSig { decl, header } , generics, body) ;
1739
1740
self . mk_item_with_info ( attrs, lo, vis, ( ident, kind, Some ( inner_attrs) ) )
@@ -1747,20 +1748,13 @@ impl<'a> Parser<'a> {
1747
1748
attrs : Vec < Attribute > ,
1748
1749
extern_sp : Span ,
1749
1750
) -> PResult < ' a , P < ForeignItem > > {
1751
+ let cfg = ParamCfg { in_assoc_item : false , is_name_required : |_| true } ;
1750
1752
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) ?;
1753
1754
let span = lo. to ( self . token . span ) ;
1754
1755
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 } ) )
1764
1758
}
1765
1759
1766
1760
fn parse_assoc_fn (
@@ -1769,9 +1763,9 @@ impl<'a> Parser<'a> {
1769
1763
attrs : & mut Vec < Attribute > ,
1770
1764
is_name_required : fn ( & token:: Token ) -> bool ,
1771
1765
) -> PResult < ' a , ( Ident , AssocItemKind , Generics ) > {
1766
+ let cfg = ParamCfg { in_assoc_item : true , is_name_required } ;
1772
1767
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) ?;
1775
1769
let sig = FnSig { header, decl } ;
1776
1770
let body = self . parse_assoc_fn_body ( at_end, attrs) ?;
1777
1771
Ok ( ( ident, AssocItemKind :: Fn ( sig, body) , generics) )
@@ -1847,7 +1841,7 @@ impl<'a> Parser<'a> {
1847
1841
}
1848
1842
1849
1843
/// 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 ) > {
1851
1845
let ident = self . parse_ident ( ) ?;
1852
1846
let mut generics = self . parse_generics ( ) ?;
1853
1847
let decl = self . parse_fn_decl ( cfg, true ) ?;
@@ -1858,7 +1852,7 @@ impl<'a> Parser<'a> {
1858
1852
/// Parses the parameter list and result type of a function declaration.
1859
1853
pub ( super ) fn parse_fn_decl (
1860
1854
& mut self ,
1861
- cfg : ParamCfg ,
1855
+ cfg : & ParamCfg ,
1862
1856
ret_allow_plus : bool ,
1863
1857
) -> PResult < ' a , P < FnDecl > > {
1864
1858
Ok ( P ( FnDecl {
@@ -1868,11 +1862,11 @@ impl<'a> Parser<'a> {
1868
1862
}
1869
1863
1870
1864
/// 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...
1874
1868
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| {
1876
1870
e. emit ( ) ;
1877
1871
let lo = p. prev_span ;
1878
1872
// Skip every token until next possible arg or end.
@@ -1881,28 +1875,28 @@ impl<'a> Parser<'a> {
1881
1875
Ok ( dummy_arg ( Ident :: new ( kw:: Invalid , lo. to ( p. prev_span ) ) ) )
1882
1876
} ) ;
1883
1877
// ...now that we've parsed the first argument, `self` is no longer allowed.
1884
- cfg . is_self_allowed = false ;
1878
+ first_param = false ;
1885
1879
param
1886
1880
} ) ?;
1887
1881
// Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1888
1882
self . deduplicate_recovered_params_names ( & mut params) ;
1889
1883
Ok ( params)
1890
1884
}
1891
1885
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 > {
1896
1890
let lo = self . token . span ;
1897
1891
let attrs = self . parse_outer_attributes ( ) ?;
1898
1892
1899
1893
// Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1900
1894
if let Some ( mut param) = self . parse_self_param ( ) ? {
1901
1895
param. attrs = attrs. into ( ) ;
1902
- return if cfg . is_self_allowed {
1896
+ return if first_param {
1903
1897
Ok ( param)
1904
1898
} else {
1905
- self . recover_bad_self_param ( param, is_trait_item )
1899
+ self . recover_bad_self_param ( param, cfg . in_assoc_item )
1906
1900
} ;
1907
1901
}
1908
1902
@@ -1919,8 +1913,8 @@ impl<'a> Parser<'a> {
1919
1913
& mut err,
1920
1914
pat,
1921
1915
is_name_required,
1922
- cfg. is_self_allowed ,
1923
- is_trait_item ,
1916
+ first_param && cfg. in_assoc_item ,
1917
+ cfg . in_assoc_item ,
1924
1918
) {
1925
1919
err. emit ( ) ;
1926
1920
Ok ( dummy_arg ( ident) )
@@ -1975,8 +1969,6 @@ impl<'a> Parser<'a> {
1975
1969
}
1976
1970
1977
1971
/// 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
1972
fn parse_self_param ( & mut self ) -> PResult < ' a , Option < Param > > {
1981
1973
// Extract an identifier *after* having confirmed that the token is one.
1982
1974
let expect_self_ident = |this : & mut Self | {
0 commit comments