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 false positive unused_lifetime when referenced in generic bounds. #2855

Merged
merged 2 commits into from
Jun 19, 2018

Conversation

oxalica
Copy link
Contributor

@oxalica oxalica commented Jun 19, 2018

It is commonly met when using lifetimes in Fn traits.

Here is an example causing false positive unused_lifetime.

fn func<'a, F: FnOnce(&'a ())>(x: &'a (), f: F) {
    f(x);
}

@oli-obk
Copy link
Contributor

oli-obk commented Jun 19, 2018

Removing all mentions of lifetimes makes this code still compile, so the lifetime is unnecessary. Do you have an example where removing the named lifetimes results in a compilation failure?

@oxalica
Copy link
Contributor Author

oxalica commented Jun 19, 2018

Removing lifetimes makes F to be for<'r> FnOnce(&'r ()), it's a much stronger bound.

And the following codes will not work. Link

// fn func<'a, F: FnOnce(&'a ())>(x: &'a (), f: F) { // This works
fn func<F: FnOnce(&())>(x: &(), f: F) {
    f(x);
}

fn main() {
    let x = ();
    let p = &x;
    let mut q = &x;
    func(p, |y| q = y); // borrowed data cannot be stored outside of its closure
}

@oli-obk
Copy link
Contributor

oli-obk commented Jun 19, 2018

Ah that makes sense. Can you add the full test so we don't regress it in the future?

@oxalica oxalica force-pushed the impl-fn-lifetime branch from ab09710 to 5b57b5f Compare June 19, 2018 15:22
@oli-obk oli-obk merged commit c573186 into rust-lang:master Jun 19, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Jun 19, 2018

Thanks!

@oxalica oxalica deleted the impl-fn-lifetime branch June 19, 2018 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants