-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 #95386 - compiler-errors:try-wrapping, r=oli-obk
Suggest wrapping patterns in enum variants Structured suggestion to wrap a pattern in a single-field enum or struct: ```diff struct A; enum B { A(A), } fn main(b: B) { match b { - A => {} + B::A(A) => {} } } ``` Half of #94942, the other half I'm not exactly sure how to fix. Also includes two drive-by changes (that I am open to splitting out into another PR, but thought they could be rolled up into this one): - 07776c1: Makes sure not to suggest wrapping if it doesn't have tuple field constructor (i.e. has named fields) - 8f2bbb18fd53e5008bb488302dbd354577698ede: Also suggest wrapping expressions in a tuple struct (not just enum variants)
- Loading branch information
Showing
9 changed files
with
243 additions
and
6 deletions.
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,41 @@ | ||
enum Foo { | ||
Bar(Bar), | ||
} | ||
struct Bar { | ||
x: i32, | ||
} | ||
|
||
fn a(f: Foo) { | ||
match f { | ||
Bar { x } => { | ||
//~^ ERROR mismatched types | ||
//~| HELP try wrapping | ||
} | ||
} | ||
} | ||
|
||
struct S; | ||
|
||
fn b(s: Option<S>) { | ||
match s { | ||
S => { | ||
//~^ ERROR mismatched types | ||
//~| HELP try wrapping | ||
//~| HELP introduce a new binding instead | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn c(s: Result<S, S>) { | ||
match s { | ||
S => { | ||
//~^ ERROR mismatched types | ||
//~| HELP try wrapping | ||
//~| HELP introduce a new binding instead | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn main() {} |
68 changes: 68 additions & 0 deletions
68
src/test/ui/did_you_mean/compatible-variants-in-pat.stderr
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,68 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/compatible-variants-in-pat.rs:10:9 | ||
| | ||
LL | match f { | ||
| - this expression has type `Foo` | ||
LL | Bar { x } => { | ||
| ^^^^^^^^^ expected enum `Foo`, found struct `Bar` | ||
| | ||
help: try wrapping the pattern in `Foo::Bar` | ||
| | ||
LL | Foo::Bar(Bar { x }) => { | ||
| +++++++++ + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/compatible-variants-in-pat.rs:21:9 | ||
| | ||
LL | struct S; | ||
| --------- unit struct defined here | ||
... | ||
LL | match s { | ||
| - this expression has type `Option<S>` | ||
LL | S => { | ||
| ^ | ||
| | | ||
| expected enum `Option`, found struct `S` | ||
| `S` is interpreted as a unit struct, not a new binding | ||
| | ||
= note: expected enum `Option<S>` | ||
found struct `S` | ||
help: try wrapping the pattern in `Some` | ||
| | ||
LL | Some(S) => { | ||
| +++++ + | ||
help: introduce a new binding instead | ||
| | ||
LL | other_s => { | ||
| ~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/compatible-variants-in-pat.rs:32:9 | ||
| | ||
LL | struct S; | ||
| --------- unit struct defined here | ||
... | ||
LL | match s { | ||
| - this expression has type `Result<S, S>` | ||
LL | S => { | ||
| ^ | ||
| | | ||
| expected enum `Result`, found struct `S` | ||
| `S` is interpreted as a unit struct, not a new binding | ||
| | ||
= note: expected enum `Result<S, S>` | ||
found struct `S` | ||
help: try wrapping the pattern in a variant of `Result` | ||
| | ||
LL | Ok(S) => { | ||
| +++ + | ||
LL | Err(S) => { | ||
| ++++ + | ||
help: introduce a new binding instead | ||
| | ||
LL | other_s => { | ||
| ~~~~~~~ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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
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