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

Emit suggestion for where &T: Trait #82312

Closed
estebank opened this issue Feb 20, 2021 · 1 comment
Closed

Emit suggestion for where &T: Trait #82312

estebank opened this issue Feb 20, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Given:

fn foo<T>(x: T) where &T: std::io::Read {
    panic!();
}

we currently emit

error[E0637]: `&` without an explicit lifetime name cannot be used here
 --> src/main.rs:1:23
  |
1 | fn foo<T>(x: T) where &T: std::io::Read {
  |                       ^ explicit lifetime name needed here

error[E0310]: the parameter type `T` may not live long enough
 --> src/main.rs:1:27
  |
1 | fn foo<T>(x: T) where &T: std::io::Read {
  |        -                  ^^^^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
  |        |
  |        help: consider adding an explicit lifetime bound...: `T: 'static`

'static suggestions are a lot of the time incorrect, but in this case what we can suggest is

fn foo<T>(x: T) where for<'a> &'a T: std::io::Read {

and both errors go away. Ideally we could silence the second error and have a structured suggestion for the first.

Taken from https://github.com/rust-lang/rust/pull/82194/files#r578364173

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Feb 20, 2021
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 20, 2021
@estebank
Copy link
Contributor Author

error[[E0637]](https://doc.rust-lang.org/nightly/error_codes/E0637.html): `&` without an explicit lifetime name cannot be used here
 --> src/main.rs:1:23
  |
1 | fn foo<T>(x: T) where &T: std::io::Read {
  |                       ^ explicit lifetime name needed here
  |
help: consider introducing a higher-ranked lifetime here
  |
1 | fn foo<T>(x: T) where for<'a> &'a T: std::io::Read {
  |                       +++++++  ++

error[[E0311]](https://doc.rust-lang.org/nightly/error_codes/E0311.html): the parameter type `T` may not live long enough
 --> src/main.rs:1:27
  |
1 | fn foo<T>(x: T) where &T: std::io::Read {
  |                           ^^^^^^^^^^^^^ ...so that the reference type `&T` does not outlive the data it points at

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 A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants