Skip to content

Commit ac2fb7a

Browse files
committed
Tweak wording
1 parent de67990 commit ac2fb7a

13 files changed

+61
-34
lines changed

src/librustc_resolve/late/diagnostics.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
10351035
lifetime_names: &FxHashSet<ast::Ident>,
10361036
params: &[ElisionFailureInfo],
10371037
) {
1038+
let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok();
1039+
10381040
err.span_label(
10391041
span,
10401042
&format!(
@@ -1044,11 +1046,10 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
10441046
),
10451047
);
10461048

1047-
let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok();
10481049
let suggest_existing = |err: &mut DiagnosticBuilder<'_>, sugg| {
10491050
err.span_suggestion_verbose(
10501051
span,
1051-
"consider using the named lifetime",
1052+
&format!("consider using the `{}` lifetime", lifetime_names.iter().next().unwrap()),
10521053
sugg,
10531054
Applicability::MaybeIncorrect,
10541055
);
@@ -1138,6 +1139,20 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
11381139
(0, _, Some(snippet)) if !snippet.ends_with('>') && count == 1 => {
11391140
suggest_new(err, &format!("{}<'a>", snippet));
11401141
}
1142+
(n, ..) if n > 1 => {
1143+
let spans: Vec<Span> = lifetime_names.iter().map(|lt| lt.span).collect();
1144+
err.span_note(spans, "these named lifetimes are available to use");
1145+
if Some("") == snippet.as_deref() {
1146+
// This happens when we have `Foo<T>` where we point at the space before `T`,
1147+
// but this can be confusing so we give a suggestion with placeholders.
1148+
err.span_suggestion_verbose(
1149+
span,
1150+
"consider using one of the available lifetimes here",
1151+
"'lifetime, ".repeat(count),
1152+
Applicability::HasPlaceholders,
1153+
);
1154+
}
1155+
}
11411156
_ => {}
11421157
}
11431158
}

src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn elision<T: Fn() -> &i32>() {
55
| ^ expected named lifetime parameter
66
|
77
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
8-
help: consider using the named lifetime
8+
help: consider using the `'static` lifetime
99
|
1010
LL | fn elision<T: Fn() -> &'static i32>() {
1111
| ^^^^^^^^

src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn elision(_: fn() -> &i32) {
55
| ^ expected named lifetime parameter
66
|
77
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
8-
help: consider using the named lifetime
8+
help: consider using the `'static` lifetime
99
|
1010
LL | fn elision(_: fn() -> &'static i32) {
1111
| ^^^^^^^^

src/test/ui/async-await/issues/issue-63388-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | ) -> &dyn Foo
77
| ^ expected named lifetime parameter
88
|
99
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `foo` or `bar`
10-
help: consider using the named lifetime
10+
help: consider using the `'a` lifetime
1111
|
1212
LL | ) -> &'a dyn Foo
1313
| ^^^

src/test/ui/c-variadic/variadic-ffi-6.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | ) -> &usize {
55
| ^ expected named lifetime parameter
66
|
77
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
8-
help: consider using the named lifetime
8+
help: consider using the `'static` lifetime
99
|
1010
LL | ) -> &'static usize {
1111
| ^^^^^^^^

src/test/ui/foreign-fn-return-lifetime.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | pub fn f() -> &u8;
55
| ^ expected named lifetime parameter
66
|
77
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
8-
help: consider using the named lifetime
8+
help: consider using the `'static` lifetime
99
|
1010
LL | pub fn f() -> &'static u8;
1111
| ^^^^^^^^

