Skip to content

Commit 823632b

Browse files
authored
Unrolled build for #147245
Rollup merge of #147245 - karolzwolak:only-replace-intended-bar-not-all-in-pattern, r=lcnr only replace the intended comma in pattern suggestions Only suggest to replace the intended comma, not all bars in the pattern. Fixes #143330. This continues #143331, the credit for making the fix goes to `@A4-Tacks.` I just blessed tests and added a regression test.
2 parents 7950f24 + d1d7b94 commit 823632b

8 files changed

+79
-30
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,26 +2939,24 @@ impl<'a> Parser<'a> {
29392939
}
29402940
let seq_span = lo.to(self.prev_token.span);
29412941
let mut err = self.dcx().struct_span_err(comma_span, "unexpected `,` in pattern");
2942-
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
2943-
err.multipart_suggestion(
2944-
format!(
2945-
"try adding parentheses to match on a tuple{}",
2946-
if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
2947-
),
2948-
vec![
2949-
(seq_span.shrink_to_lo(), "(".to_string()),
2950-
(seq_span.shrink_to_hi(), ")".to_string()),
2951-
],
2942+
err.multipart_suggestion(
2943+
format!(
2944+
"try adding parentheses to match on a tuple{}",
2945+
if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
2946+
),
2947+
vec![
2948+
(seq_span.shrink_to_lo(), "(".to_string()),
2949+
(seq_span.shrink_to_hi(), ")".to_string()),
2950+
],
2951+
Applicability::MachineApplicable,
2952+
);
2953+
if let CommaRecoveryMode::EitherTupleOrPipe = rt {
2954+
err.span_suggestion(
2955+
comma_span,
2956+
"...or a vertical bar to match on alternatives",
2957+
" |",
29522958
Applicability::MachineApplicable,
29532959
);
2954-
if let CommaRecoveryMode::EitherTupleOrPipe = rt {
2955-
err.span_suggestion(
2956-
seq_span,
2957-
"...or a vertical bar to match on multiple alternatives",
2958-
seq_snippet.replace(',', " |"),
2959-
Applicability::MachineApplicable,
2960-
);
2961-
}
29622960
}
29632961
Err(err)
29642962
}

tests/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ help: try adding parentheses to match on a tuple...
3232
|
3333
LL | (Nucleotide::Adenine, Nucleotide::Cytosine, _) => true
3434
| + +
35-
help: ...or a vertical bar to match on multiple alternatives
35+
help: ...or a vertical bar to match on alternatives
3636
|
3737
LL - Nucleotide::Adenine, Nucleotide::Cytosine, _ => true
38-
LL + Nucleotide::Adenine | Nucleotide::Cytosine | _ => true
38+
LL + Nucleotide::Adenine | Nucleotide::Cytosine, _ => true
3939
|
4040

4141
error: unexpected `,` in pattern

tests/ui/feature-gates/feature-gate-never_patterns.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ help: try adding parentheses to match on a tuple...
88
|
99
LL | (Some(_),)
1010
| + +
11-
help: ...or a vertical bar to match on multiple alternatives
11+
help: ...or a vertical bar to match on alternatives
1212
|
1313
LL - Some(_),
1414
LL + Some(_) |

tests/ui/parser/match-arm-without-body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ fn main() {
1717
Some(_),
1818
//~^ ERROR unexpected `,` in pattern
1919
//~| HELP try adding parentheses to match on a tuple
20-
//~| HELP or a vertical bar to match on multiple alternatives
20+
//~| HELP or a vertical bar to match on alternative
2121
}
2222
match Some(false) {
2323
Some(_),
2424
//~^ ERROR unexpected `,` in pattern
2525
//~| HELP try adding parentheses to match on a tuple
26-
//~| HELP or a vertical bar to match on multiple alternatives
26+
//~| HELP or a vertical bar to match on alternative
2727
_ => {}
2828
}
2929
match Some(false) {

tests/ui/parser/match-arm-without-body.stderr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ help: try adding parentheses to match on a tuple...
1616
|
1717
LL | (Some(_),)
1818
| + +
19-
help: ...or a vertical bar to match on multiple alternatives
19+
help: ...or a vertical bar to match on alternatives
2020
|
2121
LL - Some(_),
2222
LL + Some(_) |
@@ -36,13 +36,10 @@ LL |
3636
LL |
3737
LL ~ _) => {}
3838
|
39-
help: ...or a vertical bar to match on multiple alternatives
39+
help: ...or a vertical bar to match on alternatives
4040
|
41-
LL ~ Some(_) |
42-
LL +
43-
LL +
44-
LL +
45-
LL ~ _ => {}
41+
LL - Some(_),
42+
LL + Some(_) |
4643
|
4744

4845
error: expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ run-rustfix
2+
3+
// Regression test for issue #143330.
4+
// Ensure we suggest to replace only the intended coma with a bar, not all commas in the pattern.
5+
6+
fn main() {
7+
struct Foo { x: i32, ch: char }
8+
let pos = Foo { x: 2, ch: 'x' };
9+
match pos {
10+
// All commas here were replaced with bars.
11+
// Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
12+
(Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' }) => (),
13+
//~^ ERROR unexpected `,` in pattern
14+
//~| HELP try adding parentheses to match on a tuple...
15+
//~| HELP ...or a vertical bar to match on alternative
16+
_ => todo!(),
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ run-rustfix
2+
3+
// Regression test for issue #143330.
4+
// Ensure we suggest to replace only the intended coma with a bar, not all commas in the pattern.
5+
6+
fn main() {
7+
struct Foo { x: i32, ch: char }
8+
let pos = Foo { x: 2, ch: 'x' };
9+
match pos {
10+
// All commas here were replaced with bars.
11+
// Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
12+
Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
13+
//~^ ERROR unexpected `,` in pattern
14+
//~| HELP try adding parentheses to match on a tuple...
15+
//~| HELP ...or a vertical bar to match on alternative
16+
_ => todo!(),
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: unexpected `,` in pattern
2+
--> $DIR/only-replace-intended-coma-not-all-in-pattern.rs:12:30
3+
|
4+
LL | Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
5+
| ^
6+
|
7+
help: try adding parentheses to match on a tuple...
8+
|
9+
LL | (Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' }) => (),
10+
| + +
11+
help: ...or a vertical bar to match on alternatives
12+
|
13+
LL - Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
14+
LL + Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' } => (),
15+
|
16+
17+
error: aborting due to 1 previous error
18+

0 commit comments

Comments
 (0)