Skip to content

Commit e24c326

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35375 - trixnz:update-error-326, r=jonathandturner
Update error format for E0326 Fixes rust-lang#35335 as part of rust-lang#35233 r? @jonathandturner
2 parents 0fecdc5 + 034df94 commit e24c326

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/librustc_typeck/check/compare_method.rs

+23-1
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;
@@ -461,7 +463,7 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
461463
// Compute skolemized form of impl and trait const tys.
462464
let impl_ty = impl_c.ty.subst(tcx, impl_to_skol_substs);
463465
let trait_ty = trait_c.ty.subst(tcx, &trait_to_skol_substs);
464-
let origin = TypeOrigin::Misc(impl_c_span);
466+
let mut origin = TypeOrigin::Misc(impl_c_span);
465467

466468
let err = infcx.commit_if_ok(|_| {
467469
// 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>,
496498
debug!("checking associated const for compatibility: impl ty {:?}, trait ty {:?}",
497499
impl_ty,
498500
trait_ty);
501+
502+
// Locate the Span containing just the type of the offending impl
503+
if let Some(impl_trait_node) = tcx.map.get_if_local(impl_c.def_id) {
504+
if let Node::NodeImplItem(impl_trait_item) = impl_trait_node {
505+
if let ImplItemKind::Const(ref ty, _) = impl_trait_item.node {
506+
origin = TypeOrigin::Misc(ty.span);
507+
}
508+
}
509+
}
510+
499511
let mut diag = struct_span_err!(
500512
tcx.sess, origin.span(), E0326,
501513
"implemented const `{}` has an incompatible type for trait",
502514
trait_c.name
503515
);
516+
517+
// Add a label to the Span containing just the type of the item
518+
if let Some(orig_trait_node) = tcx.map.get_if_local(trait_c.def_id) {
519+
if let Node::NodeTraitItem(orig_trait_item) = orig_trait_node {
520+
if let TraitItem_::ConstTraitItem(ref ty, _) = orig_trait_item.node {
521+
diag.span_label(ty.span, &format!("original trait requirement"));
522+
}
523+
}
524+
}
525+
504526
infcx.note_type_err(
505527
&mut diag, origin,
506528
Some(infer::ValuePairs::Types(ExpectedFound {

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

+2-2
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)