forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
235 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,62 @@ | ||
fn main() { | ||
fn foo() { | ||
let mut foo = [1, 2, 3, 4]; | ||
let a = &mut foo[2]; | ||
let b = &mut foo[3]; //~ ERROR cannot borrow `foo[_]` as mutable more than once at a time | ||
*a = 5; | ||
*b = 6; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn bar() { | ||
let mut foo = [1,2,3,4]; | ||
let a = &mut foo[..2]; | ||
let b = &mut foo[2..]; //~ ERROR cannot borrow `foo` as mutable more than once at a time | ||
a[0] = 5; | ||
b[0] = 6; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn baz() { | ||
let mut foo = [1,2,3,4]; | ||
let a = &foo[..2]; | ||
let b = &mut foo[2..]; //~ ERROR cannot borrow `foo` as mutable because it is also borrowed as immutable | ||
b[0] = 6; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn qux() { | ||
let mut foo = [1,2,3,4]; | ||
let a = &mut foo[..2]; | ||
let b = &foo[2..]; //~ ERROR cannot borrow `foo` as immutable because it is also borrowed as mutable | ||
a[0] = 5; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn bad() { | ||
let mut foo = [1,2,3,4]; | ||
let a = &foo[1]; | ||
let b = &mut foo[2]; //~ ERROR cannot borrow `foo[_]` as mutable because it is also borrowed as immutable | ||
*b = 6; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn bat() { | ||
let mut foo = [1,2,3,4]; | ||
let a = &mut foo[1]; | ||
let b = &foo[2]; //~ ERROR cannot borrow `foo[_]` as immutable because it is also borrowed as mutable | ||
*a = 5; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn ang() { | ||
let mut foo = [1,2,3,4]; | ||
let a = &mut foo[0..]; | ||
let b = &foo[0..]; //~ ERROR cannot borrow `foo` as immutable because it is also borrowed as mutable | ||
a[0] = 5; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn main() { | ||
foo(); | ||
bar(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters