Skip to content

Commit 2a92d82

Browse files
committed
Suggest 'a when trait object assoc type has '_
1 parent dec7f00 commit 2a92d82

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,9 +2971,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
29712971
// we identified that the return expression references only one argument, we
29722972
// would suggest borrowing only that argument, and we'd skip the prior
29732973
// "use `'static`" suggestion entirely.
2974-
if let [lt] = &lifetime_refs[..] && lt.kind == MissingLifetimeKind::Ampersand {
2975-
let pre = if let Some((kind, _span)) =
2976-
self.diagnostic_metadata.current_function
2974+
if let [lt] = &lifetime_refs[..]
2975+
&& (lt.kind == MissingLifetimeKind::Ampersand
2976+
|| lt.kind == MissingLifetimeKind::Underscore)
2977+
{
2978+
let pre = if lt.kind == MissingLifetimeKind::Ampersand
2979+
&& let Some((kind, _span)) =
2980+
self.diagnostic_metadata.current_function
29772981
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
29782982
&& !sig.decl.inputs.is_empty()
29792983
&& let sugg = sig
@@ -3013,8 +3017,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
30133017
Applicability::MaybeIncorrect,
30143018
);
30153019
"...or alternatively, you might want"
3016-
} else if let Some((kind, _span)) =
3017-
self.diagnostic_metadata.current_function
3020+
} else if (lt.kind == MissingLifetimeKind::Ampersand
3021+
|| lt.kind == MissingLifetimeKind::Underscore)
3022+
&& let Some((kind, _span)) =
3023+
self.diagnostic_metadata.current_function
30183024
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
30193025
&& let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
30203026
&& !sig.decl.inputs.is_empty()
@@ -3059,7 +3065,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
30593065
err,
30603066
None,
30613067
|err, higher_ranked, span, message, intro_sugg| {
3062-
info!(?span, ?message, ?intro_sugg);
30633068
err.multipart_suggestion_verbose(
30643069
message,
30653070
std::iter::once((span, intro_sugg))
@@ -3093,7 +3098,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
30933098
&& segments[0].ident.name == sym::str
30943099
{
30953100
// Don't suggest `-> str`, suggest `-> String`.
3096-
sugg = vec![(lt.span.with_hi(ty.span.hi()), "String".to_string())];
3101+
sugg = vec![
3102+
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
3103+
];
30973104
}
30983105
if let TyKind::Slice(inner_ty) = &ty.kind {
30993106
// Don't suggest `-> [T]`, suggest `-> Vec<T>`.
@@ -3104,11 +3111,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
31043111
}
31053112
}
31063113
};
3107-
err.multipart_suggestion_verbose(
3108-
format!("{pre} to return an owned value"),
3109-
sugg,
3110-
Applicability::MaybeIncorrect,
3111-
);
3114+
if lt.kind == MissingLifetimeKind::Ampersand {
3115+
err.multipart_suggestion_verbose(
3116+
format!("{pre} to return an owned value"),
3117+
sugg,
3118+
Applicability::MaybeIncorrect,
3119+
);
3120+
}
31123121
}
31133122
}
31143123

tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
5151
|
5252
LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
5353
| ~~~~~~~
54+
help: consider introducing a named lifetime parameter
55+
|
56+
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
57+
| ++++ ~~~ ~~~
5458

5559
error[E0106]: missing lifetime specifier
5660
--> $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
6367
|
6468
LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
6569
| ~~~~~~~
70+
help: consider introducing a named lifetime parameter
71+
|
72+
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
73+
| ++++ ~~~ ~~~
6674

6775
error[E0106]: missing lifetime specifier
6876
--> $DIR/impl-trait-missing-lifetime-gated.rs:47:37

tests/ui/suggestions/impl-trait-missing-lifetime.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
99
|
1010
LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
1111
| ~~~~~~~
12+
help: consider introducing a named lifetime parameter
13+
|
14+
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
15+
| ++++ ~~~ ~~~
1216

1317
error[E0106]: missing lifetime specifier
1418
--> $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
2125
|
2226
LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
2327
| ~~~~~~~
28+
help: consider introducing a named lifetime parameter
29+
|
30+
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
31+
| ++++ ~~~ ~~~
2432

2533
error: lifetime may not live long enough
2634
--> $DIR/impl-trait-missing-lifetime.rs:16:69

0 commit comments

Comments
 (0)