Skip to content

Conversation

@JohnTitor
Copy link
Member

@JohnTitor JohnTitor commented Feb 7, 2026

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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 7, 2026
Comment on lines +12 to +19
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) {
| +
Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

@rust-log-analyzer

This comment has been minimized.

@JohnTitor JohnTitor force-pushed the sugg-mut-deref-borrows branch from 9d683c6 to 0de7415 Compare February 7, 2026 11:29
// 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)),
Copy link
Contributor

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.

Comment on lines 22 to 26
help: if there is only one mutable reborrow, remove the `&mut`
|
LL - h(&mut b);
LL + h(b);
|
Copy link
Contributor

@estebank estebank Feb 11, 2026

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 🤦

@estebank
Copy link
Contributor

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 11, 2026

📌 Commit 0de7415 has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 11, 2026
rust-bors bot pushed a commit that referenced this pull request Feb 12, 2026
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)
@rust-bors rust-bors bot merged commit 050b48a into rust-lang:main Feb 12, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 12, 2026
rust-timer added a commit that referenced this pull request Feb 12, 2026
Rollup merge of #152281 - JohnTitor:sugg-mut-deref-borrows, r=estebank

borrowck: suggest `&mut *x` for pattern reborrows

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.
@JohnTitor JohnTitor deleted the sugg-mut-deref-borrows branch February 12, 2026 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error message improvement: Manual reborrowing with &mut *variable

4 participants