Skip to content

Commit a75ef1d

Browse files
committed
refactor pat parser method names/doc-comments to agree with RFC 3637
1 parent e0bbb38 commit a75ef1d

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
@@ -96,9 +96,24 @@ pub enum PatternLocation {
9696
impl<'a> Parser<'a> {
9797
/// Parses a pattern.
9898
///
99-
/// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
100-
/// at the top level. Used when parsing the parameters of lambda expressions,
101-
/// functions, function pointers, and `pat` macro fragments.
99+
/// Corresponds to `Pattern` in RFC 3637 and admits guard patterns at the top level.
100+
/// Used when parsing patterns in all cases where neither `PatternNoTopGuard` nor
101+
/// `PatternNoTopAlt` (see below) are used.
102+
pub fn parse_pat_allow_top_guard(
103+
&mut self,
104+
_expected: Option<Expected>,
105+
_rc: RecoverComma,
106+
_ra: RecoverColon,
107+
_rt: CommaRecoveryMode,
108+
) -> PResult<'a, P<Pat>> {
109+
todo!()
110+
}
111+
112+
/// Parses a pattern.
113+
///
114+
/// Corresponds to `PatternNoTopAlt` in RFC 3637 and does not admit or-patterns
115+
/// or guard patterns at the top level. Used when parsing the parameters of lambda
116+
/// expressions, functions, function pointers, and `pat_param` macro fragments.
102117
pub fn parse_pat_no_top_alt(
103118
&mut self,
104119
expected: Option<Expected>,
@@ -109,25 +124,26 @@ impl<'a> Parser<'a> {
109124

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

128144
/// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
129145
/// recovered).
130-
fn parse_pat_allow_top_alt_inner(
146+
fn parse_pat_no_top_guard_inner(
131147
&mut self,
132148
expected: Option<Expected>,
133149
rc: RecoverComma,
@@ -228,7 +244,7 @@ impl<'a> Parser<'a> {
228244
// We use `parse_pat_allow_top_alt` regardless of whether we actually want top-level
229245
// or-patterns so that we can detect when a user tries to use it. This allows us to print a
230246
// better error message.
231-
let (pat, trailing_vert) = self.parse_pat_allow_top_alt_inner(
247+
let (pat, trailing_vert) = self.parse_pat_no_top_guard_inner(
232248
expected,
233249
rc,
234250
RecoverColon::No,
@@ -693,7 +709,7 @@ impl<'a> Parser<'a> {
693709
} else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
694710
// Parse `[pat, pat,...]` as a slice pattern.
695711
let (pats, _) = self.parse_delim_comma_seq(Delimiter::Bracket, |p| {
696-
p.parse_pat_allow_top_alt(
712+
p.parse_pat_no_top_guard(
697713
None,
698714
RecoverComma::No,
699715
RecoverColon::No,
@@ -941,7 +957,7 @@ impl<'a> Parser<'a> {
941957
let open_paren = self.token.span;
942958

943959
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
944-
p.parse_pat_allow_top_alt(
960+
p.parse_pat_no_top_guard(
945961
None,
946962
RecoverComma::No,
947963
RecoverColon::No,
@@ -1356,7 +1372,7 @@ impl<'a> Parser<'a> {
13561372
path: Path,
13571373
) -> PResult<'a, PatKind> {
13581374
let (fields, _) = self.parse_paren_comma_seq(|p| {
1359-
p.parse_pat_allow_top_alt(
1375+
p.parse_pat_no_top_guard(
13601376
None,
13611377
RecoverComma::No,
13621378
RecoverColon::No,
@@ -1391,7 +1407,7 @@ impl<'a> Parser<'a> {
13911407
self.parse_builtin(|self_, _lo, ident| {
13921408
Ok(match ident.name {
13931409
// builtin#deref(PAT)
1394-
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_allow_top_alt(
1410+
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_no_top_guard(
13951411
None,
13961412
RecoverComma::Yes,
13971413
RecoverColon::Yes,
@@ -1639,7 +1655,7 @@ impl<'a> Parser<'a> {
16391655
// Parsing a pattern of the form `fieldname: pat`.
16401656
let fieldname = self.parse_field_name()?;
16411657
self.bump();
1642-
let pat = self.parse_pat_allow_top_alt(
1658+
let pat = self.parse_pat_no_top_guard(
16431659
None,
16441660
RecoverComma::No,
16451661
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)