Skip to content

Lifetime trouble with hidden Self when going to edition 2018 #56719

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

Closed
vorner opened this issue Dec 11, 2018 · 2 comments
Closed

Lifetime trouble with hidden Self when going to edition 2018 #56719

vorner opened this issue Dec 11, 2018 · 2 comments
Labels
A-edition-2018 Area: The 2018 edition A-lifetimes Area: Lifetimes / regions F-rust_2018_preview `#![feature(rust_2018_preview)]`

Comments

@vorner
Copy link
Contributor

vorner commented Dec 11, 2018

OK, so I'm not sure this is a bug or annoyance, but it still acts a bit weird. And I don't know what to search for if it was already reported.

Anyway, let's have this code that compiles under edition 2015:

struct Parsed<'a> {
    parts: Vec<&'a str>,
}

impl<'a> Parsed<'a> {
    fn parse(s: &'a str) -> Self {
        let parts = s.split_whitespace().collect();
        Parsed {
            parts
        }
    }

    fn len(&self) -> usize {
        self.parts.len()
    }

    fn parsed_len(s: &str) -> usize {
        Self::parse(s).len()
    }
}

fn process<F: Fn(&str) -> usize>(f: F) {
    unimplemented!()
}

fn main() {
    process(Parsed::parsed_len)
}

When I switch to 2018, it starts complaining that the s parameter of parsed_len needs the 'a lifetime. Well, I guess that's because of Self in there (if I use Parsed instead, it is OK with it).

But if I obey, the process no longer wants to accept the associated function, it says: expected bound lifetime parameter, found concrete lifetime. Putting it into a closure (process(|s| Parsed::parsed_len(s))) fixes that. Which is strange, it's still the same function, isn't it?

Should it be doing this? I'd hope the process would still accept the associated function, even though the lifetime is on the struct, not only on the parameter.

Also, cargo fix doesn't help with this transition (though I don't know what it should do about it).

@estebank estebank added A-lifetimes Area: Lifetimes / regions F-rust_2018_preview `#![feature(rust_2018_preview)]` labels Dec 11, 2018
@jackh726
Copy link
Member

jackh726 commented Aug 8, 2019

@estebank this no longer errors on current stable/nightly.

It does give the following warning though, which is the same seen in #62953:

warning[E0621]: explicit lifetime required in the type of `s`
  --> src/lib.rs:16:9
   |
15 |     fn parsed_len(s: &str) -> usize {
   |                      ---- help: add explicit lifetime `'a` to the type of `s`: `&'a str`
16 |         Self::parse(s).len()
   |         ^^^^^^^^^^^^^^ lifetime `'a` required
   |
   = warning: this error has been downgraded to a warning for backwards compatibility with previous releases
   = warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future
   = note: for more information, try `rustc --explain E0729`

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=54dc9c4f9d81a0fa35250e09cebde1a8

@estebank
Copy link
Contributor

estebank commented Aug 8, 2019

Closing as duplicate of #62953.

@estebank estebank closed this as completed Aug 8, 2019
@fmease fmease added the A-edition-2018 Area: The 2018 edition label Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2018 Area: The 2018 edition A-lifetimes Area: Lifetimes / regions F-rust_2018_preview `#![feature(rust_2018_preview)]`
Projects
None yet
Development

No branches or pull requests

4 participants