Skip to content

Commit f995003

Browse files
committed
Fix subslice capture in closure
1 parent dd19135 commit f995003

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

compiler/rustc_hir_typeck/src/upvar.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1893,14 +1893,13 @@ fn restrict_capture_precision(
18931893

18941894
for (i, proj) in place.projections.iter().enumerate() {
18951895
match proj.kind {
1896-
ProjectionKind::Index => {
1897-
// Arrays are completely captured, so we drop Index projections
1896+
ProjectionKind::Index | ProjectionKind::Subslice => {
1897+
// Arrays are completely captured, so we drop Index and Subslice projections
18981898
truncate_place_to_len_and_update_capture_kind(&mut place, &mut curr_mode, i);
18991899
return (place, curr_mode);
19001900
}
19011901
ProjectionKind::Deref => {}
19021902
ProjectionKind::Field(..) => {} // ignore
1903-
ProjectionKind::Subslice => {} // We never capture this
19041903
}
19051904
}
19061905

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// regression test for #109298
2+
// edition: 2021
3+
4+
pub fn subslice_array(x: [u8; 3]) {
5+
let f = || {
6+
let [_x @ ..] = x;
7+
let [ref y, ref mut z @ ..] = x; //~ ERROR cannot borrow `x[..]` as mutable
8+
};
9+
10+
f(); //~ ERROR cannot borrow `f` as mutable
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0596]: cannot borrow `x[..]` as mutable, as `x` is not declared as mutable
2+
--> $DIR/array_subslice.rs:7:21
3+
|
4+
LL | pub fn subslice_array(x: [u8; 3]) {
5+
| - help: consider changing this to be mutable: `mut x`
6+
...
7+
LL | let [ref y, ref mut z @ ..] = x;
8+
| ^^^^^^^^^ cannot borrow as mutable
9+
10+
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
11+
--> $DIR/array_subslice.rs:10:5
12+
|
13+
LL | let [ref y, ref mut z @ ..] = x;
14+
| - calling `f` requires mutable binding due to mutable borrow of `x`
15+
...
16+
LL | f();
17+
| ^ cannot borrow as mutable
18+
|
19+
help: consider changing this to be mutable
20+
|
21+
LL | let mut f = || {
22+
| +++
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)