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

Fix suggestions for missing return type lifetime specifiers #85050

Merged
merged 3 commits into from
May 10, 2021

Conversation

FabianWolff
Copy link
Contributor

This pull request aims to fix #84592. The issue is that the current code seems to assume that there is only a single relevant span pointing to the missing lifetime, and only looks at the first one:

let span = lifetime_refs[0].span;

This is incorrect, though, and leads to incorrect error messages and invalid suggestions. For instance, the example from #84592:

struct TwoLifetimes<'x, 'y> {
    x: &'x (),
    y: &'y (),
}

fn two_lifetimes_needed(a: &(), b: &()) -> TwoLifetimes<'_, '_> {
    TwoLifetimes { x: &(), y: &() }
}

currently leads to:

error[E0106]: missing lifetime specifiers
 --> src/main.rs:6:57
  |
6 | fn two_lifetimes_needed(a: &(), b: &()) -> TwoLifetimes<'_, '_> {
  |                            ---     ---                  ^^ expected 2 lifetime parameters
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `a` or `b`
help: consider introducing a named lifetime parameter
  |
6 | fn two_lifetimes_needed<'a>(a: &'a (), b: &'a ()) -> TwoLifetimes<'_<'a, 'a>, '_> {
  |                        ^^^^    ^^^^^^     ^^^^^^                  ^^^^^^^^^^

There are two problems:

  • The error message is wrong. There is only one lifetime parameter expected at the location pointed to by the error message (and another one at a separate location).
  • The suggestion is incorrect and will not lead to correct code.

With the changes in this PR, I get the following output:

error[E0106]: missing lifetime specifiers
 --> p.rs:6:57
  |
6 | fn two_lifetimes_needed(a: &(), b: &()) -> TwoLifetimes<'_, '_> {
  |                            ---     ---                  ^^  ^^ expected named lifetime parameter
  |                                                         |
  |                                                         expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `a` or `b`
help: consider introducing a named lifetime parameter
  |
6 | fn two_lifetimes_needed<'a>(a: &'a (), b: &'a ()) -> TwoLifetimes<'a, 'a> {
  |                        ^^^^    ^^^^^^     ^^^^^^                  ^^  ^^

error: aborting due to previous error

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

Mainly, I changed add_missing_lifetime_specifiers_label() to receive a vector of spans (and counts) instead of just one, and adjusted its body accordingly.

@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 @petrochenkov (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

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 May 7, 2021
@petrochenkov
Copy link
Contributor

r? @jackh726

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

Initial comments

compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late/lifetimes.rs Show resolved Hide resolved
@FabianWolff
Copy link
Contributor Author

Thanks for your suggestions, @jackh726! I have just pushed a commit addressing your comments (those that I did not reply to above).

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

I think last comments :)

compiler/rustc_resolve/src/late/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_errors/src/diagnostic.rs Show resolved Hide resolved
src/test/ui/return-elided-lifetime.rs Outdated Show resolved Hide resolved
src/test/ui/return-elided-lifetime.stderr Outdated Show resolved Hide resolved
@FabianWolff
Copy link
Contributor Author

@jackh726 thanks again! I've implemented your suggested changes.

@jackh726
Copy link
Member

Thanks! @bors r+

@FabianWolff in the future, it's best not to tag someone in the commit message itself, because they get pinged as the commit goes through CI and such :)

@bors
Copy link
Contributor

bors commented May 10, 2021

📌 Commit 2448c76 has been approved by jackh726

@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 May 10, 2021
@FabianWolff
Copy link
Contributor Author

@FabianWolff in the future, it's best not to tag someone in the commit message itself, because they get pinged as the commit goes through CI and such :)

Ah, I see. Sorry about that!

@jackh726
Copy link
Member

Ah, I see. Sorry about that!

No worries, just wanted to let you know :)

bors added a commit to rust-lang-ci/rust that referenced this pull request May 10, 2021
Rollup of 6 pull requests

Successful merges:

 - rust-lang#85050 (Fix suggestions for missing return type lifetime specifiers)
 - rust-lang#85075 (Improve "panic message is not a string literal" warning)
 - rust-lang#85096 (Make unchecked_{add,sub,mul} inherent methods unstably const)
 - rust-lang#85112 (ensure failing promoteds in const/static bodies are handled correctly)
 - rust-lang#85146 (Provide io::Seek::rewind)
 - rust-lang#85147 (:arrow_up: rust-analyzer)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 0740015 into rust-lang:master May 10, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 10, 2021
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Malformed suggestion for filling in multiple placeholder return lifetimes
6 participants