@@ -98,8 +98,7 @@ impl<'a> Parser<'a> {
98
98
99
99
if self . is_fn_front_matter ( ) {
100
100
// FUNCTION ITEM
101
- let ( ident, sig, generics, body) =
102
- self . parse_fn ( & mut false , & mut attrs, & ParamCfg :: FREE ) ?;
101
+ let ( ident, sig, generics, body) = self . parse_fn ( & mut false , & mut attrs, |_| true ) ?;
103
102
let kind = ItemKind :: Fn ( sig, generics, body) ;
104
103
return self . mk_item_with_info ( attrs, lo, vis, ( ident, kind, None ) ) ;
105
104
}
@@ -715,12 +714,12 @@ impl<'a> Parser<'a> {
715
714
fn parse_assoc_item (
716
715
& mut self ,
717
716
at_end : & mut bool ,
718
- is_name_required : fn ( & token:: Token ) -> bool ,
717
+ req_name : fn ( & token:: Token ) -> bool ,
719
718
) -> PResult < ' a , P < AssocItem > > {
720
719
let attrs = self . parse_outer_attributes ( ) ?;
721
720
let mut unclosed_delims = vec ! [ ] ;
722
721
let ( mut item, tokens) = self . collect_tokens ( |this| {
723
- let item = this. parse_assoc_item_ ( at_end, attrs, is_name_required ) ;
722
+ let item = this. parse_assoc_item_ ( at_end, attrs, req_name ) ;
724
723
unclosed_delims. append ( & mut this. unclosed_delims ) ;
725
724
item
726
725
} ) ?;
@@ -736,16 +735,15 @@ impl<'a> Parser<'a> {
736
735
& mut self ,
737
736
at_end : & mut bool ,
738
737
mut attrs : Vec < Attribute > ,
739
- is_name_required : fn ( & token:: Token ) -> bool ,
738
+ req_name : fn ( & token:: Token ) -> bool ,
740
739
) -> PResult < ' a , AssocItem > {
741
740
let lo = self . token . span ;
742
741
let vis = self . parse_visibility ( FollowedByType :: No ) ?;
743
742
let defaultness = self . parse_defaultness ( ) ;
744
743
let ( name, kind, generics) = if self . eat_keyword ( kw:: Type ) {
745
744
self . parse_assoc_ty ( ) ?
746
745
} else if self . is_fn_front_matter ( ) {
747
- let cfg = ParamCfg { is_name_required } ;
748
- let ( ident, sig, generics, body) = self . parse_fn ( at_end, & mut attrs, & cfg) ?;
746
+ let ( ident, sig, generics, body) = self . parse_fn ( at_end, & mut attrs, req_name) ?;
749
747
( ident, AssocItemKind :: Fn ( sig, body) , generics)
750
748
} else if let Some ( mac) = self . parse_assoc_macro_invoc ( "associated" , Some ( & vis) , at_end) ? {
751
749
( Ident :: invalid ( ) , AssocItemKind :: Macro ( mac) , Generics :: default ( ) )
@@ -982,8 +980,7 @@ impl<'a> Parser<'a> {
982
980
self . parse_item_foreign_type ( vis, lo, attrs)
983
981
} else if self . is_fn_front_matter ( ) {
984
982
// FOREIGN FUNCTION ITEM
985
- let ( ident, sig, generics, body) =
986
- self . parse_fn ( & mut false , & mut attrs, & ParamCfg :: FREE ) ?;
983
+ let ( ident, sig, generics, body) = self . parse_fn ( & mut false , & mut attrs, |_| true ) ?;
987
984
let kind = ForeignItemKind :: Fn ( sig, generics, body) ;
988
985
let span = lo. to ( self . prev_span ) ;
989
986
Ok ( P ( ast:: ForeignItem {
@@ -1607,16 +1604,9 @@ impl<'a> Parser<'a> {
1607
1604
}
1608
1605
1609
1606
/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1610
- pub ( super ) struct ParamCfg {
1611
- /// `is_name_required` decides if, per-parameter,
1612
- /// the parameter must have a pattern or just a type.
1613
- pub is_name_required : fn ( & token:: Token ) -> bool ,
1614
- }
1615
-
1616
- impl ParamCfg {
1617
- /// Configuration for a free function in the sense that it is not associated.
1618
- const FREE : Self = ParamCfg { is_name_required : |_| true } ;
1619
- }
1607
+ ///
1608
+ /// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
1609
+ type ReqName = fn ( & token:: Token ) -> bool ;
1620
1610
1621
1611
/// Parsing of functions and methods.
1622
1612
impl < ' a > Parser < ' a > {
@@ -1625,12 +1615,12 @@ impl<'a> Parser<'a> {
1625
1615
& mut self ,
1626
1616
at_end : & mut bool ,
1627
1617
attrs : & mut Vec < Attribute > ,
1628
- cfg : & ParamCfg ,
1618
+ req_name : ReqName ,
1629
1619
) -> PResult < ' a , ( Ident , FnSig , Generics , Option < P < Block > > ) > {
1630
1620
let header = self . parse_fn_front_matter ( ) ?; // `const ... fn`
1631
1621
let ident = self . parse_ident ( ) ?; // `foo`
1632
1622
let mut generics = self . parse_generics ( ) ?; // `<'a, T, ...>`
1633
- let decl = self . parse_fn_decl ( cfg , AllowPlus :: Yes ) ?; // `(p: u8, ...)`
1623
+ let decl = self . parse_fn_decl ( req_name , AllowPlus :: Yes ) ?; // `(p: u8, ...)`
1634
1624
generics. where_clause = self . parse_where_clause ( ) ?; // `where T: Ord`
1635
1625
let body = self . parse_fn_body ( at_end, attrs) ?; // `;` or `{ ... }`.
1636
1626
Ok ( ( ident, FnSig { header, decl } , generics, body) )
@@ -1732,21 +1722,21 @@ impl<'a> Parser<'a> {
1732
1722
/// Parses the parameter list and result type of a function declaration.
1733
1723
pub ( super ) fn parse_fn_decl (
1734
1724
& mut self ,
1735
- cfg : & ParamCfg ,
1725
+ req_name : ReqName ,
1736
1726
ret_allow_plus : AllowPlus ,
1737
1727
) -> PResult < ' a , P < FnDecl > > {
1738
1728
Ok ( P ( FnDecl {
1739
- inputs : self . parse_fn_params ( cfg ) ?,
1729
+ inputs : self . parse_fn_params ( req_name ) ?,
1740
1730
output : self . parse_ret_ty ( ret_allow_plus, RecoverQPath :: Yes ) ?,
1741
1731
} ) )
1742
1732
}
1743
1733
1744
1734
/// Parses the parameter list of a function, including the `(` and `)` delimiters.
1745
- fn parse_fn_params ( & mut self , cfg : & ParamCfg ) -> PResult < ' a , Vec < Param > > {
1735
+ fn parse_fn_params ( & mut self , req_name : ReqName ) -> PResult < ' a , Vec < Param > > {
1746
1736
let mut first_param = true ;
1747
1737
// Parse the arguments, starting out with `self` being allowed...
1748
1738
let ( mut params, _) = self . parse_paren_comma_seq ( |p| {
1749
- let param = p. parse_param_general ( & cfg , first_param) . or_else ( |mut e| {
1739
+ let param = p. parse_param_general ( req_name , first_param) . or_else ( |mut e| {
1750
1740
e. emit ( ) ;
1751
1741
let lo = p. prev_span ;
1752
1742
// Skip every token until next possible arg or end.
@@ -1766,7 +1756,7 @@ impl<'a> Parser<'a> {
1766
1756
/// Parses a single function parameter.
1767
1757
///
1768
1758
/// - `self` is syntactically allowed when `first_param` holds.
1769
- fn parse_param_general ( & mut self , cfg : & ParamCfg , first_param : bool ) -> PResult < ' a , Param > {
1759
+ fn parse_param_general ( & mut self , req_name : ReqName , first_param : bool ) -> PResult < ' a , Param > {
1770
1760
let lo = self . token . span ;
1771
1761
let attrs = self . parse_outer_attributes ( ) ?;
1772
1762
@@ -1778,7 +1768,7 @@ impl<'a> Parser<'a> {
1778
1768
1779
1769
let is_name_required = match self . token . kind {
1780
1770
token:: DotDotDot => false ,
1781
- _ => ( cfg . is_name_required ) ( & self . token ) ,
1771
+ _ => req_name ( & self . token ) ,
1782
1772
} ;
1783
1773
let ( pat, ty) = if is_name_required || self . is_named_param ( ) {
1784
1774
debug ! ( "parse_param_general parse_pat (is_name_required:{})" , is_name_required) ;
0 commit comments