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 adding a new lifetime parameter when two elided lifetimes should match up for traits and impls. #94464

Merged

Conversation

kckeiks
Copy link
Contributor

@kckeiks kckeiks commented Feb 28, 2022

Suggest adding a new lifetime parameter when two elided lifetimes should match up for functions in traits and impls.

Issue #94462

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 28, 2022
@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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 28, 2022
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

I'm +1 on the code changes, but would you mind taking a look at my wording nitpick?

..
})
| hir::Node::TraitItem(&hir::TraitItem { ref generics, .. })
| hir::Node::ImplItem(&hir::ImplItem { ref generics, .. }) => generics,
Copy link
Contributor

Choose a reason for hiding this comment

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

We should handle the impl case specifically with extra wording in the suggestion to let people know that they'll have to modify the trait as well. This is likely not a problem in practice, but I'd like us to be thorough and avoid confusing people.

It might be enough to just have a boolean flag that you use to modify the suggestion description with extra wording.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. I'll make that change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about this for impl?

help: consider introducing a named lifetime parameter and update trait if needed
   |
32 |     fn foo<'a>(&'a self, x: &i32, z: &'a i32) -> Option<&i32> {
   |           ++++  ++                    ++

error: aborting due to previous error

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we would have something like "consider introducing a named lifetime parameter and update trait TraitName as necessary", by getting the TraitName from the parent DefId, but that can be left as follow up work.

Comment on lines 10 to 13
help: consider introducing a named lifetime parameter
|
LL | fn a<'a>(self: Pin<&'a Foo>, f: &'a Foo) -> &Foo { f }
| ++++ ++ ++
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm somewhat surprised by the suggestions I'm seeing that we don't also suggest adding 'a to the return type. Maybe an existing bug? No need to handle it in this case directly, but would we should make a note of it.

Copy link
Contributor Author

@kckeiks kckeiks Mar 1, 2022

Choose a reason for hiding this comment

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

Do you want me to add a comment about that note?

I'm not 100% sure it's a bug. Seems like the code that added the hint did not take into consideration return types. To be fair though, AFAICT the diagnosis in this module is implemented very differently than what is in LifetimeContext in add_missing_lifetime_specifiers_label (which does add lifetime hints for the return type). LifetimeContext has a lot more "information", which it gathers during compilation, about the error so it can construct a better hint.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just create a targeted ticket for this instead of leaving it in the code. It'd be p-low, as we already have follow up suggestions to get you to working code, although it isn't great when there's another lifetime involved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@estebank I created #94492 and made the changes you suggested.

@estebank
Copy link
Contributor

estebank commented Mar 1, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Mar 1, 2022

📌 Commit 1b08cba has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 1, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 2, 2022
…askrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#94464 (Suggest adding a new lifetime parameter when two elided lifetimes should match up for traits and impls.)
 - rust-lang#94476 (7 - Make more use of `let_chains`)
 - rust-lang#94478 (Fix panic when handling intra doc links generated from macro)
 - rust-lang#94482 (compiler: fix some typos)
 - rust-lang#94490 (Update books)
 - rust-lang#94496 (tests: accept llvm intrinsic in align-checking test)
 - rust-lang#94498 (9 - Make more use of `let_chains`)
 - rust-lang#94503 (Provide C FFI types via core::ffi, not just in std)
 - rust-lang#94513 (update Miri)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2b72ecb into rust-lang:master Mar 2, 2022
@rustbot rustbot added this to the 1.61.0 milestone Mar 2, 2022
@kckeiks
Copy link
Contributor Author

kckeiks commented Mar 2, 2022

Closes #94462

@oli-obk oli-obk assigned estebank and unassigned oli-obk Mar 2, 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.

6 participants