Skip to content

Commit 802b543

Browse files
committed
Update error format for E0326
1 parent 4c02363 commit 802b543

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/librustc_typeck/check/compare_method.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use rustc::ty;
1414
use rustc::traits::{self, ProjectionMode};
1515
use rustc::ty::error::ExpectedFound;
1616
use rustc::ty::subst::{self, Subst, Substs, VecPerParamSpace};
17+
use rustc::hir::map::Node;
18+
use rustc::hir::{ImplItemKind, TraitItem_};
1719

1820
use syntax::ast;
1921
use syntax_pos::Span;
@@ -447,7 +449,7 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
447449
// Compute skolemized form of impl and trait const tys.
448450
let impl_ty = impl_c.ty.subst(tcx, impl_to_skol_substs);
449451
let trait_ty = trait_c.ty.subst(tcx, &trait_to_skol_substs);
450-
let origin = TypeOrigin::Misc(impl_c_span);
452+
let mut origin = TypeOrigin::Misc(impl_c_span);
451453

452454
let err = infcx.commit_if_ok(|_| {
453455
// There is no "body" here, so just pass dummy id.
@@ -482,11 +484,31 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
482484
debug!("checking associated const for compatibility: impl ty {:?}, trait ty {:?}",
483485
impl_ty,
484486
trait_ty);
487+
488+
// Locate the Span containing just the type of the offending impl
489+
if let Some(impl_trait_node) = tcx.map.get_if_local(impl_c.def_id) {
490+
if let Node::NodeImplItem(impl_trait_item) = impl_trait_node {
491+
if let ImplItemKind::Const(ref ty, _) = impl_trait_item.node {
492+
origin = TypeOrigin::Misc(ty.span);
493+
}
494+
}
495+
}
496+
485497
let mut diag = struct_span_err!(
486498
tcx.sess, origin.span(), E0326,
487499
"implemented const `{}` has an incompatible type for trait",
488500
trait_c.name
489501
);
502+
503+
// Add a label to the Span containing just the type of the item
504+
if let Some(orig_trait_node) = tcx.map.get_if_local(trait_c.def_id) {
505+
if let Node::NodeTraitItem(orig_trait_item) = orig_trait_node {
506+
if let TraitItem_::ConstTraitItem(ref ty, _) = orig_trait_item.node {
507+
diag.span_label(ty.span, &format!("original trait requirement"));
508+
}
509+
}
510+
}
511+
490512
infcx.note_type_err(
491513
&mut diag, origin,
492514
Some(infer::ValuePairs::Types(ExpectedFound {

src/test/compile-fail/associated-const-impl-wrong-type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
#![feature(associated_consts)]
1212

1313
trait Foo {
14-
const BAR: u32;
14+
const BAR: u32; //~ NOTE original trait requirement
1515
}
1616

1717
struct SignedBar;
1818

1919
impl Foo for SignedBar {
2020
const BAR: i32 = -1;
2121
//~^ ERROR implemented const `BAR` has an incompatible type for trait [E0326]
22-
//~| expected u32, found i32
22+
//~| NOTE expected u32, found i32
2323
}
2424

2525
fn main() {}

0 commit comments

Comments
 (0)