-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[WIP] Suggest use mut when mutating a non mutable local #51260
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
@@ -1,6 +1,8 @@ | |||
error[E0596]: cannot borrow immutable item `x` as mutable | |||
--> $DIR/issue-37139.rs:22:18 | |||
| | |||
LL | TestEnum::Item(ref mut x) => { | |||
| --------- consider changing this to `mut x` |
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.
yeah we definitely don't want to suggest this...
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, need to look at this more carefully.
The job 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 |
if let &Place::Local(local) = place_err { | ||
let local_decl = &self.mir.local_decls[local]; | ||
if local_decl.is_user_variable && local_decl.mutability == Mutability::Not { | ||
err.span_label(local_decl.source_info.span, |
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.
Here we should use the span_suggestion
method. Something like this (I took the prompt etc from AST borrowck):
let local_name = local_decl.name.unwrap();
err.span_suggestion(
local_decl.source_info.span,
&format!("consider making `{}` mutable", local_name),
format!("mut {}", local_name),
);
This will encode the suggestion into the JSON etc so that tools like rustfix or IDEs can apply it automatically.
let local_decl = &self.mir.local_decls[local]; | ||
if local_decl.is_user_variable && local_decl.mutability == Mutability::Not { | ||
err.span_label(local_decl.source_info.span, | ||
format!("consider changing this to `mut {}`", local_decl.name.unwrap())); | ||
} | ||
} |
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.
Also, I was noting on Zulip at 4:40pm (ET) yesterday, I think we want to move this suggestion code so that it comes after the match. That will require restructuring this method somewhat. The problem with the current setup is that your suggestion will only fire when you are trying to borrow something, but not — I think — in tests like let x = 3; x = 4;
(try it!). I would combine it with some of the other suggestion-like code in the method too, yielding something like this:
let mut err = match kind {
Reservation(...) | Write(...) => {
if no error { return } else { construct_err_object() }
}
Reservation(...) | Write(...) => /* some variant of the preivous arm */,
Reservation(...) | Write(...) => ...,
};
// If we get here, then there *is* an error
match place_err {
Place::Local(l) => {
// suggest changing to `mut` variable
}
Place::Projection(...) => {
// suggest changing to `&mut` borrow (see [link 1])
}
}
err.emit()
link 1 is
rust/src/librustc_mir/borrow_check/mod.rs
Lines 1723 to 1728 in 61f35e5
let err_info = if let Place::Projection( | |
box Projection { | |
base: Place::Local(local), | |
elem: ProjectionElem::Deref | |
} | |
) = *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.
We are also going to want to watch out for "upvars" — those are cases where the place_err
will not be a Place::Local
but we want to make a similar suggestion. We can discuss on Zulip though, I'm just going to edit your PR description to have a check-list so things don't get forgotten.
Closes #51031
There's some things that are wrong in this PR, opened to start discussing with code.
Check-list:
ref mut
{ let x = 3; x = 4; }
){ let x = (3, 4); x.0 = 5; }
generates a sort-of strange message maybe? (playground — compare NLL and not NLL)let x = 3; let c = || { &mut x; }
) (playground)