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

Conversation

Stoozy
Copy link
Contributor

@Stoozy Stoozy commented Sep 26, 2022

@cjgillot This is what I have so far for #100615

@rust-highfive
Copy link
Collaborator

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.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 26, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 26, 2022
@estebank estebank closed this Sep 26, 2022
@estebank estebank reopened this Sep 26, 2022
@estebank
Copy link
Contributor

Can you see if you can call this function from here

fn suggest_introducing_lifetime(

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.

).emit();
return;

match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.to_def_id().as_local().unwrap()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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) => {
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.

@cjgillot
Copy link
Contributor

@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.

@cjgillot cjgillot self-assigned this Sep 26, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk removed their assignment Sep 27, 2022
@cjgillot
Copy link
Contributor

@Stoozy you can run ./x.py fmt to format the code according to style guide.
Once you have something you are happy with, you can run ./x.py test --bless src/test/ui/<path to test file.rs> to record the change to error output, and commit the test file.

diag.multipart_suggestion("consider introducing a named lifetime parameter",
vec![
(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned()),
(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.


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.


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?


match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) {
Some(generics) => {
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?


}
], 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?

compiler/rustc_resolve/src/late/lifetimes.rs Show resolved Hide resolved
Comment on lines +44 to +47
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(x: impl Iterator<Item = &'_'a ()>) -> Option<&'_ ()> { x.next() }
| ++++ ++
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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?

Copy link
Contributor

@cjgillot cjgillot left a 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; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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(


Copy link
Contributor

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()))
}
Copy link
Contributor

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;

Copy link
Contributor

Choose a reason for hiding this comment

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

Stray newline.

Comment on lines +44 to +47
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(x: impl Iterator<Item = &'_'a ()>) -> Option<&'_ ()> { x.next() }
| ++++ ++
Copy link
Contributor

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?

@Stoozy
Copy link
Contributor Author

Stoozy commented Oct 1, 2022

I don't mind fixing the '_'a issue, I just need the steps.

@cjgillot
Copy link
Contributor

cjgillot commented Oct 9, 2022

@Stoozy let's do that in a follow-up PR.
@bors r+ rollup

@bors
Copy link
Contributor

bors commented Oct 9, 2022

📌 Commit 2657f9d has been approved by cjgillot

It is now in the queue for this repository.

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 9, 2022
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Oct 9, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 10, 2022
Trying to suggest additional lifetime parameter

`@cjgillot` This is what I have so far for rust-lang#100615
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 10, 2022
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
@bors bors merged commit 0db05f1 into rust-lang:master Oct 10, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants