-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #65410 - Centril:intersection-pat-recover, r=davidtwc…
…o,varkor syntax: add parser recovery for intersection- / and-patterns `p1 @ p2` Fixes #65400. The recovery comes in two flavors: 1. We know that `p2` is a binding so we can invert as `p2 @ p1`: ```rust error: pattern on wrong side of `@` --> $DIR/intersection-patterns.rs:13:9 | LL | Some(x) @ y => {} | -------^^^- | | | | | binding on the right, should be to the left | pattern on the left, should be to the right | help: switch the order: `y @ Some(x)` ``` 2. Otherwise we emit a generic diagnostic for the lack of support for intersection patterns: ```rust error: left-hand side of `@` must be a binding --> $DIR/intersection-patterns.rs:23:9 | LL | Some(x) @ Some(y) => {} | -------^^^------- | | | | | also a pattern | interpreted as a pattern, not a binding | = note: bindings are `x`, `mut x`, `ref x`, and `ref mut x` ``` For more on and-patterns, see e.g. https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching#and-pattern. r? @davidtwco cc @varkor @lzutao
- Loading branch information
Showing
4 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// This tests the parser recovery in `recover_intersection_pat` | ||
// and serves as a regression test for the diagnostics issue #65400. | ||
// | ||
// The general idea is that for `$pat_lhs @ $pat_rhs` where | ||
// `$pat_lhs` is not generated by `ref? mut? $ident` we want | ||
// to suggest either switching the order or note that intersection | ||
// patterns are not allowed. | ||
|
||
fn main() { | ||
let s: Option<u8> = None; | ||
|
||
match s { | ||
Some(x) @ y => {} | ||
//~^ ERROR pattern on wrong side of `@` | ||
//~| pattern on the left, should be on the right | ||
//~| binding on the right, should be on the left | ||
//~| HELP switch the order | ||
//~| SUGGESTION y @ Some(x) | ||
_ => {} | ||
} | ||
|
||
match s { | ||
Some(x) @ Some(y) => {} | ||
//~^ ERROR left-hand side of `@` must be a binding | ||
//~| interpreted as a pattern, not a binding | ||
//~| also a pattern | ||
//~| NOTE bindings are `x`, `mut x`, `ref x`, and `ref mut x` | ||
_ => {} | ||
} | ||
|
||
match 2 { | ||
1 ..= 5 @ e => {} | ||
//~^ ERROR pattern on wrong side of `@` | ||
//~| pattern on the left, should be on the right | ||
//~| binding on the right, should be on the left | ||
//~| HELP switch the order | ||
//~| SUGGESTION e @ 1 ..=5 | ||
_ => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error: pattern on wrong side of `@` | ||
--> $DIR/intersection-patterns.rs:13:9 | ||
| | ||
LL | Some(x) @ y => {} | ||
| -------^^^- | ||
| | | | ||
| | binding on the right, should be on the left | ||
| pattern on the left, should be on the right | ||
| help: switch the order: `y @ Some(x)` | ||
|
||
error: left-hand side of `@` must be a binding | ||
--> $DIR/intersection-patterns.rs:23:9 | ||
| | ||
LL | Some(x) @ Some(y) => {} | ||
| -------^^^------- | ||
| | | | ||
| | also a pattern | ||
| interpreted as a pattern, not a binding | ||
| | ||
= note: bindings are `x`, `mut x`, `ref x`, and `ref mut x` | ||
|
||
error: pattern on wrong side of `@` | ||
--> $DIR/intersection-patterns.rs:32:9 | ||
| | ||
LL | 1 ..= 5 @ e => {} | ||
| -------^^^- | ||
| | | | ||
| | binding on the right, should be on the left | ||
| pattern on the left, should be on the right | ||
| help: switch the order: `e @ 1 ..=5` | ||
|
||
error: aborting due to 3 previous errors | ||
|