Skip to content
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

Merged
merged 6 commits into from
Apr 25, 2024

Conversation

estebank
Copy link
Contributor

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 #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.

@rustbot
Copy link
Collaborator

rustbot commented Apr 24, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 24, 2024
Comment on lines 19 to 21
| - first borrow later used here
|
= help: use `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@fee1-dead fee1-dead left a 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.

Comment on lines 1814 to 1818
/// 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 {
Copy link
Member

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?

@bors
Copy link
Contributor

bors commented Apr 25, 2024

☔ 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();
```
@estebank
Copy link
Contributor Author

@bors r=fee1-dead

@bors
Copy link
Contributor

bors commented Apr 25, 2024

📌 Commit 64a4cdc has been approved by fee1-dead

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 25, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 25, 2024
…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
@bors bors merged commit 60c825f into rust-lang:master Apr 25, 2024
10 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 25, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 25, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants