Skip to content

Incorrect lifetime inferred for closure #26937

@diwic

Description

@diwic

Example:

struct Z<'a> {
    q: &'a (FnMut(&str) + 'a)
}

impl<'a> Z<'a> {
    fn new(q: &'a (FnMut(&str) + 'a)) -> Z<'a> { Z { q: q }}
}

pub fn main() {
    let c = |s| { println!("{}", s); };
    Z::new(&c);
}

Fails with:

<anon>:12:16: 12:18 error: type mismatch resolving `for<'r> <[closure <anon>:11:17: 11:43] as core::ops::FnOnce<(&'r str,)>>::Output == ()`:
 expected bound lifetime parameter, found concrete lifetime [E0271]
<anon>:12         Z::new(&c);
                         ^~
<anon>:12:16: 12:18 help: see the detailed explanation for E0271
<anon>:12:16: 12:18 note: required for the cast to the object type `for<'r> core::ops::FnMut(&'r str)`
<anon>:12         Z::new(&c);
                         ^~
<anon>:12:16: 12:18 error: type mismatch: the type `[closure <anon>:11:17: 11:43]` implements the trait `core::ops::FnMut<(_,)>`, but the trait `for<'r> core::ops::FnMut<(&'r str,)>` is required (expected concrete lifetime, found bound lifetime parameter ) [E0281]
<anon>:12         Z::new(&c);
                         ^~
<anon>:12:16: 12:18 note: required for the cast to the object type `for<'r> core::ops::FnMut(&'r str)`
<anon>:12         Z::new(&c);
                         ^~

The problem can be worked around by writing |s: &str| instead of just |s|, but according to @eddyb this behaviour should be improved to infer the correct lifetime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-closuresArea: Closures (`|…| { … }`)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions