Skip to content

Commit

Permalink
refactor pat parser method names/doc-comments to agree with RFC 3637
Browse files Browse the repository at this point in the history
  • Loading branch information
max-niederman authored and Nadrieril committed Nov 24, 2024
1 parent f8e50d8 commit 35bbc45
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ pub fn parse_ast_fragment<'a>(
}
}
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_alt(
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::Yes,
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2630,7 +2630,7 @@ impl<'a> Parser<'a> {
};
self.bump(); // Eat `let` token
let lo = self.prev_token.span;
let pat = self.parse_pat_allow_top_alt(
let pat = self.parse_pat_no_top_guard(
None,
RecoverComma::Yes,
RecoverColon::Yes,
Expand Down Expand Up @@ -2776,7 +2776,7 @@ impl<'a> Parser<'a> {
};
// Try to parse the pattern `for ($PAT) in $EXPR`.
let pat = match (
self.parse_pat_allow_top_alt(
self.parse_pat_no_top_guard(
None,
RecoverComma::Yes,
RecoverColon::Yes,
Expand Down Expand Up @@ -3239,7 +3239,7 @@ impl<'a> Parser<'a> {
// then we should recover.
let mut snapshot = this.create_snapshot_for_diagnostic();
let pattern_follows = snapshot
.parse_pat_allow_top_alt(
.parse_pat_no_top_guard(
None,
RecoverComma::Yes,
RecoverColon::Yes,
Expand Down Expand Up @@ -3315,7 +3315,7 @@ impl<'a> Parser<'a> {
if self.token == token::OpenDelim(Delimiter::Parenthesis) {
// Detect and recover from `($pat if $cond) => $arm`.
let left = self.token.span;
match self.parse_pat_allow_top_alt(
match self.parse_pat_no_top_guard(
None,
RecoverComma::Yes,
RecoverColon::Yes,
Expand Down Expand Up @@ -3349,7 +3349,7 @@ impl<'a> Parser<'a> {
}
} else {
// Regular parser flow:
let pat = self.parse_pat_allow_top_alt(
let pat = self.parse_pat_no_top_guard(
None,
RecoverComma::Yes,
RecoverColon::Yes,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'a> Parser<'a> {
NonterminalKind::Pat(pat_kind) => {
NtPat(self.collect_tokens_no_attrs(|this| match pat_kind {
PatParam { .. } => this.parse_pat_no_top_alt(None, None),
PatWithOr => this.parse_pat_allow_top_alt(
PatWithOr => this.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand Down
29 changes: 15 additions & 14 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ pub enum PatternLocation {
impl<'a> Parser<'a> {
/// Parses a pattern.
///
/// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
/// at the top level. Used when parsing the parameters of lambda expressions,
/// functions, function pointers, and `pat` macro fragments.
/// Corresponds to `PatternNoTopAlt` in RFC 3637 and does not admit or-patterns
/// or guard patterns at the top level. Used when parsing the parameters of lambda
/// expressions, functions, function pointers, and `pat_param` macro fragments.
pub fn parse_pat_no_top_alt(
&mut self,
expected: Option<Expected>,
Expand All @@ -110,25 +110,26 @@ impl<'a> Parser<'a> {

/// Parses a pattern.
///
/// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
/// Used for parsing patterns in all cases when `pat<no_top_alt>` is not used.
/// Corresponds to `PatternNoTopGuard` in RFC 3637 and allows or-patterns, but not
/// guard patterns, at the top level. Used for parsing patterns in `pat` fragments and
/// `let`, `if let`, and `while let` expressions.
///
/// Note that after the FCP in <https://github.com/rust-lang/rust/issues/81415>,
/// a leading vert is allowed in nested or-patterns, too. This allows us to
/// simplify the grammar somewhat.
pub fn parse_pat_allow_top_alt(
pub fn parse_pat_no_top_guard(
&mut self,
expected: Option<Expected>,
rc: RecoverComma,
ra: RecoverColon,
rt: CommaRecoveryMode,
) -> PResult<'a, P<Pat>> {
self.parse_pat_allow_top_alt_inner(expected, rc, ra, rt, None).map(|(pat, _)| pat)
self.parse_pat_no_top_guard_inner(expected, rc, ra, rt, None).map(|(pat, _)| pat)
}

/// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
/// recovered).
fn parse_pat_allow_top_alt_inner(
fn parse_pat_no_top_guard_inner(
&mut self,
expected: Option<Expected>,
rc: RecoverComma,
Expand Down Expand Up @@ -229,7 +230,7 @@ impl<'a> Parser<'a> {
// We use `parse_pat_allow_top_alt` regardless of whether we actually want top-level
// or-patterns so that we can detect when a user tries to use it. This allows us to print a
// better error message.
let (pat, trailing_vert) = self.parse_pat_allow_top_alt_inner(
let (pat, trailing_vert) = self.parse_pat_no_top_guard_inner(
expected,
rc,
RecoverColon::No,
Expand Down Expand Up @@ -696,7 +697,7 @@ impl<'a> Parser<'a> {
} else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
// Parse `[pat, pat,...]` as a slice pattern.
let (pats, _) = self.parse_delim_comma_seq(Delimiter::Bracket, |p| {
p.parse_pat_allow_top_alt(
p.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand Down Expand Up @@ -944,7 +945,7 @@ impl<'a> Parser<'a> {
let open_paren = self.token.span;

let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
p.parse_pat_allow_top_alt(
p.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand Down Expand Up @@ -1359,7 +1360,7 @@ impl<'a> Parser<'a> {
path: Path,
) -> PResult<'a, PatKind> {
let (fields, _) = self.parse_paren_comma_seq(|p| {
p.parse_pat_allow_top_alt(
p.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand Down Expand Up @@ -1394,7 +1395,7 @@ impl<'a> Parser<'a> {
self.parse_builtin(|self_, _lo, ident| {
Ok(match ident.name {
// builtin#deref(PAT)
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_allow_top_alt(
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_no_top_guard(
None,
RecoverComma::Yes,
RecoverColon::Yes,
Expand Down Expand Up @@ -1669,7 +1670,7 @@ impl<'a> Parser<'a> {
// Parsing a pattern of the form `fieldname: pat`.
let fieldname = self.parse_field_name()?;
self.bump();
let pat = self.parse_pat_allow_top_alt(
let pat = self.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl<'a> Parser<'a> {
PathStyle::Pat
if let Ok(_) = self
.parse_paren_comma_seq(|p| {
p.parse_pat_allow_top_alt(
p.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand Down

0 comments on commit 35bbc45

Please sign in to comment.