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

Improve E0389 "cannot borrow data mutably in a & reference" error message #38147

Closed
mikhail-m1 opened this issue Dec 3, 2016 · 5 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@mikhail-m1
Copy link
Contributor

Error in next case could be more helpful

struct Foo<'a> {
    s: &'a mut String
}

impl<'a> Foo<'a> {
    fn f(&self) {
        self.s.push('x');
    }
}

fn main() {}

error:

error[E0389]: cannot borrow data mutably in a `&` reference
 --> 2.rs:7:9
  |
7 |         self.s.push('x');
  |         ^^^^^^ assignment into an immutable reference

cc @jonathandturner

@mikhail-m1 mikhail-m1 changed the title Improve "cannot borrow data mutably in a & reference" error message Improve E0389 "cannot borrow data mutably in a & reference" error message Dec 6, 2016
@sanxiyn sanxiyn added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 7, 2016
@GuillaumeGomez
Copy link
Member

How would you improve it? It seems pretty clear for me. :-/

@mikhail-m1
Copy link
Contributor Author

I think not will be helpful

 fn f(&self) {
      ^ use '&mut self' for make mutable

and for sample from tests

    let fancy_ref = &(&mut fancy);
                    ^use '&mut (&mut fancy)' for make mutable
    fancy_ref.num = 6; //~ ERROR E0389

I'm not sure it's easy to do, but I'll try.

@GuillaumeGomez
Copy link
Member

Oh, I actually proposed such a change here. Still some little things to debate.

@mikhail-m1
Copy link
Contributor Author

@GuillaumeGomez I don't understand how its related, are you already work on it?

@GuillaumeGomez
Copy link
Member

It's the first step. It suggests references and stuff. However, if the variable isn't mutable, it won't say "make your variable mutable and then use &mut". This step will need to be added.

bors added a commit that referenced this issue Jan 27, 2017
Point to immutable arg/fields when trying to use as &mut

Present the following output when trying to access an immutable borrow's
field as mutable:

```
error[E0389]: cannot borrow data mutably in a `&` reference
  --> $DIR/issue-38147-1.rs:27:9
   |
26 | fn f(&self) {
   |      -----  use `&mut self` here to make mutable
27 |     f.s.push('x');
   |     ^^^ assignment into an immutable reference
```

And the following when trying to access an immutable struct field as mutable:

```
error: cannot borrow immutable borrowed content `*self.s` as mutable
  --> $DIR/issue-38147-3.rs:17:9
   |
12 |     s: &'a String
   |     ------------- use `&'a mut String` here to make mutable
...|
16 |     fn f(&self) {
   |          -----  use `&mut self` here to make mutable
17 |         self.s.push('x');
   |         ^^^^^^ cannot borrow as mutable
```

Fixes #38147.
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
Projects
None yet
Development

No branches or pull requests

3 participants