-
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
Modify compile-fail/E0389 error message WIP #48914
Conversation
r? @estebank (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -70,5 +70,5 @@ fn main() { | |||
}; | |||
s[2] = 20; | |||
//[ast]~^ ERROR cannot assign to immutable indexed content | |||
//[mir]~^^ ERROR cannot assign to immutable item | |||
//[mir]~^^ ERROR cannot assign through immutable item |
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'm not sure about the look of this test though.
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 one seems ok-ish to me, but we might be able to improve upon it. I'll think about it.
This is how the error message looks like
Do I need to add a ui test for this? There's an already existing compile-fail test |
Could you move the file from |
src/librustc_mir/borrow_check/mod.rs
Outdated
let item_msg = match self.describe_place(place) { | ||
Some(name) => format!("immutable item `{}`", name), | ||
None => "immutable item".to_owned(), | ||
let err_info = match *place_err { |
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.
Given how deeply nested this is, it might make sense to move it to its own method, or at least adding a comment on top describing what the code is supposed to be doing.
src/librustc_mir/borrow_check/mod.rs
Outdated
Some(name) => format!("immutable item `{}`", name), | ||
None => "immutable item".to_owned(), | ||
let err_info = match *place_err { | ||
Place::Projection(ref proj) => { |
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.
You can match on multiple levels to avoid deeply nested structures:
enum Foo {
A { f: Bar },
B(Bar),
}
enum Bar {
X,
Y,
}
let x = Foo::B(Bar::X);
match x {
Foo::A { f: Bar::Y } => 1,
_ => 0,
}
src/librustc_mir/borrow_check/mod.rs
Outdated
_ => None, | ||
} | ||
} | ||
_ => None, |
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.
Given how many branches there are here, it might make sense to make err_info
mutable with a default value of None
, and override where applicable, but this might not be needed if the amount of branches is reduced.
src/librustc_mir/borrow_check/mod.rs
Outdated
} | ||
err.emit() | ||
}else{ |
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.
spaces around else
src/librustc_mir/borrow_check/mod.rs
Outdated
err.span_label(span, "cannot mutate"); | ||
if place != place_err { | ||
if let Some(name) = self.describe_place(place_err) { | ||
err.note(&format!("Value not mutable causing this error: `{}`", |
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.
lowercase sentences: value not mutable
src/librustc_mir/lib.rs
Outdated
@@ -39,6 +39,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! | |||
#![feature(collection_placement)] | |||
#![feature(nonzero)] | |||
#![feature(underscore_lifetimes)] | |||
#![feature(crate_visibility_modifier)] |
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.
Was this needed?
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.
yes, due to the usage here crate trait FindAssignments {
in collect_writes.rs
@@ -70,5 +70,5 @@ fn main() { | |||
}; | |||
s[2] = 20; | |||
//[ast]~^ ERROR cannot assign to immutable indexed content | |||
//[mir]~^^ ERROR cannot assign to immutable item | |||
//[mir]~^^ ERROR cannot assign through immutable item |
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 one seems ok-ish to me, but we might be able to improve upon it. I'll think about it.
@@ -27,7 +27,7 @@ fn indirect_write_to_imm_box() { | |||
let y: Box<_> = box &mut x; | |||
let p = &y; | |||
***p = 2; //[ast]~ ERROR cannot assign to data in a `&` reference | |||
//[mir]~^ ERROR cannot assign to immutable item `***p` | |||
//[mir]~^ ERROR cannot assign through `&`-reference `p` |
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.
Not convinced about this change...
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 better than it was ("imutable item"), but not I think perfect yet -- and not necessarily better than AST variant, I don't think
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.
The AST based error is this:
error[E0389]: cannot assign to data in a `&` reference
--> src/main.rs:8:5
|
8 | ***p = 2;
| ^^^^^^^^ assignment into an immutable reference
which I think has too much jargon, but some promising elements. This also intersects your style guide @estebank. Personally, my preference is that the "main" message is kind of generic and formal, like it is here, telling you nothing of the program itself, but that those details are revealed in the "underlines" and in a "conversational" tone. Maybe something like this?
error[E0389]: cannot assign to data in a `&` reference
--> src/main.rs:8:5
|
8 | ***p = 2;
| ^^^^^^^^ `p` is a `&` reference, so the data it refers to cannot be written
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.
Is this the common message for all such cases or just for the above case?
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.
error[E0594]: cannot assign to data in a `&` reference
--> src/test/compile-fail/borrowck/borrowck-issue-14498.rs:29:5
|
28 | let p = &y;
| -- help: consider changing this to be a mutable reference: `&mut`
29 | ***p = 2;
| ^^^^^^^^ `p` is a `&` reference, so the data it refers to cannot be written
is how the message looks like now @nikomatsakis
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.
Left a few nits, to start.
src/librustc_mir/borrow_check/mod.rs
Outdated
let mut err = self.tcx | ||
.cannot_borrow_path_as_mutable(span, &item_msg, Origin::Mir); | ||
err.span_label(span, "cannot borrow as mutable"); | ||
|
||
if place != place_err { | ||
if let Some(name) = self.describe_place(place_err) { | ||
err.note(&format!("Value not mutable causing this error: `{}`", name)); | ||
err.note(&format!("value not mutable causing this error: `{}`", name)); |
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.
Pre-existing, but this message is not particularly grammatical. In fact, I have a hard time even understanding it -- I guess it is saying that "the value which is causing this path not to be mutable is..."?
|
||
crate trait FindAssignments { | ||
fn find_assignments(&self, local: Local) -> Vec<Location>; | ||
} |
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.
Nit: formatting
// fn super_local() | ||
} | ||
|
||
crate trait FindAssignments { |
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 would move this to the top of the file -- this is what readers care about. Also, I would add a comment to the method:
Finds all statements that assign directly to local
(i.e., X = ...
) and returns their locations.
|
||
// The Visitor walks the MIR to return the assignment statements corresponding | ||
// to a Local. | ||
pub struct FindLocalAssignmentVisitor { |
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 does not need to be public, it is an impl detail. Just make it private.
src/librustc_mir/borrow_check/mod.rs
Outdated
let item_msg = if error_reported { | ||
if let Some(name) = | ||
self.describe_place(place_err) { | ||
let var = str::replace(&name, "*", ""); |
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, I'm not crazy about the string munging =) Maybe we should just do describe_place(&place.base)
?
src/librustc_mir/borrow_check/mod.rs
Outdated
let var = str::replace(&name, "*", ""); | ||
format!("`&`-reference `{}`", var) | ||
} else { | ||
self.get_main_error_message(place) |
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 would pull this into a helper function and I would change these get_main_error_message
cases to just return None
. The helper would be something like:
fn specialized_description(..) -> Option<String>
then if it is None
you can use get_main_error_message(place)
.
☔ The latest upstream changes (presumably #47630) made this pull request unmergeable. Please resolve the merge conflicts. |
@gaurikholkar ping from triage! There are a few comments from the reviewer, could you address them? |
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@estebank @nikomatsakis repoened the issue |
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Not sure why tidy checks are failing. Couldn't find any trailing whitespace. |
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
src/librustc_mir/borrow_check/mod.rs
Outdated
} else { | ||
self.get_default_err_msg(place) | ||
}; | ||
|
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.
A line consisting of only spaces is also considered "trailing whitespaces".
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.
thanks @kennytm :)
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.
@kennytm can you help out with the fixes for the tidy errors? I've tried certain things but they don't seem to work. One of the errors is also in the previously written code.
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.
@gaurikholkar The line numbers seems wrong. You could run tidy
locally by
./x.py test src/tools/tidy
The lines with trailing whitespaces are:
tidy error: src/librustc_mir/borrow_check/mod.rs:1532: trailing whitespace
tidy error: src/librustc_mir/borrow_check/mod.rs:1536: trailing whitespace
tidy error: src/librustc_mir/borrow_check/mod.rs:1595: trailing whitespace
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.
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.
@gaurikholkar you should just remove the entire binaryen
folder, it is no longer used.
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This is looking pretty good to me! Let's land it. We can always tinker more. @bors r+ Nice job @gaurikholkar |
📌 Commit c792d1e has been approved by |
Modify compile-fail/E0389 error message WIP This fixes #47388 cc @nikomatsakis @estebank r? @nikomatsakis Certain ui tests were failing locally. I'll check if the same happens here too.
--> $DIR/issue-47388.rs:18:5 | ||
| | ||
LL | let fancy_ref = &(&mut fancy); | ||
| ------------- help: consider changing this to be a mutable reference: `&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.
I believe either the span or the suggestion text might be incorrect. It should either suggest &mut (&mut fancy)
for this span, or use a span only selecting the &
.
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.
Filed #49859
☀️ Test successful - status-appveyor, status-travis |
@estebank I guess now we need another PR for that... |
This fixes #47388
cc @nikomatsakis @estebank
r? @nikomatsakis
Certain ui tests were failing locally. I'll check if the same happens here too.