OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // Test switch statement. |
| 5 |
| 6 // Tests some switch-case statements blocks that should and should not |
| 7 // cause static warnings. |
| 8 // This test is not testing runtime behavior, only static warnings. |
| 9 |
| 10 // None of the cases blocks should cause a warning. |
| 11 void testSwitch(int x) { |
| 12 // Catch all control flow leaving the switch. |
| 13 // Run switch in catch clause to check rethrow. |
| 14 try { |
| 15 throw x; |
| 16 } catch (x) { |
| 17 // Add loop as break/continue target. |
| 18 LOOP: do { |
| 19 switch (x) { |
| 20 case 0: |
| 21 case 1: |
| 22 nop(x); |
| 23 break; // Break switch. |
| 24 case 2: |
| 25 nop(x); |
| 26 break LOOP; |
| 27 case 3: |
| 28 nop(x); |
| 29 continue; // Continue loop. |
| 30 case 4: |
| 31 nop(x); |
| 32 continue LOOP; |
| 33 case 5: |
| 34 nop(x); |
| 35 continue LAST; |
| 36 // Avoid warning for "return;"" and "return e;" in same function. |
| 37 case 6: /// retnon: ok |
| 38 nop(x); /// retnon: continued |
| 39 return; /// retnon: continued |
| 40 case 7: /// retval: ok |
| 41 nop(x); /// retval: continued |
| 42 return x; /// retval: continued |
| 43 case 8: |
| 44 nop(x); |
| 45 throw x; |
| 46 case 9: |
| 47 nop(x); |
| 48 rethrow; |
| 49 case 10: |
| 50 case 11: { |
| 51 nop(x); |
| 52 break; // Break switch. |
| 53 } |
| 54 case 12: { |
| 55 nop(x); |
| 56 break LOOP; |
| 57 } |
| 58 case 13: { |
| 59 nop(x); |
| 60 continue; // Continue loop. |
| 61 } |
| 62 case 14: { |
| 63 nop(x); |
| 64 continue LOOP; |
| 65 } |
| 66 case 15: { |
| 67 nop(x); |
| 68 continue LAST; |
| 69 } |
| 70 case 16: { /// retnon: continued |
| 71 nop(x); /// retnon: continued |
| 72 return; /// retnon: continued |
| 73 } /// retnon: continued |
| 74 case 17: { /// retval: continued |
| 75 nop(x); /// retval: continued |
| 76 return x; /// retval: continued |
| 77 } /// retval: continued |
| 78 case 18: { |
| 79 nop(x); |
| 80 throw x; |
| 81 } |
| 82 case 19: { |
| 83 nop(x); |
| 84 rethrow; |
| 85 } |
| 86 LAST: |
| 87 case 20: { |
| 88 nop(x); |
| 89 // Fallthrough allowed on last statements. |
| 90 } |
| 91 } |
| 92 } while (false); |
| 93 } finally { |
| 94 // Catch all control flow leaving the switch and ignore it. |
| 95 return; |
| 96 } |
| 97 } |
| 98 |
| 99 // All these switch cases should cause warnings. |
| 100 void testSwitchWarn(x) { |
| 101 // Catch all control flow from the switch and ignore it. |
| 102 try { |
| 103 throw 0; |
| 104 } catch (e) { |
| 105 // Wrap in loop as target for continue/break. |
| 106 LOOP: do { |
| 107 switch (x) { |
| 108 case 0: /// 01: static type warning |
| 109 case 1: { /// 01: continued |
| 110 { /// 01: continued |
| 111 nop(x); /// 01: continued |
| 112 break; // Break switch. /// 01: continued |
| 113 } /// 01: continued |
| 114 } /// 01: continued |
| 115 case 2: { /// 02: static type warning |
| 116 { /// 02: continued |
| 117 nop(x); /// 02: continued |
| 118 break LOOP; /// 02: continued |
| 119 } /// 02: continued |
| 120 } /// 02: continued |
| 121 case 3: { /// 03: static type warning |
| 122 { /// 03: continued |
| 123 nop(x); /// 03: continued |
| 124 continue; // Continue loop. /// 03: continued |
| 125 } /// 03: continued |
| 126 } /// 03: continued |
| 127 case 4: { /// 04: static type warning |
| 128 { /// 04: continued |
| 129 nop(x); /// 04: continued |
| 130 continue LOOP; /// 04: continued |
| 131 } /// 04: continued |
| 132 } /// 04: continued |
| 133 case 5: { /// 05: static type warning |
| 134 { /// 05: continued |
| 135 nop(x); /// 05: continued |
| 136 continue LAST; /// 05: continued |
| 137 } /// 05: continued |
| 138 } /// 05: continued |
| 139 case 6: { /// 06: static type warning |
| 140 { /// 06: continued |
| 141 nop(x); /// 06: continued |
| 142 return; /// 06: continued |
| 143 } /// 06: continued |
| 144 } /// 06: continued |
| 145 case 7: { /// 07: static type warning |
| 146 { /// 07: continued |
| 147 nop(x); /// 07: continued |
| 148 return x; /// 07: continued |
| 149 } /// 07: continued |
| 150 } /// 07: continued |
| 151 case 8: { /// 08: static type warning |
| 152 { /// 08: continued |
| 153 nop(x); /// 08: continued |
| 154 throw x; /// 08: continued |
| 155 } /// 08: continued |
| 156 } /// 08: continued |
| 157 case 9: { /// 09: static type warning |
| 158 { /// 09: continued |
| 159 nop(x); /// 09: continued |
| 160 rethrow; /// 09: continued |
| 161 } /// 09: continued |
| 162 } /// 09: continued |
| 163 case 10: /// 10: static type warning |
| 164 while (true) break; /// 10: continued |
| 165 case 11: /// 11: static type warning |
| 166 do break; while (true); /// 11: continued |
| 167 case 12: /// 12: static type warning |
| 168 for (;;) break; /// 12: continued |
| 169 case 13: /// 13: static type warning |
| 170 for (var _ in []) break; /// 13: continued |
| 171 case 14: /// 14: static type warning |
| 172 if (x) break; else break; /// 14: continued |
| 173 case 15: /// 15: static type warning |
| 174 (throw 0); /// 15: continued |
| 175 case 16: /// 16: static type warning |
| 176 nop(x); // fallthrough. /// 16: continued |
| 177 case 17: /// 17: static type warning |
| 178 L: break; /// 17: continued |
| 179 LAST: |
| 180 case 99: |
| 181 // Last case can't cause static warning. |
| 182 } |
| 183 } while (false); |
| 184 } finally { |
| 185 // Catch all control flow leaving the switch and ignore it. |
| 186 return; |
| 187 } |
| 188 } |
| 189 |
| 190 main() { |
| 191 // Ensure that all the cases compile and run (even if they might throw). |
| 192 for (int i = 0; i <= 20; i++) { |
| 193 testSwitch(i); // Just make sure it runs. |
| 194 } |
| 195 for (int i = 0; i <= 18; i++) { |
| 196 testSwitchWarn(i); |
| 197 } |
| 198 } |
| 199 |
| 200 /// Don't make it obvious that a switch case isn't doing anything. |
| 201 void nop(x) {} |
OLD | NEW |