src/test/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,26 @@ error[E0106]: missing lifetime specifier
99
|
1010
LL | fn foo<'b, L: X<&'b Nested<K>>>();
1111
| ^ expected named lifetime parameter
12+
|
13+
note: these named lifetimes are available to use
14+
--> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:9:12
15+
|
16+
LL | trait X<'a, K: 'a> {
17+
| ^^
18+
LL | fn foo<'b, L: X<&'b Nested<K>>>();
19+
| ^^
20+
help: consider using one of the available lifetimes here
21+
|
22+
LL | fn foo<'b, L: X<'lifetime, &'b Nested<K>>>();
23+
| ^^^^^^^^^^
1224

1325
error[E0106]: missing lifetime specifier
1426
--> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:13:17
1527
|
1628
LL | fn bar<'b, L: X<&'b Nested<i32>>>(){}
1729
| ^ expected named lifetime parameter
1830
|
19-
help: consider using the named lifetime
31+
help: consider using the `'b` lifetime
2032
|
2133
LL | fn bar<'b, L: X<'b, &'b Nested<i32>>>(){}
2234
| ^^^

src/test/ui/issues/issue-13497.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | &str
55
| ^ expected named lifetime parameter
66
|
77
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
8-
help: consider using the named lifetime
8+
help: consider using the `'static` lifetime
99
|
1010
LL | &'static str
1111
| ^^^^^^^^

src/test/ui/issues/issue-26638.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() }
1717
| ^ expected named lifetime parameter
1818
|
1919
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
20-
help: consider using the named lifetime
20+
help: consider using the `'static` lifetime
2121
|
2222
LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &'static str { iter() }
2323
| ^^^^^^^^
@@ -29,7 +29,7 @@ LL | fn parse_type_3() -> &str { unimplemented!() }
2929
| ^ expected named lifetime parameter
3030
|
3131
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
32-
help: consider using the named lifetime
32+
help: consider using the `'static` lifetime
3333
|
3434
LL | fn parse_type_3() -> &'static str { unimplemented!() }
3535
| ^^^^^^^^

