Skip to content

Not sufficiently useful error message when FnMut is needed instead of Fn #31701

Closed
@eugene-bulkin

Description

@eugene-bulkin

This code:

fn counter() -> Box<Fn() -> usize> {
    let mut x = 0;
    Box::new(move || {
        x += 1;
        x
    })
}

results in the following error messages in stable:

<anon>:4:9: 4:15 error: cannot assign to captured outer variable in an `Fn` closure
<anon>:4         x += 1;
                 ^~~~~~
<anon>:3:14: 6:6 help: consider changing this closure to take self by mutable reference
<anon>:3     Box::new(move || {
<anon>:4         x += 1;
<anon>:5         x
<anon>:6     })

The problem here is that we're using Fn() instead of FnMut(), but the phrase "consider changing this closure to take self by mutable reference" is not particularly useful in telling us that that specifically is the problem. That is, the fact that we cannot assign to the captured outer variable is because we are trying to return Box<Fn() -> usize> instead of Box<FnMut() -> usize>, but that's not immediately clear from the original error message.

I'm not entirely sure how this would be rectified, but at the very least mentioning FnMut somewhere would be useful. @eddyb mentioned that this issue was "because a bound/trait object of Fn() was used but only FnMut() allows mutable borrows", so I think some variation on that would be reasonably sufficient to clarify the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.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