]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-closures-slice-patterns.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-closures-slice-patterns.rs
1 // Check that closure captures for slice patterns are inferred correctly
2
3 fn arr_by_ref(mut x: [String; 3]) {
4     let f = || {
5         let [ref y, ref z @ ..] = x;
6     };
7     let r = &mut x;
8     //~^ ERROR cannot borrow
9     f();
10 }
11
12 fn arr_by_mut(mut x: [String; 3]) {
13     let mut f = || {
14         let [ref mut y, ref mut z @ ..] = x;
15     };
16     let r = &x;
17     //~^ ERROR cannot borrow
18     f();
19 }
20
21 fn arr_by_move(x: [String; 3]) {
22     let f = || {
23         let [y, z @ ..] = x;
24     };
25     &x;
26     //~^ ERROR borrow of moved value
27 }
28
29 fn arr_ref_by_ref(x: &mut [String; 3]) {
30     let f = || {
31         let [ref y, ref z @ ..] = *x;
32     };
33     let r = &mut *x;
34     //~^ ERROR cannot borrow
35     f();
36 }
37
38 fn arr_ref_by_uniq(x: &mut [String; 3]) {
39     let mut f = || {
40         let [ref mut y, ref mut z @ ..] = *x;
41     };
42     let r = &x;
43     //~^ ERROR cannot borrow
44     f();
45 }
46
47 fn arr_box_by_move(x: Box<[String; 3]>) {
48     let f = || {
49         let [y, z @ ..] = *x;
50     };
51     &x;
52     //~^ ERROR borrow of moved value
53 }
54
55 fn slice_by_ref(x: &mut [String]) {
56     let f = || {
57         if let [ref y, ref z @ ..] = *x {}
58     };
59     let r = &mut *x;
60     //~^ ERROR cannot borrow
61     f();
62 }
63
64 fn slice_by_uniq(x: &mut [String]) {
65     let mut f = || {
66         if let [ref mut y, ref mut z @ ..] = *x {}
67     };
68     let r = &x;
69     //~^ ERROR cannot borrow
70     f();
71 }
72
73 fn main() {
74     arr_by_ref(Default::default());
75     arr_by_mut(Default::default());
76     arr_by_move(Default::default());
77     arr_ref_by_ref(&mut Default::default());
78     arr_ref_by_uniq(&mut Default::default());
79     arr_box_by_move(Default::default());
80     slice_by_ref(&mut <[_; 3]>::default());
81     slice_by_uniq(&mut <[_; 3]>::default());
82 }