diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-01-17 06:08:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-17 06:08:13 +0100 |
| commit | ff1b653cdb1f176bf995b58cec3c7e74965cdac5 (patch) | |
| tree | 50b250aa1fb28a9b693cdbc521baa3a8b1e14f35 /src | |
| parent | 3de7276689fd7ff443218fa479de4984acebead6 (diff) | |
| parent | 272fb2395c0867e2ab8aa8cdb141b616d1e52c62 (diff) | |
| download | rust-ff1b653cdb1f176bf995b58cec3c7e74965cdac5.tar.gz rust-ff1b653cdb1f176bf995b58cec3c7e74965cdac5.zip | |
Rollup merge of #92808 - compiler-errors:wrap-struct-shorthand-field-in-variant, r=davidtwco
Fix `try wrapping expression in variant` suggestion with struct field shorthand
Fixes a broken suggestion: [playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=83fe2dbfe1485f8cfca1aef2a6582e77)
before:
```
error[E0308]: mismatched types
--> src/main.rs:7:19
|
7 | let x = Foo { bar };
| ^^^ expected enum `Option`, found integer
|
= note: expected enum `Option<i32>`
found type `{integer}`
help: try wrapping the expression in `Some`
|
7 | let x = Foo { Some(bar) };
| +++++ +
```
after:
```
error[E0308]: mismatched types
--> src/main.rs:7:19
|
7 | let x = Foo { bar };
| ^^^ expected enum `Option`, found integer
|
= note: expected enum `Option<i32>`
found type `{integer}`
help: try wrapping the expression in `Some`
|
7 | let x = Foo { bar: Some(bar) };
| ~~~~~~~~~~~~~~
```
r? ``@m-ou-se``
since you touched the code last in #91080
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/ui/did_you_mean/compatible-variants.rs | 8 | ||||
| -rw-r--r-- | src/test/ui/did_you_mean/compatible-variants.stderr | 31 | ||||
| -rw-r--r-- | src/test/ui/inference/deref-suggestion.stderr | 2 |
3 files changed, 31 insertions, 10 deletions
diff --git a/src/test/ui/did_you_mean/compatible-variants.rs b/src/test/ui/did_you_mean/compatible-variants.rs index fb6b6a5673d..a70dda8386f 100644 --- a/src/test/ui/did_you_mean/compatible-variants.rs +++ b/src/test/ui/did_you_mean/compatible-variants.rs @@ -3,6 +3,10 @@ enum Hey<A, B> { B(B), } +struct Foo { + bar: Option<i32>, +} + fn f() {} fn a() -> Option<()> { @@ -40,4 +44,8 @@ fn main() { let _: Hey<i32, bool> = false; //~^ ERROR mismatched types //~| HELP try wrapping + let bar = 1i32; + let _ = Foo { bar }; + //~^ ERROR mismatched types + //~| HELP try wrapping } diff --git a/src/test/ui/did_you_mean/compatible-variants.stderr b/src/test/ui/did_you_mean/compatible-variants.stderr index e77949687fc..0dfd8f5c128 100644 --- a/src/test/ui/did_you_mean/compatible-variants.stderr +++ b/src/test/ui/did_you_mean/compatible-variants.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:9:5 + --> $DIR/compatible-variants.rs:13:5 | LL | fn a() -> Option<()> { | ---------- expected `Option<()>` because of return type @@ -21,7 +21,7 @@ LL + Some(()) | error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:17:5 + --> $DIR/compatible-variants.rs:21:5 | LL | fn b() -> Result<(), ()> { | -------------- expected `Result<(), ()>` because of return type @@ -37,7 +37,7 @@ LL + Ok(()) | error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:23:25 + --> $DIR/compatible-variants.rs:27:25 | LL | let _: Option<()> = while false {}; | ---------- ^^^^^^^^^^^^^^ expected enum `Option`, found `()` @@ -52,7 +52,7 @@ LL | let _: Option<()> = Some(while false {}); | +++++ + error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:27:9 + --> $DIR/compatible-variants.rs:31:9 | LL | while false {} | ^^^^^^^^^^^^^^ expected enum `Option`, found `()` @@ -69,7 +69,7 @@ LL + Some(()) | error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:31:31 + --> $DIR/compatible-variants.rs:35:31 | LL | let _: Result<i32, i32> = 1; | ---------------- ^ expected enum `Result`, found integer @@ -86,7 +86,7 @@ LL | let _: Result<i32, i32> = Err(1); | ++++ + error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:34:26 + --> $DIR/compatible-variants.rs:38:26 | LL | let _: Option<i32> = 1; | ----------- ^ expected enum `Option`, found integer @@ -101,7 +101,7 @@ LL | let _: Option<i32> = Some(1); | +++++ + error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:37:28 + --> $DIR/compatible-variants.rs:41:28 | LL | let _: Hey<i32, i32> = 1; | ------------- ^ expected enum `Hey`, found integer @@ -118,7 +118,7 @@ LL | let _: Hey<i32, i32> = Hey::B(1); | +++++++ + error[E0308]: mismatched types - --> $DIR/compatible-variants.rs:40:29 + --> $DIR/compatible-variants.rs:44:29 | LL | let _: Hey<i32, bool> = false; | -------------- ^^^^^ expected enum `Hey`, found `bool` @@ -132,6 +132,19 @@ help: try wrapping the expression in `Hey::B` LL | let _: Hey<i32, bool> = Hey::B(false); | +++++++ + -error: aborting due to 8 previous errors +error[E0308]: mismatched types + --> $DIR/compatible-variants.rs:48:19 + | +LL | let _ = Foo { bar }; + | ^^^ expected enum `Option`, found `i32` + | + = note: expected enum `Option<i32>` + found type `i32` +help: try wrapping the expression in `Some` + | +LL | let _ = Foo { bar: Some(bar) }; + | ++++++++++ + + +error: aborting due to 9 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/inference/deref-suggestion.stderr b/src/test/ui/inference/deref-suggestion.stderr index da11ba204cb..28c9afaa52c 100644 --- a/src/test/ui/inference/deref-suggestion.stderr +++ b/src/test/ui/inference/deref-suggestion.stderr @@ -87,7 +87,7 @@ LL | let r = R { i }; help: consider dereferencing the borrow | LL | let r = R { i: *i }; - | ~~~~~ + | ++++ error[E0308]: mismatched types --> $DIR/deref-suggestion.rs:46:20 |
