Suggest 'a when trait object assoc type has '_

This commit is contained in:
Esteban Küber 2023-11-15 16:56:41 +00:00
parent dec7f00e15
commit 2a92d820c7
3 changed files with 37 additions and 12 deletions

View File

@ -2971,9 +2971,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// we identified that the return expression references only one argument, we // we identified that the return expression references only one argument, we
// would suggest borrowing only that argument, and we'd skip the prior // would suggest borrowing only that argument, and we'd skip the prior
// "use `'static`" suggestion entirely. // "use `'static`" suggestion entirely.
if let [lt] = &lifetime_refs[..] && lt.kind == MissingLifetimeKind::Ampersand { if let [lt] = &lifetime_refs[..]
let pre = if let Some((kind, _span)) = && (lt.kind == MissingLifetimeKind::Ampersand
self.diagnostic_metadata.current_function || lt.kind == MissingLifetimeKind::Underscore)
{
let pre = if lt.kind == MissingLifetimeKind::Ampersand
&& let Some((kind, _span)) =
self.diagnostic_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind && let FnKind::Fn(_, _, sig, _, _, _) = kind
&& !sig.decl.inputs.is_empty() && !sig.decl.inputs.is_empty()
&& let sugg = sig && let sugg = sig
@ -3013,8 +3017,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
"...or alternatively, you might want" "...or alternatively, you might want"
} else if let Some((kind, _span)) = } else if (lt.kind == MissingLifetimeKind::Ampersand
self.diagnostic_metadata.current_function || lt.kind == MissingLifetimeKind::Underscore)
&& let Some((kind, _span)) =
self.diagnostic_metadata.current_function
&& let FnKind::Fn(_, _, sig, _, _, _) = kind && let FnKind::Fn(_, _, sig, _, _, _) = kind
&& let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output && let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
&& !sig.decl.inputs.is_empty() && !sig.decl.inputs.is_empty()
@ -3059,7 +3065,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
err, err,
None, None,
|err, higher_ranked, span, message, intro_sugg| { |err, higher_ranked, span, message, intro_sugg| {
info!(?span, ?message, ?intro_sugg);
err.multipart_suggestion_verbose( err.multipart_suggestion_verbose(
message, message,
std::iter::once((span, intro_sugg)) std::iter::once((span, intro_sugg))
@ -3093,7 +3098,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
&& segments[0].ident.name == sym::str && segments[0].ident.name == sym::str
{ {
// Don't suggest `-> str`, suggest `-> String`. // Don't suggest `-> str`, suggest `-> String`.
sugg = vec![(lt.span.with_hi(ty.span.hi()), "String".to_string())]; sugg = vec![
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
];
} }
if let TyKind::Slice(inner_ty) = &ty.kind { if let TyKind::Slice(inner_ty) = &ty.kind {
// Don't suggest `-> [T]`, suggest `-> Vec<T>`. // Don't suggest `-> [T]`, suggest `-> Vec<T>`.
@ -3104,11 +3111,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
} }
} }
}; };
err.multipart_suggestion_verbose( if lt.kind == MissingLifetimeKind::Ampersand {
format!("{pre} to return an owned value"), err.multipart_suggestion_verbose(
sugg, format!("{pre} to return an owned value"),
Applicability::MaybeIncorrect, sugg,
); Applicability::MaybeIncorrect,
);
}
} }
} }

View File

@ -51,6 +51,10 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
| |
LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
| ~~~~~~~ | ~~~~~~~
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime-gated.rs:37:64 --> $DIR/impl-trait-missing-lifetime-gated.rs:37:64
@ -63,6 +67,10 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
| |
LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
| ~~~~~~~ | ~~~~~~~
help: consider introducing a named lifetime parameter
|
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime-gated.rs:47:37 --> $DIR/impl-trait-missing-lifetime-gated.rs:47:37

View File

@ -9,6 +9,10 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
| |
LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
| ~~~~~~~ | ~~~~~~~
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime.rs:16:60 --> $DIR/impl-trait-missing-lifetime.rs:16:60
@ -21,6 +25,10 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
| |
LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() } LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
| ~~~~~~~ | ~~~~~~~
help: consider introducing a named lifetime parameter
|
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/impl-trait-missing-lifetime.rs:16:69 --> $DIR/impl-trait-missing-lifetime.rs:16:69