-
Notifications
You must be signed in to change notification settings - Fork 12.7k
/
borrowck-closures-slice-patterns-ok.rs
118 lines (107 loc) · 2.13 KB
/
borrowck-closures-slice-patterns-ok.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Check that closure captures for slice patterns are inferred correctly
#![feature(slice_patterns)]
#![allow(unused_variables)]
// run-pass
fn arr_by_ref(x: [String; 3]) {
let r = &x;
let f = || {
let [ref y, ref z @ ..] = x;
};
f();
f();
// Ensure `x` was borrowed
drop(r);
// Ensure that `x` wasn't moved from.
drop(x);
}
fn arr_by_mut(mut x: [String; 3]) {
let mut f = || {
let [ref mut y, ref mut z @ ..] = x;
};
f();
f();
drop(x);
}
fn arr_by_move(x: [String; 3]) {
let f = || {
let [y, z @ ..] = x;
};
f();
}
fn arr_ref_by_ref(x: &[String; 3]) {
let r = &x;
let f = || {
let [ref y, ref z @ ..] = *x;
};
let g = || {
let [y, z @ ..] = x;
};
f();
g();
f();
g();
drop(r);
drop(x);
}
fn arr_ref_by_mut(x: &mut [String; 3]) {
let mut f = || {
let [ref mut y, ref mut z @ ..] = *x;
};
f();
f();
let mut g = || {
let [y, z @ ..] = x;
// Ensure binding mode was chosen correctly:
std::mem::swap(y, &mut z[0]);
};
g();
g();
drop(x);
}
fn arr_box_by_move(x: Box<[String; 3]>) {
let f = || {
let [y, z @ ..] = *x;
};
f();
}
fn slice_by_ref(x: &[String]) {
let r = &x;
let f = || {
if let [ref y, ref z @ ..] = *x {}
};
let g = || {
if let [y, z @ ..] = x {}
};
f();
g();
f();
g();
drop(r);
drop(x);
}
fn slice_by_mut(x: &mut [String]) {
let mut f = || {
if let [ref mut y, ref mut z @ ..] = *x {}
};
f();
f();
let mut g = || {
if let [y, z @ ..] = x {
// Ensure binding mode was chosen correctly:
std::mem::swap(y, &mut z[0]);
}
};
g();
g();
drop(x);
}
fn main() {
arr_by_ref(Default::default());
arr_by_mut(Default::default());
arr_by_move(Default::default());
arr_ref_by_ref(&Default::default());
arr_ref_by_mut(&mut Default::default());
arr_box_by_move(Default::default());
slice_by_ref(&<[_; 3]>::default());
slice_by_mut(&mut <[_; 3]>::default());
}