Skip to content

Commit 81cbdde

Browse files
committed
refactor pat parser method names/doc-comments to agree with RFC 3637
1 parent 84ef6ae commit 81cbdde

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ pub fn parse_ast_fragment<'a>(
987987
}
988988
}
989989
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
990-
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_alt(
990+
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_no_top_guard(
991991
None,
992992
RecoverComma::No,
993993
RecoverColon::Yes,

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ impl<'a> Parser<'a> {
26332633
};
26342634
self.bump(); // Eat `let` token
26352635
let lo = self.prev_token.span;
2636-
let pat = self.parse_pat_allow_top_alt(
2636+
let pat = self.parse_pat_no_top_guard(
26372637
None,
26382638
RecoverComma::Yes,
26392639
RecoverColon::Yes,
@@ -2770,7 +2770,7 @@ impl<'a> Parser<'a> {
27702770
};
27712771
// Try to parse the pattern `for ($PAT) in $EXPR`.
27722772
let pat = match (
2773-
self.parse_pat_allow_top_alt(
2773+
self.parse_pat_no_top_guard(
27742774
None,
27752775
RecoverComma::Yes,
27762776
RecoverColon::Yes,
@@ -3230,7 +3230,7 @@ impl<'a> Parser<'a> {
32303230
// then we should recover.
32313231
let mut snapshot = this.create_snapshot_for_diagnostic();
32323232
let pattern_follows = snapshot
3233-
.parse_pat_allow_top_alt(
3233+
.parse_pat_no_top_guard(
32343234
None,
32353235
RecoverComma::Yes,
32363236
RecoverColon::Yes,
@@ -3306,7 +3306,7 @@ impl<'a> Parser<'a> {
33063306
if self.token == token::OpenDelim(Delimiter::Parenthesis) {
33073307
// Detect and recover from `($pat if $cond) => $arm`.
33083308
let left = self.token.span;
3309-
match self.parse_pat_allow_top_alt(
3309+
match self.parse_pat_no_top_guard(
33103310
None,
33113311
RecoverComma::Yes,
33123312
RecoverColon::Yes,
@@ -3340,7 +3340,7 @@ impl<'a> Parser<'a> {
33403340
}
33413341
} else {
33423342
// Regular parser flow:
3343-
let pat = self.parse_pat_allow_top_alt(
3343+
let pat = self.parse_pat_no_top_guard(
33443344
None,
33453345
RecoverComma::Yes,
33463346
RecoverColon::Yes,

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a> Parser<'a> {
132132
NonterminalKind::Pat(pat_kind) => {
133133
NtPat(self.collect_tokens_no_attrs(|this| match pat_kind {
134134
PatParam { .. } => this.parse_pat_no_top_alt(None, None),
135-
PatWithOr => this.parse_pat_allow_top_alt(
135+
PatWithOr => this.parse_pat_no_top_guard(
136136
None,
137137
RecoverComma::No,
138138
RecoverColon::No,

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,24 @@ pub enum PatternLocation {
9494
impl<'a> Parser<'a> {
9595
/// Parses a pattern.
9696
///
97-
/// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
98-
/// at the top level. Used when parsing the parameters of lambda expressions,
99-
/// functions, function pointers, and `pat` macro fragments.
97+
/// Corresponds to `Pattern` in RFC 3637 and admits guard patterns at the top level.
98+
/// Used when parsing patterns in all cases where neither `PatternNoTopGuard` nor
99+
/// `PatternNoTopAlt` (see below) are used.
100+
pub fn parse_pat_allow_top_guard(
101+
&mut self,
102+
_expected: Option<Expected>,
103+
_rc: RecoverComma,
104+
_ra: RecoverColon,
105+
_rt: CommaRecoveryMode,
106+
) -> PResult<'a, P<Pat>> {
107+
todo!()
108+
}
109+
110+
/// Parses a pattern.
111+
///
112+
/// Corresponds to `PatternNoTopAlt` in RFC 3637 and does not admit or-patterns
113+
/// or guard patterns at the top level. Used when parsing the parameters of lambda
114+
/// expressions, functions, function pointers, and `pat_param` macro fragments.
100115
pub fn parse_pat_no_top_alt(
101116
&mut self,
102117
expected: Option<Expected>,
@@ -107,25 +122,26 @@ impl<'a> Parser<'a> {
107122

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

126142
/// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
127143
/// recovered).
128-
fn parse_pat_allow_top_alt_inner(
144+
fn parse_pat_no_top_guard_inner(
129145
&mut self,
130146
expected: Option<Expected>,
131147
rc: RecoverComma,
@@ -226,7 +242,7 @@ impl<'a> Parser<'a> {
226242
// We use `parse_pat_allow_top_alt` regardless of whether we actually want top-level
227243
// or-patterns so that we can detect when a user tries to use it. This allows us to print a
228244
// better error message.
229-
let (pat, trailing_vert) = self.parse_pat_allow_top_alt_inner(
245+
let (pat, trailing_vert) = self.parse_pat_no_top_guard_inner(
230246
expected,
231247
rc,
232248
RecoverColon::No,
@@ -461,7 +477,7 @@ impl<'a> Parser<'a> {
461477
} else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
462478
// Parse `[pat, pat,...]` as a slice pattern.
463479
let (pats, _) = self.parse_delim_comma_seq(Delimiter::Bracket, |p| {
464-
p.parse_pat_allow_top_alt(
480+
p.parse_pat_no_top_guard(
465481
None,
466482
RecoverComma::No,
467483
RecoverColon::No,
@@ -709,7 +725,7 @@ impl<'a> Parser<'a> {
709725
let open_paren = self.token.span;
710726

711727
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
712-
p.parse_pat_allow_top_alt(
728+
p.parse_pat_no_top_guard(
713729
None,
714730
RecoverComma::No,
715731
RecoverColon::No,
@@ -1118,7 +1134,7 @@ impl<'a> Parser<'a> {
11181134
path: Path,
11191135
) -> PResult<'a, PatKind> {
11201136
let (fields, _) = self.parse_paren_comma_seq(|p| {
1121-
p.parse_pat_allow_top_alt(
1137+
p.parse_pat_no_top_guard(
11221138
None,
11231139
RecoverComma::No,
11241140
RecoverColon::No,
@@ -1153,7 +1169,7 @@ impl<'a> Parser<'a> {
11531169
self.parse_builtin(|self_, _lo, ident| {
11541170
Ok(match ident.name {
11551171
// builtin#deref(PAT)
1156-
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_allow_top_alt(
1172+
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_no_top_guard(
11571173
None,
11581174
RecoverComma::Yes,
11591175
RecoverColon::Yes,
@@ -1401,7 +1417,7 @@ impl<'a> Parser<'a> {
14011417
// Parsing a pattern of the form `fieldname: pat`.
14021418
let fieldname = self.parse_field_name()?;
14031419
self.bump();
1404-
let pat = self.parse_pat_allow_top_alt(
1420+
let pat = self.parse_pat_no_top_guard(
14051421
None,
14061422
RecoverComma::No,
14071423
RecoverColon::No,

compiler/rustc_parse/src/parser/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl<'a> Parser<'a> {
457457
PathStyle::Pat
458458
if let Ok(_) = self
459459
.parse_paren_comma_seq(|p| {
460-
p.parse_pat_allow_top_alt(
460+
p.parse_pat_no_top_guard(
461461
None,
462462
RecoverComma::No,
463463
RecoverColon::No,

0 commit comments

Comments
 (0)