]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-slice-pattern-element-loan.rs
Simplify SaveHandler trait
[rust.git] / src / test / ui / borrowck / borrowck-slice-pattern-element-loan.rs
1 //compile-flags: -Z borrowck=mir
2
3 #![feature(slice_patterns)]
4
5 fn nop(_s: &[& i32]) {}
6 fn nop_subslice(_s: &[i32]) {}
7
8 fn const_index_ok(s: &mut [i32]) {
9     if let [ref first, ref second, _, ref fourth, ..] = *s {
10         if let [_, _, ref mut third, ..] = *s {
11             nop(&[first, second, third, fourth]);
12         }
13     }
14 }
15
16 fn const_index_err(s: &mut [i32]) {
17     if let [ref first, ref second, ..] = *s {
18         if let [_, ref mut  second2, ref mut third, ..] = *s { //~ERROR
19             nop(&[first, second, second2, third]);
20         }
21     }
22 }
23
24 fn const_index_from_end_ok(s: &mut [i32]) {
25     if let [.., ref fourth, ref third, _, ref first] = *s {
26         if let [.., ref mut second, _] = *s {
27             nop(&[first, second, third, fourth]);
28         }
29     }
30 }
31
32 fn const_index_from_end_err(s: &mut [i32]) {
33     if let [.., ref fourth, ref third, _, ref first] = *s {
34         if let [.., ref mut third2, _, _] = *s { //~ERROR
35             nop(&[first, third, third2, fourth]);
36         }
37     }
38 }
39
40 fn const_index_mixed(s: &mut [i32]) {
41     if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s {
42         if let [ref mut from_begin0, ..] = *s {
43             nop(&[from_begin0, from_end1, from_end3, from_end4]);
44         }
45         if let [_, ref mut from_begin1, ..] = *s { //~ERROR
46             nop(&[from_begin1, from_end1, from_end3, from_end4]);
47         }
48         if let [_, _, ref mut from_begin2, ..] = *s { //~ERROR
49             nop(&[from_begin2, from_end1, from_end3, from_end4]);
50         }
51         if let [_, _, _, ref mut from_begin3, ..] = *s { //~ERROR
52             nop(&[from_begin3, from_end1, from_end3, from_end4]);
53         }
54     }
55     if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s {
56         if let [.., ref mut from_end1] = *s {
57             nop(&[from_begin0, from_begin1, from_begin3, from_end1]);
58         }
59         if let [.., ref mut from_end2, _] = *s { //~ERROR
60             nop(&[from_begin0, from_begin1, from_begin3, from_end2]);
61         }
62         if let [.., ref mut from_end3, _,  _] = *s { //~ERROR
63             nop(&[from_begin0, from_begin1, from_begin3, from_end3]);
64         }
65         if let [.., ref mut from_end4, _, _, _] = *s { //~ERROR
66             nop(&[from_begin0, from_begin1, from_begin3, from_end4]);
67         }
68     }
69 }
70
71 fn const_index_and_subslice_ok(s: &mut [i32]) {
72     if let [ref first, ref second, ..] = *s {
73         if let [_, _, ref mut tail..] = *s {
74             nop(&[first, second]);
75             nop_subslice(tail);
76         }
77     }
78 }
79
80 fn const_index_and_subslice_err(s: &mut [i32]) {
81     if let [ref first, ref second, ..] = *s {
82         if let [_, ref mut tail..] = *s { //~ERROR
83             nop(&[first, second]);
84             nop_subslice(tail);
85         }
86     }
87 }
88
89 fn const_index_and_subslice_from_end_ok(s: &mut [i32]) {
90     if let [.., ref second, ref first] = *s {
91         if let [ref mut tail.., _, _] = *s {
92             nop(&[first, second]);
93             nop_subslice(tail);
94         }
95     }
96 }
97
98 fn const_index_and_subslice_from_end_err(s: &mut [i32]) {
99     if let [.., ref second, ref first] = *s {
100         if let [ref mut tail.., _] = *s { //~ERROR
101             nop(&[first, second]);
102             nop_subslice(tail);
103         }
104     }
105 }
106
107 fn subslices(s: &mut [i32]) {
108     if let [_, _, _, ref s1..] = *s {
109         if let [ref mut s2.., _, _, _] = *s { //~ERROR
110             nop_subslice(s1);
111             nop_subslice(s2);
112         }
113     }
114 }
115
116 fn main() {
117     let mut v = [1,2,3,4];
118     const_index_ok(&mut v);
119     const_index_err(&mut v);
120     const_index_from_end_ok(&mut v);
121     const_index_from_end_err(&mut v);
122     const_index_and_subslice_ok(&mut v);
123     const_index_and_subslice_err(&mut v);
124     const_index_and_subslice_from_end_ok(&mut v);
125     const_index_and_subslice_from_end_err(&mut v);
126     subslices(&mut v);
127 }