Skip to content

Commit aba49ac

Browse files
authored
Rollup merge of rust-lang#35372 - Keats:err-323, r=jonathandturner
Update error message for E0323, E0324 and E0325 Fixes rust-lang#35325, rust-lang#35327 and rust-lang#35329 as part of rust-lang#35233 r? @jonathandturner
2 parents b722358 + e0035c9 commit aba49ac

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/librustc_typeck/check/mod.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
995995
// Check existing impl methods to see if they are both present in trait
996996
// and compatible with trait signature
997997
for impl_item in impl_items {
998-
let ty_impl_item = ccx.tcx.impl_or_trait_item(ccx.tcx.map.local_def_id(impl_item.id));
998+
let ty_impl_item = tcx.impl_or_trait_item(tcx.map.local_def_id(impl_item.id));
999999
let ty_trait_item = trait_items.iter()
10001000
.find(|ac| ac.name() == ty_impl_item.name());
10011001

@@ -1016,11 +1016,18 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10161016
trait_const,
10171017
&impl_trait_ref);
10181018
} else {
1019-
span_err!(tcx.sess, impl_item.span, E0323,
1019+
let mut err = struct_span_err!(tcx.sess, impl_item.span, E0323,
10201020
"item `{}` is an associated const, \
10211021
which doesn't match its trait `{:?}`",
10221022
impl_const.name,
1023-
impl_trait_ref)
1023+
impl_trait_ref);
1024+
err.span_label(impl_item.span, &format!("does not match trait"));
1025+
// We can only get the spans from local trait definition
1026+
// Same for E0324 and E0325
1027+
if let Some(trait_span) = tcx.map.span_if_local(ty_trait_item.def_id()) {
1028+
err.span_label(trait_span, &format!("original trait requirement"));
1029+
}
1030+
err.emit()
10241031
}
10251032
}
10261033
hir::ImplItemKind::Method(ref sig, ref body) => {
@@ -1039,11 +1046,16 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10391046
&trait_method,
10401047
&impl_trait_ref);
10411048
} else {
1042-
span_err!(tcx.sess, impl_item.span, E0324,
1049+
let mut err = struct_span_err!(tcx.sess, impl_item.span, E0324,
10431050
"item `{}` is an associated method, \
10441051
which doesn't match its trait `{:?}`",
10451052
impl_method.name,
1046-
impl_trait_ref)
1053+
impl_trait_ref);
1054+
err.span_label(impl_item.span, &format!("does not match trait"));
1055+
if let Some(trait_span) = tcx.map.span_if_local(ty_trait_item.def_id()) {
1056+
err.span_label(trait_span, &format!("original trait requirement"));
1057+
}
1058+
err.emit()
10471059
}
10481060
}
10491061
hir::ImplItemKind::Type(_) => {
@@ -1057,11 +1069,16 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10571069
overridden_associated_type = Some(impl_item);
10581070
}
10591071
} else {
1060-
span_err!(tcx.sess, impl_item.span, E0325,
1072+
let mut err = struct_span_err!(tcx.sess, impl_item.span, E0325,
10611073
"item `{}` is an associated type, \
10621074
which doesn't match its trait `{:?}`",
10631075
impl_type.name,
1064-
impl_trait_ref)
1076+
impl_trait_ref);
1077+
err.span_label(impl_item.span, &format!("does not match trait"));
1078+
if let Some(trait_span) = tcx.map.span_if_local(ty_trait_item.def_id()) {
1079+
err.span_label(trait_span, &format!("original trait requirement"));
1080+
}
1081+
err.emit()
10651082
}
10661083
}
10671084
}

src/test/compile-fail/impl-wrong-item-for-trait.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
trait Foo {
1414
fn bar(&self);
15-
const MY_CONST: u32;
15+
//~^ NOTE original trait requirement
16+
//~| NOTE original trait requirement
17+
const MY_CONST: u32; //~ NOTE original trait requirement
1618
}
1719

1820
pub struct FooConstForMethod;
@@ -21,6 +23,7 @@ impl Foo for FooConstForMethod {
2123
//~^ ERROR E0046
2224
const bar: u64 = 1;
2325
//~^ ERROR E0323
26+
//~| NOTE does not match trait
2427
const MY_CONST: u32 = 1;
2528
}
2629

@@ -31,6 +34,7 @@ impl Foo for FooMethodForConst {
3134
fn bar(&self) {}
3235
fn MY_CONST() {}
3336
//~^ ERROR E0324
37+
//~| NOTE does not match trait
3438
}
3539

3640
pub struct FooTypeForMethod;
@@ -39,6 +43,7 @@ impl Foo for FooTypeForMethod {
3943
//~^ ERROR E0046
4044
type bar = u64;
4145
//~^ ERROR E0325
46+
//~| NOTE does not match trait
4247
const MY_CONST: u32 = 1;
4348
}
4449

0 commit comments

Comments
 (0)