Commit 6c22e04
authored
Rollup merge of rust-lang#111403 - y21:suggest-slice-swap, r=compiler-errors
suggest `slice::swap` for `mem::swap(&mut x[0], &mut x[1])` borrowck error
Recently saw someone ask why this code (example slightly modified):
```rs
fn main() {
let mut foo = [1, 2];
std::mem::swap(&mut foo[0], &mut foo[1]);
}
```
triggers this error and how to fix it:
```
error[E0499]: cannot borrow `foo[_]` as mutable more than once at a time
--> src/main.rs:4:33
|
4 | std::mem::swap(&mut foo[0], &mut foo[1]);
| -------------- ----------- ^^^^^^^^^^^ second mutable borrow occurs here
| | |
| | first mutable borrow occurs here
| first borrow later used by call
|
= help: consider using `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices
```
The current help message is nice and goes in the right direction, but I think we can do better for this specific instance and suggest `slice::swap`, which makes this compileFile tree
2 files changed
+5
-3
lines changed- compiler/rustc_borrowck/src/diagnostics
- tests/ui/suggestions
2 files changed
+5
-3
lines changedLines changed: 4 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
982 | 982 | | |
983 | 983 | | |
984 | 984 | | |
985 | | - | |
| 985 | + | |
986 | 986 | | |
987 | 987 | | |
988 | 988 | | |
| |||
1262 | 1262 | | |
1263 | 1263 | | |
1264 | 1264 | | |
1265 | | - | |
| 1265 | + | |
1266 | 1266 | | |
1267 | 1267 | | |
1268 | 1268 | | |
| |||
1274 | 1274 | | |
1275 | 1275 | | |
1276 | 1276 | | |
1277 | | - | |
| 1277 | + | |
| 1278 | + | |
1278 | 1279 | | |
1279 | 1280 | | |
1280 | 1281 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
0 commit comments