]> git.lizzy.rs Git - rust.git/blob - src/test/ui/for-loop-while/label_break_value.rs
Auto merge of #100989 - lcnr:implied-bounds-uwu, r=spastorino
[rust.git] / src / test / ui / for-loop-while / label_break_value.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(unused_assignments)]
4
5 // Test control flow to follow label_break_value semantics
6 fn label_break(a: bool, b: bool) -> u32 {
7     let mut v = 0;
8     'b: {
9         v = 1;
10         if a {
11             break 'b;
12         }
13         v = 2;
14         if b {
15             break 'b;
16         }
17         v = 3;
18     }
19     return v;
20 }
21
22 // Test that values can be returned
23 fn break_value(a: bool, b: bool) -> u32 {
24     let result = 'block: {
25         if a { break 'block 1; }
26         if b { break 'block 2; }
27         3
28     };
29     result
30 }
31
32 // Test nesting of labeled blocks
33 // here we only check that it compiles
34 fn label_break_nested() {
35     'b: {
36         println!("hi");
37         if false {
38             break 'b;
39         }
40         'c: {
41             if false {
42                 break 'b;
43             }
44             break 'c;
45         }
46         println!("hello");
47         if true {
48             break 'b;
49         }
50     }
51 }
52
53 // Tests for mixing labeled blocks with loop constructs
54 // This function should be the identity function
55 fn label_break_mixed(v: u32) -> u32 {
56     let mut r = 0;
57     'b: {
58         // Unlabeled break still works
59         // (only crossing boundaries is an error)
60         loop {
61             break;
62         }
63         if v == 0 {
64             break 'b;
65         }
66         // Labeled breaking an inner loop still works
67         'c: loop {
68             if r == 1 {
69                 break 'c;
70             }
71             r += 1;
72         }
73         assert_eq!(r, 1);
74         if v == 1 {
75             break 'b;
76         }
77         // Labeled breaking an outer loop still works
78         'd: loop {
79             {
80                 if v == r {
81                     break 'b;
82                 }
83                 if r == 5 {
84                     break 'd;
85                 }
86                 r += 1;
87             }
88         }
89         assert_eq!(r, 5);
90         assert!(v > r);
91         // Here we test return from inside a labeled block
92         return v;
93     }
94     r
95 }
96
97 fn label_break_match(c: u8, xe: u8, ye: i8) {
98     let mut x = 0;
99     let y = 'a: {
100         match c {
101             0 => break 'a 0,
102             v if { if v % 2 == 0 { break 'a 1; }; v % 3 == 0 } => { x += 1; },
103             v if { 'b: { break 'b v == 5; } } => { x = 41; },
104             _ => 'b: {
105                 break 'b ();
106             },
107         }
108         x += 1;
109         -1
110     };
111
112     assert_eq!(x, xe);
113     assert_eq!(y, ye);
114 }
115
116 #[allow(unused_labels)]
117 fn label_break_macro() {
118     macro_rules! mac1 {
119         ($target:lifetime, $val:expr) => {
120             break $target $val;
121         };
122     }
123     let x: u8 = 'a: {
124         'b: {
125             mac1!('b, 1);
126         };
127         0
128     };
129     assert_eq!(x, 0);
130     let x: u8 = 'a: {
131         'b: {
132             if true {
133                 mac1!('a, 1);
134             }
135         };
136         0
137     };
138     assert_eq!(x, 1);
139 }
140
141 pub fn main() {
142     assert_eq!(label_break(true, false), 1);
143     assert_eq!(label_break(false, true), 2);
144     assert_eq!(label_break(false, false), 3);
145
146     assert_eq!(break_value(true, false), 1);
147     assert_eq!(break_value(false, true), 2);
148     assert_eq!(break_value(false, false), 3);
149
150     assert_eq!(label_break_mixed(0), 0);
151     assert_eq!(label_break_mixed(1), 1);
152     assert_eq!(label_break_mixed(2), 2);
153     assert_eq!(label_break_mixed(3), 3);
154     assert_eq!(label_break_mixed(4), 4);
155     assert_eq!(label_break_mixed(5), 5);
156     assert_eq!(label_break_mixed(6), 6);
157
158     label_break_match(0, 0, 0);
159     label_break_match(1, 1, -1);
160     label_break_match(2, 0, 1);
161     label_break_match(3, 2, -1);
162     label_break_match(5, 42, -1);
163     label_break_match(7, 1, -1);
164
165     label_break_macro();
166 }