Skip to content

Commit 39f5e5b

Browse files
committed
parser: move maybe_recover_unexpected_comma to a more appropriate place.
1 parent 6498959 commit 39f5e5b

File tree

1 file changed

+49
-50
lines changed
  • src/libsyntax/parse/parser

1 file changed

+49
-50
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,62 +35,12 @@ impl<'a> Parser<'a> {
3535
}
3636
}
3737

38-
/// A wrapper around `parse_pat` with some special error handling for the
39-
/// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
40-
/// to subpatterns within such).
4138
pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
4239
let pat = self.parse_pat(None)?;
4340
self.maybe_recover_unexpected_comma(pat.span, true)?;
4441
Ok(pat)
4542
}
4643

47-
fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: bool) -> PResult<'a, ()> {
48-
if !top_level || self.token != token::Comma {
49-
return Ok(());
50-
}
51-
52-
// An unexpected comma after a top-level pattern is a clue that the
53-
// user (perhaps more accustomed to some other language) forgot the
54-
// parentheses in what should have been a tuple pattern; return a
55-
// suggestion-enhanced error here rather than choking on the comma later.
56-
let comma_span = self.token.span;
57-
self.bump();
58-
if let Err(mut err) = self.skip_pat_list() {
59-
// We didn't expect this to work anyway; we just wanted to advance to the
60-
// end of the comma-sequence so we know the span to suggest parenthesizing.
61-
err.cancel();
62-
}
63-
let seq_span = lo.to(self.prev_span);
64-
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
65-
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
66-
err.span_suggestion(
67-
seq_span,
68-
"try adding parentheses to match on a tuple..",
69-
format!("({})", seq_snippet),
70-
Applicability::MachineApplicable
71-
)
72-
.span_suggestion(
73-
seq_span,
74-
"..or a vertical bar to match on multiple alternatives",
75-
format!("{}", seq_snippet.replace(",", " |")),
76-
Applicability::MachineApplicable
77-
);
78-
}
79-
Err(err)
80-
}
81-
82-
/// Parse and throw away a parentesized comma separated
83-
/// sequence of patterns until `)` is reached.
84-
fn skip_pat_list(&mut self) -> PResult<'a, ()> {
85-
while !self.check(&token::CloseDelim(token::Paren)) {
86-
self.parse_pat(None)?;
87-
if !self.eat(&token::Comma) {
88-
return Ok(())
89-
}
90-
}
91-
Ok(())
92-
}
93-
9444
/// Parses a pattern, that may be a or-pattern (e.g. `Foo | Bar` in `Some(Foo | Bar)`).
9545
/// Corresponds to `pat<allow_top_alt>` in RFC 2535.
9646
fn parse_pat_with_or(
@@ -151,6 +101,55 @@ impl<'a> Parser<'a> {
151101
.emit();
152102
}
153103

104+
/// Some special error handling for the "top-level" patterns in a match arm,
105+
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
106+
fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: bool) -> PResult<'a, ()> {
107+
if !top_level || self.token != token::Comma {
108+
return Ok(());
109+
}
110+
111+
// An unexpected comma after a top-level pattern is a clue that the
112+
// user (perhaps more accustomed to some other language) forgot the
113+
// parentheses in what should have been a tuple pattern; return a
114+
// suggestion-enhanced error here rather than choking on the comma later.
115+
let comma_span = self.token.span;
116+
self.bump();
117+
if let Err(mut err) = self.skip_pat_list() {
118+
// We didn't expect this to work anyway; we just wanted to advance to the
119+
// end of the comma-sequence so we know the span to suggest parenthesizing.
120+
err.cancel();
121+
}
122+
let seq_span = lo.to(self.prev_span);
123+
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
124+
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
125+
err.span_suggestion(
126+
seq_span,
127+
"try adding parentheses to match on a tuple..",
128+
format!("({})", seq_snippet),
129+
Applicability::MachineApplicable
130+
)
131+
.span_suggestion(
132+
seq_span,
133+
"..or a vertical bar to match on multiple alternatives",
134+
format!("{}", seq_snippet.replace(",", " |")),
135+
Applicability::MachineApplicable
136+
);
137+
}
138+
Err(err)
139+
}
140+
141+
/// Parse and throw away a parentesized comma separated
142+
/// sequence of patterns until `)` is reached.
143+
fn skip_pat_list(&mut self) -> PResult<'a, ()> {
144+
while !self.check(&token::CloseDelim(token::Paren)) {
145+
self.parse_pat(None)?;
146+
if !self.eat(&token::Comma) {
147+
return Ok(())
148+
}
149+
}
150+
Ok(())
151+
}
152+
154153
/// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
155154
/// allowed).
156155
fn parse_pat_with_range_pat(

0 commit comments

Comments
 (0)