Skip to content

Commit 50398f2

Browse files
committed
refactor pat parser method names/doc-comments to agree with RFC 3637
1 parent 140b6ba commit 50398f2

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
@@ -989,7 +989,7 @@ pub fn parse_ast_fragment<'a>(
989989
}
990990
}
991991
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
992-
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_alt(
992+
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_no_top_guard(
993993
None,
994994
RecoverComma::No,
995995
RecoverColon::Yes,

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,7 @@ impl<'a> Parser<'a> {
26302630
};
26312631
self.bump(); // Eat `let` token
26322632
let lo = self.prev_token.span;
2633-
let pat = self.parse_pat_allow_top_alt(
2633+
let pat = self.parse_pat_no_top_guard(
26342634
None,
26352635
RecoverComma::Yes,
26362636
RecoverColon::Yes,
@@ -2767,7 +2767,7 @@ impl<'a> Parser<'a> {
27672767
};
27682768
// Try to parse the pattern `for ($PAT) in $EXPR`.
27692769
let pat = match (
2770-
self.parse_pat_allow_top_alt(
2770+
self.parse_pat_no_top_guard(
27712771
None,
27722772
RecoverComma::Yes,
27732773
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
@@ -97,9 +97,24 @@ pub enum PatternLocation {
9797
impl<'a> Parser<'a> {
9898
/// Parses a pattern.
9999
///
100-
/// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
101-
/// at the top level. Used when parsing the parameters of lambda expressions,
102-
/// functions, function pointers, and `pat` macro fragments.
100+
/// Corresponds to `Pattern` in RFC 3637 and admits guard patterns at the top level.
101+
/// Used when parsing patterns in all cases where neither `PatternNoTopGuard` nor
102+
/// `PatternNoTopAlt` (see below) are used.
103+
pub fn parse_pat_allow_top_guard(
104+
&mut self,
105+
_expected: Option<Expected>,
106+
_rc: RecoverComma,
107+
_ra: RecoverColon,
108+
_rt: CommaRecoveryMode,
109+
) -> PResult<'a, P<Pat>> {
110+
todo!()
111+
}
112+
113+
/// Parses a pattern.
114+
///
115+
/// Corresponds to `PatternNoTopAlt` in RFC 3637 and does not admit or-patterns
116+
/// or guard patterns at the top level. Used when parsing the parameters of lambda
117+
/// expressions, functions, function pointers, and `pat_param` macro fragments.
103118
pub fn parse_pat_no_top_alt(
104119
&mut self,
105120
expected: Option<Expected>,
@@ -110,25 +125,26 @@ impl<'a> Parser<'a> {
110125

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

129145
/// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
130146
/// recovered).
131-
fn parse_pat_allow_top_alt_inner(
147+
fn parse_pat_no_top_guard_inner(
132148
&mut self,
133149
expected: Option<Expected>,
134150
rc: RecoverComma,
@@ -229,7 +245,7 @@ impl<'a> Parser<'a> {
229245
// We use `parse_pat_allow_top_alt` regardless of whether we actually want top-level
230246
// or-patterns so that we can detect when a user tries to use it. This allows us to print a
231247
// better error message.
232-
let (pat, trailing_vert) = self.parse_pat_allow_top_alt_inner(
248+
let (pat, trailing_vert) = self.parse_pat_no_top_guard_inner(
233249
expected,
234250
rc,
235251
RecoverColon::No,
@@ -694,7 +710,7 @@ impl<'a> Parser<'a> {
694710
} else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
695711
// Parse `[pat, pat,...]` as a slice pattern.
696712
let (pats, _) = self.parse_delim_comma_seq(Delimiter::Bracket, |p| {
697-
p.parse_pat_allow_top_alt(
713+
p.parse_pat_no_top_guard(
698714
None,
699715
RecoverComma::No,
700716
RecoverColon::No,
@@ -942,7 +958,7 @@ impl<'a> Parser<'a> {
942958
let open_paren = self.token.span;
943959

944960
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
945-
p.parse_pat_allow_top_alt(
961+
p.parse_pat_no_top_guard(
946962
None,
947963
RecoverComma::No,
948964
RecoverColon::No,
@@ -1357,7 +1373,7 @@ impl<'a> Parser<'a> {
13571373
path: Path,
13581374
) -> PResult<'a, PatKind> {
13591375
let (fields, _) = self.parse_paren_comma_seq(|p| {
1360-
p.parse_pat_allow_top_alt(
1376+
p.parse_pat_no_top_guard(
13611377
None,
13621378
RecoverComma::No,
13631379
RecoverColon::No,
@@ -1392,7 +1408,7 @@ impl<'a> Parser<'a> {
13921408
self.parse_builtin(|self_, _lo, ident| {
13931409
Ok(match ident.name {
13941410
// builtin#deref(PAT)
1395-
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_allow_top_alt(
1411+
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_no_top_guard(
13961412
None,
13971413
RecoverComma::Yes,
13981414
RecoverColon::Yes,
@@ -1667,7 +1683,7 @@ impl<'a> Parser<'a> {
16671683
// Parsing a pattern of the form `fieldname: pat`.
16681684
let fieldname = self.parse_field_name()?;
16691685
self.bump();
1670-
let pat = self.parse_pat_allow_top_alt(
1686+
let pat = self.parse_pat_no_top_guard(
16711687
None,
16721688
RecoverComma::No,
16731689
RecoverColon::No,

compiler/rustc_parse/src/parser/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'a> Parser<'a> {
469469
PathStyle::Pat
470470
if let Ok(_) = self
471471
.parse_paren_comma_seq(|p| {
472-
p.parse_pat_allow_top_alt(
472+
p.parse_pat_no_top_guard(
473473
None,
474474
RecoverComma::No,
475475
RecoverColon::No,

0 commit comments

Comments
 (0)