Skip to content

Commit eb0f4d5

Browse files
committed
Tweak output for mismatched impl item
Detect type parameter that might require lifetime constraint. Do not name `ReVar`s in expected/found output. Reword text suggesting to check the lifetimes.
1 parent 5ba2220 commit eb0f4d5

38 files changed

+126
-56
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -993,10 +993,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
993993
s: &mut DiagnosticStyledString,
994994
) {
995995
let mut r = region.to_string();
996-
if let ty::RegionKind::ReVar(var) = region {
997-
// Show these named, not as `'_` or elide them in "expected/found" notes.
998-
r = format!("'z{} ", var.index());
999-
} else if r == "'_" {
996+
if r == "'_" {
1000997
r.clear();
1001998
} else {
1002999
r.push(' ');

src/librustc_infer/infer/error_reporting/nice_region_error/trait_impl_difference.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Error Reporting for `impl` items that do not match the obligations from their `trait`.
22
3+
use crate::hir;
34
use crate::hir::def_id::DefId;
45
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
56
use crate::infer::lexical_region_resolve::RegionResolutionError;
@@ -40,7 +41,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4041
var_origin.span(),
4142
sub_expected_found.expected,
4243
sub_expected_found.found,
43-
self.tcx().def_span(*trait_item_def_id),
44+
*trait_item_def_id,
4445
);
4546
return Some(ErrorReported);
4647
}
@@ -51,23 +52,56 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5152
None
5253
}
5354

54-
fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>, impl_sp: Span) {
55+
fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>, trait_def_id: DefId) {
56+
let tcx = self.tcx();
57+
let trait_sp = self.tcx().def_span(trait_def_id);
5558
let mut err = self
5659
.tcx()
5760
.sess
5861
.struct_span_err(sp, "`impl` item signature doesn't match `trait` item signature");
5962
err.span_label(sp, &format!("found {:?}", found));
60-
err.span_label(impl_sp, &format!("expected {:?}", expected));
63+
err.span_label(trait_sp, &format!("expected {:?}", expected));
64+
let trait_fn_sig = tcx.fn_sig(trait_def_id);
65+
66+
struct AssocTypeFinder(FxHashSet<ty::ParamTy>);
67+
impl<'tcx> ty::fold::TypeVisitor<'tcx> for AssocTypeFinder {
68+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
69+
debug!("assoc type finder ty {:?} {:?}", ty, ty.kind);
70+
match ty.kind {
71+
ty::Param(param) => {
72+
self.0.insert(param);
73+
}
74+
_ => {}
75+
}
76+
ty.super_visit_with(self)
77+
}
78+
}
79+
let mut visitor = AssocTypeFinder(FxHashSet::default());
80+
trait_fn_sig.output().visit_with(&mut visitor);
81+
82+
if let Some(id) = tcx.hir().as_local_hir_id(trait_def_id) {
83+
let parent_id = tcx.hir().get_parent_item(id);
84+
let trait_item = tcx.hir().expect_item(parent_id);
85+
if let hir::ItemKind::Trait(_, _, generics, _, _) = &trait_item.kind {
86+
for param_ty in visitor.0 {
87+
if let Some(generic) = generics.get_named(param_ty.name) {
88+
err.span_label(generic.span, &format!(
89+
"in order for `impl` items to be able to implement the method, this \
90+
type parameter might need a lifetime restriction like `{}: 'a`",
91+
param_ty.name,
92+
));
93+
}
94+
}
95+
}
96+
}
6197

6298
struct EarlyBoundRegionHighlighter(FxHashSet<DefId>);
6399
impl<'tcx> ty::fold::TypeVisitor<'tcx> for EarlyBoundRegionHighlighter {
64100
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
65-
debug!("LateBoundRegionNameCollector visit_region {:?}", r);
66101
match *r {
67102
ty::ReFree(free) => {
68103
self.0.insert(free.scope);
69104
}
70-
71105
ty::ReEarlyBound(bound) => {
72106
self.0.insert(bound.def_id);
73107
}
@@ -94,12 +128,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
94128
}
95129
if note {
96130
err.note(
97-
"the lifetime requirements from the `trait` could not be fulfilled by the \
98-
`impl`",
131+
"the lifetime requirements from the `trait` could not be fulfilled by the `impl`",
99132
);
100133
err.help(
101-
"consider adding a named lifetime to the `trait` that constrains the item's \
102-
`self` argument, its inputs and its output with it",
134+
"verify the lifetime relationships in the `trait` and `impl` between the \
135+
`self` argument, the other inputs and its output",
103136
);
104137
}
105138
err.emit();

src/test/ui/coercion/coerce-mut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main() {
44
let x = 0;
55
f(&x);
66
//~^ ERROR mismatched types
7-
//~| expected mutable reference `&'z1 mut i32`
8-
//~| found reference `&'z2 {integer}`
7+
//~| expected mutable reference `&mut i32`
8+
//~| found reference `&{integer}`
99
//~| types differ in mutability
1010
}

src/test/ui/coercion/coerce-mut.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | f(&x);
55
| ^^ types differ in mutability
66
|
7-
= note: expected mutable reference `&'z1 mut i32`
8-
found reference `&'z2 {integer}`
7+
= note: expected mutable reference `&mut i32`
8+
found reference `&{integer}`
99

1010
error: aborting due to previous error
1111

src/test/ui/compare-method/reordered-type-param.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | fn b<F:Clone,G>(&self, _x: G) -> G { panic!() }
1111
| expected type parameter
1212
|
1313
= note: expected fn pointer `fn(&E, F) -> F`
14-
found fn pointer `fn(&'z0 E, G) -> G`
14+
found fn pointer `fn(&E, G) -> G`
1515
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
1616
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
1717

src/test/ui/hrtb/hrtb-exists-forall-fn.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let _: for<'b> fn(&'b u32) = foo();
77
| expected due to this
88
|
99
= note: expected fn pointer `for<'b> fn(&'b u32)`
10-
found fn pointer `fn(&'z0 u32)`
10+
found fn pointer `fn(&u32)`
1111

1212
error: aborting due to previous error
1313

src/test/ui/impl-trait/impl-generic-mismatch-ab.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn foo<B: Debug>(&self, a: &impl Debug, b: &B) { }
1010
| expected type parameter
1111
|
1212
= note: expected fn pointer `fn(&(), &B, &impl Debug)`
13-
found fn pointer `fn(&'z0 (), &impl Debug, &B)`
13+
found fn pointer `fn(&(), &impl Debug, &B)`
1414
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
1515
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
1616

src/test/ui/impl-trait/trait_type.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn fmt(&self, x: &str) -> () { }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
66
|
77
= note: expected fn pointer `fn(&MyType, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
8-
found fn pointer `fn(&'z0 MyType, &str)`
8+
found fn pointer `fn(&MyType, &str)`
99

1010
error[E0050]: method `fmt` has 1 parameter but the declaration in trait `std::fmt::Display::fmt` has 2
1111
--> $DIR/trait_type.rs:12:11

src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ LL | fn deref(&self) -> &Self::Target;
1010
| --------------------------------- expected fn(&Struct) -> &(dyn Trait + 'static)
1111
|
1212
= note: expected `fn(&Struct) -> &(dyn Trait + 'static)`
13-
found `fn(&'z0 Struct) -> &dyn Trait`
13+
found `fn(&Struct) -> &dyn Trait`
1414
= note: the lifetime requirements from the `trait` could not be fulfilled by the `impl`
15-
= help: consider adding a named lifetime to the `trait` that constrains the item's `self` argument, its inputs and its output with it
15+
= help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
1616

1717
error: aborting due to previous error
1818

src/test/ui/in-band-lifetimes/mismatched_trait_impl.nll.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ LL | fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {
99
|
1010
= note: expected `fn(&i32, &'a u32, &u32) -> &'a u32`
1111
found `fn(&i32, &u32, &u32) -> &u32`
12+
= note: the lifetime requirements from the `trait` could not be fulfilled by the `impl`
13+
= help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
1214

1315
error: aborting due to previous error
1416

0 commit comments

Comments
 (0)