Skip to content

Commit fc289a0

Browse files
suggest wrapping in struct tuples as well
1 parent 07776c1 commit fc289a0

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

compiler/rustc_typeck/src/check/demand.rs

-4
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
268268
expr_ty: Ty<'tcx>,
269269
) {
270270
if let ty::Adt(expected_adt, substs) = expected.kind() {
271-
if !expected_adt.is_enum() {
272-
return;
273-
}
274-
275271
// If the expression is of type () and it's the return expression of a block,
276272
// we suggest adding a separate return expression instead.
277273
// (To avoid things like suggesting `Ok(while .. { .. })`.)

src/test/ui/did_you_mean/compatible-variants.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ enum A {
6969
B { b: B},
7070
}
7171

72+
struct A2(B);
73+
7274
enum B {
7375
Fst,
7476
Snd,
@@ -78,4 +80,11 @@ fn foo() {
7880
// We don't want to suggest `A::B(B::Fst)` here.
7981
let a: A = B::Fst;
8082
//~^ ERROR mismatched types
81-
}
83+
}
84+
85+
fn bar() {
86+
// But we _do_ want to suggest `A2(B::Fst)` here!
87+
let a: A2 = B::Fst;
88+
//~^ ERROR mismatched types
89+
//~| HELP try wrapping
90+
}

src/test/ui/did_you_mean/compatible-variants.stderr

+15-2
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,26 @@ LL | let _ = Foo { bar: Some(bar) };
191191
| ++++++++++ +
192192

193193
error[E0308]: mismatched types
194-
--> $DIR/compatible-variants.rs:79:16
194+
--> $DIR/compatible-variants.rs:81:16
195195
|
196196
LL | let a: A = B::Fst;
197197
| - ^^^^^^ expected enum `A`, found enum `B`
198198
| |
199199
| expected due to this
200200

201-
error: aborting due to 12 previous errors
201+
error[E0308]: mismatched types
202+
--> $DIR/compatible-variants.rs:87:17
203+
|
204+
LL | let a: A2 = B::Fst;
205+
| -- ^^^^^^ expected struct `A2`, found enum `B`
206+
| |
207+
| expected due to this
208+
|
209+
help: try wrapping the expression in `A2`
210+
|
211+
LL | let a: A2 = A2(B::Fst);
212+
| +++ +
213+
214+
error: aborting due to 13 previous errors
202215

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

0 commit comments

Comments
 (0)