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