Skip to content

Lifetime suggestion hint interacts weirdly with closures #17811

Closed
@ftxqxd

Description

@ftxqxd
fn main() {
    let closure = |x: &uint| -> &uint { x };
}

Produces:

<anon>:2:41: 2:42 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
<anon>:2     let closure = |x: &uint| -> &uint { x };
                                                 ^
<anon>:1:1: 3:2 note: consider using an explicit lifetime parameter as shown: fn main<'a>()
<anon>:1 fn main() {
<anon>:2     let closure = |x: &uint| -> &uint { x };
<anon>:3 }
error: aborting due to previous error

This is suggesting to put a lifetime on the main function, which certainly is not going to solve the problem here. The real solution is to give closure an explicit type declaration: let closure: <'a>|&'a uint| -> &'a uint = ...;.

Incidentally, is lifetime elision supposed to work on closures, too? Evidently it does not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler 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