-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Suggest removing &mut
when encountering mutability error
#68723
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
Suggest removing &mut
when encountering mutability error
#68723
Conversation
Applicability::MachineApplicable, | ||
); | ||
|
||
if let ty::Ref(_, _, Mutability::Mut) = local_decl.ty.kind { |
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 condition seems insufficient. You'd need to check that the expected type requires &mut T
and not &mut &mut T
, otherwise it seems like it would trigger on this (which is a good test to add, to ensure that this behaves correctly):
struct S;
fn bar(x: &mut &mut S) {}
fn foo() {
let x = &mut S;
bar(&mut x);
}
...but this makes me wonder whether this is doable in borrowck if you don't know what type is expected (seems you only know that you want to take a mutable borrow and cannot due to lack of mut
on the binding. On the other hand, if you do check it in typeck, then we need to check that:
struct S;
fn bar(x: &mut S) {}
fn foo() {
let mut x = &mut S;
bar(&mut x);
}
still compiles... which is probably not a good idea in typeck.
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 |
Ping from triage: @LeSeulArtichaut Can you please address the test failures? |
@JohnCSimon As Centril pointed out, my fix was very naive and will not work. I'll close this now |
Resolves #68697
I haven't checked that this change doesn't break any of the already existing UI tests, I'll use the CI to figure out for me.
r? @Centril