Skip to content

Commit b7657e9

Browse files
authored
Rollup merge of #106066 - JohnTitor:rm-bindings-after-at-fixme, r=compiler-errors
Always suggest as `MachineApplicable` in `recover_intersection_pat` This resolves one FIXME in `recover_intersection_pat` by always applying `MachineApplicable` when suggesting, as `bindings_after_at` is now stable. This also separates a test to apply `// run-rustfix`. Signed-off-by: Yuki Okushi <[email protected]>
2 parents d5b975c + 668d9fd commit b7657e9

File tree

6 files changed

+81
-35
lines changed

6 files changed

+81
-35
lines changed

compiler/rustc_parse/src/parser/pat.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -491,17 +491,6 @@ impl<'a> Parser<'a> {
491491

492492
if let PatKind::Ident(_, _, sub @ None) = &mut rhs.kind {
493493
// The user inverted the order, so help them fix that.
494-
let mut applicability = Applicability::MachineApplicable;
495-
// FIXME(bindings_after_at): Remove this code when stabilizing the feature.
496-
lhs.walk(&mut |p| match p.kind {
497-
// `check_match` is unhappy if the subpattern has a binding anywhere.
498-
PatKind::Ident(..) => {
499-
applicability = Applicability::MaybeIncorrect;
500-
false // Short-circuit.
501-
}
502-
_ => true,
503-
});
504-
505494
let lhs_span = lhs.span;
506495
// Move the LHS into the RHS as a subpattern.
507496
// The RHS is now the full pattern.
@@ -510,7 +499,12 @@ impl<'a> Parser<'a> {
510499
self.struct_span_err(sp, "pattern on wrong side of `@`")
511500
.span_label(lhs_span, "pattern on the left, should be on the right")
512501
.span_label(rhs.span, "binding on the right, should be on the left")
513-
.span_suggestion(sp, "switch the order", pprust::pat_to_string(&rhs), applicability)
502+
.span_suggestion(
503+
sp,
504+
"switch the order",
505+
pprust::pat_to_string(&rhs),
506+
Applicability::MachineApplicable,
507+
)
514508
.emit();
515509
} else {
516510
// The special case above doesn't apply so we may have e.g. `A(x) @ B(y)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This tests the parser recovery in `recover_intersection_pat`
2+
// and serves as a regression test for the diagnostics issue #65400.
3+
//
4+
// The general idea is that for `$pat_lhs @ $pat_rhs` where
5+
// `$pat_lhs` is not generated by `ref? mut? $ident` we want
6+
// to suggest either switching the order or note that intersection
7+
// patterns are not allowed.
8+
9+
// run-rustfix
10+
11+
#![allow(unused_variables)]
12+
13+
fn main() {
14+
let s: Option<u8> = None;
15+
16+
match s {
17+
y @ Some(x) => {}
18+
//~^ ERROR pattern on wrong side of `@`
19+
//~| pattern on the left, should be on the right
20+
//~| binding on the right, should be on the left
21+
//~| HELP switch the order
22+
//~| SUGGESTION y @ Some(x)
23+
_ => {}
24+
}
25+
26+
match 2 {
27+
e @ 1..=5 => {}
28+
//~^ ERROR pattern on wrong side of `@`
29+
//~| pattern on the left, should be on the right
30+
//~| binding on the right, should be on the left
31+
//~| HELP switch the order
32+
//~| SUGGESTION e @ 1..=5
33+
_ => {}
34+
}
35+
}

src/test/ui/parser/intersection-patterns.rs renamed to src/test/ui/parser/intersection-patterns-1.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
// to suggest either switching the order or note that intersection
77
// patterns are not allowed.
88

9+
// run-rustfix
10+
11+
#![allow(unused_variables)]
12+
913
fn main() {
1014
let s: Option<u8> = None;
1115

@@ -19,15 +23,6 @@ fn main() {
1923
_ => {}
2024
}
2125

22-
match s {
23-
Some(x) @ Some(y) => {}
24-
//~^ ERROR left-hand side of `@` must be a binding
25-
//~| interpreted as a pattern, not a binding
26-
//~| also a pattern
27-
//~| NOTE bindings are `x`, `mut x`, `ref x`, and `ref mut x`
28-
_ => {}
29-
}
30-
3126
match 2 {
3227
1 ..= 5 @ e => {}
3328
//~^ ERROR pattern on wrong side of `@`
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: pattern on wrong side of `@`
2-
--> $DIR/intersection-patterns.rs:13:9
2+
--> $DIR/intersection-patterns-1.rs:17:9
33
|
44
LL | Some(x) @ y => {}
55
| -------^^^-
@@ -8,19 +8,8 @@ LL | Some(x) @ y => {}
88
| pattern on the left, should be on the right
99
| help: switch the order: `y @ Some(x)`
1010

11-
error: left-hand side of `@` must be a binding
12-
--> $DIR/intersection-patterns.rs:23:9
13-
|
14-
LL | Some(x) @ Some(y) => {}
15-
| -------^^^-------
16-
| | |
17-
| | also a pattern
18-
| interpreted as a pattern, not a binding
19-
|
20-
= note: bindings are `x`, `mut x`, `ref x`, and `ref mut x`
21-
2211
error: pattern on wrong side of `@`
23-
--> $DIR/intersection-patterns.rs:32:9
12+
--> $DIR/intersection-patterns-1.rs:27:9
2413
|
2514
LL | 1 ..= 5 @ e => {}
2615
| -------^^^-
@@ -29,5 +18,5 @@ LL | 1 ..= 5 @ e => {}
2918
| pattern on the left, should be on the right
3019
| help: switch the order: `e @ 1..=5`
3120

32-
error: aborting due to 3 previous errors
21+
error: aborting due to 2 previous errors
3322

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This tests the parser recovery in `recover_intersection_pat`
2+
// and serves as a regression test for the diagnostics issue #65400.
3+
//
4+
// The general idea is that for `$pat_lhs @ $pat_rhs` where
5+
// `$pat_lhs` is not generated by `ref? mut? $ident` we want
6+
// to suggest either switching the order or note that intersection
7+
// patterns are not allowed.
8+
9+
fn main() {
10+
let s: Option<u8> = None;
11+
12+
match s {
13+
Some(x) @ Some(y) => {}
14+
//~^ ERROR left-hand side of `@` must be a binding
15+
//~| interpreted as a pattern, not a binding
16+
//~| also a pattern
17+
//~| NOTE bindings are `x`, `mut x`, `ref x`, and `ref mut x`
18+
_ => {}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: left-hand side of `@` must be a binding
2+
--> $DIR/intersection-patterns-2.rs:13:9
3+
|
4+
LL | Some(x) @ Some(y) => {}
5+
| -------^^^-------
6+
| | |
7+
| | also a pattern
8+
| interpreted as a pattern, not a binding
9+
|
10+
= note: bindings are `x`, `mut x`, `ref x`, and `ref mut x`
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)