Skip to content

Weird lifetime error with closure and early-bound lifetime #28092

Description

@eefriedman
struct S;
impl S {
    fn f<'a:'a>(_x:&'a i32) {}
}
fn main() {
    let g1: &Fn(&i32) = &|x| S::f(x);
    g1(&1);
    let g2 = &|x| S::f(x);
    g2(&1);
}
<anon>:9:9: 9:10 error: borrowed value does not live long enough
<anon>:9     g2(&1);
                 ^
<anon>:8:27: 10:2 note: reference must be valid for the block suffix following statement 2 at 8:26...
<anon>: 8     let g2 = &|x| S::f(x);
<anon>: 9     g2(&1);
<anon>:10 }
<anon>:9:5: 9:12 note: ...but borrowed value is only valid for the statement at 9:4
<anon>:9     g2(&1);
             ^~~~~~~
<anon>:9:5: 9:12 help: consider using a `let` binding to increase its lifetime
<anon>:9     g2(&1);
             ^~~~~~~
error: aborting due to previous error

Lifetime inference is doing something weird with the second closure.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.fixed-by-NLLBugs fixed, but only when NLL is enabled.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions