Skip to content

Commit 5ba2220

Browse files
committed
Name RegionKind::ReVar lifetimes in diagnostics
1 parent 3fea832 commit 5ba2220

35 files changed

+105
-45
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,13 +987,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
987987
}
988988

989989
fn push_ty_ref<'tcx>(
990-
r: &ty::Region<'tcx>,
990+
region: &ty::Region<'tcx>,
991991
ty: Ty<'tcx>,
992992
mutbl: hir::Mutability,
993993
s: &mut DiagnosticStyledString,
994994
) {
995-
let mut r = r.to_string();
996-
if r == "'_" {
995+
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 == "'_" {
9971000
r.clear();
9981001
} else {
9991002
r.push(' ');

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
//! Error Reporting for `impl` items that do not match the obligations from their `trait`.
22
3+
use crate::hir::def_id::DefId;
34
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
45
use crate::infer::lexical_region_resolve::RegionResolutionError;
56
use crate::infer::{Subtype, ValuePairs};
67
use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
8+
use rustc_data_structures::fx::FxHashSet;
79
use rustc_errors::ErrorReported;
8-
use rustc_middle::ty::Ty;
10+
use rustc_middle::ty::error::ExpectedFound;
11+
use rustc_middle::ty::fold::TypeFoldable;
12+
use rustc_middle::ty::{self, Ty};
913
use rustc_span::Span;
1014

1115
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
@@ -52,9 +56,52 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5256
.tcx()
5357
.sess
5458
.struct_span_err(sp, "`impl` item signature doesn't match `trait` item signature");
55-
err.note(&format!("expected `{:?}`\n found `{:?}`", expected, found));
5659
err.span_label(sp, &format!("found {:?}", found));
5760
err.span_label(impl_sp, &format!("expected {:?}", expected));
61+
62+
struct EarlyBoundRegionHighlighter(FxHashSet<DefId>);
63+
impl<'tcx> ty::fold::TypeVisitor<'tcx> for EarlyBoundRegionHighlighter {
64+
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
65+
debug!("LateBoundRegionNameCollector visit_region {:?}", r);
66+
match *r {
67+
ty::ReFree(free) => {
68+
self.0.insert(free.scope);
69+
}
70+
71+
ty::ReEarlyBound(bound) => {
72+
self.0.insert(bound.def_id);
73+
}
74+
_ => {}
75+
}
76+
r.super_visit_with(self)
77+
}
78+
}
79+
80+
let mut visitor = EarlyBoundRegionHighlighter(FxHashSet::default());
81+
expected.visit_with(&mut visitor);
82+
83+
let note = !visitor.0.is_empty();
84+
85+
if let Some((expected, found)) = self
86+
.tcx()
87+
.infer_ctxt()
88+
.enter(|infcx| infcx.expected_found_str_ty(&ExpectedFound { expected, found }))
89+
{
90+
err.note_expected_found(&"", expected, &"", found);
91+
} else {
92+
// This fallback shouldn't be necessary, but let's keep it in just in case.
93+
err.note(&format!("expected `{:?}`\n found `{:?}`", expected, found));
94+
}
95+
if note {
96+
err.note(
97+
"the lifetime requirements from the `trait` could not be fulfilled by the \
98+
`impl`",
99+
);
100+
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",
103+
);
104+
}
58105
err.emit();
59106
}
60107
}

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 `&mut i32`
8-
//~| found reference `&{integer}`
7+
//~| expected mutable reference `&'z1 mut i32`
8+
//~| found reference `&'z2 {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 `&mut i32`
8-
found reference `&{integer}`
7+
= note: expected mutable reference `&'z1 mut i32`
8+
found reference `&'z2 {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(&E, G) -> G`
14+
found fn pointer `fn(&'z0 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(&u32)`
10+
found fn pointer `fn(&'z0 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(&(), &impl Debug, &B)`
13+
found fn pointer `fn(&'z0 (), &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(&MyType, &str)`
8+
found fn pointer `fn(&'z0 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +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(&Struct) -> &dyn Trait`
13+
found `fn(&'z0 Struct) -> &dyn Trait`
14+
= 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
1416

1517
error: aborting due to previous error
1618

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ LL | fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&i32, &u32, &u32) -> &u32
99
|
1010
= note: expected `fn(&i32, &'a u32, &u32) -> &'a u32`
11-
found `fn(&i32, &u32, &u32) -> &u32`
11+
found `fn(&'z0 i32, &'z1 u32, &'z2 u32) -> &'z2 u32`
12+
= note: the lifetime requirements from the `trait` could not be fulfilled by the `impl`
13+
= help: consider adding a named lifetime to the `trait` that constrains the item's `self` argument, its inputs and its output with it
1214

1315
error[E0623]: lifetime mismatch
1416
--> $DIR/mismatched_trait_impl.rs:10:9

0 commit comments

Comments
 (0)