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

Don't suggest adding 'static lifetime to function arguments #69350

Closed
kornelski opened this issue Feb 21, 2020 · 5 comments · Fixed by #85240
Closed

Don't suggest adding 'static lifetime to function arguments #69350

kornelski opened this issue Feb 21, 2020 · 5 comments · Fixed by #85240
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kornelski
Copy link
Contributor

kornelski commented Feb 21, 2020

When using a temporary reference in a context where temporary references are not allowed, rustc gives a technically-correct suggestion that in practice is most often misleading and makes beginners stuck trying to solve a wrong problem:

fn naninani(arg: &str) {
    std::thread::spawn(|| {
        println!("{}", arg);
    });
}
error[E0621]: explicit lifetime required in the type of `arg`
 --> src/lib.rs:2:5
  |
1 | fn naninani(arg: &str) {
  |                  ---- help: add explicit lifetime `'static` to the type of `arg`: `&'static str`
2 |     std::thread::spawn(|| {
  |     ^^^^^^^^^^^^^^^^^^ lifetime `'static` required

This suggestion is very unlikely to be useful in a real program. If the function is changed to require arguments with a 'static lifetime, it generally just causes more borrow checking errors at the call sites of the function, causing inexperienced users dig deeper in the wrong direction.

My suggestions:

  • Don't make code change suggestions for 'static lifetime outside of globals. In this case no suggestion at all would be better than a misleading code change.

  • Because the correct solution is most likely not to use a reference at all, don't focus the message on the (unusual) kind of reference that would work, but explain that most (i.e. non-'static) references won't work.

I suggest changing "lifetime 'static required" say something along the lines of "temporary references are not allowed".

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 21, 2020
@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Feb 21, 2020
@timleg002
Copy link

How should I go to avoid this error?

@kornelski
Copy link
Contributor Author

kornelski commented Nov 22, 2020

This error in practice means references are forbidden, so don't use references. Usually you'll want to use Arc or some other self-contained type like String (not &str), or Vec (not &[]).

This issue is about development of the Rust compiler. Please ask Rust programming questions on https://users.rust-lang.org/

@SorteKanin
Copy link

Just ran into this incredibly useless compiler message and had to spend some time digging. I agree that this "help" message does more harm than good and it would probably be better to have no help than this suggestion.

@kornelski
Copy link
Contributor Author

kornelski commented Jan 14, 2021

Another case of a user stuck, because the compiler told them to have &'static self:

https://users.rust-lang.org/t/how-to-solve-this-lifetime-problem/54129

@timleg002
Copy link

This error in practice means references are forbidden, so don't use references. Usually you'll want to use Arc or some other self-contained type like String (not &str), or Vec (not &[]).

This issue is about development of the Rust compiler. Please ask Rust programming questions on https://users.rust-lang.org/

Thanks for the answer. This needs to be fixed asap

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue May 13, 2021
…dtwco

Don't suggest adding `'static` lifetime to arguments

Fixes rust-lang#69350

This is almost always the wrong this to do
@bors bors closed this as completed in bc6330d May 13, 2021
Aaron1011 added a commit to Aaron1011/rust that referenced this issue Oct 4, 2021
Fixes rust-lang#73159

This is similar to rust-lang#69350 - if the user didn't initially
write out a 'static lifetime, adding 'static in response to
a lifetime error is usually the wrong thing to do.
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 5, 2021
…tsakis

Don't suggest replacing region with 'static in NLL

Fixes rust-lang#73159

This is similar to rust-lang#69350 - if the user didn't initially
write out a 'static lifetime, adding 'static in response to
a lifetime error is usually the wrong thing to do.
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 5, 2021
…tsakis

Don't suggest replacing region with 'static in NLL

Fixes rust-lang#73159

This is similar to rust-lang#69350 - if the user didn't initially
write out a 'static lifetime, adding 'static in response to
a lifetime error is usually the wrong thing to do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants