Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 16 additions & 18 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2947,26 +2947,24 @@ impl<'a> Parser<'a> {
}
let seq_span = lo.to(self.prev_token.span);
let mut err = self.dcx().struct_span_err(comma_span, "unexpected `,` in pattern");
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
err.multipart_suggestion(
format!(
"try adding parentheses to match on a tuple{}",
if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
),
vec![
(seq_span.shrink_to_lo(), "(".to_string()),
(seq_span.shrink_to_hi(), ")".to_string()),
],
err.multipart_suggestion(
format!(
"try adding parentheses to match on a tuple{}",
if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
),
vec![
(seq_span.shrink_to_lo(), "(".to_string()),
(seq_span.shrink_to_hi(), ")".to_string()),
],
Applicability::MachineApplicable,
);
if let CommaRecoveryMode::EitherTupleOrPipe = rt {
err.span_suggestion(
comma_span,
"...or a vertical bar to match on alternative",
" |",
Applicability::MachineApplicable,
);
if let CommaRecoveryMode::EitherTupleOrPipe = rt {
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 @@ -32,10 +32,10 @@ help: try adding parentheses to match on a tuple...
|
LL | (Nucleotide::Adenine, Nucleotide::Cytosine, _) => true
| + +
help: ...or a vertical bar to match on multiple alternatives
help: ...or a vertical bar to match on alternative
|
LL - Nucleotide::Adenine, Nucleotide::Cytosine, _ => true
LL + Nucleotide::Adenine | Nucleotide::Cytosine | _ => true
LL + Nucleotide::Adenine | Nucleotide::Cytosine, _ => true
|

error: unexpected `,` in pattern
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/feature-gates/feature-gate-never_patterns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ help: try adding parentheses to match on a tuple...
|
LL | (Some(_),)
| + +
help: ...or a vertical bar to match on multiple alternatives
help: ...or a vertical bar to match on alternative
|
LL - Some(_),
LL + Some(_) |
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/match-arm-without-body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ fn main() {
Some(_),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple
//~| HELP or a vertical bar to match on multiple alternatives
//~| HELP or a vertical bar to match on alternative
}
match Some(false) {
Some(_),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple
//~| HELP or a vertical bar to match on multiple alternatives
//~| HELP or a vertical bar to match on alternative
_ => {}
}
match Some(false) {
Expand Down
11 changes: 4 additions & 7 deletions tests/ui/parser/match-arm-without-body.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ help: try adding parentheses to match on a tuple...
|
LL | (Some(_),)
| + +
help: ...or a vertical bar to match on multiple alternatives
help: ...or a vertical bar to match on alternative
|
LL - Some(_),
LL + Some(_) |
Expand All @@ -36,13 +36,10 @@ LL |
LL |
LL ~ _) => {}
|
help: ...or a vertical bar to match on multiple alternatives
help: ...or a vertical bar to match on alternative
|
LL ~ Some(_) |
LL +
LL +
LL +
LL ~ _ => {}
LL - Some(_),
LL + Some(_) |
|

error: expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ run-rustfix

// Regression test for issue #143330.
// Ensure we suggest to replace only the intended bar with a comma, not all bars in the pattern.

fn main() {
struct Foo { x: i32, ch: char }
let pos = Foo { x: 2, ch: 'x' };
match pos {
// All commas here were replaced with bars.
// Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
(Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' }) => (),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple...
//~| HELP ...or a vertical bar to match on alternative
_ => todo!(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ run-rustfix

// Regression test for issue #143330.
// Ensure we suggest to replace only the intended bar with a comma, not all bars in the pattern.

fn main() {
struct Foo { x: i32, ch: char }
let pos = Foo { x: 2, ch: 'x' };
match pos {
// All commas here were replaced with bars.
// Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple...
//~| HELP ...or a vertical bar to match on alternative
_ => todo!(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: unexpected `,` in pattern
--> $DIR/only-replace-intended-bar-not-all-in-pattern.rs:12:30
|
LL | Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
| ^
|
help: try adding parentheses to match on a tuple...
|
LL | (Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' }) => (),
| + +
help: ...or a vertical bar to match on alternative
|
LL - Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
LL + Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' } => (),
|

error: aborting due to 1 previous error

Loading