Skip to content

Parse and recover from type ascription in patterns #107602

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 39 additions & 12 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,26 +2405,42 @@ impl<'a> Parser<'a> {
if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..))
|| !self.look_ahead(1, |token| token.is_ident() && !token.is_reserved_ident())
{
let mut snapshot_type = self.create_snapshot_for_diagnostic();
snapshot_type.bump(); // `:`
match snapshot_type.parse_ty() {
Err(inner_err) => {
inner_err.cancel();
}
Ok(ty) => {
let Err(mut err) = self.expected_one_of_not_found(&[], &[]) else {
return first_pat;
};
err.span_label(ty.span, "specifying the type of a pattern isn't supported");
self.restore_snapshot(snapshot_type);
let span = first_pat.span.to(ty.span);
first_pat = self.mk_pat(span, PatKind::Wild);
err.emit();
}
}
return first_pat;
}
// The pattern looks like it might be a path with a `::` -> `:` typo:
// `match foo { bar:baz => {} }`
let span = self.token.span;
let colon_span = self.token.span;
// We only emit "unexpected `:`" error here if we can successfully parse the
// whole pattern correctly in that case.
let snapshot = self.create_snapshot_for_diagnostic();
let mut snapshot_pat = self.create_snapshot_for_diagnostic();
let mut snapshot_type = self.create_snapshot_for_diagnostic();

// Create error for "unexpected `:`".
match self.expected_one_of_not_found(&[], &[]) {
Err(mut err) => {
self.bump(); // Skip the `:`.
match self.parse_pat_no_top_alt(expected) {
// Skip the `:`.
snapshot_pat.bump();
snapshot_type.bump();
match snapshot_pat.parse_pat_no_top_alt(expected) {
Err(inner_err) => {
// Carry on as if we had not done anything, callers will emit a
// reasonable error.
inner_err.cancel();
err.cancel();
self.restore_snapshot(snapshot);
}
Ok(mut pat) => {
// We've parsed the rest of the pattern.
Expand Down Expand Up @@ -2488,22 +2504,33 @@ impl<'a> Parser<'a> {
_ => {}
}
if show_sugg {
err.span_suggestion(
span,
err.span_suggestion_verbose(
colon_span.until(self.look_ahead(1, |t| t.span)),
"maybe write a path separator here",
"::",
Applicability::MaybeIncorrect,
);
} else {
first_pat = self.mk_pat(new_span, PatKind::Wild);
}
err.emit();
self.restore_snapshot(snapshot_pat);
}
}
match snapshot_type.parse_ty() {
Err(inner_err) => {
inner_err.cancel();
}
Ok(ty) => {
err.span_label(ty.span, "specifying the type of a pattern isn't supported");
self.restore_snapshot(snapshot_type);
let new_span = first_pat.span.to(ty.span);
first_pat = self.mk_pat(new_span, PatKind::Wild);
}
}
err.emit();
}
_ => {
// Carry on as if we had not done anything. This should be unreachable.
self.restore_snapshot(snapshot);
}
};
first_pat
Expand Down
1 change: 0 additions & 1 deletion tests/ui/parser/issues/issue-87086-colon-path-sep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ 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 => {}
Expand Down
81 changes: 55 additions & 26 deletions tests/ui/parser/issues/issue-87086-colon-path-sep.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,118 @@ error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:17:12
|
LL | Foo:Bar => {}
| ^
| ^--- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | Foo::Bar => {}
| ~~

error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `{`, or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:23:17
|
LL | qux::Foo:Bar => {}
| ^
| ^--- specifying the type of a pattern isn't supported
| |
| expected one of 8 possible tokens
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | qux::Foo::Bar => {}
| ~~

error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:29:12
|
LL | qux:Foo::Baz => {}
| ^
| ^-------- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | qux::Foo::Baz => {}
| ~~

error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:35:12
|
LL | qux: Foo::Baz if true => {}
| ^
| ^ -------- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | qux::Foo::Baz if true => {}
| ~~

error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:40:15
|
LL | if let Foo:Bar = f() {
| ^
| ^--- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | if let Foo::Bar = f() {
| ~~

error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:48:16
|
LL | ref qux: Foo::Baz => {}
| ^
| ^ -------- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | ref qux::Foo::Baz => {}
| ~~

error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:57:16
|
LL | mut qux: Foo::Baz => {}
| ^
| ^ -------- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | mut qux::Foo::Baz => {}
| ~~

error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:68:12
|
LL | Foo:Bar::Baz => {}
| ^
| ^-------- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
| help: maybe write a path separator here: `::`
|
help: maybe write a path separator here
|
LL | Foo::Bar::Baz => {}
| ~~

error: expected one of `@` or `|`, found `:`
--> $DIR/issue-87086-colon-path-sep.rs:75:12
--> $DIR/issue-87086-colon-path-sep.rs:74:12
|
LL | Foo:Bar => {}
| ^
| ^--- specifying the type of a pattern isn't supported
| |
| 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
help: maybe write a path separator here
|
LL | Foo::Bar => {}
| ~~

error: aborting due to 10 previous errors
error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0433`.
16 changes: 16 additions & 0 deletions tests/ui/parser/type-ascription-in-pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn foo(x: bool) -> i32 {
match x {
x: i32 => x, //~ ERROR expected
//~^ ERROR mismatched types
true => 42.,
false => 0.333,
}
}

fn main() {
match foo(true) {
42: i32 => (), //~ ERROR expected
_: f64 => (), //~ ERROR expected
x: i32 => (), //~ ERROR expected
}
}
54 changes: 54 additions & 0 deletions tests/ui/parser/type-ascription-in-pattern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
error: expected one of `@` or `|`, found `:`
--> $DIR/type-ascription-in-pattern.rs:3:10
|
LL | x: i32 => x,
| ^ --- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
|
help: maybe write a path separator here
|
LL | x::i32 => x,
| ~~

error: expected one of `...`, `..=`, `..`, or `|`, found `:`
--> $DIR/type-ascription-in-pattern.rs:12:11
|
LL | 42: i32 => (),
| ^ --- specifying the type of a pattern isn't supported
| |
| expected one of `...`, `..=`, `..`, or `|`

error: expected `|`, found `:`
--> $DIR/type-ascription-in-pattern.rs:13:10
|
LL | _: f64 => (),
| ^ --- specifying the type of a pattern isn't supported
| |
| expected `|`

error: expected one of `@` or `|`, found `:`
--> $DIR/type-ascription-in-pattern.rs:14:10
|
LL | x: i32 => (),
| ^ --- specifying the type of a pattern isn't supported
| |
| expected one of `@` or `|`
|
help: maybe write a path separator here
|
LL | x::i32 => (),
| ~~

error[E0308]: mismatched types
--> $DIR/type-ascription-in-pattern.rs:3:19
|
LL | fn foo(x: bool) -> i32 {
| --- expected `i32` because of return type
LL | match x {
LL | x: i32 => x,
| ^ expected `i32`, found `bool`

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0308`.