Skip to content

Commit 21d9b85

Browse files
committed
parser: extract maybe_recover_unexpected_comma.
1 parent f852c7c commit 21d9b85

File tree

1 file changed

+36
-31
lines changed
  • src/libsyntax/parse/parser

1 file changed

+36
-31
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,40 +57,45 @@ impl<'a> Parser<'a> {
5757
/// to subpatterns within such).
5858
pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
5959
let pat = self.parse_pat(None)?;
60-
if self.token == token::Comma {
61-
// An unexpected comma after a top-level pattern is a clue that the
62-
// user (perhaps more accustomed to some other language) forgot the
63-
// parentheses in what should have been a tuple pattern; return a
64-
// suggestion-enhanced error here rather than choking on the comma
65-
// later.
66-
let comma_span = self.token.span;
67-
self.bump();
68-
if let Err(mut err) = self.skip_pat_list() {
69-
// We didn't expect this to work anyway; we just wanted
70-
// to advance to the end of the comma-sequence so we know
71-
// the span to suggest parenthesizing
72-
err.cancel();
73-
}
74-
let seq_span = pat.span.to(self.prev_span);
75-
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
76-
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
77-
err.span_suggestion(
78-
seq_span,
79-
"try adding parentheses to match on a tuple..",
80-
format!("({})", seq_snippet),
81-
Applicability::MachineApplicable
82-
).span_suggestion(
83-
seq_span,
84-
"..or a vertical bar to match on multiple alternatives",
85-
format!("{}", seq_snippet.replace(",", " |")),
86-
Applicability::MachineApplicable
87-
);
88-
}
89-
return Err(err);
90-
}
60+
self.maybe_recover_unexpected_comma(pat.span)?;
9161
Ok(pat)
9262
}
9363

64+
fn maybe_recover_unexpected_comma(&mut self, lo: Span) -> PResult<'a, ()> {
65+
if self.token != token::Comma {
66+
return Ok(());
67+
}
68+
69+
// An unexpected comma after a top-level pattern is a clue that the
70+
// user (perhaps more accustomed to some other language) forgot the
71+
// parentheses in what should have been a tuple pattern; return a
72+
// suggestion-enhanced error here rather than choking on the comma later.
73+
let comma_span = self.token.span;
74+
self.bump();
75+
if let Err(mut err) = self.skip_pat_list() {
76+
// We didn't expect this to work anyway; we just wanted to advance to the
77+
// end of the comma-sequence so we know the span to suggest parenthesizing.
78+
err.cancel();
79+
}
80+
let seq_span = lo.to(self.prev_span);
81+
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
82+
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
83+
err.span_suggestion(
84+
seq_span,
85+
"try adding parentheses to match on a tuple..",
86+
format!("({})", seq_snippet),
87+
Applicability::MachineApplicable
88+
)
89+
.span_suggestion(
90+
seq_span,
91+
"..or a vertical bar to match on multiple alternatives",
92+
format!("{}", seq_snippet.replace(",", " |")),
93+
Applicability::MachineApplicable
94+
);
95+
}
96+
Err(err)
97+
}
98+
9499
/// Parse and throw away a parentesized comma separated
95100
/// sequence of patterns until `)` is reached.
96101
fn skip_pat_list(&mut self) -> PResult<'a, ()> {

0 commit comments

Comments
 (0)