Skip to content

Commit

Permalink
Check equivalence of indices in more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Apr 25, 2024
1 parent ad6ae61 commit abdb64d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 7 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2116,7 +2116,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
None
}
}) else {
note_default_suggestion();
let hir::Node::Expr(parent) = tcx.parent_hir_node(index1.hir_id) else { return };
let hir::ExprKind::Index(_, idx1, _) = parent.kind else { return };
let hir::Node::Expr(parent) = tcx.parent_hir_node(index2.hir_id) else { return };
let hir::ExprKind::Index(_, idx2, _) = parent.kind else { return };
if !idx1.equals(idx2) {
err.help("use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices");
}
return;
};

Expand Down
9 changes: 3 additions & 6 deletions tests/ui/suggestions/suggest-split-at-mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ LL | let b = &mut foo[3];
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
= help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices

error[E0499]: cannot borrow `foo` as mutable more than once at a time
--> $DIR/suggest-split-at-mut.rs:13:18
Expand Down Expand Up @@ -59,8 +58,7 @@ LL | *b = 6;
LL | println!("{:?} {:?}", a, b);
| - immutable 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
= help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices

error[E0502]: cannot borrow `foo[_]` as immutable because it is also borrowed as mutable
--> $DIR/suggest-split-at-mut.rs:46:13
Expand All @@ -72,8 +70,7 @@ LL | let b = &foo[2];
LL | *a = 5;
| ------ mutable 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
= help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices

error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
--> $DIR/suggest-split-at-mut.rs:54:14
Expand Down

0 comments on commit abdb64d

Please sign in to comment.