-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Better suggested fix available for passing &mut (&mut val) #45392
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
Comments
I also found this disturbing. I'm ok to work on a fix or an improvement of the suggestion. If someone can mentor it... :) |
cc @estebank @rust-lang/compiler - could someone provide mentoring instructions? |
I believe that in the following code: rust/src/librustc_borrowck/borrowck/mod.rs Lines 1208 to 1223 in 3dfbc88
we must verify that the mutability (the @Freyskeyd apologies for not having seen this before. If you're still interested, feel free to reach out again here or in gitter! The amount of code shouldn't be too much, but there's a bunch of machinery you'd get familiar with (the I believe the output should resemble the following (by using
|
@estebank I'm interested, I will work on it next week. |
@estebank This seemed kinda stale so I decided to try my hand at a fix. I opened a PR. I have a lot of questions, but I'll ask those in the PR. |
Since @klnusbaum mentioned on #48708 that they're dropping this issue, I would like to do it. |
Suggest not mutably borrowing a mutable reference This PR would (hopefully) solve #45392. I deviated a bit from @estebank's instructions since the error span only included the borrowed expression (e.g. the `b` in `&mut b`). I also didn't check the mutability of the local binding, since this whole case is concerned with an immutable local. I can see two outstanding questions: 1. `note_immutability_blame` is called in two places, but I only have one test case. I think it covers the call in `report_bckerror`, but I'm not sure how to trigger the call from `report_aliasability_violation`. 2. There is one failing test, where the local binding is `self: &mut Self`. I'm not entirely sure what the correct output should be, but I think the new message should also apply. Unfortunately, since this parameter is parsed differently, its `let_span` covers both the pattern and the type, leading to a wrong suggestion text. I'm not sure how to correctly identify this case.
I think this issue can be closed now -- what's the process to do that? |
Given code like:
rustc
correctly points out that you "cannot borrow immutable argumentb
as mutable", and offers a fix of:A better fix, in this case (and many others where I make this error), is to simply drop the
&mut
:I hit this error quite frequently, and remember finding the addition and removal of
&mut
until the compiler shut up about syntactic noise that I didn't care about (etc. etc.) pretty confusing; especially aroundWrite
.Full code as a playground: https://play.rust-lang.org/?gist=9b69414d92287359bd6f1b91a53b254a&version=stable
If anyone can think of a better way to phrase the subject of this issue, please do change it.
The text was updated successfully, but these errors were encountered: