Skip to content

Opaque error message when typechecking closure #26046

Closed
@rozbb

Description

@rozbb

UPDATE: Mentoring instructions found in this comment below.


The following code fails because the closure moves out of vec, and anything implementing Fn only gets a reference to its environment (i.e can't move self)

fn get_closure() -> Box<Fn() -> Vec<u8>> {
    let vec = vec![1u8, 2u8];

    let closure = move || {
        vec
    };

    Box::new(closure)
}

A simple fix that would allow the closure to properly implement Fn is to replace vec with vec.clone() inside the closure. The error message, however, is of no help:

error: the trait `core::ops::Fn<()>` is not implemented for the type `[closure <anon>:6:24: 8:6]` [E0277]
<line #> Box::new(closure)
         ^~~~~~~~~~~~~~~~~

In cases where the closure is more complex than this trivial example, the error message is no more descriptive than above, making it much more difficult to debug.

I would like to see something more along the lines of:

error: the trait `core::ops::Fn<()>` is not implemented for the type `[closure <anon>:6:24: 8:6]`
<line #> note: closure moves out of environment: vec
                                                 ^~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-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