]> git.lizzy.rs Git - rust.git/commitdiff
Use correct type in diagnostics again
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Thu, 24 Sep 2020 08:06:07 +0000 (10:06 +0200)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Thu, 24 Sep 2020 08:06:07 +0000 (10:06 +0200)
compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.stderr
src/test/ui/consts/const_in_pattern/reject_non_partial_eq.stderr
src/test/ui/consts/const_in_pattern/warn_corner_cases.stderr

index 5025bacafa1db8ca6407230d9872636e904ad646..3c8671486bf8fc837270af05bd58ebe506f48c6e 100644 (file)
@@ -89,11 +89,42 @@ fn tcx(&self) -> TyCtxt<'tcx> {
         self.infcx.tcx
     }
 
-    fn search_for_structural_match_violation(
-        &self,
-        ty: Ty<'tcx>,
-    ) -> Option<traits::NonStructuralMatchTy<'tcx>> {
-        traits::search_for_structural_match_violation(self.id, self.span, self.tcx(), ty)
+    fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
+        traits::search_for_structural_match_violation(self.id, self.span, self.tcx(), ty).map(
+            |non_sm_ty| {
+                with_no_trimmed_paths(|| match non_sm_ty {
+                    traits::NonStructuralMatchTy::Adt(adt_def) => {
+                        let path = self.tcx().def_path_str(adt_def.did);
+                        format!(
+                            "to use a constant of type `{}` in a pattern, \
+                         `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
+                            path, path,
+                        )
+                    }
+                    traits::NonStructuralMatchTy::Dynamic => {
+                        "trait objects cannot be used in patterns".to_string()
+                    }
+                    traits::NonStructuralMatchTy::Opaque => {
+                        "opaque types cannot be used in patterns".to_string()
+                    }
+                    traits::NonStructuralMatchTy::Generator => {
+                        "generators cannot be used in patterns".to_string()
+                    }
+                    traits::NonStructuralMatchTy::Closure => {
+                        "closures cannot be used in patterns".to_string()
+                    }
+                    traits::NonStructuralMatchTy::Param => {
+                        bug!("use of a constant whose type is a parameter inside a pattern")
+                    }
+                    traits::NonStructuralMatchTy::Projection => {
+                        bug!("use of a constant whose type is a projection inside a pattern")
+                    }
+                    traits::NonStructuralMatchTy::Foreign => {
+                        bug!("use of a value of a foreign type inside a pattern")
+                    }
+                })
+            },
+        )
     }
 
     fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool {
@@ -135,39 +166,7 @@ fn to_pat(
                 return inlined_const_as_pat;
             }
 
-            if let Some(non_sm_ty) = structural {
-                let msg = with_no_trimmed_paths(|| match non_sm_ty {
-                    traits::NonStructuralMatchTy::Adt(adt_def) => {
-                        let path = self.tcx().def_path_str(adt_def.did);
-                        format!(
-                            "to use a constant of type `{}` in a pattern, \
-                             `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
-                            path, path,
-                        )
-                    }
-                    traits::NonStructuralMatchTy::Dynamic => {
-                        "trait objects cannot be used in patterns".to_string()
-                    }
-                    traits::NonStructuralMatchTy::Opaque => {
-                        "opaque types cannot be used in patterns".to_string()
-                    }
-                    traits::NonStructuralMatchTy::Generator => {
-                        "generators cannot be used in patterns".to_string()
-                    }
-                    traits::NonStructuralMatchTy::Closure => {
-                        "closures cannot be used in patterns".to_string()
-                    }
-                    traits::NonStructuralMatchTy::Param => {
-                        bug!("use of a constant whose type is a parameter inside a pattern")
-                    }
-                    traits::NonStructuralMatchTy::Projection => {
-                        bug!("use of a constant whose type is a projection inside a pattern")
-                    }
-                    traits::NonStructuralMatchTy::Foreign => {
-                        bug!("use of a value of a foreign type inside a pattern")
-                    }
-                });
-
+            if let Some(msg) = structural {
                 if !self.type_may_have_partial_eq_impl(cv.ty) {
                     // span_fatal avoids ICE from resolution of non-existent method (rare case).
                     self.tcx().sess.span_fatal(self.span, &msg);
@@ -272,11 +271,9 @@ fn recur(
                     // `search_for_structural_match_violation` and then remove this condition.
                     && self.search_for_structural_match_violation(cv.ty).is_some() =>
             {
-                let msg = format!(
-                    "to use a constant of type `{}` in a pattern, \
-                    `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
-                    cv.ty, cv.ty,
-                );
+                // Obtain the actual type that isn't annotated. If we just looked at `cv.ty` we
+                // could get `Option<NonStructEq>`, even though `Option` is annotated with derive.
+                let msg = self.search_for_structural_match_violation(cv.ty).unwrap();
                 self.saw_const_match_error.set(true);
                 if self.include_lint_checks {
                     tcx.sess.span_err(self.span, &msg);
@@ -512,11 +509,11 @@ fn recur(
             && self.search_for_structural_match_violation(cv.ty).is_some()
         {
             self.saw_const_match_lint.set(true);
-            let msg = format!(
-                "to use a constant of type `{}` in a pattern, \
-                 the constant's initializer must be trivial or all types \
-                 in the constant must be annotated with `#[derive(PartialEq, Eq)]`",
-                cv.ty,
+            // Obtain the actual type that isn't annotated. If we just looked at `cv.ty` we
+            // could get `Option<NonStructEq>`, even though `Option` is annotated with derive.
+            let msg = self.search_for_structural_match_violation(cv.ty).unwrap().replace(
+                "in a pattern,",
+                "in a pattern, the constant's initializer must be trivial or",
             );
             tcx.struct_span_lint_hir(
                 lint::builtin::NONTRIVIAL_STRUCTURAL_MATCH,
index fd6732195e49411793415288a6b07b0a63ef1f15..e51d6f916498ef4cb36f416d7a7feba1afa7fdb7 100644 (file)
@@ -1,4 +1,4 @@
-warning: to use a constant of type `Foo` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
+warning: to use a constant of type `CustomEq` in a pattern, the constant's initializer must be trivial or `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
   --> $DIR/custom-eq-branch-warn.rs:29:9
    |
 LL |         BAR_BAZ => panic!(),
index 8462c32ea809b401eb3056eb62ca27b45faefec5..95cfa4a9ebe95a24b6f29a0f4e0998e68d14f747 100644 (file)
@@ -1,4 +1,4 @@
-error: to use a constant of type `Option<NoPartialEq>` in a pattern, `Option<NoPartialEq>` must be annotated with `#[derive(PartialEq, Eq)]`
+error: to use a constant of type `NoPartialEq` in a pattern, `NoPartialEq` must be annotated with `#[derive(PartialEq, Eq)]`
   --> $DIR/reject_non_partial_eq.rs:28:9
    |
 LL |         NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),
index a4feaff55b2e115e43eb19879992bb9e1ad95b20..a24c8d181843d9973e2a238c0bf6f2ecd0b48b4f 100644 (file)
@@ -1,4 +1,4 @@
-warning: to use a constant of type `Option<NoDerive>` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
+warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
   --> $DIR/warn_corner_cases.rs:26:47
    |
 LL |     match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
@@ -8,7 +8,7 @@ LL |     match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
 
-warning: to use a constant of type `Option<NoDerive>` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
+warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
   --> $DIR/warn_corner_cases.rs:32:47
    |
 LL |     match None { Some(_) => panic!("whoops"), CALL => dbg!(CALL), };
@@ -17,7 +17,7 @@ LL |     match None { Some(_) => panic!("whoops"), CALL => dbg!(CALL), };
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
 
-warning: to use a constant of type `Option<NoDerive>` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
+warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
   --> $DIR/warn_corner_cases.rs:38:47
    |
 LL |     match None { Some(_) => panic!("whoops"), METHOD_CALL => dbg!(METHOD_CALL), };