Skip to content

Fixes #79357 unstable or-pat suggestions #79364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
const WHILE_PARSING_OR_MSG: &str = "while parsing this or-pattern starting here";

/// Whether or not an or-pattern should be gated when occurring in the current context.
#[derive(PartialEq)]
#[derive(PartialEq, Clone, Copy)]
pub(super) enum GateOr {
Yes,
No,
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<'a> Parser<'a> {
) -> PResult<'a, P<Pat>> {
// Parse the first pattern (`p_0`).
let first_pat = self.parse_pat(expected)?;
self.maybe_recover_unexpected_comma(first_pat.span, rc)?;
self.maybe_recover_unexpected_comma(first_pat.span, rc, gate_or)?;

// If the next token is not a `|`,
// this is not an or-pattern and we should exit here.
Expand All @@ -110,7 +110,7 @@ impl<'a> Parser<'a> {
err.span_label(lo, WHILE_PARSING_OR_MSG);
err
})?;
self.maybe_recover_unexpected_comma(pat.span, rc)?;
self.maybe_recover_unexpected_comma(pat.span, rc, gate_or)?;
pats.push(pat);
}
let or_pattern_span = lo.to(self.prev_token.span);
Expand Down Expand Up @@ -190,7 +190,12 @@ impl<'a> Parser<'a> {

/// Some special error handling for the "top-level" patterns in a match arm,
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
fn maybe_recover_unexpected_comma(&mut self, lo: Span, rc: RecoverComma) -> PResult<'a, ()> {
fn maybe_recover_unexpected_comma(
&mut self,
lo: Span,
rc: RecoverComma,
gate_or: GateOr,
) -> PResult<'a, ()> {
if rc == RecoverComma::No || self.token != token::Comma {
return Ok(());
}
Expand All @@ -209,18 +214,24 @@ impl<'a> Parser<'a> {
let seq_span = lo.to(self.prev_token.span);
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
const MSG: &str = "try adding parentheses to match on a tuple...";

let or_suggestion =
gate_or == GateOr::No || !self.sess.gated_spans.is_ungated(sym::or_patterns);
err.span_suggestion(
seq_span,
"try adding parentheses to match on a tuple...",
if or_suggestion { MSG } else { MSG.trim_end_matches('.') },
format!("({})", seq_snippet),
Applicability::MachineApplicable,
)
.span_suggestion(
seq_span,
"...or a vertical bar to match on multiple alternatives",
seq_snippet.replace(",", " |"),
Applicability::MachineApplicable,
);
if or_suggestion {
err.span_suggestion(
seq_span,
"...or a vertical bar to match on multiple alternatives",
seq_snippet.replace(",", " |"),
Applicability::MachineApplicable,
);
}
}
Err(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,46 +47,19 @@ error: unexpected `,` in pattern
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:67:10
|
LL | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
| ^
|
help: try adding parentheses to match on a tuple...
|
LL | for (x, _barr_body) in women.iter().map(|woman| woman.allosomes.clone()) {
| ^^^^^^^^^^^^^^^
help: ...or a vertical bar to match on multiple alternatives
|
LL | for x | _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
| ^^^^^^^^^^^^^^
| -^----------- help: try adding parentheses to match on a tuple: `(x, _barr_body)`

error: unexpected `,` in pattern
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:75:10
|
LL | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
| ^
|
help: try adding parentheses to match on a tuple...
|
LL | for (x, y @ Allosome::Y(_)) in men.iter().map(|man| man.allosomes.clone()) {
| ^^^^^^^^^^^^^^^^^^^^^^^
help: ...or a vertical bar to match on multiple alternatives
|
LL | for x | y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
| ^^^^^^^^^^^^^^^^^^^^^^
| -^------------------- help: try adding parentheses to match on a tuple: `(x, y @ Allosome::Y(_))`

error: unexpected `,` in pattern
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14
|
LL | let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
| ^
|
help: try adding parentheses to match on a tuple...
|
LL | let (women, men): (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
| ^^^^^^^^^^^^
help: ...or a vertical bar to match on multiple alternatives
|
LL | let women | men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
| ^^^^^^^^^^^
| -----^---- help: try adding parentheses to match on a tuple: `(women, men)`

error: aborting due to 6 previous errors