|
1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 8214031 |
|
27 * @summary Verify that definite assignment when true works (legal code) |
|
28 * @compile --enable-preview --source 12 DefiniteAssignment1.java |
|
29 * @run main/othervm --enable-preview DefiniteAssignment1 |
|
30 */ |
|
31 public class DefiniteAssignment1 { |
|
32 public static void main(String[] args) { |
|
33 int a = 0; |
|
34 boolean b = true; |
|
35 |
|
36 { |
|
37 int x; |
|
38 |
|
39 boolean t1 = (b && switch(a) { |
|
40 case 0: break (x = 1) == 1 || true; |
|
41 default: break false; |
|
42 }) && x == 1; //x is definitelly assigned here |
|
43 |
|
44 if (!t1) { |
|
45 throw new IllegalStateException("Unexpected result."); |
|
46 } |
|
47 } |
|
48 |
|
49 { |
|
50 int x; |
|
51 |
|
52 boolean t1 = (b && switch(a) { |
|
53 case 0: break (x = 1) == 1 || isTrue(); |
|
54 default: break false; |
|
55 }) && x == 1; //x is definitelly assigned here |
|
56 |
|
57 if (!t1) { |
|
58 throw new IllegalStateException("Unexpected result."); |
|
59 } |
|
60 } |
|
61 |
|
62 { |
|
63 int x; |
|
64 |
|
65 boolean t1a = (b && switch(a) { |
|
66 case 0: break (x = 1) == 1; |
|
67 default: break false; |
|
68 }) && x == 1; //x is definitelly assigned here |
|
69 |
|
70 if (!t1a) { |
|
71 throw new IllegalStateException("Unexpected result."); |
|
72 } |
|
73 } |
|
74 |
|
75 { |
|
76 int x; |
|
77 |
|
78 boolean t1b = (switch(a) { |
|
79 case 0: break (x = 1) == 1; |
|
80 default: break false; |
|
81 }) && x == 1; //x is definitelly assigned here |
|
82 |
|
83 if (!t1b) { |
|
84 throw new IllegalStateException("Unexpected result."); |
|
85 } |
|
86 } |
|
87 |
|
88 { |
|
89 int x; |
|
90 |
|
91 boolean t2 = !(b && switch(a) { |
|
92 case 0: break (x = 1) == 1 || true; |
|
93 default: break false; |
|
94 }) || x == 1; |
|
95 |
|
96 if (!t2) { |
|
97 throw new IllegalStateException("Unexpected result."); |
|
98 } |
|
99 } |
|
100 |
|
101 { |
|
102 int x; |
|
103 |
|
104 boolean t2 = !(b && switch(a) { |
|
105 case 0: break (x = 1) == 1 || isTrue(); |
|
106 default: break false; |
|
107 }) || x == 1; |
|
108 |
|
109 if (!t2) { |
|
110 throw new IllegalStateException("Unexpected result."); |
|
111 } |
|
112 } |
|
113 |
|
114 { |
|
115 int x; |
|
116 |
|
117 boolean t3 = !(switch(a) { |
|
118 case 0: break (x = 1) == 1 || true; |
|
119 default: break false; |
|
120 }) || x == 2; |
|
121 |
|
122 if (t3) { |
|
123 throw new IllegalStateException("Unexpected result."); |
|
124 } |
|
125 } |
|
126 |
|
127 { |
|
128 int x; |
|
129 |
|
130 boolean t3 = !(switch(a) { |
|
131 case 0: break (x = 1) == 1 || isTrue(); |
|
132 default: break false; |
|
133 }) || x == 2; |
|
134 |
|
135 if (t3) { |
|
136 throw new IllegalStateException("Unexpected result."); |
|
137 } |
|
138 } |
|
139 |
|
140 { |
|
141 int x; |
|
142 |
|
143 boolean t4 = (b && switch(a) { |
|
144 case 0: break (x = 1) == 1 || true; |
|
145 default: throw new IllegalStateException(); |
|
146 }) && x == 1; //x is definitelly assigned here |
|
147 |
|
148 if (!t4) { |
|
149 throw new IllegalStateException("Unexpected result."); |
|
150 } |
|
151 } |
|
152 |
|
153 |
|
154 { |
|
155 int x; |
|
156 |
|
157 boolean t4 = (b && switch(a) { |
|
158 case 0: break (x = 1) == 1 || isTrue(); |
|
159 default: throw new IllegalStateException(); |
|
160 }) && x == 1; //x is definitelly assigned here |
|
161 |
|
162 if (!t4) { |
|
163 throw new IllegalStateException("Unexpected result."); |
|
164 } |
|
165 } |
|
166 |
|
167 { |
|
168 int x; |
|
169 |
|
170 String s = "a"; |
|
171 |
|
172 boolean t5 = (switch(s) { |
|
173 case "a": break (x = 1) == 1 || true; |
|
174 default: break false; |
|
175 }) && x == 1; //x is definitelly assigned here |
|
176 |
|
177 if (!t5) { |
|
178 throw new IllegalStateException("Unexpected result."); |
|
179 } |
|
180 } |
|
181 |
|
182 { |
|
183 int x; |
|
184 |
|
185 String s = "a"; |
|
186 |
|
187 boolean t5 = (switch(s) { |
|
188 case "a": break (x = 1) == 1 || isTrue(); |
|
189 default: break false; |
|
190 }) && x == 1; //x is definitelly assigned here |
|
191 |
|
192 if (!t5) { |
|
193 throw new IllegalStateException("Unexpected result."); |
|
194 } |
|
195 } |
|
196 |
|
197 { |
|
198 int x; |
|
199 E e = E.B; |
|
200 |
|
201 boolean t6 = (switch(e) { |
|
202 case B: break (x = 1) == 1 || true; |
|
203 default: break false; |
|
204 }) && x == 1; //x is definitelly assigned here |
|
205 |
|
206 if (!t6) { |
|
207 throw new IllegalStateException("Unexpected result."); |
|
208 } |
|
209 } |
|
210 |
|
211 { |
|
212 int x; |
|
213 E e = E.B; |
|
214 |
|
215 boolean t6 = (switch(e) { |
|
216 case B: break (x = 1) == 1 || isTrue(); |
|
217 default: break false; |
|
218 }) && x == 1; //x is definitelly assigned here |
|
219 |
|
220 if (!t6) { |
|
221 throw new IllegalStateException("Unexpected result."); |
|
222 } |
|
223 } |
|
224 |
|
225 { |
|
226 int x; |
|
227 |
|
228 int t7 = new DefiniteAssignment1().id(switch(0) { |
|
229 default -> true; |
|
230 } && (x = 1) == 1 && x == 1 ? 2 : -1); |
|
231 |
|
232 if (t7 != 2) { |
|
233 throw new IllegalStateException("Unexpected result."); |
|
234 } |
|
235 } |
|
236 |
|
237 { |
|
238 int x; |
|
239 |
|
240 int t7 = new DefiniteAssignment1().id(switch(0) { |
|
241 default -> isTrue(); |
|
242 } && (x = 1) == 1 && x == 1 ? 2 : -1); |
|
243 |
|
244 if (t7 != 2) { |
|
245 throw new IllegalStateException("Unexpected result."); |
|
246 } |
|
247 } |
|
248 |
|
249 { |
|
250 int x; |
|
251 E e = E.B; |
|
252 |
|
253 boolean t8 = (switch(e) { |
|
254 case A: x = 1; break true; |
|
255 case B: break (x = 1) == 1 || true; |
|
256 default: break false; |
|
257 }) && x == 1; //x is definitelly assigned here |
|
258 |
|
259 if (!t8) { |
|
260 throw new IllegalStateException("Unexpected result."); |
|
261 } |
|
262 } |
|
263 |
|
264 { |
|
265 int x; |
|
266 E e = E.B; |
|
267 |
|
268 boolean t8 = (switch(e) { |
|
269 case A: x = 1; break isTrue(); |
|
270 case B: break (x = 1) == 1 || isTrue(); |
|
271 default: break false; |
|
272 }) && x == 1; //x is definitelly assigned here |
|
273 |
|
274 if (!t8) { |
|
275 throw new IllegalStateException("Unexpected result."); |
|
276 } |
|
277 } |
|
278 |
|
279 { |
|
280 int x; |
|
281 E e = E.A; |
|
282 |
|
283 boolean t9 = (switch(e) { |
|
284 case A: x = 1; break true; |
|
285 case B: break (x = 1) == 1 || true; |
|
286 default: break false; |
|
287 }) && x == 1; //x is definitelly assigned here |
|
288 |
|
289 if (!t9) { |
|
290 throw new IllegalStateException("Unexpected result."); |
|
291 } |
|
292 } |
|
293 |
|
294 { |
|
295 int x; |
|
296 E e = E.A; |
|
297 |
|
298 boolean t9 = (switch(e) { |
|
299 case A: x = 1; break isTrue(); |
|
300 case B: break (x = 1) == 1 || isTrue(); |
|
301 default: break false; |
|
302 }) && x == 1; //x is definitelly assigned here |
|
303 |
|
304 if (!t9) { |
|
305 throw new IllegalStateException("Unexpected result."); |
|
306 } |
|
307 } |
|
308 |
|
309 { |
|
310 int x; |
|
311 E e = E.C; |
|
312 |
|
313 boolean tA = (switch(e) { |
|
314 case A: x = 1; break true; |
|
315 case B: break (x = 1) == 1 || true; |
|
316 default: break false; |
|
317 }) && x == 1; //x is definitelly assigned here |
|
318 |
|
319 if (tA) { |
|
320 throw new IllegalStateException("Unexpected result."); |
|
321 } |
|
322 } |
|
323 |
|
324 { |
|
325 int x; |
|
326 E e = E.C; |
|
327 |
|
328 boolean tA = (switch(e) { |
|
329 case A: x = 1; break isTrue(); |
|
330 case B: break (x = 1) == 1 || isTrue(); |
|
331 default: break false; |
|
332 }) && x == 1; //x is definitelly assigned here |
|
333 |
|
334 if (tA) { |
|
335 throw new IllegalStateException("Unexpected result."); |
|
336 } |
|
337 } |
|
338 |
|
339 { |
|
340 final int x; |
|
341 E e = E.C; |
|
342 |
|
343 boolean tA = (switch(e) { |
|
344 case A: x = 1; break true; |
|
345 case B: break (x = 2) == 2 || true; |
|
346 default: break false; |
|
347 }) || (x = 3) == 3; //x is definitelly unassigned here |
|
348 |
|
349 if (x != 3) { |
|
350 throw new IllegalStateException("Unexpected result."); |
|
351 } |
|
352 } |
|
353 |
|
354 { |
|
355 int x; |
|
356 E e = E.A; |
|
357 |
|
358 boolean tA = (switch(e) { |
|
359 case A: break isTrue() && (x = 1) == 1; |
|
360 case B: break (x = 1) == 1 || isTrue(); |
|
361 default: break false; |
|
362 }) && x == 1; //x is definitelly assigned here |
|
363 |
|
364 if (!tA) { |
|
365 throw new IllegalStateException("Unexpected result."); |
|
366 } |
|
367 } |
|
368 |
|
369 { |
|
370 int x; |
|
371 E e = E.A; |
|
372 |
|
373 boolean tA = (switch(e) { |
|
374 case A: break isTrue() && e != E.C ? (x = 1) == 1 && e != E.B : false; |
|
375 case B: break (x = 1) == 1 || true; |
|
376 default: break false; |
|
377 }) && x == 1; //x is definitelly assigned here |
|
378 |
|
379 if (!tA) { |
|
380 throw new IllegalStateException("Unexpected result."); |
|
381 } |
|
382 } |
|
383 |
|
384 { |
|
385 int x; |
|
386 E e = E.A; |
|
387 |
|
388 boolean tA = (switch(e) { |
|
389 case A: break isTrue() && e != E.C ? (x = 1) == 1 && e != E.B : false; |
|
390 case B: break (x = 1) == 1 || isTrue(); |
|
391 default: break false; |
|
392 }) && x == 1; //x is definitelly assigned here |
|
393 |
|
394 if (!tA) { |
|
395 throw new IllegalStateException("Unexpected result."); |
|
396 } |
|
397 } |
|
398 } |
|
399 |
|
400 private int id(int v) { |
|
401 return v; |
|
402 } |
|
403 |
|
404 private static boolean isTrue() { |
|
405 return true; |
|
406 } |
|
407 |
|
408 enum E { |
|
409 A, B, C; |
|
410 } |
|
411 } |