Rollup merge of #87160 - estebank:colon-recovery, r=nagisa

When recovering from a `:` in a pattern, use adequate AST pattern

If the suggestion to use `::` instead of `:` in the pattern isn't correct, a second resolution error will be emitted.
This commit is contained in:
Matthias Krüger 2021-12-01 10:50:18 +01:00 committed by GitHub
commit e68e5d2391
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 253 additions and 135 deletions

View file

@ -1,11 +1,15 @@
// Tests that a suggestion is issued if the user wrote a colon instead of
// a path separator in a match arm.
enum Foo {
Bar,
Baz,
mod qux {
pub enum Foo {
Bar,
Baz,
}
}
use qux::Foo;
fn f() -> Foo { Foo::Bar }
fn g1() {
@ -16,24 +20,24 @@ fn g1() {
_ => {}
}
match f() {
Foo::Bar:Baz => {}
qux::Foo:Bar => {}
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
_ => {}
}
match f() {
Foo:Bar::Baz => {}
qux:Foo::Baz => {}
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
_ => {}
}
match f() {
Foo: Bar::Baz if true => {}
qux: Foo::Baz if true => {}
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
_ => {}
}
if let Bar:Baz = f() {
if let Foo:Bar = f() {
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
}
@ -41,16 +45,18 @@ fn g1() {
fn g1_neg() {
match f() {
ref Foo: Bar::Baz => {}
ref qux: Foo::Baz => {}
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
_ => {}
}
}
fn g2_neg() {
match f() {
mut Foo: Bar::Baz => {}
mut qux: Foo::Baz => {}
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
_ => {}
}
}
@ -62,5 +68,12 @@ fn main() {
Foo:Bar::Baz => {}
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
//~| ERROR: failed to resolve: `Bar` is a variant, not a module
}
match myfoo {
Foo::Bar => {}
Foo:Bar => {}
//~^ ERROR: expected one of
//~| HELP: maybe write a path separator here
}
}

View file

@ -1,5 +1,5 @@
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:13:12
--> $DIR/issue-87086-colon-path-sep.rs:17:12
|
LL | Foo:Bar => {}
| ^
@ -8,55 +8,61 @@ LL | Foo:Bar => {}
| help: maybe write a path separator here: `::`
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `{`, or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:19:17
--> $DIR/issue-87086-colon-path-sep.rs:23:17
|
LL | Foo::Bar:Baz => {}
LL | qux::Foo:Bar => {}
| ^
| |
| expected one of 8 possible tokens
| help: maybe write a path separator here: `::`
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:25:12
--> $DIR/issue-87086-colon-path-sep.rs:29:12
|
LL | Foo:Bar::Baz => {}
LL | qux:Foo::Baz => {}
| ^
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:31:12
--> $DIR/issue-87086-colon-path-sep.rs:35:12
|
LL | Foo: Bar::Baz if true => {}
LL | qux: Foo::Baz if true => {}
| ^
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:36:15
--> $DIR/issue-87086-colon-path-sep.rs:40:15
|
LL | if let Bar:Baz = f() {
LL | if let Foo:Bar = f() {
| ^
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
error: expected one of `=>`, `@`, `if`, or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:44:16
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:48:16
|
LL | ref Foo: Bar::Baz => {}
| ^ expected one of `=>`, `@`, `if`, or `|`
error: expected one of `=>`, `@`, `if`, or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:52:16
|
LL | mut Foo: Bar::Baz => {}
| ^ expected one of `=>`, `@`, `if`, or `|`
LL | ref qux: Foo::Baz => {}
| ^
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:62:12
--> $DIR/issue-87086-colon-path-sep.rs:57:16
|
LL | mut qux: Foo::Baz => {}
| ^
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:68:12
|
LL | Foo:Bar::Baz => {}
| ^
@ -64,5 +70,21 @@ LL | Foo:Bar::Baz => {}
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
error: aborting due to 8 previous errors
error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:75:12
|
LL | Foo:Bar => {}
| ^
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
error[E0433]: failed to resolve: `Bar` is a variant, not a module
--> $DIR/issue-87086-colon-path-sep.rs:68:13
|
LL | Foo:Bar::Baz => {}
| ^^^ `Bar` is a variant, not a module
error: aborting due to 10 previous errors
For more information about this error, try `rustc --explain E0433`.