Skip to content
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

borrowck diagnostics: make add_move_error_suggestions use the HIR rather than SourceMap #133486

Merged
merged 1 commit into from
Jan 1, 2025

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Nov 26, 2024

This PR aims to fix #132806 by rewriting add_move_error_suggestions1. Previously, it manually scanned the source text to find a leading &, which isn't always going to produce a correct result (see: that issue). Admittedly, the HIR visitor in this PR introduces a lot of boilerplate, but hopefully the logic at its core isn't too complicated (I go over it in the comments). I also tried a simpler version that didn't use a HIR visitor and suggested adding ref always, but the &ref x suggestions really didn't look good. As a bonus for the added complexity though, it's now able to produce nice &-removing suggestions in more cases.

I tried to do this such that it avoids edition-dependent checks and its suggestions can be applied together with those from the match ergonomics 2024 migration lint. I haven't added tests for that since the details of match ergonomics 2024 are still being sorted out, but I can try if desired once that's finalized.

Footnotes

  1. In brief, it fires on patterns where users try to bind by-value in such a way that moves out of a reference to a non-Copy type (including slice references with non-copy elements). The suggestions are to change the binding's mode to be by-reference, either by removing2 an enclosing &/&mut or adding ref to the binding.

  2. Incidentally, I find the terminology of "consider removing the borrow" a bit confusing for a suggestion to remove a & pattern in order to make bindings borrow rather than move. I'm not sure what a good, concise way to explain that would be though, and that should go in a separate PR anyway.

@rustbot
Copy link
Collaborator

rustbot commented Nov 26, 2024

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
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 Nov 26, 2024
Copy link
Contributor Author

@dianne dianne Nov 26, 2024

Choose a reason for hiding this comment

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

To be honest, I'm not sure why the "consider borrowing the pattern bindings" lines weren't in this test's output before; they appeared when I tested it on the playground. Either way, the suggestions look okay1 to me, though I'm not a deref-patterns expert.

Footnotes

  1. Apart from being non-run-rustfix-able, due to the type errors resulting from binding by-reference.

@BoxyUwU
Copy link
Member

BoxyUwU commented Dec 5, 2024

r? @compiler-errors

@rustbot rustbot assigned compiler-errors and unassigned BoxyUwU Dec 5, 2024
@compiler-errors
Copy link
Member

r? diagnostics

@rustbot rustbot assigned estebank and unassigned compiler-errors Dec 16, 2024
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Dec 31, 2024

📌 Commit 04d9bb7 has been approved by estebank

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 Dec 31, 2024
Noratrieb added a commit to Noratrieb/rust that referenced this pull request Dec 31, 2024
…r=estebank

borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`

This PR aims to fix rust-lang#132806 by rewriting `add_move_error_suggestions`[^1]. Previously, it manually scanned the source text to find a leading `&`, which isn't always going to produce a correct result (see: that issue). Admittedly, the HIR visitor in this PR introduces a lot of boilerplate, but hopefully the logic at its core isn't too complicated (I go over it in the comments). I also tried a simpler version that didn't use a HIR visitor and suggested adding `ref` always, but the `&ref x` suggestions really didn't look good. As a bonus for the added complexity though, it's now able to produce nice `&`-removing suggestions in more cases.

I tried to do this such that it avoids edition-dependent checks and its suggestions can be applied together with those from the match ergonomics 2024 migration lint. I haven't added tests for that since the details of match ergonomics 2024 are still being sorted out, but I can try if desired once that's finalized.

[^1]: In brief, it fires on patterns where users try to bind by-value in such a way that moves out of a reference to a non-Copy type (including slice references with non-copy elements). The suggestions are to change the binding's mode to be by-reference, either by removing[^2] an enclosing `&`/`&mut` or adding `ref` to the binding.

[^2]: Incidentally, I find the terminology of "consider removing the borrow" a bit confusing for a suggestion to remove a `&` pattern in order to make bindings borrow rather than move. I'm not sure what a good, concise way to explain that would be though, and that should go in a separate PR anyway.
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 31, 2024
Rollup of 9 pull requests

Successful merges:

 - rust-lang#132474 (Add more mailmap entries)
 - rust-lang#133486 (borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`)
 - rust-lang#134063 (dec2flt: Clean up float parsing modules)
 - rust-lang#134861 (Add GUI test for item info elements color)
 - rust-lang#134968 (Print how to rebless Python formatting in tidy)
 - rust-lang#134971 (chore: fix typos)
 - rust-lang#134972 (add .mailmap entry for myself)
 - rust-lang#134974 (Revert rust-lang#119515 single line where clause style guide)
 - rust-lang#134975 (Revert style guide rhs break)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 1, 2025
Rollup of 8 pull requests

Successful merges:

 - rust-lang#132474 (Add more mailmap entries)
 - rust-lang#133486 (borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`)
 - rust-lang#134861 (Add GUI test for item info elements color)
 - rust-lang#134968 (Print how to rebless Python formatting in tidy)
 - rust-lang#134971 (chore: fix typos)
 - rust-lang#134972 (add .mailmap entry for myself)
 - rust-lang#134974 (Revert rust-lang#119515 single line where clause style guide)
 - rust-lang#134975 (Revert style guide rhs break)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3d3d898 into rust-lang:master Jan 1, 2025
6 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Jan 1, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 1, 2025
Rollup merge of rust-lang#133486 - dianne:fix-move-error-suggestion, r=estebank

borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`

This PR aims to fix rust-lang#132806 by rewriting `add_move_error_suggestions`[^1]. Previously, it manually scanned the source text to find a leading `&`, which isn't always going to produce a correct result (see: that issue). Admittedly, the HIR visitor in this PR introduces a lot of boilerplate, but hopefully the logic at its core isn't too complicated (I go over it in the comments). I also tried a simpler version that didn't use a HIR visitor and suggested adding `ref` always, but the `&ref x` suggestions really didn't look good. As a bonus for the added complexity though, it's now able to produce nice `&`-removing suggestions in more cases.

I tried to do this such that it avoids edition-dependent checks and its suggestions can be applied together with those from the match ergonomics 2024 migration lint. I haven't added tests for that since the details of match ergonomics 2024 are still being sorted out, but I can try if desired once that's finalized.

[^1]: In brief, it fires on patterns where users try to bind by-value in such a way that moves out of a reference to a non-Copy type (including slice references with non-copy elements). The suggestions are to change the binding's mode to be by-reference, either by removing[^2] an enclosing `&`/`&mut` or adding `ref` to the binding.

[^2]: Incidentally, I find the terminology of "consider removing the borrow" a bit confusing for a suggestion to remove a `&` pattern in order to make bindings borrow rather than move. I'm not sure what a good, concise way to explain that would be though, and that should go in a separate PR anyway.
@dianne dianne deleted the fix-move-error-suggestion branch January 1, 2025 21:28
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.

Suggestion for "error[E0507]: cannot move out of a shared reference" suggests removing the wrong borrow
6 participants