Uh oh!
There was an error while loading. Please reload this page.
Point to immutable arg/fields when trying to use as &mut - #39139
Conversation
rust-highfive
commented
Jan 17, 2017
r? @nrc (rust_highfive has picked a reviewer for you, use r? to override) |
estebank
commented
Jan 17, 2017
There was a problem hiding this comment.
Leftover from previous exploration.
GuillaumeGomez
commented
Jan 17, 2017
This is great! 👍 |
nrc
commented
Jan 18, 2017
r? @nikomatsakis (I'm not familiar enough with these bits of the compiler any more). I would prefer less assertive language in the error messages - rather than "X should be Y", I prefer, "if you want X, then you can change X to Y" or something similar. Also, could these be |
estebank
commented
Jan 18, 2017
Introduced an error in two tests (removed two hints). Will try to fix it tonight. |
estebank
commented
Jan 18, 2017
@nrc, I'm ok with doing so. For now, I copied the message already being used for the same kind of suggestion:
The only things that makes me uneasy about using suggestions are that the actual output turns out to be slightly longer, and because of the out of order and lack of line number, less comprehensible: I just filed #39152 so it can be improved going forward, but as it is now I really prefer the |
sophiajt
commented
Jan 19, 2017
I'm definitely liking this revision. One question, is this part missing something? Seems like it needs a label there |
estebank
commented
Jan 19, 2017
@jonathandturner the current output doesn't set a label. To add one, I could definitely use some help with the wording. Something along the lines of: or seems reasonable. |
sophiajt
commented
Jan 19, 2017
0df4676 to
a9a0f48Compareestebank
commented
Jan 19, 2017
@jonathandturner done. |
nikomatsakis
commented
Jan 19, 2017
👍 to shorter wording on the "primary label". In fact, though I think this is a separate thing, I have long wanted to aggressively shorten the borrowck messages, e.g. removing the phrase "borrowed as mutable" and just say "write" or "mutate", and definitely not talking about distracting but complex things like "immutable borrowed content". Also 👍 to this general pull request. =) |
estebank
commented
Jan 20, 2017
@nikomatsakis is there anything else that should be part of this PR? |
nikomatsakis
left a comment
There was a problem hiding this comment.
Looks like it's close but not quite there. We should make some tests targeting this specific case (the existing tests seem to have caught this overenthusiastic error message "by accident", no?)
There was a problem hiding this comment.
So...this fn is pretty hard to read. =) Maybe we can use some temporaries or something? I just find it hard to follow, but maybe it's ok, if there was at least a comment. Basically when I read it I just see "walk a bunch of data structures to pull out data", but my eyes slide over the details...
There was a problem hiding this comment.
This is moved code from bellow so that I could use it twice without copying code around :)
I will see how I can make it easier to follow.
There was a problem hiding this comment.
maybe we can adjust the wording to make it clearer that BOTH parts need to be &mut self? That's a bit tricky, particularly since we don't control the relative order of things here
There was a problem hiding this comment.
I could look at the span's line number to identify order and change this to something like
error: cannot borrow immutable borrowed content `*self.s` as mutable
--> $DIR/issue-38147-3.rs:17:9
|
12 | s: &'a String
| ------------- this field should be `&mut`...
...
16 | fn f(&self) {
| ----- ...and use `&mut self` here to make mutable
17 | self.s.push('x');
| ^^^^^^ cannot borrow as mutable
But I don't think it's unreasonable to show both labels, and if the developer only changes one of them, to still get the same error with the remaining label.
There was a problem hiding this comment.
Hmm, this seems wrong, no?
There was a problem hiding this comment.
Certainly there is nothing that would easily be converted to &mut here
There was a problem hiding this comment.
What should be the appropriate message for this case? Nothing if the type is not a ref?
There was a problem hiding this comment.
seems like we have to check here that the type of the field is some sort of reference, right? (hence the "over fire" case in the ui tests below)
estebank
left a comment
There was a problem hiding this comment.
We should make some tests targeting this specific case
issue-38147-1, issue-38147-2, issue-38147-3 & issue-38147-4 are new tests targeting this case.
There was a problem hiding this comment.
What should be the appropriate message for this case? Nothing if the type is not a ref?
There was a problem hiding this comment.
This is moved code from bellow so that I could use it twice without copying code around :)
I will see how I can make it easier to follow.
There was a problem hiding this comment.
I could look at the span's line number to identify order and change this to something like
error: cannot borrow immutable borrowed content `*self.s` as mutable
--> $DIR/issue-38147-3.rs:17:9
|
12 | s: &'a String
| ------------- this field should be `&mut`...
...
16 | fn f(&self) {
| ----- ...and use `&mut self` here to make mutable
17 | self.s.push('x');
| ^^^^^^ cannot borrow as mutable
But I don't think it's unreasonable to show both labels, and if the developer only changes one of them, to still get the same error with the remaining label.
nikomatsakis
commented
Jan 23, 2017
Yeah, no change is needed in this case. |
nikomatsakis
commented
Jan 25, 2017
Travis is failing because a UI test needs to be updated: |
nikomatsakis
commented
Jan 25, 2017
but the new output looks correct =) |
estebank
commented
Jan 25, 2017
@nikomatsakis tests are fixed. Can you check wether the method Also, please verify that using |
bors
commented
Jan 26, 2017
☔ The latest upstream changes (presumably #39309) made this pull request unmergeable. Please resolve the merge conflicts. |
Point to immutable borrow arguments and fields when trying to use them as
mutable borrows. Add label to primary span on "cannot borrow as mutable"
errors.
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
```nikomatsakis
commented
Jan 26, 2017
Yes; it's a wonder what some comments will do! :) Thanks.
Yes that is correct. |
nikomatsakis
commented
Jan 26, 2017
@bors r+ |
bors
commented
Jan 26, 2017
📌 Commit e1280d8 has been approved by |
bors
commented
Jan 26, 2017
⌛ Testing commit e1280d8 with merge f453929... |
bors
commented
Jan 26, 2017
💔 Test failed - status-travis |
alexcrichton
commented
Jan 26, 2017
via email
| … On Thu, Jan 26, 2017 at 1:47 PM, bors ***@***.***> wrote:
💔 Test failed - status-travis
<https://travis-ci.org/rust-lang/rust/builds/195662190>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#39139 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95NgcEoXZgz9hNDlNtfdjiKWfXLNQks5rWRRugaJpZM4LmO9M>
.
|
bors
commented
Jan 26, 2017
⌛ Testing commit e1280d8 with merge 4805ad6... |
bors
commented
Jan 27, 2017
💔 Test failed - status-travis |
alexcrichton
commented
Jan 27, 2017
via email
| … On Thu, Jan 26, 2017 at 4:39 PM, bors ***@***.***> wrote:
💔 Test failed - status-travis
<https://travis-ci.org/rust-lang/rust/builds/195714707>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#39139 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95D-rx6ugjwGc_a0vzHqZ5J0HI529ks5rWTyugaJpZM4LmO9M>
.
|
bors
commented
Jan 27, 2017
⌛ Testing commit e1280d8 with merge 9876cb0... |
bors
commented
Jan 27, 2017
💔 Test failed - status-travis |
alexcrichton
commented
Jan 27, 2017
via email
@bors: retry
* presumed bug in travis …On Thu, Jan 26, 2017 at 8:50 PM, bors ***@***.***> wrote:
💔 Test failed - status-travis
<https://travis-ci.org/rust-lang/rust/builds/195760346>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#39139 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95F0qzRFshg7CladBntz0ewYL9Dujks5rWXd6gaJpZM4LmO9M>
.
|
bors
commented
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.bors
commented
Jan 27, 2017
☀️ Test successful - status-appveyor, status-travis |
arielb1
commented
Feb 6, 2017
This causes the ICE #39544. |
Present the following output when trying to access an immutable borrow's
field as mutable:
And the following when trying to access an immutable struct field as mutable:
Fixes#38147.