Skip to content

Commit 79d139a

Browse files
committed
parser: simplify ParamCfg -> ReqName
1 parent 05e5530 commit 79d139a

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/librustc_parse/parser/item.rs

+17-27
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ impl<'a> Parser<'a> {
9898

9999
if self.is_fn_front_matter() {
100100
// 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)?;
103102
let kind = ItemKind::Fn(sig, generics, body);
104103
return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
105104
}
@@ -715,12 +714,12 @@ impl<'a> Parser<'a> {
715714
fn parse_assoc_item(
716715
&mut self,
717716
at_end: &mut bool,
718-
is_name_required: fn(&token::Token) -> bool,
717+
req_name: fn(&token::Token) -> bool,
719718
) -> PResult<'a, P<AssocItem>> {
720719
let attrs = self.parse_outer_attributes()?;
721720
let mut unclosed_delims = vec![];
722721
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);
724723
unclosed_delims.append(&mut this.unclosed_delims);
725724
item
726725
})?;
@@ -736,16 +735,15 @@ impl<'a> Parser<'a> {
736735
&mut self,
737736
at_end: &mut bool,
738737
mut attrs: Vec<Attribute>,
739-
is_name_required: fn(&token::Token) -> bool,
738+
req_name: fn(&token::Token) -> bool,
740739
) -> PResult<'a, AssocItem> {
741740
let lo = self.token.span;
742741
let vis = self.parse_visibility(FollowedByType::No)?;
743742
let defaultness = self.parse_defaultness();
744743
let (name, kind, generics) = if self.eat_keyword(kw::Type) {
745744
self.parse_assoc_ty()?
746745
} 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)?;
749747
(ident, AssocItemKind::Fn(sig, body), generics)
750748
} else if let Some(mac) = self.parse_assoc_macro_invoc("associated", Some(&vis), at_end)? {
751749
(Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
@@ -982,8 +980,7 @@ impl<'a> Parser<'a> {
982980
self.parse_item_foreign_type(vis, lo, attrs)
983981
} else if self.is_fn_front_matter() {
984982
// 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)?;
987984
let kind = ForeignItemKind::Fn(sig, generics, body);
988985
let span = lo.to(self.prev_span);
989986
Ok(P(ast::ForeignItem {
@@ -1607,16 +1604,9 @@ impl<'a> Parser<'a> {
16071604
}
16081605

16091606
/// 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;
16201610

16211611
/// Parsing of functions and methods.
16221612
impl<'a> Parser<'a> {
@@ -1625,12 +1615,12 @@ impl<'a> Parser<'a> {
16251615
&mut self,
16261616
at_end: &mut bool,
16271617
attrs: &mut Vec<Attribute>,
1628-
cfg: &ParamCfg,
1618+
req_name: ReqName,
16291619
) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
16301620
let header = self.parse_fn_front_matter()?; // `const ... fn`
16311621
let ident = self.parse_ident()?; // `foo`
16321622
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, ...)`
16341624
generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
16351625
let body = self.parse_fn_body(at_end, attrs)?; // `;` or `{ ... }`.
16361626
Ok((ident, FnSig { header, decl }, generics, body))
@@ -1732,21 +1722,21 @@ impl<'a> Parser<'a> {
17321722
/// Parses the parameter list and result type of a function declaration.
17331723
pub(super) fn parse_fn_decl(
17341724
&mut self,
1735-
cfg: &ParamCfg,
1725+
req_name: ReqName,
17361726
ret_allow_plus: AllowPlus,
17371727
) -> PResult<'a, P<FnDecl>> {
17381728
Ok(P(FnDecl {
1739-
inputs: self.parse_fn_params(cfg)?,
1729+
inputs: self.parse_fn_params(req_name)?,
17401730
output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
17411731
}))
17421732
}
17431733

17441734
/// 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>> {
17461736
let mut first_param = true;
17471737
// Parse the arguments, starting out with `self` being allowed...
17481738
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| {
17501740
e.emit();
17511741
let lo = p.prev_span;
17521742
// Skip every token until next possible arg or end.
@@ -1766,7 +1756,7 @@ impl<'a> Parser<'a> {
17661756
/// Parses a single function parameter.
17671757
///
17681758
/// - `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> {
17701760
let lo = self.token.span;
17711761
let attrs = self.parse_outer_attributes()?;
17721762

@@ -1778,7 +1768,7 @@ impl<'a> Parser<'a> {
17781768

17791769
let is_name_required = match self.token.kind {
17801770
token::DotDotDot => false,
1781-
_ => (cfg.is_name_required)(&self.token),
1771+
_ => req_name(&self.token),
17821772
};
17831773
let (pat, ty) = if is_name_required || self.is_named_param() {
17841774
debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);

src/librustc_parse/parser/ty.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::item::ParamCfg;
21
use super::{Parser, PathStyle, TokenType};
32

43
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
@@ -311,8 +310,7 @@ impl<'a> Parser<'a> {
311310
let unsafety = self.parse_unsafety();
312311
let ext = self.parse_extern()?;
313312
self.expect_keyword(kw::Fn)?;
314-
let cfg = ParamCfg { is_name_required: |_| false };
315-
let decl = self.parse_fn_decl(&cfg, AllowPlus::No)?;
313+
let decl = self.parse_fn_decl(|_| false, AllowPlus::No)?;
316314
Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params, decl })))
317315
}
318316

0 commit comments

Comments
 (0)