Skip to content

Commit

Permalink
do not suggest enum tuple variant for named field variant
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Mar 27, 2022
1 parent dd6683f commit 07776c1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let compatible_variants: Vec<String> = expected_adt
.variants()
.iter()
.filter(|variant| variant.fields.len() == 1)
.filter(|variant| {
variant.fields.len() == 1 && variant.ctor_kind == hir::def::CtorKind::Fn
})
.filter_map(|variant| {
let sole_field = &variant.fields[0];
let sole_field_ty = sole_field.ty(self.tcx, substs);
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/did_you_mean/compatible-variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ fn main() {
//~^ ERROR mismatched types
//~| HELP try wrapping
}

enum A {
B { b: B},
}

enum B {
Fst,
Snd,
}

fn foo() {
// We don't want to suggest `A::B(B::Fst)` here.
let a: A = B::Fst;
//~^ ERROR mismatched types
}
10 changes: 9 additions & 1 deletion src/test/ui/did_you_mean/compatible-variants.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ help: try wrapping the expression in `Some`
LL | let _ = Foo { bar: Some(bar) };
| ++++++++++ +

error: aborting due to 11 previous errors
error[E0308]: mismatched types
--> $DIR/compatible-variants.rs:79:16
|
LL | let a: A = B::Fst;
| - ^^^^^^ expected enum `A`, found enum `B`
| |
| expected due to this

error: aborting due to 12 previous errors

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

0 comments on commit 07776c1

Please sign in to comment.