diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-06-30 08:01:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-30 08:01:12 +0200 |
| commit | 6c22e0419f2307710204d1b6051cb3b8755fa9fc (patch) | |
| tree | d9106a9f55205431f219a8de0b9ec688be18a0ca | |
| parent | 016c306ce64560c4741ff7b5893b0e00cfa3d56f (diff) | |
| parent | 679c5be4057597bf6265663d740dbf14210c3291 (diff) | |
Rollup merge of #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 compile
| -rw-r--r-- | compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs | 7 | ||||
| -rw-r--r-- | tests/ui/suggestions/suggest-split-at-mut.stderr | 1 |
2 files changed, 5 insertions, 3 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index a068f2d6926..33b07aae325 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -982,7 +982,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { &msg_borrow, None, ); - self.suggest_split_at_mut_if_applicable( + self.suggest_slice_method_if_applicable( &mut err, place, issued_borrow.borrowed_place, @@ -1262,7 +1262,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ); } - fn suggest_split_at_mut_if_applicable( + fn suggest_slice_method_if_applicable( &self, err: &mut Diagnostic, place: Place<'tcx>, @@ -1274,7 +1274,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { err.help( "consider using `.split_at_mut(position)` or similar method to obtain \ two mutable non-overlapping sub-slices", - ); + ) + .help("consider using `.swap(index_1, index_2)` to swap elements at the specified indices"); } } diff --git a/tests/ui/suggestions/suggest-split-at-mut.stderr b/tests/ui/suggestions/suggest-split-at-mut.stderr index 330f012b2a9..bb185138383 100644 --- a/tests/ui/suggestions/suggest-split-at-mut.stderr +++ b/tests/ui/suggestions/suggest-split-at-mut.stderr @@ -9,6 +9,7 @@ LL | *a = 5; | ------ first borrow later used here | = help: consider using `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices + = help: consider using `.swap(index_1, index_2)` to swap elements at the specified indices error: aborting due to previous error |