src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn f() -> &isize {
55
| ^ expected named lifetime parameter
66
|
77
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
8-
help: consider using the named lifetime
8+
help: consider using the `'static` lifetime
99
|
1010
LL | fn f() -> &'static isize {
1111
| ^^^^^^^^
@@ -41,7 +41,7 @@ LL | fn i(_x: isize) -> &isize {
4141
| ^ expected named lifetime parameter
4242
|
4343
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
44-
help: consider using the named lifetime
44+
help: consider using the `'static` lifetime
4545
|
4646
LL | fn i(_x: isize) -> &'static isize {
4747
| ^^^^^^^^
@@ -53,7 +53,7 @@ LL | fn j(_x: StaticStr) -> &isize {
5353
| ^ expected named lifetime parameter
5454
|
5555
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
56-
help: consider using the named lifetime
56+
help: consider using the `'static` lifetime
5757
|
5858
LL | fn j(_x: StaticStr) -> &'static isize {
5959
| ^^^^^^^^
@@ -65,7 +65,7 @@ LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize {
6565
| ^ expected named lifetime parameter
6666
|
6767
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
68-
help: consider using the named lifetime
68+
help: consider using the `'a` lifetime
6969
|
7070
LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &'a isize {
7171
| ^^^

src/test/ui/suggestions/missing-lifetime-specifier.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | static a: RefCell<HashMap<i32, Vec<Vec<Foo>>>> = RefCell::new(HashMap::
55
| ^^^ expected 2 lifetime parameters
66
|
77
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
8-
help: consider using the named lifetime
8+
help: consider using the `'static` lifetime
99
|
1010
LL | static a: RefCell<HashMap<i32, Vec<Vec<Foo<'static, 'static>>>>> = RefCell::new(HashMap::new());
1111
| ^^^^^^^^^^^^^^^^^^^^^
@@ -17,7 +17,7 @@ LL | static a: RefCell<HashMap<i32, Vec<Vec<Foo>>>> = RefCell::new(HashMap::
1717
| ^^^ expected 2 lifetime parameters
1818
|
1919
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
20-
help: consider using the named lifetime
20+
help: consider using the `'static` lifetime
2121
|
2222
LL | static a: RefCell<HashMap<i32, Vec<Vec<Foo<'static, 'static>>>>> = RefCell::new(HashMap::new());
2323
| ^^^^^^^^^^^^^^^^^^^^^
@@ -29,7 +29,7 @@ LL | static b: RefCell<HashMap<i32, Vec<Vec<&Bar>>>> = RefCell::new(HashMap:
2929
| ^ expected named lifetime parameter
3030
|
3131
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
32-
help: consider using the named lifetime
32+
help: consider using the `'static` lifetime
3333
|
3434
LL | static b: RefCell<HashMap<i32, Vec<Vec<&'static Bar>>>> = RefCell::new(HashMap::new());
3535
| ^^^^^^^^
@@ -41,7 +41,7 @@ LL | static b: RefCell<HashMap<i32, Vec<Vec<&Bar>>>> = RefCell::new(HashMap:
4141
| ^^^ expected 2 lifetime parameters
4242
|
4343
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
44-
help: consider using the named lifetime
44+
help: consider using the `'static` lifetime
4545
|
4646
LL | static b: RefCell<HashMap<i32, Vec<Vec<&Bar<'static, 'static>>>>> = RefCell::new(HashMap::new());
4747
| ^^^^^^^^^^^^^^^^^^^^^
@@ -53,7 +53,7 @@ LL | static b: RefCell<HashMap<i32, Vec<Vec<&Bar>>>> = RefCell::new(HashMap:
5353
| ^ expected named lifetime parameter
5454
|
5555
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
56-
help: consider using the named lifetime
56+
help: consider using the `'static` lifetime
5757
|
5858
LL | static b: RefCell<HashMap<i32, Vec<Vec<&'static Bar>>>> = RefCell::new(HashMap::new());
5959
| ^^^^^^^^
@@ -65,7 +65,7 @@ LL | static b: RefCell<HashMap<i32, Vec<Vec<&Bar>>>> = RefCell::new(HashMap:
6565
| ^^^ expected 2 lifetime parameters
6666
|
6767
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
68-
help: consider using the named lifetime
68+
help: consider using the `'static` lifetime
6969
|
7070
LL | static b: RefCell<HashMap<i32, Vec<Vec<&Bar<'static, 'static>>>>> = RefCell::new(HashMap::new());
7171
| ^^^^^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ LL | static c: RefCell<HashMap<i32, Vec<Vec<Qux<i32>>>>> = RefCell::new(Hash
7777
| ^ expected 2 lifetime parameters
7878
|
7979
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
80-
help: consider using the named lifetime
80+
help: consider using the `'static` lifetime
8181
|
8282
LL | static c: RefCell<HashMap<i32, Vec<Vec<Qux<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
8383
| ^^^^^^^^^^^^^^^^^
@@ -89,7 +89,7 @@ LL | static c: RefCell<HashMap<i32, Vec<Vec<Qux<i32>>>>> = RefCell::new(Hash
8989
| ^ expected 2 lifetime parameters
9090
|
9191
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
92-
help: consider using the named lifetime
92+
help: consider using the `'static` lifetime
9393
|
9494
LL | static c: RefCell<HashMap<i32, Vec<Vec<Qux<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
9595
| ^^^^^^^^^^^^^^^^^
@@ -101,7 +101,7 @@ LL | static d: RefCell<HashMap<i32, Vec<Vec<&Tar<i32>>>>> = RefCell::new(Has
101101
| ^ expected named lifetime parameter
102102
|
103103
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
104-
help: consider using the named lifetime
104+
help: consider using the `'static` lifetime
105105
|
106106
LL | static d: RefCell<HashMap<i32, Vec<Vec<&'static Tar<i32>>>>> = RefCell::new(HashMap::new());
107107
| ^^^^^^^^
@@ -113,7 +113,7 @@ LL | static d: RefCell<HashMap<i32, Vec<Vec<&Tar<i32>>>>> = RefCell::new(Has
113113
| ^ expected 2 lifetime parameters
114114
|
115115
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
116-
help: consider using the named lifetime
116+
help: consider using the `'static` lifetime
117117
|
118118
LL | static d: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
119119
| ^^^^^^^^^^^^^^^^^
@@ -125,7 +125,7 @@ LL | static d: RefCell<HashMap<i32, Vec<Vec<&Tar<i32>>>>> = RefCell::new(Has
125125
| ^ expected named lifetime parameter
126126
|
127127
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
128-
help: consider using the named lifetime
128+
help: consider using the `'static` lifetime
129129
|
130130
LL | static d: RefCell<HashMap<i32, Vec<Vec<&'static Tar<i32>>>>> = RefCell::new(HashMap::new());
131131
| ^^^^^^^^
@@ -137,7 +137,7 @@ LL | static d: RefCell<HashMap<i32, Vec<Vec<&Tar<i32>>>>> = RefCell::new(Has
137137
| ^ expected 2 lifetime parameters
138138
|
139139
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
140-
help: consider using the named lifetime
140+
help: consider using the `'static` lifetime
141141
|
142142
LL | static d: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
143143
| ^^^^^^^^^^^^^^^^^
@@ -149,7 +149,7 @@ LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, i32>>>>> = RefCell
149149
| ^ expected named lifetime parameter
150150
|
151151
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
152-
help: consider using the named lifetime
152+
help: consider using the `'static` lifetime
153153
|
154154
LL | static f: RefCell<HashMap<i32, Vec<Vec<&'static Tar<'static, i32>>>>> = RefCell::new(HashMap::new());
155155
| ^^^^^^^^
@@ -161,7 +161,7 @@ LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, i32>>>>> = RefCell
161161
| ^ expected named lifetime parameter
162162
|
163163
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
164-
help: consider using the named lifetime
164+
help: consider using the `'static` lifetime
165165
|
166166
LL | static f: RefCell<HashMap<i32, Vec<Vec<&'static Tar<'static, i32>>>>> = RefCell::new(HashMap::new());
167167
| ^^^^^^^^

src/test/ui/suggestions/return-without-lifetime.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0106]: missing lifetime specifier
44
LL | struct Foo<'a>(&usize);
55
| ^ expected named lifetime parameter
66
|
7-
help: consider using the named lifetime
7+
help: consider using the `'a` lifetime
88
|
99
LL | struct Foo<'a>(&'a usize);
1010
| ^^^
@@ -16,7 +16,7 @@ LL | fn func1<'a>(_arg: &'a Thing) -> &() { unimplemented!() }
1616
| --------- ^ expected named lifetime parameter
1717
|
1818
= 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
19-
help: consider using the named lifetime
19+
help: consider using the `'a` lifetime
2020
|
2121
LL | fn func1<'a>(_arg: &'a Thing) -> &'a () { unimplemented!() }
2222
| ^^^
@@ -28,7 +28,7 @@ LL | fn func2<'a>(_arg: &Thing<'a>) -> &() { unimplemented!() }
2828
| ---------- ^ expected named lifetime parameter
2929
|
3030
= 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
31-
help: consider using the named lifetime
31+
help: consider using the `'a` lifetime
3232
|
3333
LL | fn func2<'a>(_arg: &Thing<'a>) -> &'a () { unimplemented!() }
3434
| ^^^

src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0106]: missing lifetime specifier
1616
LL | struct Baz<'a>(&'_ &'a u8);
1717
| ^^ expected named lifetime parameter
1818
|
19-
help: consider using the named lifetime
19+
help: consider using the `'a` lifetime
2020
|
2121
LL | struct Baz<'a>(&'a &'a u8);
2222
| ^^
@@ -28,7 +28,7 @@ LL | fn meh() -> Box<dyn for<'_> Meh<'_>>
2828
| ^^ expected named lifetime parameter
2929
|
3030
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
31-
help: consider using the named lifetime
31+
help: consider using the `'static` lifetime
3232
|
3333
LL | fn meh() -> Box<dyn for<'_> Meh<'static>>
3434
| ^^^^^^^

0 commit comments

Comments
 (0)