Skip to content

Incorrect lifetime error with a generic FnMut after annotating the type in a closure. #22557

Closed
@theemathas

Description

@theemathas

The code:

struct Nothing;

fn foo<'a, F: FnMut(&'a mut Nothing) -> T, T>(_: F) {}

fn bar<'a, F: FnMut(&'a mut Nothing) -> &'a mut Nothing>(_: F) {}

fn main() {
    bar(|x| x); // OK
    bar(|x: &mut Nothing| x); // OK
    foo(|x| x); // OK
    foo(|x: &mut Nothing| x); // Error
}

Playpen

Compiling this on playpen results in this compile error:

<anon>:11:27: 11:28 error: cannot infer an appropriate lifetime due to conflicting requirements
<anon>:11     foo(|x: &mut Nothing| x); // Error
                                    ^
<anon>:11:27: 11:28 note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the block at 11:26...
<anon>:11     foo(|x: &mut Nothing| x); // Error
                                    ^
<anon>:11:27: 11:28 note: ...so that expression is assignable (expected `&mut Nothing`, found `&mut Nothing`)
<anon>:11     foo(|x: &mut Nothing| x); // Error
                                    ^
<anon>:11:5: 11:8 note: but, the lifetime must be valid for the expression at 11:4...
<anon>:11     foo(|x: &mut Nothing| x); // Error
              ^~~
<anon>:11:5: 11:8 note: ...so that the type `&mut Nothing` will meet its required lifetime bounds
<anon>:11     foo(|x: &mut Nothing| x); // Error
              ^~~
error: aborting due to previous error

I think that this code is supposed to compile, and the error message isn't very helpful either. What exactly is (expected &mut Nothing, found &mut Nothing)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-needs-decisionIssue: In need of a decision.P-lowLow priorityT-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions