-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect borrow error involving sub-slices and suggest split_at_mut
#124313
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.