about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonathan Turner <jonathandturner@users.noreply.github.com>2016-08-11 06:33:57 -0700
committerGitHub <noreply@github.com>2016-08-11 06:33:57 -0700
commite24c326400d4833b8a25596d0a529f07c809c6dd (patch)
tree0b21d305edcfe61bbffbdee29b69f5d03f0e3b55
parent0fecdc59312aff11190bb1218420d09d59df1056 (diff)
parent034df9478013dc05d06598c6e39494a8b518b004 (diff)
downloadrust-e24c326400d4833b8a25596d0a529f07c809c6dd.tar.gz
rust-e24c326400d4833b8a25596d0a529f07c809c6dd.zip
Rollup merge of #35375 - trixnz:update-error-326, r=jonathandturner
Update error format for E0326

Fixes #35335 as part of #35233

r? @jonathandturner
-rw-r--r--src/librustc_typeck/check/compare_method.rs24
-rw-r--r--src/test/compile-fail/associated-const-impl-wrong-type.rs4
2 files changed, 25 insertions, 3 deletions
diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs
index b971ae02cd0..140fabce76b 100644
--- a/src/librustc_typeck/check/compare_method.rs
+++ b/src/librustc_typeck/check/compare_method.rs
@@ -14,6 +14,8 @@ use rustc::ty;
 use rustc::traits::{self, ProjectionMode};
 use rustc::ty::error::ExpectedFound;
 use rustc::ty::subst::{self, Subst, Substs, VecPerParamSpace};
+use rustc::hir::map::Node;
+use rustc::hir::{ImplItemKind, TraitItem_};
 
 use syntax::ast;
 use syntax_pos::Span;
@@ -461,7 +463,7 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
         // Compute skolemized form of impl and trait const tys.
         let impl_ty = impl_c.ty.subst(tcx, impl_to_skol_substs);
         let trait_ty = trait_c.ty.subst(tcx, &trait_to_skol_substs);
-        let origin = TypeOrigin::Misc(impl_c_span);
+        let mut origin = TypeOrigin::Misc(impl_c_span);
 
         let err = infcx.commit_if_ok(|_| {
             // There is no "body" here, so just pass dummy id.
@@ -496,11 +498,31 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
             debug!("checking associated const for compatibility: impl ty {:?}, trait ty {:?}",
                    impl_ty,
                    trait_ty);
+
+            // Locate the Span containing just the type of the offending impl
+            if let Some(impl_trait_node) = tcx.map.get_if_local(impl_c.def_id) {
+                if let Node::NodeImplItem(impl_trait_item) = impl_trait_node {
+                    if let ImplItemKind::Const(ref ty, _) = impl_trait_item.node {
+                        origin = TypeOrigin::Misc(ty.span);
+                    }
+                }
+            }
+
             let mut diag = struct_span_err!(
                 tcx.sess, origin.span(), E0326,
                 "implemented const `{}` has an incompatible type for trait",
                 trait_c.name
             );
+
+            // Add a label to the Span containing just the type of the item
+            if let Some(orig_trait_node) = tcx.map.get_if_local(trait_c.def_id) {
+                if let Node::NodeTraitItem(orig_trait_item) = orig_trait_node {
+                    if let TraitItem_::ConstTraitItem(ref ty, _) = orig_trait_item.node {
+                        diag.span_label(ty.span, &format!("original trait requirement"));
+                    }
+                }
+            }
+
             infcx.note_type_err(
                 &mut diag, origin,
                 Some(infer::ValuePairs::Types(ExpectedFound {
diff --git a/src/test/compile-fail/associated-const-impl-wrong-type.rs b/src/test/compile-fail/associated-const-impl-wrong-type.rs
index 95508a31044..b3776091682 100644
--- a/src/test/compile-fail/associated-const-impl-wrong-type.rs
+++ b/src/test/compile-fail/associated-const-impl-wrong-type.rs
@@ -11,7 +11,7 @@
 #![feature(associated_consts)]
 
 trait Foo {
-    const BAR: u32;
+    const BAR: u32; //~ NOTE original trait requirement
 }
 
 struct SignedBar;
@@ -19,7 +19,7 @@ struct SignedBar;
 impl Foo for SignedBar {
     const BAR: i32 = -1;
     //~^ ERROR implemented const `BAR` has an incompatible type for trait [E0326]
-    //~| expected u32, found i32
+    //~| NOTE expected u32, found i32
 }
 
 fn main() {}