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

Trying to suggest additional lifetime parameter #102323

Merged
merged 6 commits into from
Oct 10, 2022
Merged
Changes from 3 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
45 changes: 38 additions & 7 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,13 +1318,44 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id)
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
{
rustc_session::parse::feature_err(
&self.tcx.sess.parse_sess,
sym::anonymous_lifetime_in_impl_trait,
lifetime_ref.span,
"anonymous lifetimes in `impl Trait` are unstable",
).emit();
Copy link
Contributor

Choose a reason for hiding this comment

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

You can keep this a feature error. You can keep this line, but instead of calling emit immediately, store it in a mut variable diag, decorate it using span_... methods, and call emit when you're done.

return;

match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) {
Some(generics) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Now you have generics you want 2 things to know where to suggest the lifetime parameter param_span, and in what form param_sugg.
Once you have that, you call

diag.multipart_suggestion(
  "consider introducing a named lifetime parameter",
  vec![
    (lifetime_ref.span, "'a".to_owned()), // this is at the use site
    (param_span, param_sugg), // this is in generics
  ],
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it seems if I put "'a" for the message, it replaces the ampersand symbol from the original posters input. Should I keep this?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use lifetime_ref.span.shrink_to_hi() instead then.

for i in 0..generics.params.len() {
Copy link
Contributor

Choose a reason for hiding this comment

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

You only use a single parameter. Could you separate the choice of param from the suggestion / move the suggestion code out of the loop?


if !generics.span.contains(generics.params[i].span) {

let mut diag = rustc_session::parse::feature_err(
Copy link
Contributor

Choose a reason for hiding this comment

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

This code is common to both branches. Could you move it above the match?

&self.tcx.sess.parse_sess,
sym::anonymous_lifetime_in_impl_trait,
lifetime_ref.span,
"anonymous lifetimes in `impl Trait` are unstable",
);

diag.span_label(lifetime_ref.span, "expected named lifetime parameter");

diag.multipart_suggestion("consider introducing a named lifetime parameter",
vec![
(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned()),
Copy link
Contributor

Choose a reason for hiding this comment

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

This suggestion is only correct if you have a &, but it's a good start 😄. You can also have an elided lifetime with the '_ syntax, or in a path Ref<u8> instead of Ref<'_, u8> for instance.

(generics.span, "<'a>".to_owned())
Copy link
Contributor

Choose a reason for hiding this comment

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

<'a> is only correct if there are no explicit generics. When there are explicit generics, you need to insert a 'a, just before the first explicit generic parameter.

], rustc_errors::Applicability::MaybeIncorrect);
diag.emit();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is also common to both branches. Could you move this below the match?


return;
}
}
cjgillot marked this conversation as resolved.
Show resolved Hide resolved
},
None => {
rustc_session::parse::feature_err(
&self.tcx.sess.parse_sess,
sym::anonymous_lifetime_in_impl_trait,
lifetime_ref.span,
"anonymous lifetimes in `impl Trait` are unstable",
).emit();
return;
}
}

}
scope = s;
}
Expand Down