Skip to content

Commit b965a19

Browse files
committed
Fix incorrect lifetime suggestion
1 parent b312b66 commit b965a19

File tree

2 files changed

+4
-21
lines changed

2 files changed

+4
-21
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2654,8 +2654,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
26542654
"this function's return type contains a borrowed value, but there is no value \
26552655
for it to be borrowed from",
26562656
);
2657-
maybe_static = true;
26582657
if in_scope_lifetimes.is_empty() {
2658+
maybe_static = true;
26592659
in_scope_lifetimes = vec![(
26602660
Ident::with_dummy_span(kw::StaticLifetime),
26612661
(DUMMY_NODE_ID, LifetimeRes::Static),
@@ -2666,8 +2666,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
26662666
"this function's return type contains a borrowed value with an elided \
26672667
lifetime, but the lifetime cannot be derived from the arguments",
26682668
);
2669-
maybe_static = true;
26702669
if in_scope_lifetimes.is_empty() {
2670+
maybe_static = true;
26712671
in_scope_lifetimes = vec![(
26722672
Ident::with_dummy_span(kw::StaticLifetime),
26732673
(DUMMY_NODE_ID, LifetimeRes::Static),

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

+2-19
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,10 @@ LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize {
8888
| ^ expected named lifetime parameter
8989
|
9090
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
91-
help: consider using the `'a` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
91+
help: consider using the `'a` lifetime
9292
|
9393
LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &'a isize {
9494
| ++
95-
help: instead, you are more likely to want to change the argument to be borrowed...
96-
|
97-
LL | fn k<'a, T: WithLifetime<'a>>(_x: &T::Output) -> &isize {
98-
| +
99-
help: ...or alternatively, you might want to return an owned value
100-
|
101-
LL - fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize {
102-
LL + fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> isize {
103-
|
10495

10596
error[E0106]: missing lifetime specifier
10697
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:45:37
@@ -109,18 +100,10 @@ LL | fn l<'a>(_: &'a str, _: &'a str) -> &str { "" }
109100
| ------- ------- ^ expected named lifetime parameter
110101
|
111102
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
112-
help: consider using the `'a` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
103+
help: consider using the `'a` lifetime
113104
|
114105
LL | fn l<'a>(_: &'a str, _: &'a str) -> &'a str { "" }
115106
| ++
116-
help: instead, you are more likely to want to change one of the arguments to be borrowed...
117-
|
118-
LL | fn l<'a>(_: &&'a str, _: &&'a str) -> &str { "" }
119-
| + +
120-
help: ...or alternatively, you might want to return an owned value
121-
|
122-
LL | fn l<'a>(_: &'a str, _: &'a str) -> String { "" }
123-
| ~~~~~~
124107

125108
error: aborting due to 7 previous errors
126109

0 commit comments

Comments
 (0)