Skip to content

Error for mut borrow of a immut Box should point to where the Box was bound #36400

Closed
@kmcallister

Description

@kmcallister

This code:

fn f(x: &mut u32) { }

fn main() {
    let x = 3;
    f(&mut x);
}

produces a helpful error message:

error: cannot borrow immutable local variable `x` as mutable
 --> <anon>:5:12
  |
4 |     let x = 3;
  |         - use `mut x` here to make mutable
5 |     f(&mut x);
  |            ^ cannot borrow mutably

But the analogous code using a Box:

fn f(x: &mut u32) { }

fn main() {
    let x = Box::new(3);
    f(&mut *x);
}

gives an error which doesn't point at the binding x:

error: cannot borrow immutable `Box` content `*x` as mutable
 --> <anon>:5:12
  |
5 |     f(&mut *x);
  |            ^^

Metadata

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.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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