Skip to content

Commit cae02f7

Browse files
committed
Suggest 'a when trait object assoc type has '_
1 parent b965a19 commit cae02f7

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -2772,9 +2772,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
27722772
// we identified that the return expression references only one argument, we
27732773
// would suggest borrowing only that argument, and we'd skip the prior
27742774
// "use `'static`" suggestion entirely.
2775-
if let [lt] = &lifetime_refs[..] && lt.kind == MissingLifetimeKind::Ampersand {
2776-
let pre = if let Some((kind, _span)) =
2777-
self.diagnostic_metadata.current_function
2775+
if let [lt] = &lifetime_refs[..]
2776+
&& (lt.kind == MissingLifetimeKind::Ampersand
2777+
|| lt.kind == MissingLifetimeKind::Underscore)
2778+
{
2779+
let pre = if lt.kind == MissingLifetimeKind::Ampersand
2780+
&& let Some((kind, _span)) =
2781+
self.diagnostic_metadata.current_function
27782782
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
27792783
&& !sig.decl.inputs.is_empty()
27802784
&& let sugg = sig
@@ -2814,8 +2818,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
28142818
Applicability::MaybeIncorrect,
28152819
);
28162820
"...or alternatively, you might want"
2817-
} else if let Some((kind, _span)) =
2818-
self.diagnostic_metadata.current_function
2821+
} else if (lt.kind == MissingLifetimeKind::Ampersand
2822+
|| lt.kind == MissingLifetimeKind::Underscore)
2823+
&& let Some((kind, _span)) =
2824+
self.diagnostic_metadata.current_function
28192825
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
28202826
&& let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
28212827
&& !sig.decl.inputs.is_empty()
@@ -2860,7 +2866,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
28602866
err,
28612867
None,
28622868
|err, higher_ranked, span, message, intro_sugg| {
2863-
info!(?span, ?message, ?intro_sugg);
28642869
err.multipart_suggestion_verbose(
28652870
message,
28662871
std::iter::once((span, intro_sugg))
@@ -2894,7 +2899,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
28942899
&& segments[0].ident.name == sym::str
28952900
{
28962901
// Don't suggest `-> str`, suggest `-> String`.
2897-
sugg = vec![(lt.span.with_hi(ty.span.hi()), "String".to_string())];
2902+
sugg = vec![
2903+
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
2904+
];
28982905
}
28992906
if let TyKind::Slice(inner_ty) = &ty.kind {
29002907
// Don't suggest `-> [T]`, suggest `-> Vec<T>`.
@@ -2905,11 +2912,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
29052912
}
29062913
}
29072914
};
2908-
err.multipart_suggestion_verbose(
2909-
format!("{pre} to return an owned value"),
2910-
sugg,
2911-
Applicability::MaybeIncorrect,
2912-
);
2915+
if lt.kind == MissingLifetimeKind::Ampersand {
2916+
err.multipart_suggestion_verbose(
2917+
format!("{pre} to return an owned value"),
2918+
sugg,
2919+
Applicability::MaybeIncorrect,
2920+
);
2921+
}
29132922
}
29142923
}
29152924

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

+8
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

+8
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)