Skip to content

More explanatory error when calling a closure via & #15506

Closed
@brson

Description

@brson

When calling a closure behind a shared reference the error reported is simply "closure invocation in a & reference". When I first encountered this I had no idea what the problem was or how to fix it. Simply mentioning that closures behind a reference can only be called via &mut would help.

Test case:

fn main() {
    let foo: &|| = &|| ();
    (*foo)();
}

Error:

test.rs:3:5: 3:11 error: closure invocation in a `&` reference
test.rs:3     (*foo)();
              ^~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions