Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest Explicit Lifetime for Associated Type Bindings #123245

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
#[instrument(level = "debug", skip(self))]
fn resolve_anonymous_lifetime(&mut self, lifetime: &Lifetime, elided: bool) {
debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);

let kind =
if elided { MissingLifetimeKind::Ampersand } else { MissingLifetimeKind::Underscore };
let missing_lifetime =
Expand Down Expand Up @@ -1685,14 +1684,53 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
..
} = &rib.kind
{
diag.multipart_suggestion(
"consider introducing a higher-ranked lifetime here",
vec![
(span.shrink_to_lo(), "for<'a> ".into()),
(lifetime.ident.span.shrink_to_hi(), "'a ".into()),
],
Applicability::MachineApplicable,
);
// hacky way to check if lifetime suggestion is for
// an associated type binding
let is_associated_type_binding = lifetime.ident.span
!= self.r.tcx.sess.source_map().span_extend_to_prev_char(
lifetime.ident.span,
'=',
false,
);
if is_associated_type_binding {
for upper_rib in self.lifetime_ribs[0..i].iter().rev() {
if let LifetimeRibKind::Generics {
kind: LifetimeBinderKind::Function,
span: function_span,
..
} = upper_rib.kind
{
let type_parameter_span = self
.r
.tcx
.sess
.source_map()
.span_through_char(function_span, '<')
.shrink_to_hi();
diag.multipart_suggestion(
"consider adding an explicit lifetime here",
vec![
(type_parameter_span, "'a, ".into()),
(
lifetime.ident.span.shrink_to_hi(),
"'a ".into(),
),
],
Applicability::MaybeIncorrect,
);
break;
}
}
} else {
diag.multipart_suggestion(
"consider introducing a higher-ranked lifetime here",
vec![
(span.shrink_to_lo(), "for<'a> ".into()),
(lifetime.ident.span.shrink_to_hi(), "'a ".into()),
],
Applicability::MachineApplicable,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is existing diagnostic code in rustc_resolve::late::diagnostics. We should adapt and reuse add_missing_lifetime_specifiers_label and suggest_introducing_lifetime.

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
struct MyType;

fn foo<F>()
where
F: Iterator<Item = &MyType>, //~ `&` without an explicit lifetime name cannot be used here
{
}

fn bar() {
fn baz<F>()
where
F: Iterator<Item = &MyType>, //~ `&` without an explicit lifetime name cannot be used here
{
}
}

fn function<T>()
where
T: Iterator<Item = &MyType>, //~ `&` without an explicit lifetime name cannot be used here
{
fn lambda<A>()
where
A: Iterator<Item = &u8>, //~ `&` without an explicit lifetime name cannot be used here
{
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
error[E0637]: `&` without an explicit lifetime name cannot be used here
--> $DIR/suggest-explicit-lifetime-for-associated-types-issue-122025.rs:5:24
|
LL | F: Iterator<Item = &MyType>,
| ^ explicit lifetime name needed here
|
help: consider adding an explicit lifetime here
|
LL ~ fn foo<'a, F>()
LL | where
LL ~ F: Iterator<Item = &'a MyType>,
|

error[E0637]: `&` without an explicit lifetime name cannot be used here
--> $DIR/suggest-explicit-lifetime-for-associated-types-issue-122025.rs:12:28
|
LL | F: Iterator<Item = &MyType>,
| ^ explicit lifetime name needed here
|
help: consider adding an explicit lifetime here
|
LL ~ fn baz<'a, F>()
LL | where
LL ~ F: Iterator<Item = &'a MyType>,
|

error[E0637]: `&` without an explicit lifetime name cannot be used here
--> $DIR/suggest-explicit-lifetime-for-associated-types-issue-122025.rs:19:24
|
LL | T: Iterator<Item = &MyType>,
| ^ explicit lifetime name needed here
|
help: consider adding an explicit lifetime here
|
LL ~ fn function<'a, T>()
LL | where
LL ~ T: Iterator<Item = &'a MyType>,
|

error[E0637]: `&` without an explicit lifetime name cannot be used here
--> $DIR/suggest-explicit-lifetime-for-associated-types-issue-122025.rs:23:28
|
LL | A: Iterator<Item = &u8>,
| ^ explicit lifetime name needed here
|
help: consider adding an explicit lifetime here
|
LL ~ fn lambda<'a, A>()
LL | where
LL ~ A: Iterator<Item = &'a u8>,
|

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0637`.
Loading