Skip to content

overhaul &mut suggestions in borrowck errors #145013

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

Merged
merged 3 commits into from
Aug 19, 2025

Conversation

fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Aug 6, 2025

  • This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR.
  • If something comes from a custom Index impl for which the IndexMut trait does not apply, do not suggest adding mut after &.
  • Suggest get_mut with unwrap if error is fired on BTreeMap or HashMap.

Supersedes #144018 cc @xizheyin
Closes #143732

@rustbot
Copy link
Collaborator

rustbot commented Aug 6, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Aug 6, 2025
Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only looked at the tests and their outputs so far, some of the suggestions seem suboptimal. Is this expected?

@rustbot author

|
LL | let x: &[isize] = &mut [1, 2, 3, 4, 5];
| +++
LL | let x: &mut [isize] = &[1, 2, 3, 4, 5];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still invalid, it needs to be &mut [1, 2, 3, 4, 5].

|
LL | let x = &mut 0 as *const i32;
| +++
LL | let x: *mut i32 = &0 as *const i32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also still invalid, let x = &mut 0 as *mut i32; would work, without needing the type annotation.

help: consider changing this binding's type
|
LL - let ptr_x: *const _ = &x;
LL + let ptr_x: *mut i32 = &x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be &x instead of &mut x.

@nnethercote
Copy link
Contributor

This is marked as waiting on review, but my comments about the invalid suggestions haven't been addressed. What's the status there?

@fee1-dead
Copy link
Member Author

Rustbot didn't change it when you included as the "review comment", it has to be a normal comment

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 16, 2025

This PR was rebased onto a different master commit! Check out the changes with our range-diff.

@fee1-dead
Copy link
Member Author

fee1-dead commented Aug 16, 2025

w.r.t. invalid suggestions: every time I look into this code I find things to refactor, so this is currently turning into an endless time sink.

The logic is an entangled mess and I tried to untangle it a bit. It is still entangled, but better from the previous state of using stringy logic for everything, having the same code path for everything from ref x to let x to &x to &T.

Some of them have complicated mir, which is probably why the original author chose to do string operations instead of matching on mir. (if you want to take a look, run with RUSTC_LOG=rustc_borrowck::diagnostics rustc +stage1 PATH_TO_TEST -Zdump-mir=nll)

I put down some infrastructure to allow both an expr suggestion and a type change suggestion to exist. For some reason this doesn't fire at the moment. But this is a pre-existing issue and my PR doesn't regress them too much so I propose we could land this and fix later.

@rust-log-analyzer

This comment has been minimized.

@fee1-dead
Copy link
Member Author

if you want me to look into this even more, feel free to turn the labels back

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 16, 2025
return;
}

// no suggestion for expression; find a binding's type to make mutable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My most boomer-coded opinion is that I like sentences to begin with an upper-case letter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify:

  • Punctuationless phrase fragments are bearable, e.g. "this is a comment"
  • A trailing period makes it worse, e..g "this is a comment."
  • Multiple sentences are beyond the pale e.g. "this is a comment. this is another comment." If it's heading into "proper paragraph" territory there are rules for a reason, they make the text easier to read.

I almost always write comments as full sentences with upper-case first letter and closing period. Occasionally I'll do a "this is a comment"-style punctuation-free sentence fragment, perhaps as a trailing comment on a line with code.

@nnethercote
Copy link
Contributor

Is it accurate to mark this as fully fixing #143732?

The PR is hard to review. It's hard to see what is new code and what is just moved around. I'm not sure if it could have been broken into small pieces. I'm not inclined to go over it with a fine-tooth comb but I also don't want to block it, based on the fact that it does somewhat improve the error messages and you say there is follow-up work to be done. So you can have an r=me if you want it and think it's progress.

@fee1-dead
Copy link
Member Author

Is it accurate to mark this as fully fixing #143732?

Yes, because I believe either the "specify this binding's type" will fire or it will go down the path of either suggesting .get_mut if possible or not suggesting anything at all.

I like sentences to begin with an upper-case letter.

Went through the file and applied the stylistic changes you suggested.

It's hard to see what is new code and what is just moved around. I'm not sure if it could have been broken into small pieces.

Yeah, I don't know either. Perhaps it would be easier if the refactor was done first, and the new logic was added later. But I still think it would be hard to review. The followups would probably be easier.

@bors r=nnethercote

@bors
Copy link
Collaborator

bors commented Aug 18, 2025

📌 Commit c6db6f2 has been approved by nnethercote

It is now in the queue for this repository.

@bors bors 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 Aug 18, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 19, 2025
…m, r=nnethercote

overhaul `&mut` suggestions in borrowck errors

* This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR.
* If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`.
* Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`.

Supersedes rust-lang#144018 cc `@xizheyin`
Closes rust-lang#143732
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 19, 2025
…m, r=nnethercote

overhaul `&mut` suggestions in borrowck errors

* This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR.
* If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`.
* Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`.

Supersedes rust-lang#144018 cc ``@xizheyin``
Closes rust-lang#143732
bors added a commit that referenced this pull request Aug 19, 2025
Rollup of 19 pull requests

Successful merges:

 - #140956 (`impl PartialEq<{str,String}> for {Path,PathBuf}`)
 - #141744 (Stabilize `ip_from`)
 - #142681 (Remove the `#[no_sanitize]` attribute in favor of `#[sanitize(xyz = "on|off")]`)
 - #142871 (Trivial improve doc for transpose )
 - #144252 (Do not copy .rmeta files into the sysroot of the build compiler during check of rustc/std)
 - #144476 (rustdoc-search: search backend with partitioned suffix tree)
 - #144567 (Fix RISC-V Test Failures in ./x test for Multiple Codegen Cases)
 - #144804 (Don't warn on never to any `as` casts as unreachable)
 - #144960 ([RTE-513] Ignore sleep_until test on SGX)
 - #145013 (overhaul `&mut` suggestions in borrowck errors)
 - #145041 (rework GAT borrowck limitation error)
 - #145243 (take attr style into account in diagnostics)
 - #145405 (cleanup: use run_in_tmpdir in run-make/rustdoc-scrape-examples-paths)
 - #145432 (cg_llvm: Small cleanups to `owned_target_machine`)
 - #145484 (Remove `LlvmArchiveBuilder` and supporting code/bindings)
 - #145557 (Fix uplifting in `Assemble` step)
 - #145563 (Remove the `From` derive macro from prelude)
 - #145565 (Improve context of bootstrap errors in CI)
 - #145584 (interpret: avoid forcing all integer newtypes into memory during clear_provenance)

Failed merges:

 - #145359 (Fix bug where `rustdoc-js` tester would not pick the right `search.js` file if there is more than one)
 - #145573 (Add an experimental unsafe(force_target_feature) attribute.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 181480d into rust-lang:master Aug 19, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 19, 2025
rust-timer added a commit that referenced this pull request Aug 19, 2025
Rollup merge of #145013 - fee1-dead-contrib:push-vwvsqsqnrxqm, r=nnethercote

overhaul `&mut` suggestions in borrowck errors

* This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR.
* If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`.
* Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`.

Supersedes #144018 cc ```@xizheyin```
Closes #143732
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.

rust suggests using IndexMut for maps, even though they don't implement the trait
5 participants