review comment: tweak wording and account for span overlap
This commit is contained in:
parent
65f492be12
commit
731ea85f21
@ -1806,8 +1806,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||
|err: &mut DiagnosticBuilder<'tcx>,
|
||||
type_param_span: Option<(Span, bool, bool)>,
|
||||
bound_kind: GenericKind<'tcx>| {
|
||||
let msg = "consider introducing an explicit lifetime bound to unify the type \
|
||||
parameter and the output";
|
||||
let msg = "consider introducing an explicit lifetime bound";
|
||||
if let Some((sp, has_lifetimes, is_impl_trait)) = type_param_span {
|
||||
let suggestion = if is_impl_trait {
|
||||
(sp.shrink_to_hi(), format!(" + {}", new_lt))
|
||||
|
@ -27,8 +27,23 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
||||
let return_sp = sub_origin.span();
|
||||
let mut err =
|
||||
self.tcx().sess.struct_span_err(sp, "cannot infer an appropriate lifetime");
|
||||
err.span_label(return_sp, "this evaluates to the `'static` lifetime...");
|
||||
err.span_label(sup_origin.span(), "...but this borrow...");
|
||||
if sp == sup_origin.span() && return_sp == sp {
|
||||
// Example: `ui/object-lifetime/object-lifetime-default-from-box-error.rs`
|
||||
err.span_label(
|
||||
sup_origin.span(),
|
||||
"this needs to be `'static` but the borrow...",
|
||||
);
|
||||
} else {
|
||||
err.span_label(return_sp, "this is `'static`...");
|
||||
// We try to make the output have fewer overlapping spans if possible.
|
||||
if sp == sup_origin.span() || !return_sp.overlaps(sup_origin.span()) {
|
||||
// When `sp == sup_origin` we already have overlapping spans in the
|
||||
// main diagnostic output, so we don't split this into its own note.
|
||||
err.span_label(sup_origin.span(), "...but this borrow...");
|
||||
} else {
|
||||
err.span_note(sup_origin.span(), "...but this borrow...");
|
||||
}
|
||||
}
|
||||
|
||||
let (lifetime, lt_sp_opt) = msg_span_from_free_region(self.tcx(), sup_r);
|
||||
if let Some(lifetime_sp) = lt_sp_opt {
|
||||
|
@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime
|
||||
LL | pub async fn run_dummy_fn(&self) {
|
||||
| ^^^^^ ...but this borrow...
|
||||
LL | foo(|| self.bar()).await;
|
||||
| --- this evaluates to the `'static` lifetime...
|
||||
| --- this is `'static`...
|
||||
|
|
||||
note: ...can't outlive the lifetime `'_` as defined on the method body at 12:31
|
||||
--> $DIR/issue-62097.rs:12:31
|
||||
|
@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime
|
||||
LL | fn elided(x: &i32) -> impl Copy { x }
|
||||
| --------- ^ ...but this borrow...
|
||||
| |
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| this is `'static`...
|
||||
|
|
||||
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1
|
||||
--> $DIR/must_outlive_least_region_or_bound.rs:3:1
|
||||
@ -22,7 +22,7 @@ error: cannot infer an appropriate lifetime
|
||||
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
|
||||
| --------- ^ ...but this borrow...
|
||||
| |
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| this is `'static`...
|
||||
|
|
||||
note: ...can't outlive the lifetime `'a` as defined on the function body at 6:13
|
||||
--> $DIR/must_outlive_least_region_or_bound.rs:6:13
|
||||
@ -40,7 +40,7 @@ error: cannot infer an appropriate lifetime
|
||||
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
|
||||
| -------------------------------- ^ ...but this borrow...
|
||||
| |
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| this is `'static`...
|
||||
|
|
||||
note: ...can't outlive the lifetime `'a` as defined on the function body at 12:15
|
||||
--> $DIR/must_outlive_least_region_or_bound.rs:12:15
|
||||
|
@ -2,7 +2,7 @@ error: cannot infer an appropriate lifetime
|
||||
--> $DIR/static-return-lifetime-infered.rs:7:16
|
||||
|
|
||||
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
|
||||
| ----------------------- this evaluates to the `'static` lifetime...
|
||||
| ----------------------- this is `'static`...
|
||||
LL | self.x.iter().map(|a| a.0)
|
||||
| ------ ^^^^
|
||||
| |
|
||||
@ -24,7 +24,7 @@ error: cannot infer an appropriate lifetime
|
||||
--> $DIR/static-return-lifetime-infered.rs:11:16
|
||||
|
|
||||
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
|
||||
| ----------------------- this evaluates to the `'static` lifetime...
|
||||
| ----------------------- this is `'static`...
|
||||
LL | self.x.iter().map(|a| a.0)
|
||||
| ------ ^^^^
|
||||
| |
|
||||
|
@ -5,7 +5,7 @@ LL | Box::new(value) as Box<dyn Any>
|
||||
| ---------^^^^^-
|
||||
| | |
|
||||
| | ...but this borrow...
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| this is `'static`...
|
||||
|
|
||||
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1
|
||||
--> $DIR/issue-16922.rs:3:1
|
||||
|
@ -2,10 +2,7 @@ error: cannot infer an appropriate lifetime
|
||||
--> $DIR/object-lifetime-default-from-box-error.rs:18:5
|
||||
|
|
||||
LL | ss.r
|
||||
| ^^^^
|
||||
| |
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| ...but this borrow...
|
||||
| ^^^^ this needs to be `'static` but the borrow...
|
||||
|
|
||||
note: ...can't outlive the anonymous lifetime #2 defined on the function body at 14:1
|
||||
--> $DIR/object-lifetime-default-from-box-error.rs:14:1
|
||||
|
@ -21,7 +21,7 @@ LL | Box::new(v)
|
||||
| ---------^-
|
||||
| | |
|
||||
| | ...but this borrow...
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| this is `'static`...
|
||||
|
|
||||
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 17:1
|
||||
--> $DIR/region-object-lifetime-in-coercion.rs:17:1
|
||||
|
@ -2,7 +2,7 @@ error: cannot infer an appropriate lifetime
|
||||
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:16
|
||||
|
|
||||
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
|
||||
| ^^^^ ---------- this evaluates to the `'static` lifetime...
|
||||
| ^^^^ ---------- this is `'static`...
|
||||
| |
|
||||
| ...but this borrow...
|
||||
|
|
||||
|
@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime
|
||||
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
|
||||
| ---------- ^^^^ ...but this borrow...
|
||||
| |
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| this is `'static`...
|
||||
|
|
||||
note: ...can't outlive the anonymous lifetime #1 defined on the method body at 6:5
|
||||
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6:5
|
||||
|
@ -10,7 +10,7 @@ error: cannot infer an appropriate lifetime
|
||||
--> $DIR/missing-lifetimes-in-signature.rs:19:5
|
||||
|
|
||||
LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
|
||||
| ------------- this evaluates to the `'static` lifetime...
|
||||
| ------------- this is `'static`...
|
||||
...
|
||||
LL | / move || {
|
||||
LL | | *dest = g.get();
|
||||
@ -55,7 +55,7 @@ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:30:5:
|
||||
|
|
||||
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
help: consider introducing an explicit lifetime bound to unify the type parameter and the output
|
||||
help: consider introducing an explicit lifetime bound
|
||||
|
|
||||
LL | fn bar<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
|
||||
| ^^^^^ ^^^^
|
||||
@ -82,7 +82,7 @@ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:52:5:
|
||||
|
|
||||
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
help: consider introducing an explicit lifetime bound to unify the type parameter and the output
|
||||
help: consider introducing an explicit lifetime bound
|
||||
|
|
||||
LL | fn qux<'b, 'a, G: 'b + 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'b
|
||||
| ^^^ ^^^^^^^ ^^^^
|
||||
|
@ -2,11 +2,13 @@ error: cannot infer an appropriate lifetime
|
||||
--> $DIR/dyn-trait-underscore.rs:8:20
|
||||
|
|
||||
LL | Box::new(items.iter())
|
||||
| ---------------^^^^---
|
||||
| | |
|
||||
| | ...but this borrow...
|
||||
| this evaluates to the `'static` lifetime...
|
||||
| ---------------^^^^--- this is `'static`...
|
||||
|
|
||||
note: ...but this borrow...
|
||||
--> $DIR/dyn-trait-underscore.rs:8:14
|
||||
|
|
||||
LL | Box::new(items.iter())
|
||||
| ^^^^^
|
||||
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 6:1
|
||||
--> $DIR/dyn-trait-underscore.rs:6:1
|
||||
|
|
||||
|
Loading…
x
Reference in New Issue
Block a user