-
Notifications
You must be signed in to change notification settings - Fork 13k
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 #109680 - clubby789:array-subslice-2229, r=davidtwco
Fix subslice capture in closure Fixes #109298 by refining captures in the same way for Subslices and Indexes. The comment `// we never capture this` seems to have been inaccurate, as changing it to an assert causes many test failures `@rustbot` label +A-closures
- Loading branch information
Showing
3 changed files
with
41 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// regression test for #109298 | ||
// edition: 2021 | ||
|
||
pub fn subslice_array(x: [u8; 3]) { | ||
let f = || { | ||
let [_x @ ..] = x; | ||
let [ref y, ref mut z @ ..] = x; //~ ERROR cannot borrow `x[..]` as mutable | ||
}; | ||
|
||
f(); //~ ERROR cannot borrow `f` as mutable | ||
} | ||
|
||
fn main() {} |
26 changes: 26 additions & 0 deletions
26
tests/ui/closures/2229_closure_analysis/array_subslice.stderr
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error[E0596]: cannot borrow `x[..]` as mutable, as `x` is not declared as mutable | ||
--> $DIR/array_subslice.rs:7:21 | ||
| | ||
LL | pub fn subslice_array(x: [u8; 3]) { | ||
| - help: consider changing this to be mutable: `mut x` | ||
... | ||
LL | let [ref y, ref mut z @ ..] = x; | ||
| ^^^^^^^^^ cannot borrow as mutable | ||
|
||
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable | ||
--> $DIR/array_subslice.rs:10:5 | ||
| | ||
LL | let [ref y, ref mut z @ ..] = x; | ||
| - calling `f` requires mutable binding due to mutable borrow of `x` | ||
... | ||
LL | f(); | ||
| ^ cannot borrow as mutable | ||
| | ||
help: consider changing this to be mutable | ||
| | ||
LL | let mut f = || { | ||
| +++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0596`. |