From 07776c111f07b887cd46b752870cd3fd76b2ba7c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 27 Mar 2022 16:05:14 -0700 Subject: [PATCH] do not suggest enum tuple variant for named field variant --- compiler/rustc_typeck/src/check/demand.rs | 4 +++- src/test/ui/did_you_mean/compatible-variants.rs | 15 +++++++++++++++ .../ui/did_you_mean/compatible-variants.stderr | 10 +++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 58e5c9315c30c..17b08fe1e4039 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -336,7 +336,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let compatible_variants: Vec = 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); diff --git a/src/test/ui/did_you_mean/compatible-variants.rs b/src/test/ui/did_you_mean/compatible-variants.rs index b078064b26745..d9457cf5e3251 100644 --- a/src/test/ui/did_you_mean/compatible-variants.rs +++ b/src/test/ui/did_you_mean/compatible-variants.rs @@ -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 +} \ No newline at end of file diff --git a/src/test/ui/did_you_mean/compatible-variants.stderr b/src/test/ui/did_you_mean/compatible-variants.stderr index 51c1bf97c4e2c..6224af3976b91 100644 --- a/src/test/ui/did_you_mean/compatible-variants.stderr +++ b/src/test/ui/did_you_mean/compatible-variants.stderr @@ -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`.