@@ -1596,6 +1596,11 @@ pub(super) struct ParamCfg {
1596
1596
pub is_name_required: fn(&token::Token) -> bool,
1597
1597
}
1598
1598
1599
+ impl ParamCfg {
1600
+ /// Configuration for a free function in the sense that it is not associated.
1601
+ const FREE: Self = ParamCfg { is_name_required: |_| true };
1602
+ }
1603
+
1599
1604
/// Parsing of functions and methods.
1600
1605
impl<'a> Parser<'a> {
1601
1606
/// Parses an item-position function declaration.
@@ -1605,11 +1610,9 @@ impl<'a> Parser<'a> {
1605
1610
vis: Visibility,
1606
1611
mut attrs: Vec<Attribute>,
1607
1612
) -> PResult<'a, Option<P<Item>>> {
1608
- let cfg = ParamCfg { is_name_required: |_| true };
1609
- let header = self.parse_fn_front_matter()?;
1610
- let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
1611
- let body = self.parse_fn_body(&mut false, &mut attrs)?;
1612
- let kind = ItemKind::Fn(FnSig { decl, header }, generics, body);
1613
+ let (ident, sig, generics, body) =
1614
+ self.parse_fn(&mut false, &mut attrs, &ParamCfg::FREE)?;
1615
+ let kind = ItemKind::Fn(sig, generics, body);
1613
1616
self.mk_item_with_info(attrs, lo, vis, (ident, kind, None))
1614
1617
}
1615
1618
@@ -1620,11 +1623,9 @@ impl<'a> Parser<'a> {
1620
1623
lo: Span,
1621
1624
mut attrs: Vec<Attribute>,
1622
1625
) -> PResult<'a, P<ForeignItem>> {
1623
- let cfg = ParamCfg { is_name_required: |_| true };
1624
- let header = self.parse_fn_front_matter()?;
1625
- let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
1626
- let body = self.parse_fn_body(&mut false, &mut attrs)?;
1627
- let kind = ForeignItemKind::Fn(FnSig { header, decl }, generics, body);
1626
+ let (ident, sig, generics, body) =
1627
+ self.parse_fn(&mut false, &mut attrs, &ParamCfg::FREE)?;
1628
+ let kind = ForeignItemKind::Fn(sig, generics, body);
1628
1629
let span = lo.to(self.prev_span);
1629
1630
Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
1630
1631
}
@@ -1635,10 +1636,25 @@ impl<'a> Parser<'a> {
1635
1636
attrs: &mut Vec<Attribute>,
1636
1637
is_name_required: fn(&token::Token) -> bool,
1637
1638
) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
1638
- let header = self.parse_fn_front_matter()?;
1639
- let (ident, decl, generics) = self.parse_fn_sig(&&ParamCfg { is_name_required })?;
1640
- let body = self.parse_fn_body(at_end, attrs)?;
1641
- Ok((ident, AssocItemKind::Fn(FnSig { header, decl }, body), generics))
1639
+ let cfg = ParamCfg { is_name_required };
1640
+ let (ident, sig, generics, body) = self.parse_fn(at_end, attrs, &cfg)?;
1641
+ Ok((ident, AssocItemKind::Fn(sig, body), generics))
1642
+ }
1643
+
1644
+ /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
1645
+ fn parse_fn(
1646
+ &mut self,
1647
+ at_end: &mut bool,
1648
+ attrs: &mut Vec<Attribute>,
1649
+ cfg: &ParamCfg,
1650
+ ) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
1651
+ let header = self.parse_fn_front_matter()?; // `const ... fn`
1652
+ let ident = self.parse_ident()?; // `foo`
1653
+ let mut generics = self.parse_generics()?; // `<'a, T, ...>`
1654
+ let decl = self.parse_fn_decl(cfg, AllowPlus::Yes)?; // `(p: u8, ...)`
1655
+ generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
1656
+ let body = self.parse_fn_body(at_end, attrs)?; // `;` or `{ ... }`.
1657
+ Ok((ident, FnSig { header, decl }, generics, body))
1642
1658
}
1643
1659
1644
1660
/// Parse the "body" of a function.
@@ -1722,15 +1738,6 @@ impl<'a> Parser<'a> {
1722
1738
Ok(FnHeader { constness, unsafety, asyncness, ext })
1723
1739
}
1724
1740
1725
- /// Parse the "signature", including the identifier, parameters, and generics of a function.
1726
- fn parse_fn_sig(&mut self, cfg: &ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
1727
- let ident = self.parse_ident()?;
1728
- let mut generics = self.parse_generics()?;
1729
- let decl = self.parse_fn_decl(cfg, AllowPlus::Yes)?;
1730
- generics.where_clause = self.parse_where_clause()?;
1731
- Ok((ident, decl, generics))
1732
- }
1733
-
1734
1741
/// Parses the parameter list and result type of a function declaration.
1735
1742
pub(super) fn parse_fn_decl(
1736
1743
&mut self,
0 commit comments