-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
borrowck: suggest &mut *x for pattern reborrows
#152281
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
| help: consider making the binding mutable if you need to reborrow multiple times | ||
| | | ||
| LL | fn test(mut outer: &mut Option<i32>) { | ||
| | +++ | ||
| help: to reborrow the mutable reference, add `*` | ||
| | | ||
| LL | match (&mut *outer, 23) { | ||
| | + |
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's a discussion point, maybe we should dedup help annotations.
I don't have strong preference and would like to follow someone's favor.
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.
In the past I've encountered cases where it isn't clear which of the two suggestions would be best, but gaining that certainty would require performing borrowck on the whole item with the two alternatives. For now, I find it ok to provide more than one suggestion, as long as it is clear that they are alternatives, and not that both need to be applied.
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.
How hard would it be to get the number of spans where we mention "cannot borrow as mutable"? Because that's pretty much the information we need to make the "you need to reborrow multiple times" determination to avoid the prior suggestion.
This comment has been minimized.
This comment has been minimized.
9d683c6 to
0de7415
Compare
| // We don't have access to the HIR to get accurate spans, but we can | ||
| // give a best effort structured suggestion. | ||
| err.span_suggestion_verbose( | ||
| source_info.span.with_hi(source_info.span.lo() + BytePos(5)), |
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.
Happy to see this go away.
| help: if there is only one mutable reborrow, remove the `&mut` | ||
| | | ||
| LL - h(&mut b); | ||
| LL + h(b); | ||
| | |
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 work that still needs to be addressed: we should be able to identify that there are more than one mutable reborrow and not suggest this.
Wait, in this case this is ok 🤦
|
@bors r+ |
Rollup of 7 pull requests Successful merges: - #152505 (Sync relnotes for stable 1.93.1) - #137487 (Stabilize `assert_matches`) - #152281 (borrowck: suggest `&mut *x` for pattern reborrows) - #151142 (Support ADT types in type info reflection) - #152477 (rustc-dev-guide subtree update) - #152488 (allow `deprecated(since = "CURRENT_RUSTC_VERSION")`) - #152491 (Remove unused `fluent-syntax` dependency from tidy)
Fixes #81059
r? @estebank as you should have some context here, but feel free to re-assign if you don't have time to review right now.