about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2020-07-24 10:01:39 -0700
committerGitHub <noreply@github.com>2020-07-24 10:01:39 -0700
commit7f24c7d39e27442ce9b1e43e2c51af62bb4871f8 (patch)
treeab32d744552ab09242fdea904c962f859cf38909 /src
parentdb83a21de1c5b5a3b2e5fbbecd03d70daa4ce331 (diff)
parentb75ed4f61cb867388eb17280f3acd933fe933f26 (diff)
downloadrust-7f24c7d39e27442ce9b1e43e2c51af62bb4871f8.tar.gz
rust-7f24c7d39e27442ce9b1e43e2c51af62bb4871f8.zip
Rollup merge of #74698 - ayrtonm:handle-traitref-mismatch, r=estebank
fixed error reporting for mismatched traits

mismatched traits were previously referred to as types

closes #72217
Diffstat (limited to 'src')
-rw-r--r--src/librustc_infer/infer/error_reporting/mod.rs29
-rw-r--r--src/test/ui/error-codes/E0308-2.rs12
-rw-r--r--src/test/ui/error-codes/E0308-2.stderr18
3 files changed, 54 insertions, 5 deletions
diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs
index ff905faa95a..063246f79fe 100644
--- a/src/librustc_infer/infer/error_reporting/mod.rs
+++ b/src/librustc_infer/infer/error_reporting/mod.rs
@@ -1402,8 +1402,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
         }
 
         debug!("note_type_err(diag={:?})", diag);
+        enum Mismatch<'a> {
+            Variable(ty::error::ExpectedFound<Ty<'a>>),
+            Fixed(&'static str),
+        }
         let (expected_found, exp_found, is_simple_error) = match values {
-            None => (None, None, false),
+            None => (None, Mismatch::Fixed("type"), false),
             Some(values) => {
                 let (is_simple_error, exp_found) = match values {
                     ValuePairs::Types(exp_found) => {
@@ -1417,9 +1421,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                         )
                         .report(diag);
 
-                        (is_simple_err, Some(exp_found))
+                        (is_simple_err, Mismatch::Variable(exp_found))
                     }
-                    _ => (false, None),
+                    ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
+                    _ => (false, Mismatch::Fixed("type")),
                 };
                 let vals = match self.values_str(&values) {
                     Some((expected, found)) => Some((expected, found)),
@@ -1445,8 +1450,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
             }
         };
         if let Some((expected, found)) = expected_found {
-            let expected_label = exp_found.map_or("type".into(), |ef| ef.expected.prefix_string());
-            let found_label = exp_found.map_or("type".into(), |ef| ef.found.prefix_string());
+            let expected_label = match exp_found {
+                Mismatch::Variable(ef) => ef.expected.prefix_string(),
+                Mismatch::Fixed(s) => s.into(),
+            };
+            let found_label = match exp_found {
+                Mismatch::Variable(ef) => ef.found.prefix_string(),
+                Mismatch::Fixed(s) => s.into(),
+            };
+            let exp_found = match exp_found {
+                Mismatch::Variable(exp_found) => Some(exp_found),
+                Mismatch::Fixed(_) => None,
+            };
             match (&terr, expected == found) {
                 (TypeError::Sorts(values), extra) => {
                     let sort_string = |ty: Ty<'tcx>| match (extra, &ty.kind) {
@@ -1499,6 +1514,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                 }
             }
         }
+        let exp_found = match exp_found {
+            Mismatch::Variable(exp_found) => Some(exp_found),
+            Mismatch::Fixed(_) => None,
+        };
         if let Some(exp_found) = exp_found {
             self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
         }
diff --git a/src/test/ui/error-codes/E0308-2.rs b/src/test/ui/error-codes/E0308-2.rs
new file mode 100644
index 00000000000..157f992da99
--- /dev/null
+++ b/src/test/ui/error-codes/E0308-2.rs
@@ -0,0 +1,12 @@
+trait DynEq {}
+
+impl<'a> PartialEq for &'a (dyn DynEq + 'static) {
+    fn eq(&self, _other: &Self) -> bool {
+        true
+    }
+}
+
+impl Eq for &dyn DynEq {} //~ ERROR E0308
+
+fn main() {
+}
diff --git a/src/test/ui/error-codes/E0308-2.stderr b/src/test/ui/error-codes/E0308-2.stderr
new file mode 100644
index 00000000000..e7c5e4b4240
--- /dev/null
+++ b/src/test/ui/error-codes/E0308-2.stderr
@@ -0,0 +1,18 @@
+error[E0308]: mismatched types
+  --> $DIR/E0308-2.rs:9:6
+   |
+LL | impl Eq for &dyn DynEq {}
+   |      ^^ lifetime mismatch
+   |
+   = note: expected trait `std::cmp::PartialEq`
+              found trait `std::cmp::PartialEq`
+note: the lifetime `'_` as defined on the impl at 9:13...
+  --> $DIR/E0308-2.rs:9:13
+   |
+LL | impl Eq for &dyn DynEq {}
+   |             ^
+   = note: ...does not necessarily outlive the static lifetime
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.