]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_typeck/check/demand.rs
only issue "variant of the expected type" suggestion for enums
[rust.git] / src / librustc_typeck / check / demand.rs
index d82d36a1937bf4b5449249789643be1b5285e2e2..7773e2d570844b892d2157b0941d05580c7e7e26 100644 (file)
@@ -111,34 +111,35 @@ pub fn demand_coerce_diag(&self,
         let expr_ty = self.resolve_type_vars_with_obligations(checked_ty);
         let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
 
-        // If the expected type is an enum with any variants whose sole
-        // field is of the found type, suggest such variants. See Issue
-        // #42764.
+        // If the expected type is an enum (Issue #55250) with any variants whose
+        // sole field is of the found type, suggest such variants. (Issue #42764)
         if let ty::Adt(expected_adt, substs) = expected.sty {
-            let mut compatible_variants = expected_adt.variants
-                                                  .iter()
-                                                  .filter(|variant| variant.fields.len() == 1)
-                                                  .filter_map(|variant| {
-                let sole_field = &variant.fields[0];
-                let sole_field_ty = sole_field.ty(self.tcx, substs);
-                if self.can_coerce(expr_ty, sole_field_ty) {
-                    let variant_path = self.tcx.item_path_str(variant.did);
-                    Some(variant_path.trim_left_matches("std::prelude::v1::").to_string())
-                } else {
-                    None
+            if expected_adt.is_enum() {
+                let mut compatible_variants = expected_adt.variants
+                    .iter()
+                    .filter(|variant| variant.fields.len() == 1)
+                    .filter_map(|variant| {
+                        let sole_field = &variant.fields[0];
+                        let sole_field_ty = sole_field.ty(self.tcx, substs);
+                        if self.can_coerce(expr_ty, sole_field_ty) {
+                            let variant_path = self.tcx.item_path_str(variant.did);
+                            Some(variant_path.trim_left_matches("std::prelude::v1::").to_string())
+                        } else {
+                            None
+                        }
+                    }).peekable();
+
+                if compatible_variants.peek().is_some() {
+                    let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
+                    let suggestions = compatible_variants
+                        .map(|v| format!("{}({})", v, expr_text)).collect::<Vec<_>>();
+                    err.span_suggestions_with_applicability(
+                        expr.span,
+                        "try using a variant of the expected type",
+                        suggestions,
+                        Applicability::MaybeIncorrect,
+                    );
                 }
-            }).peekable();
-
-            if compatible_variants.peek().is_some() {
-                let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
-                let suggestions = compatible_variants.map(|v|
-                    format!("{}({})", v, expr_text)).collect::<Vec<_>>();
-                err.span_suggestions_with_applicability(
-                     expr.span,
-                     "try using a variant of the expected type",
-                     suggestions,
-                     Applicability::MaybeIncorrect,
-                );
             }
         }