From 1f43fc020935aaf0ca545c1fca2ec7c2ae923f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 17 Apr 2020 10:46:22 -0700 Subject: [PATCH] Tweak wording --- src/librustc_resolve/late/diagnostics.rs | 19 +++++++++++-- ...nd-lifetime-in-binding-only.elision.stderr | 2 +- ...und-lifetime-in-return-only.elision.stderr | 2 +- .../async-await/issues/issue-63388-2.stderr | 2 +- src/test/ui/c-variadic/variadic-ffi-6.stderr | 2 +- src/test/ui/foreign-fn-return-lifetime.stderr | 2 +- ...rrect-explicit-lifetime-name-needed.stderr | 14 +++++++++- src/test/ui/issues/issue-13497.stderr | 2 +- src/test/ui/issues/issue-26638.stderr | 4 +-- ...urn-type-requires-explicit-lifetime.stderr | 8 +++--- .../missing-lifetime-specifier.stderr | 28 +++++++++---------- .../return-without-lifetime.stderr | 6 ++-- .../underscore-lifetime-binders.stderr | 4 +-- 13 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index 5e03d4e1140..c25a3524dc1 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -1034,6 +1034,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { lifetime_names: &FxHashSet, params: &[ElisionFailureInfo], ) { + let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok(); + err.span_label( span, &format!( @@ -1043,11 +1045,10 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { ), ); - let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok(); let suggest_existing = |err: &mut DiagnosticBuilder<'_>, sugg| { err.span_suggestion_verbose( span, - "consider using the named lifetime", + &format!("consider using the `{}` lifetime", lifetime_names.iter().next().unwrap()), sugg, Applicability::MaybeIncorrect, ); @@ -1137,6 +1138,20 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { (0, _, Some(snippet)) if !snippet.ends_with('>') && count == 1 => { suggest_new(err, &format!("{}<'a>", snippet)); } + (n, ..) if n > 1 => { + let spans: Vec = lifetime_names.iter().map(|lt| lt.span).collect(); + err.span_note(spans, "these named lifetimes are available to use"); + if Some("") == snippet.as_deref() { + // This happens when we have `Foo` where we point at the space before `T`, + // but this can be confusing so we give a suggestion with placeholders. + err.span_suggestion_verbose( + span, + "consider using one of the available lifetimes here", + "'lifetime, ".repeat(count), + Applicability::HasPlaceholders, + ); + } + } _ => {} } } diff --git a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr index 7608c4a68f1..00f44129cc8 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr @@ -5,7 +5,7 @@ LL | fn elision &i32>() { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn elision &'static i32>() { | ^^^^^^^^ diff --git a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr index 2e31a4f3abb..a5242707c71 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr @@ -5,7 +5,7 @@ LL | fn elision(_: fn() -> &i32) { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn elision(_: fn() -> &'static i32) { | ^^^^^^^^ diff --git a/src/test/ui/async-await/issues/issue-63388-2.stderr b/src/test/ui/async-await/issues/issue-63388-2.stderr index 74f760d6c92..ca42263dfed 100644 --- a/src/test/ui/async-await/issues/issue-63388-2.stderr +++ b/src/test/ui/async-await/issues/issue-63388-2.stderr @@ -7,7 +7,7 @@ LL | ) -> &dyn Foo | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `foo` or `bar` -help: consider using the named lifetime +help: consider using the `'a` lifetime | LL | ) -> &'a dyn Foo | ^^^ diff --git a/src/test/ui/c-variadic/variadic-ffi-6.stderr b/src/test/ui/c-variadic/variadic-ffi-6.stderr index 0d491faa9d1..4626a4bc2dc 100644 --- a/src/test/ui/c-variadic/variadic-ffi-6.stderr +++ b/src/test/ui/c-variadic/variadic-ffi-6.stderr @@ -5,7 +5,7 @@ LL | ) -> &usize { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | ) -> &'static usize { | ^^^^^^^^ diff --git a/src/test/ui/foreign-fn-return-lifetime.stderr b/src/test/ui/foreign-fn-return-lifetime.stderr index 14153f302da..feecb6d80e7 100644 --- a/src/test/ui/foreign-fn-return-lifetime.stderr +++ b/src/test/ui/foreign-fn-return-lifetime.stderr @@ -5,7 +5,7 @@ LL | pub fn f() -> &u8; | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | pub fn f() -> &'static u8; | ^^^^^^^^ diff --git a/src/test/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr b/src/test/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr index 3c5bdccd966..321ce7eb347 100644 --- a/src/test/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr +++ b/src/test/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr @@ -9,6 +9,18 @@ error[E0106]: missing lifetime specifier | LL | fn foo<'b, L: X<&'b Nested>>(); | ^ expected named lifetime parameter + | +note: these named lifetimes are available to use + --> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:9:12 + | +LL | trait X<'a, K: 'a> { + | ^^ +LL | fn foo<'b, L: X<&'b Nested>>(); + | ^^ +help: consider using one of the available lifetimes here + | +LL | fn foo<'b, L: X<'lifetime, &'b Nested>>(); + | ^^^^^^^^^^ error[E0106]: missing lifetime specifier --> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:13:17 @@ -16,7 +28,7 @@ error[E0106]: missing lifetime specifier LL | fn bar<'b, L: X<&'b Nested>>(){} | ^ expected named lifetime parameter | -help: consider using the named lifetime +help: consider using the `'b` lifetime | LL | fn bar<'b, L: X<'b, &'b Nested>>(){} | ^^^ diff --git a/src/test/ui/issues/issue-13497.stderr b/src/test/ui/issues/issue-13497.stderr index 8454c6be61d..a231f73d067 100644 --- a/src/test/ui/issues/issue-13497.stderr +++ b/src/test/ui/issues/issue-13497.stderr @@ -5,7 +5,7 @@ LL | &str | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | &'static str | ^^^^^^^^ diff --git a/src/test/ui/issues/issue-26638.stderr b/src/test/ui/issues/issue-26638.stderr index 3049ea772f2..3df58d66d1f 100644 --- a/src/test/ui/issues/issue-26638.stderr +++ b/src/test/ui/issues/issue-26638.stderr @@ -17,7 +17,7 @@ LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() } | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &'static str { iter() } | ^^^^^^^^ @@ -29,7 +29,7 @@ LL | fn parse_type_3() -> &str { unimplemented!() } | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn parse_type_3() -> &'static str { unimplemented!() } | ^^^^^^^^ diff --git a/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr b/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr index dae1e390189..5809b5bd661 100644 --- a/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr +++ b/src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr @@ -5,7 +5,7 @@ LL | fn f() -> &isize { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn f() -> &'static isize { | ^^^^^^^^ @@ -41,7 +41,7 @@ LL | fn i(_x: isize) -> &isize { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn i(_x: isize) -> &'static isize { | ^^^^^^^^ @@ -53,7 +53,7 @@ LL | fn j(_x: StaticStr) -> &isize { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn j(_x: StaticStr) -> &'static isize { | ^^^^^^^^ @@ -65,7 +65,7 @@ LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments -help: consider using the named lifetime +help: consider using the `'a` lifetime | LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &'a isize { | ^^^ diff --git a/src/test/ui/suggestions/missing-lifetime-specifier.stderr b/src/test/ui/suggestions/missing-lifetime-specifier.stderr index aeb1229d174..6dd81961c44 100644 --- a/src/test/ui/suggestions/missing-lifetime-specifier.stderr +++ b/src/test/ui/suggestions/missing-lifetime-specifier.stderr @@ -5,7 +5,7 @@ LL | static a: RefCell>>> = RefCell::new(HashMap:: | ^^^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static a: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^ @@ -17,7 +17,7 @@ LL | static a: RefCell>>> = RefCell::new(HashMap:: | ^^^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static a: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^ @@ -29,7 +29,7 @@ LL | static b: RefCell>>> = RefCell::new(HashMap: | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static b: RefCell>>> = RefCell::new(HashMap::new()); | ^^^^^^^^ @@ -41,7 +41,7 @@ LL | static b: RefCell>>> = RefCell::new(HashMap: | ^^^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static b: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^ @@ -53,7 +53,7 @@ LL | static b: RefCell>>> = RefCell::new(HashMap: | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static b: RefCell>>> = RefCell::new(HashMap::new()); | ^^^^^^^^ @@ -65,7 +65,7 @@ LL | static b: RefCell>>> = RefCell::new(HashMap: | ^^^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static b: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^ @@ -77,7 +77,7 @@ LL | static c: RefCell>>>> = RefCell::new(Hash | ^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static c: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^ @@ -89,7 +89,7 @@ LL | static c: RefCell>>>> = RefCell::new(Hash | ^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static c: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^ @@ -101,7 +101,7 @@ LL | static d: RefCell>>>> = RefCell::new(Has | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static d: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^ @@ -113,7 +113,7 @@ LL | static d: RefCell>>>> = RefCell::new(Has | ^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static d: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^ @@ -125,7 +125,7 @@ LL | static d: RefCell>>>> = RefCell::new(Has | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static d: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^ @@ -137,7 +137,7 @@ LL | static d: RefCell>>>> = RefCell::new(Has | ^ expected 2 lifetime parameters | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static d: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^^^^^^^^^^ @@ -149,7 +149,7 @@ LL | static f: RefCell>>>> = RefCell | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static f: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^ @@ -161,7 +161,7 @@ LL | static f: RefCell>>>> = RefCell | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | static f: RefCell>>>> = RefCell::new(HashMap::new()); | ^^^^^^^^ diff --git a/src/test/ui/suggestions/return-without-lifetime.stderr b/src/test/ui/suggestions/return-without-lifetime.stderr index a93830edf3e..2a237d61f50 100644 --- a/src/test/ui/suggestions/return-without-lifetime.stderr +++ b/src/test/ui/suggestions/return-without-lifetime.stderr @@ -4,7 +4,7 @@ error[E0106]: missing lifetime specifier LL | struct Foo<'a>(&usize); | ^ expected named lifetime parameter | -help: consider using the named lifetime +help: consider using the `'a` lifetime | LL | struct Foo<'a>(&'a usize); | ^^^ @@ -16,7 +16,7 @@ LL | fn func1<'a>(_arg: &'a Thing) -> &() { unimplemented!() } | --------- ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but the signature does not say which one of `_arg`'s 2 lifetimes it is borrowed from -help: consider using the named lifetime +help: consider using the `'a` lifetime | LL | fn func1<'a>(_arg: &'a Thing) -> &'a () { unimplemented!() } | ^^^ @@ -28,7 +28,7 @@ LL | fn func2<'a>(_arg: &Thing<'a>) -> &() { unimplemented!() } | ---------- ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but the signature does not say which one of `_arg`'s 2 lifetimes it is borrowed from -help: consider using the named lifetime +help: consider using the `'a` lifetime | LL | fn func2<'a>(_arg: &Thing<'a>) -> &'a () { unimplemented!() } | ^^^ diff --git a/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr b/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr index ffccd100cb0..594cdd245b3 100644 --- a/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr +++ b/src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr @@ -16,7 +16,7 @@ error[E0106]: missing lifetime specifier LL | struct Baz<'a>(&'_ &'a u8); | ^^ expected named lifetime parameter | -help: consider using the named lifetime +help: consider using the `'a` lifetime | LL | struct Baz<'a>(&'a &'a u8); | ^^ @@ -28,7 +28,7 @@ LL | fn meh() -> Box Meh<'_>> | ^^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from -help: consider using the named lifetime +help: consider using the `'static` lifetime | LL | fn meh() -> Box Meh<'static>> | ^^^^^^^