OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Test switch statement. | 4 // Test switch statement. |
5 | 5 |
6 // Tests some switch-case statements blocks that should and should not | 6 // Tests some switch-case statements blocks that should and should not |
7 // cause static warnings. | 7 // cause static warnings. |
8 // This test is not testing runtime behavior, only static warnings. | 8 // This test is not testing runtime behavior, only static warnings. |
9 | 9 |
10 // None of the cases blocks should cause a warning. | 10 // None of the cases blocks should cause a warning. |
11 void testSwitch(int x) { | 11 void testSwitch(int x) { |
12 // Catch all control flow leaving the switch. | 12 // Catch all control flow leaving the switch. |
13 // Run switch in catch clause to check rethrow. | 13 // Run switch in catch clause to check rethrow. |
14 TRY: | 14 TRY: try { |
15 try { | |
16 throw x; | 15 throw x; |
17 } catch (x) { | 16 } catch (x) { |
18 // Add loop as break/continue target. | 17 // Add loop as break/continue target. |
19 LOOP: | 18 LOOP: do { |
20 do { | |
21 switch (x) { | 19 switch (x) { |
22 case 0: | 20 case 0: |
23 case 1: | 21 case 1: |
24 nop(x); | 22 nop(x); |
25 break; // Break switch. | 23 break; // Break switch. |
26 case 2: | 24 case 2: |
27 nop(x); | 25 nop(x); |
28 break LOOP; | 26 break LOOP; |
29 case 3: | 27 case 3: |
30 nop(x); | 28 nop(x); |
31 continue; // Continue loop. | 29 continue; // Continue loop. |
32 case 4: | 30 case 4: |
33 nop(x); | 31 nop(x); |
34 continue LOOP; | 32 continue LOOP; |
35 case 5: | 33 case 5: |
36 nop(x); | 34 nop(x); |
37 continue LAST; | 35 continue LAST; |
38 // Avoid warning for "return;"" and "return e;" in same function. | 36 // Avoid warning for "return;"" and "return e;" in same function. |
39 case 6: // //# retnon: ok | 37 case 6: // //# retnon: ok |
40 nop(x); // //# retnon: continued | 38 nop(x); // //# retnon: continued |
41 return; // //# retnon: continued | 39 return; // //# retnon: continued |
42 case 7: // //# retval: ok | 40 case 7: // //# retval: ok |
43 nop(x); // //# retval: continued | 41 nop(x); // //# retval: continued |
44 return x; // //# retval: continued | 42 return x; // //# retval: continued |
45 case 8: | 43 case 8: |
46 nop(x); | 44 nop(x); |
47 throw x; | 45 throw x; |
48 case 9: | 46 case 9: |
49 nop(x); | 47 nop(x); |
50 rethrow; | 48 rethrow; |
51 case 10: | 49 case 10: |
52 case 11: | 50 case 11: { |
53 { | 51 nop(x); |
54 nop(x); | 52 break; // Break switch. |
55 break; // Break switch. | 53 } |
56 } | 54 case 12: { |
57 case 12: | 55 nop(x); |
58 { | 56 break LOOP; |
59 nop(x); | 57 } |
60 break LOOP; | 58 case 13: { |
61 } | 59 nop(x); |
62 case 13: | 60 continue; // Continue loop. |
63 { | 61 } |
64 nop(x); | 62 case 14: { |
65 continue; // Continue loop. | 63 nop(x); |
66 } | 64 continue LOOP; |
67 case 14: | 65 } |
68 { | 66 case 15: { |
69 nop(x); | 67 nop(x); |
70 continue LOOP; | 68 continue LAST; |
71 } | 69 } |
72 case 15: | |
73 { | |
74 nop(x); | |
75 continue LAST; | |
76 } | |
77 case 16: { // //# retnon: continued | 70 case 16: { // //# retnon: continued |
78 nop(x); // //# retnon: continued | 71 nop(x); // //# retnon: continued |
79 return; // //# retnon: continued | 72 return; // //# retnon: continued |
80 } // //# retnon: continued | 73 } // //# retnon: continued |
81 case 17: { // //# retval: continued | 74 case 17: { // //# retval: continued |
82 nop(x); // //# retval: continued | 75 nop(x); // //# retval: continued |
83 return x; // //# retval: continued | 76 return x; // //# retval: continued |
84 } // //# retval: continued | 77 } // //# retval: continued |
85 case 18: | 78 case 18: { |
86 { | 79 nop(x); |
87 nop(x); | 80 throw x; |
88 throw x; | 81 } |
89 } | 82 case 19: { |
90 case 19: | 83 nop(x); |
91 { | 84 rethrow; |
92 nop(x); | 85 } |
93 rethrow; | |
94 } | |
95 LAST: | 86 LAST: |
96 case 20: | 87 case 20: { |
97 { | 88 nop(x); |
98 nop(x); | 89 // Fallthrough allowed on last statements. |
99 // Fallthrough allowed on last statements. | 90 } |
100 } | |
101 } | 91 } |
102 } while (false); | 92 } while (false); |
103 } finally { | 93 } finally { |
104 // Catch all control flow leaving the switch and ignore it. | 94 // Catch all control flow leaving the switch and ignore it. |
105 // Use break instead of return to avoid warning for `return` and `return e` | 95 // Use break instead of return to avoid warning for `return` and `return e` |
106 // in same function. | 96 // in same function. |
107 break TRY; | 97 break TRY; |
108 } | 98 } |
109 } | 99 } |
110 | 100 |
111 // All these switch cases should cause warnings. | 101 // All these switch cases should cause warnings. |
112 void testSwitchWarn(x) { | 102 void testSwitchWarn(x) { |
113 // Catch all control flow from the switch and ignore it. | 103 // Catch all control flow from the switch and ignore it. |
114 TRY: | 104 TRY: try { |
115 try { | |
116 throw 0; | 105 throw 0; |
117 } catch (e) { | 106 } catch (e) { |
118 // Wrap in loop as target for continue/break. | 107 // Wrap in loop as target for continue/break. |
119 LOOP: | 108 LOOP: do { |
120 do { | |
121 switch (x) { | 109 switch (x) { |
122 case 0: // //# 01: static type warning | 110 case 0: // //# 01: static type warning |
123 case 1: { // //# 01: continued | 111 case 1: { // //# 01: continued |
124 { // //# 01: continued | 112 { // //# 01: continued |
125 nop(x); // //# 01: continued | 113 nop(x); // //# 01: continued |
126 break; // Break switch. // //# 01: continued | 114 break; // Break switch. // //# 01: continued |
127 } // //# 01: continued | 115 } // //# 01: continued |
128 } // //# 01: continued | 116 } // //# 01: continued |
129 case 2: { // //# 02: static type warning | 117 case 2: { // //# 02: static type warning |
130 { // //# 02: continued | 118 { // //# 02: continued |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 case 14: // //# 14: static type warning | 173 case 14: // //# 14: static type warning |
186 if (x) break; else break; // //# 14: continued | 174 if (x) break; else break; // //# 14: continued |
187 case 15: // //# 15: static type warning | 175 case 15: // //# 15: static type warning |
188 (throw 0); // //# 15: continued | 176 (throw 0); // //# 15: continued |
189 case 16: // //# 16: static type warning | 177 case 16: // //# 16: static type warning |
190 nop(x); // fallthrough. // //# 16: continued | 178 nop(x); // fallthrough. // //# 16: continued |
191 case 17: // //# 17: static type warning | 179 case 17: // //# 17: static type warning |
192 L: break; // //# 17: continued | 180 L: break; // //# 17: continued |
193 LAST: | 181 LAST: |
194 case 99: | 182 case 99: |
195 // Last case can't cause static warning. | 183 // Last case can't cause static warning. |
196 } | 184 } |
197 } while (false); | 185 } while (false); |
198 } finally { | 186 } finally { |
199 // Catch all control flow leaving the switch and ignore it. | 187 // Catch all control flow leaving the switch and ignore it. |
200 // Use break instead of return to avoid warning for `return` and `return e` | 188 // Use break instead of return to avoid warning for `return` and `return e` |
201 // in same function. | 189 // in same function. |
202 break TRY; | 190 break TRY; |
203 } | 191 } |
204 } | 192 } |
205 | 193 |
206 main() { | 194 main() { |
207 // Ensure that all the cases compile and run (even if they might throw). | 195 // Ensure that all the cases compile and run (even if they might throw). |
208 for (int i = 0; i <= 20; i++) { | 196 for (int i = 0; i <= 20; i++) { |
209 testSwitch(i); // Just make sure it runs. | 197 testSwitch(i); // Just make sure it runs. |
210 } | 198 } |
211 for (int i = 0; i <= 18; i++) { | 199 for (int i = 0; i <= 18; i++) { |
212 testSwitchWarn(i); | 200 testSwitchWarn(i); |
213 } | 201 } |
214 } | 202 } |
215 | 203 |
216 /// Don't make it obvious that a switch case isn't doing anything. | 204 /// Don't make it obvious that a switch case isn't doing anything. |
217 void nop(x) {} | 205 void nop(x) {} |
OLD | NEW |