-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Point to immutable arg/fields when trying to use as &mut #39139
Conversation
r? @nrc (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -177,7 +177,7 @@ fn check_aliasability<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, | |||
debug!("check_aliasability aliasability={:?} req_kind={:?}", | |||
aliasability, req_kind); | |||
|
|||
match (aliasability, req_kind) { | |||
match (aliasability.clone(), req_kind.clone()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why suddenly cloning them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover from previous exploration.
This is great! 👍 |
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 |
Introduced an error in two tests (removed two hints). Will try to fix it tonight. |
@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 |
I'm definitely liking this revision. One question, is this part missing something?
Seems like it needs a label there |
@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. |
0df4676
to
a9a0f48
Compare
@jonathandturner done. |
👍 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. =) |
@nikomatsakis is there anything else that should be part of this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?)
fn immutable_argument_should_be_mut(&self, nid: ast::NodeId, db: &mut DiagnosticBuilder) { | ||
let parent = self.tcx.map.get_parent_node(nid); | ||
|
||
if let Some(fn_like) = FnLikeNode::from_node(self.tcx.map.get(parent)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
--> $DIR/issue-38147-2.rs:17:9 | ||
| | ||
12 | s: &'a String | ||
| ------------- this field should be `&mut` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice :)
| ------------- this field should be `&mut` | ||
... | ||
16 | fn f(&self) { | ||
| ----- use `&mut self` here to make mutable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
||
error: cannot borrow immutable `Box` content `*f.f` as mutable | ||
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:44:5 | ||
| | ||
18 | f: Box<FnMut() + 'a> | ||
| -------------------- this field should be `&mut` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this seems wrong, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly there is nothing that would easily be converted to &mut
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should be the appropriate message for this case? Nothing if the type is not a ref?
|
||
let mut db = self.struct_span_err(span, msg); | ||
if let Some(span) = field_span { | ||
db.span_label(span, &"this field should be `&mut`"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
||
error: cannot borrow immutable `Box` content `*f.f` as mutable | ||
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:44:5 | ||
| | ||
18 | f: Box<FnMut() + 'a> | ||
| -------------------- this field should be `&mut` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should be the appropriate message for this case? Nothing if the type is not a ref?
fn immutable_argument_should_be_mut(&self, nid: ast::NodeId, db: &mut DiagnosticBuilder) { | ||
let parent = self.tcx.map.get_parent_node(nid); | ||
|
||
if let Some(fn_like) = FnLikeNode::from_node(self.tcx.map.get(parent)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| ------------- this field should be `&mut` | ||
... | ||
16 | fn f(&self) { | ||
| ----- use `&mut self` here to make mutable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Yeah, no change is needed in this case. |
Travis is failing because a UI test needs to be updated:
|
but the new output looks correct =) |
@nikomatsakis tests are fixed. Can you check wether the method Also, please verify that using |
☔ 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 ```
Yes; it's a wonder what some comments will do! :) Thanks.
Yes that is correct. |
@bors r+ |
📌 Commit e1280d8 has been approved by |
⌛ Testing commit e1280d8 with merge f453929... |
💔 Test failed - status-travis |
… 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>
.
|
⌛ Testing commit e1280d8 with merge 4805ad6... |
💔 Test failed - status-travis |
… 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>
.
|
⌛ Testing commit e1280d8 with merge 9876cb0... |
💔 Test failed - status-travis |
@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>
.
|
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.
☀️ Test successful - status-appveyor, status-travis |
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.