forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
11 changed files
with
249 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/ui/moves/do-not-suggest-removing-wrong-ref-pattern-issue-132806.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//@ run-rustfix | ||
//! diagnostic test for #132806: make sure the suggestion to bind by-reference in patterns doesn't | ||
//! erroneously remove the wrong `&` | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let _ = HashMap::<String, i32>::new().iter().filter(|&(_k, &_v)| { true }); | ||
//~^ ERROR cannot move out of a shared reference | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/ui/moves/do-not-suggest-removing-wrong-ref-pattern-issue-132806.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//@ run-rustfix | ||
//! diagnostic test for #132806: make sure the suggestion to bind by-reference in patterns doesn't | ||
//! erroneously remove the wrong `&` | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let _ = HashMap::<String, i32>::new().iter().filter(|&(&_k, &_v)| { true }); | ||
//~^ ERROR cannot move out of a shared reference | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/ui/moves/do-not-suggest-removing-wrong-ref-pattern-issue-132806.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error[E0507]: cannot move out of a shared reference | ||
--> $DIR/do-not-suggest-removing-wrong-ref-pattern-issue-132806.rs:8:58 | ||
| | ||
LL | let _ = HashMap::<String, i32>::new().iter().filter(|&(&_k, &_v)| { true }); | ||
| ^^^--^^^^^^ | ||
| | | ||
| data moved here | ||
| move occurs because `_k` has type `String`, which does not implement the `Copy` trait | ||
| | ||
help: consider removing the borrow | ||
| | ||
LL - let _ = HashMap::<String, i32>::new().iter().filter(|&(&_k, &_v)| { true }); | ||
LL + let _ = HashMap::<String, i32>::new().iter().filter(|&(_k, &_v)| { true }); | ||
| | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0507`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.