-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @oli-obk (or someone else) soon. Please see the contribution instructions for more information. |
Can you see if you can call this function from here
|
sym::anonymous_lifetime_in_impl_trait, | ||
lifetime_ref.span, | ||
"anonymous lifetimes in `impl Trait` are unstable", | ||
).emit(); |
There was a problem hiding this comment.
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.
).emit(); | ||
return; | ||
|
||
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.to_def_id().as_local().unwrap()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.to_def_id().as_local().unwrap()) { | |
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) { |
return; | ||
|
||
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.to_def_id().as_local().unwrap()) { | ||
Some(generics) => { |
There was a problem hiding this comment.
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
],
)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@estebank That method is on the AST-based resolver, while the diagnostic is on the HIR-based resolver. There is no simple way to reuse that code. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@Stoozy you can run |
diag.multipart_suggestion("consider introducing a named lifetime parameter", | ||
vec![ | ||
(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned()), | ||
(generics.span, "<'a>".to_owned()) |
There was a problem hiding this comment.
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.
|
||
diag.multipart_suggestion("consider introducing a named lifetime parameter", | ||
vec![ | ||
(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned()), |
There was a problem hiding this comment.
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.
|
||
if !generics.span.contains(generics.params[i].span) { | ||
|
||
let mut diag = rustc_session::parse::feature_err( |
There was a problem hiding this comment.
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?
|
||
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) { | ||
Some(generics) => { | ||
for i in 0..generics.params.len() { |
There was a problem hiding this comment.
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?
|
||
} | ||
], rustc_errors::Applicability::MaybeIncorrect); | ||
diag.emit(); |
There was a problem hiding this comment.
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?
help: consider introducing a named lifetime parameter | ||
| | ||
LL | fn g<'a>(x: impl Iterator<Item = &'_'a ()>) -> Option<&'_ ()> { x.next() } | ||
| ++++ ++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the resulting code here, the .shrink_to_hi()
of the span without checking whether the span covers only &
or '_
causes invalid code to be produced. It also seems like we might also want to replace the anon lifetime in the return type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing the anon lifetime in the return type seems like an outsized change. I'd rather stabilize this feature sooner, to get rid of the question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there anything else I should do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question is: do you want to take a bit more time in this PR to fix the '_'a
situation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is starting to look good, thanks for your patience @Stoozy. Could you clean-up formatting a bit? Rustfmt
unfortunately skips let chains.
rustc_errors::Applicability::MaybeIncorrect); | ||
|
||
}, | ||
None => { continue; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None => { continue; } | |
None => {} |
We still want to emit an error if there are no generics.
@@ -1318,13 +1318,49 @@ 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( | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray newlines.
Some((self.tcx.sess.source_map().span_through_char(generics.span, '<').shrink_to_hi(), "'a, ".to_owned())) | ||
}, | ||
None => Some((generics.span, "<'a>".to_owned())) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop body is independent of the loop itself. Could you remove the loop and just keep the assignment?
return; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray newline.
help: consider introducing a named lifetime parameter | ||
| | ||
LL | fn g<'a>(x: impl Iterator<Item = &'_'a ()>) -> Option<&'_ ()> { x.next() } | ||
| ++++ ++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question is: do you want to take a bit more time in this PR to fix the '_'a
situation?
I don't mind fixing the |
Trying to suggest additional lifetime parameter `@cjgillot` This is what I have so far for rust-lang#100615
Rollup of 6 pull requests Successful merges: - rust-lang#102275 (Stabilize `half_open_range_patterns`) - rust-lang#102323 (Trying to suggest additional lifetime parameter) - rust-lang#102345 (Recover from impl Trait in type param bound) - rust-lang#102845 (Elaborate trait ref to compute object safety.) - rust-lang#102860 (Add missing documentation for FileNameDisplayPreference variants) - rust-lang#102862 (From<Alignment> for usize & NonZeroUsize) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
@cjgillot This is what I have so far for #100615