Detect borrow error involving sub-slices and suggest split_at_mut#124313
Detect borrow error involving sub-slices and suggest split_at_mut#124313bors merged 6 commits intorust-lang:masterfrom
split_at_mut#124313Conversation
|
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
| | - first borrow later used here | ||
| | | ||
| = help: use `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices |
There was a problem hiding this comment.
This seem to be a regression. We should check that the indexing is actually done on a slice and not a user custom type.
There was a problem hiding this comment.
I can easily check whether we have a [] or Vec and be more accurate that split_at_mut will work. We can also look for all methods on any custom type that being indexed on that returns (&mut T, &mut T) or (&mut T, T). I was hoping not doing that as part of this PR, but I can.
If the comment instead means "they are doing p.use_mut(), where's the indexing, it happens earlier.
I am of the opinion that any type that supports indexing should have a method akin to split_at_mut. We should likely customize the error for types from the local crate, as those are much less likely to have an analogue.
fee1-dead
left a comment
There was a problem hiding this comment.
One small nit, otherwise looks good to me, thanks! r=me.
compiler/rustc_hir/src/hir.rs
Outdated
| /// Whether this and the `other` expression are the same for purposes of an indexing operation. | ||
| /// | ||
| /// This is only used for diagnostics to see if we have things like `foo[i]` where `foo` is | ||
| /// borrowed multiple times with `i`. | ||
| pub fn equals(&self, other: &Expr<'_>) -> bool { |
There was a problem hiding this comment.
The naming seems confusing as it doesn't specify that this is only in terms of indexing operations. Perhaps rename to equivalent_for_indexing?
|
☔ The latest upstream changes (presumably #124136) made this pull request unmergeable. Please resolve the merge conflicts. |
``` error[E0499]: cannot borrow `foo` as mutable more than once at a time --> $DIR/suggest-split-at-mut.rs:13:18 | LL | let a = &mut foo[..2]; | --- first mutable borrow occurs here LL | let b = &mut foo[2..]; | ^^^ second mutable borrow occurs here LL | a[0] = 5; | ---- first borrow later used here | = help: use `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices ``` Address most of rust-lang#58792. For follow up work, we should emit a structured suggestion for cases where we can identify the exact `let (a, b) = foo.split_at_mut(2);` call that is needed.
Emit suggestion when encountering ```rust let a = &mut foo[0]; let b = &foo[1]; a.use_mut(); ```
|
@bors r=fee1-dead |
…iaskrgr Rollup of 3 pull requests Successful merges: - rust-lang#124313 (Detect borrow error involving sub-slices and suggest `split_at_mut`) - rust-lang#124374 (Don't ICE when `codegen_select_candidate` returns ambiguity in new solver) - rust-lang#124380 (`Range` iteration specialization: remove trivial bounds) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#124313 - estebank:split-at-mut, r=fee1-dead Detect borrow error involving sub-slices and suggest `split_at_mut` ``` error[E0499]: cannot borrow `foo` as mutable more than once at a time --> $DIR/suggest-split-at-mut.rs:13:18 | LL | let a = &mut foo[..2]; | --- first mutable borrow occurs here LL | let b = &mut foo[2..]; | ^^^ second mutable borrow occurs here LL | a[0] = 5; | ---- first borrow later used here | = help: use `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices ``` Address most of rust-lang#58792. For follow up work, we should emit a structured suggestion for cases where we can identify the exact `let (a, b) = foo.split_at_mut(2);` call that is needed.
Address most of #58792.
For follow up work, we should emit a structured suggestion for cases where we can identify the exact
let (a, b) = foo.split_at_mut(2);call that is needed.