]> git.lizzy.rs Git - rust.git/commitdiff
do not suggest enum tuple variant for named field variant
authorMichael Goulet <michael@errs.io>
Sun, 27 Mar 2022 23:05:14 +0000 (16:05 -0700)
committerMichael Goulet <michael@errs.io>
Sun, 27 Mar 2022 23:10:02 +0000 (16:10 -0700)
compiler/rustc_typeck/src/check/demand.rs
src/test/ui/did_you_mean/compatible-variants.rs
src/test/ui/did_you_mean/compatible-variants.stderr

index 58e5c9315c30c7c706702ef4135253f8d3f73c79..17b08fe1e403942da6273e7d30f9d16c3bba1986 100644 (file)
@@ -336,7 +336,9 @@ fn suggest_compatible_variants(
             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);
index b078064b26745c6835dd24319219c027d6e69b18..d9457cf5e3251d50e440c08ab90ae336454480af 100644 (file)
@@ -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
index 51c1bf97c4e2c5c91df8b341fac1ec8b2e778bbf..6224af3976b91f383eaaf22c46625ac5bb1f7176 100644 (file)
@@ -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`.