forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
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#120126 - sjwang05:issue-102269, r=compiler-…
…errors Suggest `.swap()` when encountering conflicting borrows from `mem::swap` on a slice This PR modifies the existing suggestion by matching on `[ProjectionElem::Deref, ProjectionElem::Index(_)]` instead of just `[ProjectionElem::Index(_)]`, which caused us to miss many cases. Additionally, it adds a more specific, machine-applicable suggestion in the case we determine `mem::swap` was used to swap elements in a slice. Closes rust-lang#102269
- Loading branch information
Showing
4 changed files
with
125 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
#![allow(dead_code)] | ||
|
||
fn swap(arr: &mut [u32; 2]) { | ||
arr.swap(1, 0); | ||
//~^ ERROR cannot borrow `arr[_]` as mutable more than once at a time | ||
} | ||
|
||
fn main() {} |
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,9 @@ | ||
// run-rustfix | ||
#![allow(dead_code)] | ||
|
||
fn swap(arr: &mut [u32; 2]) { | ||
std::mem::swap(&mut arr[0], &mut arr[1]); | ||
//~^ ERROR cannot borrow `arr[_]` as mutable more than once at a time | ||
} | ||
|
||
fn main() {} |
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,17 @@ | ||
error[E0499]: cannot borrow `arr[_]` as mutable more than once at a time | ||
--> $DIR/suggest-slice-swap.rs:5:33 | ||
| | ||
LL | std::mem::swap(&mut arr[0], &mut arr[1]); | ||
| -------------- ----------- ^^^^^^^^^^^ second mutable borrow occurs here | ||
| | | | ||
| | first mutable borrow occurs here | ||
| first borrow later used by call | ||
| | ||
help: use `.swap()` to swap elements at the specified indices instead | ||
| | ||
LL | arr.swap(1, 0); | ||
| ~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0499`. |