Skip to content

Improve error message "cannot borrow immutable Box content **f as mutable" #51157

Closed

Description

fn test(f: Box<FnMut()>) {
    let _f = move || {
        f()
    };
}

error[E0596]: cannot borrow immutable `Box` content `**f` as mutable
 --> src/main.rs:3:9
  |
3 |         f()
  |         ^ cannot borrow as mutable

This error message seems a bit weird, because the problem isn't that box content isn't mutable, but the box itself is immutable. Changing the line 2 to let mut f: … fixes the issue. Notice that the same error message is shown if we construct f within the function.

I think the error message should be improved to look similar to the following (not sure on the phrasing of the error itself):

error[E0596]: cannot borrow immutable argument `Box` as mutable
 --> src/main.rs:2:5
  |
1 | fn test(f: Box<FnMut()>) {
  |         - consider changing this to `mut f`
2 |     let _f = move || {
3 |         f()
  |         ^ cannot borrow mutably
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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