Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kmcallister opened this issue Sep 11, 2016 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kmcallister
Copy link
Contributor

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);
  |            ^^
@kmcallister kmcallister added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 11, 2016
@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

This is fixed today. I'm going to mark as E-needstest since it feels like the kind of thing that's easy to regress on -- a UI test would be best here.

error[E0596]: cannot borrow immutable `Box` content `*x` as mutable
 --> test.rs:5:12
  |
4 |     let x = Box::new(3);
  |         - consider changing this to `mut x`
5 |     f(&mut *x);
  |            ^^ cannot borrow as mutable

error: aborting due to previous error(s)

@Mark-Simulacrum Mark-Simulacrum added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jun 4, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
MaloJaffre added a commit to MaloJaffre/rust that referenced this issue Oct 4, 2017
Also ignore `attr-on-trait` test on stage-1 to keep `./x.py test --stage 1` successful.

Fixes rust-lang#30355.
Fixes rust-lang#33241.
Fixes rust-lang#36400.
Fixes rust-lang#37887.
Fixes rust-lang#44578.
bors added a commit that referenced this issue Oct 8, 2017
Fix some E-needstest issues.

Also ignore `attr-on-trait` test on stage-1 to keep `./x.py test --stage 1` successful.

Fixes #30355.
Fixes #33241.
Fixes #36400.
Fixes #37887.
Fixes #44578.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants