Skip to content

Commit 07776c1

Browse files
do not suggest enum tuple variant for named field variant
1 parent dd6683f commit 07776c1

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

compiler/rustc_typeck/src/check/demand.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
336336
let compatible_variants: Vec<String> = expected_adt
337337
.variants()
338338
.iter()
339-
.filter(|variant| variant.fields.len() == 1)
339+
.filter(|variant| {
340+
variant.fields.len() == 1 && variant.ctor_kind == hir::def::CtorKind::Fn
341+
})
340342
.filter_map(|variant| {
341343
let sole_field = &variant.fields[0];
342344
let sole_field_ty = sole_field.ty(self.tcx, substs);

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

+15
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@ fn main() {
6464
//~^ ERROR mismatched types
6565
//~| HELP try wrapping
6666
}
67+
68+
enum A {
69+
B { b: B},
70+
}
71+
72+
enum B {
73+
Fst,
74+
Snd,
75+
}
76+
77+
fn foo() {
78+
// We don't want to suggest `A::B(B::Fst)` here.
79+
let a: A = B::Fst;
80+
//~^ ERROR mismatched types
81+
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ help: try wrapping the expression in `Some`
190190
LL | let _ = Foo { bar: Some(bar) };
191191
| ++++++++++ +
192192

193-
error: aborting due to 11 previous errors
193+
error[E0308]: mismatched types
194+
--> $DIR/compatible-variants.rs:79:16
195+
|
196+
LL | let a: A = B::Fst;
197+
| - ^^^^^^ expected enum `A`, found enum `B`
198+
| |
199+
| expected due to this
200+
201+
error: aborting due to 12 previous errors
194202

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

0 commit comments

Comments
 (0)