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

Using &T in where clause should suggest HRTB #105422

Open
Swatinem opened this issue Dec 7, 2022 · 0 comments
Open

Using &T in where clause should suggest HRTB #105422

Swatinem opened this issue Dec 7, 2022 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. 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

@Swatinem
Copy link
Contributor

Swatinem commented Dec 7, 2022

Given the following code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0649baa344cf71ca09719ebebe1bf5d2

pub struct Foo;
pub struct Bar;

impl From<&Foo> for Bar {
    fn from(_: &Foo) -> Bar { Bar }
}

pub fn fails<E>(e: E) -> Bar where &E: Into<Bar> {
    (&e).into()
}

The current output is:

error[[E0637]](https://doc.rust-lang.org/stable/error-index.html#E0637): `&` without an explicit lifetime name cannot be used here
 --> src/lib.rs:8:36
  |
8 | pub fn fails<E>(e: E) -> Bar where &E: Into<Bar> {
  |                                    ^ explicit lifetime name needed here

error[[E0310]](https://doc.rust-lang.org/stable/error-index.html#E0310): the parameter type `E` may not live long enough
 --> src/lib.rs:8:40
  |
8 | pub fn fails<E>(e: E) -> Bar where &E: Into<Bar> {
  |                                        ^^^^^^^^^ ...so that the reference type `&'static E` does not outlive the data it points at
  |
help: consider adding an explicit lifetime bound...
  |
8 | pub fn fails<E: 'static>(e: E) -> Bar where &E: Into<Bar> {
  |               +++++++++

Ideally the output should suggest adding a HRTB like this:

pub fn works<E>(e: E) -> Bar where for<'e> &'e E: Into<Bar> {
    (&e).into()
}

TBH, HRTB are a very obscure feature to me, and it is not obvious in which circumstances I need to use them. This is one of those, and the compiler should suggest this to me.

I think there are quite some usecases when you have a Into / From that works with a reference, and you want to write code that is generic over that Into / From type.

@Swatinem Swatinem added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 7, 2022
@estebank estebank added A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Dec 7, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 7, 2023
…=davidtwco

Point out span where we could introduce higher-ranked lifetime

Somewhat addresses rust-lang#105422, but not really. We don't have that much useful information here since we're still in resolution :^(

Maybe this suggestion isn't worth it. If the reviewer has an idea how we can get a more succinct binder information for a structured suggestion, it would be appreciated.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 7, 2023
…=davidtwco

Point out span where we could introduce higher-ranked lifetime

Somewhat addresses rust-lang#105422, but not really. We don't have that much useful information here since we're still in resolution :^(

Maybe this suggestion isn't worth it. If the reviewer has an idea how we can get a more succinct binder information for a structured suggestion, it would be appreciated.
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: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. 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