Skip to content

Commit

Permalink
Tweak binding lifetime suggestion text
Browse files Browse the repository at this point in the history
We already have a structured suggestion, but the wording made it seem
like that wasn't the case.
Fix #65286. r? @varkor
  • Loading branch information
estebank committed Feb 20, 2020
1 parent a643ee8 commit c816430
Show file tree
Hide file tree
Showing 23 changed files with 62 additions and 61 deletions.
22 changes: 12 additions & 10 deletions src/librustc_infer/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1781,28 +1781,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
bound_kind: GenericKind<'tcx>,
sub: S,
) {
let consider = format!(
"consider adding an explicit lifetime bound {}",
if type_param_span.map(|(_, _, is_impl_trait)| is_impl_trait).unwrap_or(false) {
format!(" `{}` to `{}`...", sub, bound_kind)
} else {
format!("`{}: {}`...", bound_kind, sub)
},
);
let msg = "consider adding an explicit lifetime bound";
if let Some((sp, has_lifetimes, is_impl_trait)) = type_param_span {
let suggestion = if is_impl_trait {
format!("{} + {}", bound_kind, sub)
} else {
let tail = if has_lifetimes { " + " } else { "" };
format!("{}: {}{}", bound_kind, sub, tail)
};
err.span_suggestion_short(
err.span_suggestion(
sp,
&consider,
&format!("{}...", msg),
suggestion,
Applicability::MaybeIncorrect, // Issue #41966
);
} else {
let consider = format!(
"{} {}...",
msg,
if type_param_span.map(|(_, _, is_impl_trait)| is_impl_trait).unwrap_or(false) {
format!(" `{}` to `{}`", sub, bound_kind)
} else {
format!("`{}: {}`", bound_kind, sub)
},
);
err.help(&consider);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0310]: the parameter type `T` may not live long enough
LL | impl <T: Sync> Foo for T { }
| -- ^^^
| |
| help: consider adding an explicit lifetime bound `T: 'static`...
| help: consider adding an explicit lifetime bound...: `T: 'static +`
|
note: ...so that the type `T` will meet its required lifetime bounds
--> $DIR/builtin-superkinds-self-type.rs:10:16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0310]: the parameter type `U` may not live long enough
--> $DIR/feature-gate-infer_static_outlives_requirements.rs:5:5
|
LL | struct Foo<U> {
| - help: consider adding an explicit lifetime bound `U: 'static`...
| - help: consider adding an explicit lifetime bound...: `U: 'static`
LL | bar: Bar<U>
| ^^^^^^^^^^^
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ error[E0310]: the parameter type `T` may not live long enough
LL | fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
| -- ^^^^^^^^^^^^^^^^^^^^
| |
| help: consider adding an explicit lifetime bound `T: 'static`...
| help: consider adding an explicit lifetime bound...: `T: 'static +`
|
note: ...so that the type `T` will meet its required lifetime bounds
--> $DIR/must_outlive_least_region_or_bound.rs:22:51
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/type_parameters_captured.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0310]: the parameter type `T` may not live long enough
LL | fn foo<T>(x: T) -> impl Any + 'static {
| - ^^^^^^^^^^^^^^^^^^
| |
| help: consider adding an explicit lifetime bound `T: 'static`...
| help: consider adding an explicit lifetime bound...: `T: 'static`
|
note: ...so that the type `T` will meet its required lifetime bounds
--> $DIR/type_parameters_captured.rs:7:20
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/lifetimes/lifetime-doesnt-live-long-enough.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:19:5
|
LL | struct Foo<T> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | foo: &'static T
| ^^^^^^^^^^^^^^^
|
Expand All @@ -16,7 +16,7 @@ error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:24:19
|
LL | trait X<K>: Sized {
| - help: consider adding an explicit lifetime bound `K: 'a`...
| - help: consider adding an explicit lifetime bound...: `K: 'a`
LL | fn foo<'a, L: X<&'a Nested<K>>>();
| ^^^^^^^^^^^^^^^^
|
Expand Down Expand Up @@ -45,7 +45,7 @@ error[E0309]: the parameter type `L` may not live long enough
LL | fn baz<'a, L, M: X<&'a Nested<L>>>() {
| - ^^^^^^^^^^^^^^^^
| |
| help: consider adding an explicit lifetime bound `L: 'a`...
| help: consider adding an explicit lifetime bound...: `L: 'a`
|
note: ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
--> $DIR/lifetime-doesnt-live-long-enough.rs:32:22
Expand All @@ -57,7 +57,7 @@ error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:41:33
|
LL | impl<K> Nested<K> {
| - help: consider adding an explicit lifetime bound `K: 'a`...
| - help: consider adding an explicit lifetime bound...: `K: 'a`
LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
| ^^^^^^^^^^^^^^^^
|
Expand All @@ -71,7 +71,7 @@ error[E0309]: the parameter type `M` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
|
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
| ^^^^^^^^^^^^^^^^ -- help: consider adding an explicit lifetime bound `M: 'a`...
| ^^^^^^^^^^^^^^^^ -- help: consider adding an explicit lifetime bound...: `M: 'a +`
|
note: ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
Expand Down
14 changes: 7 additions & 7 deletions src/test/ui/regions/regions-close-object-into-object-5.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-object-into-object-5.rs:17:5
|
LL | fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | // oh dear!
LL | box B(&*v) as Box<X>
| ^^^^^^^^^^
Expand All @@ -17,7 +17,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-object-into-object-5.rs:17:5
|
LL | fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | // oh dear!
LL | box B(&*v) as Box<X>
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -32,7 +32,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-object-into-object-5.rs:17:9
|
LL | fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | // oh dear!
LL | box B(&*v) as Box<X>
| ^
Expand All @@ -47,7 +47,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-object-into-object-5.rs:17:9
|
LL | fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | // oh dear!
LL | box B(&*v) as Box<X>
| ^^^^^^
Expand All @@ -62,7 +62,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-object-into-object-5.rs:17:11
|
LL | fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | // oh dear!
LL | box B(&*v) as Box<X>
| ^^^
Expand All @@ -77,7 +77,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-object-into-object-5.rs:17:11
|
LL | fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | // oh dear!
LL | box B(&*v) as Box<X>
| ^^^
Expand All @@ -92,7 +92,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-object-into-object-5.rs:17:11
|
LL | fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
LL | // oh dear!
LL | box B(&*v) as Box<X>
| ^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0310]: the parameter type `A` may not live long enough
--> $DIR/regions-close-over-type-parameter-1.rs:10:5
|
LL | fn make_object1<A:SomeTrait>(v: A) -> Box<dyn SomeTrait + 'static> {
| -- help: consider adding an explicit lifetime bound `A: 'static`...
| -- help: consider adding an explicit lifetime bound...: `A: 'static +`
LL | box v as Box<dyn SomeTrait + 'static>
| ^^^^^
|
Expand All @@ -16,7 +16,7 @@ error[E0310]: the parameter type `A` may not live long enough
--> $DIR/regions-close-over-type-parameter-1.rs:10:5
|
LL | fn make_object1<A:SomeTrait>(v: A) -> Box<dyn SomeTrait + 'static> {
| -- help: consider adding an explicit lifetime bound `A: 'static`...
| -- help: consider adding an explicit lifetime bound...: `A: 'static +`
LL | box v as Box<dyn SomeTrait + 'static>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
Expand All @@ -30,7 +30,7 @@ error[E0309]: the parameter type `A` may not live long enough
--> $DIR/regions-close-over-type-parameter-1.rs:20:5
|
LL | fn make_object3<'a,'b,A:SomeTrait+'a>(v: A) -> Box<dyn SomeTrait + 'b> {
| -- help: consider adding an explicit lifetime bound `A: 'b`...
| -- help: consider adding an explicit lifetime bound...: `A: 'b +`
LL | box v as Box<dyn SomeTrait + 'b>
| ^^^^^
|
Expand All @@ -44,7 +44,7 @@ error[E0309]: the parameter type `A` may not live long enough
--> $DIR/regions-close-over-type-parameter-1.rs:20:5
|
LL | fn make_object3<'a,'b,A:SomeTrait+'a>(v: A) -> Box<dyn SomeTrait + 'b> {
| -- help: consider adding an explicit lifetime bound `A: 'b`...
| -- help: consider adding an explicit lifetime bound...: `A: 'b +`
LL | box v as Box<dyn SomeTrait + 'b>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/regions/regions-close-param-into-object.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-param-into-object.rs:6:5
|
LL | fn p1<T>(v: T) -> Box<dyn X + 'static>
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
...
LL | Box::new(v)
| ^^^^^^^^^^^
Expand All @@ -17,7 +17,7 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/regions-close-param-into-object.rs:12:5
|
LL | fn p2<T>(v: Box<T>) -> Box<dyn X + 'static>
| - help: consider adding an explicit lifetime bound `T: 'static`...
| - help: consider adding an explicit lifetime bound...: `T: 'static`
...
LL | Box::new(v)
| ^^^^^^^^^^^
Expand All @@ -32,7 +32,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-close-param-into-object.rs:18:5
|
LL | fn p3<'a,T>(v: T) -> Box<dyn X + 'a>
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
...
LL | Box::new(v)
| ^^^^^^^^^^^
Expand All @@ -47,7 +47,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-close-param-into-object.rs:24:5
|
LL | fn p4<'a,T>(v: Box<T>) -> Box<dyn X + 'a>
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
...
LL | Box::new(v)
| ^^^^^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/regions/regions-enum-not-wf.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:18:18
|
LL | enum Ref1<'a, T> {
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | Ref1Variant1(RequireOutlives<'a, T>)
| ^^^^^^^^^^^^^^^^^^^^^^
|
Expand All @@ -16,7 +16,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:23:25
|
LL | enum Ref2<'a, T> {
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | Ref2Variant1,
LL | Ref2Variant2(isize, RequireOutlives<'a, T>),
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -31,7 +31,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:35:1
|
LL | enum RefDouble<'a, 'b, T> {
| ^ - help: consider adding an explicit lifetime bound `T: 'b`...
| ^ - help: consider adding an explicit lifetime bound...: `T: 'b`
| _|
| |
LL | | RefDoubleVariant1(&'a RequireOutlives<'b, T>)
Expand All @@ -52,7 +52,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:36:23
|
LL | enum RefDouble<'a, 'b, T> {
| - help: consider adding an explicit lifetime bound `T: 'b`...
| - help: consider adding an explicit lifetime bound...: `T: 'b`
LL | RefDoubleVariant1(&'a RequireOutlives<'b, T>)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-implied-bounds-projection-gap-1.rs:16:10
|
LL | fn func<'x, T:Trait1<'x>>(t: &'x T::Foo)
| -- help: consider adding an explicit lifetime bound `T: 'x`...
| -- help: consider adding an explicit lifetime bound...: `T: 'x +`
LL | {
LL | wf::<&'x T>();
| ^^^^^
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/regions/regions-infer-bound-from-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0309]: the parameter type `A` may not live long enough
--> $DIR/regions-infer-bound-from-trait.rs:33:5
|
LL | fn bar1<'a,A>(x: Inv<'a>, a: A) {
| - help: consider adding an explicit lifetime bound `A: 'a`...
| - help: consider adding an explicit lifetime bound...: `A: 'a`
LL | check_bound(x, a)
| ^^^^^^^^^^^
|
Expand All @@ -16,7 +16,7 @@ error[E0309]: the parameter type `A` may not live long enough
--> $DIR/regions-infer-bound-from-trait.rs:37:5
|
LL | fn bar2<'a,'b,A:Is<'b>>(x: Inv<'a>, y: Inv<'b>, a: A) {
| -- help: consider adding an explicit lifetime bound `A: 'a`...
| -- help: consider adding an explicit lifetime bound...: `A: 'a +`
LL | check_bound(x, a)
| ^^^^^^^^^^^
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0310]: the parameter type `U` may not live long enough
--> $DIR/dont-infer-static.rs:8:5
|
LL | struct Foo<U> {
| - help: consider adding an explicit lifetime bound `U: 'static`...
| - help: consider adding an explicit lifetime bound...: `U: 'static`
LL | bar: Bar<U>
| ^^^^^^^^^^^
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:18:18
|
LL | enum Ref1<'a, T> {
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | Ref1Variant1(RequireOutlives<'a, T>)
| ^^^^^^^^^^^^^^^^^^^^^^
|
Expand All @@ -16,7 +16,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:23:25
|
LL | enum Ref2<'a, T> {
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | Ref2Variant1,
LL | Ref2Variant2(isize, RequireOutlives<'a, T>),
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -31,7 +31,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:35:1
|
LL | enum RefDouble<'a, 'b, T> {
| ^ - help: consider adding an explicit lifetime bound `T: 'b`...
| ^ - help: consider adding an explicit lifetime bound...: `T: 'b`
| _|
| |
LL | | RefDoubleVariant1(&'a RequireOutlives<'b, T>)
Expand All @@ -52,7 +52,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-enum-not-wf.rs:36:23
|
LL | enum RefDouble<'a, 'b, T> {
| - help: consider adding an explicit lifetime bound `T: 'b`...
| - help: consider adding an explicit lifetime bound...: `T: 'b`
LL | RefDoubleVariant1(&'a RequireOutlives<'b, T>)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-struct-not-wf.rs:13:5
|
LL | impl<'a, T> Trait<'a, T> for usize {
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | type Out = &'a T;
| ^^^^^^^^^^^^^^^^^
|
Expand All @@ -16,7 +16,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-struct-not-wf.rs:21:5
|
LL | impl<'a, T> Trait<'a, T> for u32 {
| - help: consider adding an explicit lifetime bound `T: 'a`...
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | type Out = RefOk<'a, T>;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/suggest-impl-trait-lifetime.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fmt::Debug;

fn foo(d: impl Debug + 'static) {
//~^ HELP consider adding an explicit lifetime bound `'static` to `impl Debug`
//~^ HELP consider adding an explicit lifetime bound...
bar(d);
//~^ ERROR the parameter type `impl Debug` may not live long enough
//~| NOTE ...so that the type `impl Debug` will meet its required lifetime bounds
Expand Down
Loading

0 comments on commit c816430

Please sign in to comment.