From dec7f00e158f04054320f63796fea1445ac18917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 15 Nov 2023 04:07:19 +0000 Subject: [PATCH] Fix incorrect lifetime suggestion --- .../rustc_resolve/src/late/diagnostics.rs | 4 ++-- ...urn-type-requires-explicit-lifetime.stderr | 21 ++----------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 2cd3e673382..ce981c333d8 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2853,8 +2853,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { "this function's return type contains a borrowed value, but there is no value \ for it to be borrowed from", ); - maybe_static = true; if in_scope_lifetimes.is_empty() { + maybe_static = true; in_scope_lifetimes = vec![( Ident::with_dummy_span(kw::StaticLifetime), (DUMMY_NODE_ID, LifetimeRes::Static), @@ -2865,8 +2865,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { "this function's return type contains a borrowed value with an elided \ lifetime, but the lifetime cannot be derived from the arguments", ); - maybe_static = true; if in_scope_lifetimes.is_empty() { + maybe_static = true; in_scope_lifetimes = vec![( Ident::with_dummy_span(kw::StaticLifetime), (DUMMY_NODE_ID, LifetimeRes::Static), diff --git a/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr b/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr index 37ebb178ec6..23ef36888f0 100644 --- a/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr +++ b/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr @@ -88,19 +88,10 @@ 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, but there is no value for it to be borrowed from -help: consider using the `'a` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static` +help: consider using the `'a` lifetime | LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &'a isize { | ++ -help: instead, you are more likely to want to change the argument to be borrowed... - | -LL | fn k<'a, T: WithLifetime<'a>>(_x: &T::Output) -> &isize { - | + -help: ...or alternatively, you might want to return an owned value - | -LL - fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize { -LL + fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> isize { - | error[E0106]: missing lifetime specifier --> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:45:37 @@ -109,18 +100,10 @@ LL | fn l<'a>(_: &'a str, _: &'a str) -> &str { "" } | ------- ------- ^ 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 `'a` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static` +help: consider using the `'a` lifetime | LL | fn l<'a>(_: &'a str, _: &'a str) -> &'a str { "" } | ++ -help: instead, you are more likely to want to change one of the arguments to be borrowed... - | -LL | fn l<'a>(_: &&'a str, _: &&'a str) -> &str { "" } - | + + -help: ...or alternatively, you might want to return an owned value - | -LL | fn l<'a>(_: &'a str, _: &'a str) -> String { "" } - | ~~~~~~ error: aborting due to 7 previous errors