File: PrimitivePatternsSwitch.java

package info (click to toggle)
openjdk-25 25.0.1%2B8-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (641 lines) | stat: -rw-r--r-- 19,809 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
 * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
/*
 * @test
 * @bug 8304487
 * @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview)
 * @enablePreview
 * @compile PrimitivePatternsSwitch.java
 * @run main/othervm PrimitivePatternsSwitch
 */
public class PrimitivePatternsSwitch {
    public static void main(String[] args) {
        assertEquals(1,  primitiveSwitch(42));
        assertEquals(2,  primitiveSwitch(123));
        assertEquals(1,  primitiveSwitchUnnamed(42));
        assertEquals(2,  primitiveSwitchUnnamed(123));
        assertEquals(42, primitiveSwitch2());
        assertEquals(42, primitiveSwitch3());
        assertEquals(1,  primitiveSwitch4(0.0f));
        assertEquals(2,  primitiveSwitch4(1.0f));
        assertEquals(1,  primitiveSwitchUnconditionallyExact(Byte.MAX_VALUE));
        assertEquals(42, exhaustive0());
        assertEquals(1,  exhaustive1WithDefault());
        assertEquals(2,  exhaustive2WithDefault());
        assertEquals(1,  exhaustive1());
        assertEquals(1,  exhaustive2());
        assertEquals(1,  exhaustive3());
        assertEquals(1,  exhaustive4());
        assertEquals(2,  exhaustive5());
        assertEquals(1,  exhaustive6());
        assertEquals(1,  exhaustive7(true));
        assertEquals(1,  exhaustive7s(true));
        assertEquals(1,  exhaustive8(true));
        assertEquals(1,  exhaustive9(true));
        assertEquals(1,  exhaustive9(false));
        assertEquals(1,  exhaustiveWithRecords1());
        assertEquals(1,  exhaustiveWithRecords2());
        assertEquals(1,  exhaustiveWithRecords4());
        assertEquals(1,  exhaustiveWithRecords5());
        assertEquals(1,  exhaustiveWithRecords6());
        assertEquals(2,  ensureProperSelectionWithRecords());
        assertEquals(1,  ensureProperSelectionWithRecords2());
        assertEquals(3,  ensureProperSelectionWithRecords3());
        assertEquals(42, switchAndDowncastFromObjectPrimitive());
        assertEquals(42, dominationBetweenBoxedAndPrimitive());
        assertEquals(2,  wideningAndUnboxing());
        assertEquals(2,  wideningAndUnboxingInRecord());
        assertEquals(2,  wideningAndInferredUnboxingInRecord());
        assertEquals(5f, switchOverBoxedFloat(0f));
        assertEquals(7f, switchOverBoxedFloat(1f));
        assertEquals(9f, switchOverBoxedFloat(2f));
        assertEquals(9f, switchOverBoxedFloat(2f));
        assertEquals(5f, switchOverPrimitiveDouble(0d));
        assertEquals(7f, switchOverPrimitiveDouble(1d));
        assertEquals(9f, switchOverPrimitiveDouble(2d));
        assertEquals(1, switchOverPrimitiveChar('a'));
        assertEquals(-1, switchOverPrimitiveChar('x'));
        assertTrue(switchOverBoxedBooleanWithUnconditional(Boolean.valueOf(true)));
        assertTrue(switchOverBoxedBooleanWithUnconditional(true));
        assertTrue(!switchOverBoxedBooleanWithUnconditional(false));
        assertEquals(1, switchOverPrimitiveBooleanWithDefault(true));
        assertEquals(2, switchOverPrimitiveBooleanWithDefault(false));
        assertEquals(1, switchOverPrimitiveBoolean(true));
        assertEquals(2, switchOverPrimitiveBoolean(false));
        assertEquals(1, switchOverPrimitiveFloat(0.0f/0.0f));
        assertEquals(2, switchOverPrimitiveFloat((float) Math.pow(0.0f/0.0f, 0)));
        assertEquals(3, switchOverPrimitiveFloat(0.0f));
        assertEquals(4, switchOverPrimitiveFloat(-0.0f));
        assertEquals(1, switchRedirectedExactnessMethods1('a'));
        assertEquals(-1, switchRedirectedExactnessMethods1('\u03A9'));
        assertEquals(1, switchRedirectedExactnessMethods2('\u03A9'));
        assertEquals(-1, switchRedirectedExactnessMethods2('\uFFFF'));
        assertEquals(1, switchLongAndUnconditional(32778L));
        assertEquals(2, switchLongAndUnconditional(42L));
        assertEquals(1, switchByte((byte) 128));
        assertEquals(2, switchByte((byte) 42));
        assertEquals(1, switchShort((short) 32778));
        assertEquals(2, switchShort((short) 42));
        assertEquals(1, switchInt(32778));
        assertEquals(2, switchInt(42));
        assertEquals(1, switchChar( '\u0010'));
        assertEquals(2, switchChar('a'));
        assertEquals(1, testIntInNonEnhancedSwitchStatement(1));
        assertEquals(0, testIntInNonEnhancedSwitchStatement(0));
        assertEquals(1, testFloatInEnhancedSwitchStatement(1.0f));
        assertEquals(0, testFloatInEnhancedSwitchStatement(0.0f));
        assertEquals(1, testDoubleInEnhancedSwitchStatement(1.0d));
        assertEquals(0, testDoubleInEnhancedSwitchStatement(0.0d));
        assertEquals(1, testLongInEnhancedSwitchStatement(1l));
        assertEquals(0, testLongInEnhancedSwitchStatement(0l));
        assertEquals(1, testBooleanInEnhancedSwitchStatement(true));
        assertEquals(0, testBooleanInEnhancedSwitchStatement(false));
        assertEquals(1, testByteWrapperToIntUnconditionallyExact());
        assertEquals(1, testIntegerWrapperToFloat());
        assertEquals(-1, testIntegerWrapperToFloatInexact());
        assertEquals(Character.MAX_VALUE, testUnboxingAndWideningCharacter1(Character.MAX_VALUE));
        assertEquals(Character.MAX_VALUE, testUnboxingAndWideningCharacter2(Character.MAX_VALUE));
        assertEquals(Character.MAX_VALUE, testUnboxingAndWideningCharacter3(Character.MAX_VALUE));
        assertEquals(Float.MAX_VALUE, testUnboxingAndWideningFloat(Float.MAX_VALUE));
        assertEquals(Float.MAX_VALUE, testUnboxingAndWideningFloatExplicitCast(Float.MAX_VALUE));
        assertEquals(42f, testUnboxingAndWideningLong(42L));
        assertEquals(2, testUnboxingAndWideningLong(Long.MAX_VALUE));
    }

    public static int primitiveSwitch(int i) {
        return switch (i) {
            case int j when j == 42-> 1;
            case int j -> 2;
        };
    }

    public static int primitiveSwitchUnnamed(int i) {
        return switch (i) {
            case int _ when i == 42-> 1;
            case int _ -> 2;
        };
    }

    public static int primitiveSwitch2() {
        Object o = Integer.valueOf(42);
        switch (o) {
            case int i: return i;
            default: break;
        }
        return -1;
    }

    public static int primitiveSwitch3() {
        int i = 42;
        switch (i) {
            case Integer ii: return ii;
        }
    }

    public static int primitiveSwitch4(float f) {
        return switch (f) {
            case 0.0f -> 1;
            case Float fi when fi == 1f -> 2;
            case Float fi -> 3;
        };
    }

    public static int primitiveSwitchUnconditionallyExact(byte c) {
        return switch (c) {
            case short _ -> 1;
        };
    }

    public static int exhaustive0() {
        Integer i = 42;
        switch (i) {
            case int j: return j;
        }
    }

    public static int exhaustive1WithDefault() {
        int i = 42;
        return switch (i) {
            case byte  b -> 1;
            default -> 2;
        };
    }

    public static int exhaustive2WithDefault() {
        int i = 30000;
        return switch (i) {
            case byte  b -> 1;
            case short s -> 2;
            default -> 3;
        };
    }

    public static int exhaustive1() {
        int i = 42;
        return switch (i) {
            case Integer p -> 1;
        };
    }

    public static int exhaustive2() {
        int i = 42;
        return switch (i) {
            case long d -> 1;
        };
    }

    public static int exhaustive3() {
        int i = 42;
        return switch (i) {
            case double d -> 1;
        };
    }

    public static int exhaustive4() {
        int i = 127;
        return switch (i) {
            case byte b -> 1;
            case double d -> 2;
        };
    }

    public static int exhaustive5() {
        int i = 127 + 1;
        return switch (i) {
            case byte b -> 1;
            case double d -> 2;
        };
    }

    public static int exhaustive6() {
        Integer i = Integer.valueOf(42);
        return switch (i) {
            case int p -> 1;
        };
    }

    public static int exhaustive7(Boolean b) {
        switch (b) {
            case true: return 1;
            case false: return 2;  // with reminder, null, OK
        }
    }

    public static int exhaustive7s(Boolean b) {
        return switch (b) {
            case true -> 1;
            case false -> 2;      // with reminder, null, OK
        };
    }

    public static int exhaustive8(Boolean b) {
        switch (b) {
            case boolean bb: return 1;
        }
    }

    public static int exhaustive9(boolean b) {
        switch (b) {
            case Boolean bb: return 1;
        }
    }

    public static int exhaustiveWithRecords1() {
        R_int r = new R_int(42);
        return switch (r) {
            // exhaustive, because Integer exhaustive at type int
            case R_int(Integer x) -> 1;
        };
    }

    public static int exhaustiveWithRecords2() {
        R_int r = new R_int(42);
        return switch (r) {
            // exhaustive, because double unconditional at int
            case R_int(double x) -> 1;
        };
    }

    public static int exhaustiveWithRecords4() {
        R_Integer r = new R_Integer(42);
        return switch (r) {
            // exhaustive, because R_Integer(int) exhaustive at type R_Integer(Integer), because int exhaustive at type Integer
            case R_Integer(int x) -> 1;
        };
    }

    public static int exhaustiveWithRecords5() {
        R_Integer r = new R_Integer(42);
        return switch (r) {
            // exhaustive, because double exhaustive at Integer
            case R_Integer(double x) -> 1;
        };
    }

    public static int exhaustiveWithRecords6() {
        R_int r = new R_int(42);
        return switch (r) {
            case R_int(byte x) -> 1;
            case R_int(int x) -> 2;
        };
    }

    public static int ensureProperSelectionWithRecords() {
        R_int r = new R_int(4242);
        return switch (r) {
            case R_int(byte x) -> 1;
            case R_int(int x) -> 2;
        };
    }

    public static int ensureProperSelectionWithRecords2() {
        R_double r = new R_double(42);
        switch (r) {
            case R_double(int i):
                return meth_int(i);
            case R_double(double x):
                return meth_double(x);
        }
    }

    public static int ensureProperSelectionWithRecords3() {
        R_int r = new R_int(4242);
        return switch (r) {
            case R_int(byte x) -> 1;
            case R_int(int x) when x == 236 -> 2;
            case R_int(int x) -> 3;
        };
    }

    public static int meth_int(int i) { return 1; }
    public static int meth_double(double d) { return 2;}

    public static int switchAndDowncastFromObjectPrimitive() {
        Object i = 42;
        return switch (i) {
            case Integer ib  -> ib;
            default -> -1;
        };
    }

    public static int dominationBetweenBoxedAndPrimitive() {
        Object i = 42;
        return switch (i) {
            case Integer ib  -> ib;
            case byte ip     -> ip;
            default -> -1;
        };
    }

    static int wideningAndUnboxing() {
        Number o = Integer.valueOf(42);
        return switch (o) {
            case byte b -> 1;
            case int i -> 2;
            case float f -> 3;
            default -> 4;
        };
    }

    static int wideningAndUnboxingInRecord() {
        Box<Number> box = new Box<>(Integer.valueOf(42));
        return switch (box) {
            case Box<Number>(byte b) -> 1;
            case Box<Number>(int i) -> 2;
            case Box<Number>(float f) -> 3;
            default -> 4;
        };
    }

    static int wideningAndInferredUnboxingInRecord() {
        Box<Number> box = new Box<>(Integer.valueOf(42));
        return switch (box) {
            case Box(byte b) -> 1;
            case Box(int i) -> 2;
            case Box(float f) -> 3;
            default -> 4;
        };
    }

    public static float switchOverBoxedFloat(Float f) {
        return switch (f) {
            case 0f -> 5f + 0f;
            case Float fi when fi == 1f -> 6f + fi;
            case Float fi -> 7f + fi;
        };
    }

    public static double switchOverPrimitiveDouble(Double d) {
        return switch (d) {
            case 0d -> 5d + 0d;
            case Double di when di == 1d -> 6d + di;
            case Double di -> 7d + di;
        };
    }

    public static boolean switchOverBoxedBooleanWithUnconditional(Boolean b) {
        return switch (b) {
            case true -> true;
            case Boolean bi -> bi;
        };
    }

    public static int switchOverPrimitiveBooleanWithDefault(boolean b) {
        return switch (b) {
            case true -> 1;
            default -> 2;
        };
    }

    public static int switchOverPrimitiveBoolean(boolean b) {
        return switch (b) {
            case true -> 1;
            case false -> 2;
        };
    }

    public static int switchOverPrimitiveChar(char c) {
        return switch (c) {
            case 'a' -> 1;
            default -> -1;
        };
    }

    public static final float NaNconstant = Float.NaN;
    public static int switchOverPrimitiveFloat(float f) {
        return switch (f) {
            case NaNconstant -> 1;
            case 1.0f -> 2;
            case 0.0f -> 3;
            case -0.0f -> 4;
            default -> -1;
        };
    }

    // tests that Exactness.char_byte is properly redirected to int_byte
    public static int switchRedirectedExactnessMethods1(char c) {
        return switch (c) {
            case byte _ -> 1;
            default -> -1;
        };
    }

    // tests that Exactness.char_short is properly redirected to int_short
    public static int switchRedirectedExactnessMethods2(char c) {
        return switch (c) {
            case short _ -> 1;
            default -> -1;
        };
    }

    // tests that Exactness.short_byte is properly redirected to int_byte
    public static int switchRedirectedExactnessMethods2(short c) {
        return switch (c) {
            case byte _ -> 1;
            default -> -1;
        };
    }

    public static int switchLongAndUnconditional(long l) {
        return switch (l) {
            case 32778L -> 1;
            case long c -> 2;
        };
    }

    public static int switchByte(byte b) {
        return switch (b) {
            case (byte)128 -> 1;
            case byte c -> 2;
        };
    }

    public static int switchShort(short s) {
        return switch (s) {
            case (short)32778 -> 1;
            case short c -> 2;
        };
    }

    public static int switchInt(int i) {
        return switch (i) {
            case 32778 -> 1;
            case int c -> 2;
        };
    }

    public static int switchChar(char c) {
        return switch (c) {
            case '\u0010' -> 1;
            case char cc -> 2;
        };
    }

    public static int testIntInNonEnhancedSwitchStatement(int v1) {
        int i = 0;
        switch (v1) {
            case 1:
                i = 1;
                break;
        }
        return i;
    }

    public static int testFloatInEnhancedSwitchStatement(float v1) {
        int i = 0;
        switch (v1) {
            case 1.0f:
                i = 1;
                break;
            default:
                i = 0;
        }
        return i;
    }

    public static int testDoubleInEnhancedSwitchStatement(double v1) {
        int i = 0;
        switch (v1) {
            case 1d:
                i = 1;
                break;
            default:
                i = 0;
        }
        return i;
    }

    public static int testLongInEnhancedSwitchStatement(long v1) {
        int i = 0;
        switch (v1) {
            case 1l:
                i = 1;
                break;
            default:
                i = 0;
        }
        return i;
    }

    public static int testBooleanInEnhancedSwitchStatement(boolean v1) {
        int i = 0;
        switch (v1) {
            case true:
                i = 1;
                break;
            default:
                i = 0;
        }
        return i;
    }

    public static int testByteWrapperToIntUnconditionallyExact() {
        Byte b = Byte.valueOf((byte) 42);
        return switch (b) {
            case int p -> 1;
        };
    }

    public static int testIntegerWrapperToFloat() {
        Integer i = Integer.valueOf(42);
        return switch (i) {
            case float p -> 1;
            default -> -1;
        };
    }

    public static int testIntegerWrapperToFloatInexact() {
        Integer i = Integer.valueOf(Integer.MAX_VALUE);
        return switch (i) {
            case float p -> 1;
            default -> -1;
        };
    }

    public static char testUnboxingAndWideningCharacter1(Character test) {
        return switch (test) {
            case char c -> c;
        };
    }

    public static int testUnboxingAndWideningCharacter2(Character test) {
        return switch (test) {
            case int c -> c;
        };
    }

    public static float testUnboxingAndWideningCharacter3(Character test) {
        return switch (test) {
            case float f -> f;
        };
    }
    public static float testUnboxingAndWideningLong(Long test) {
        return switch (test) {
            case float y -> y;
            default -> 2;
        };
    }

    public static double testUnboxingAndWideningFloat(Float test) {
        return switch (test) {
            case double y -> y;
            default -> 2;
        };
    }

    public static double testUnboxingAndWideningFloatExplicitCast(Object test) {
        return switch ((Float) test) {
            case double y -> y;
            default -> 2;
        };
    }

    record R_Integer(Integer x) {}
    record R_int(int x) {}
    record R_double(double x) {}
    record Box<N extends Number>(N num) {}

    static void assertEquals(int expected, int actual) {
        if (expected != actual) {
            throw new AssertionError("Expected: " + expected + ", actual: " + actual);
        }
    }

    static void assertEquals(float expected, float actual) {
        if (Float.compare(expected, actual) != 0) {
            throw new AssertionError("Expected: " + expected + ", but got: " + actual);
        }
    }

    static void assertEquals(double expected, double actual) {
        if (Double.compare(expected, actual) != 0) {
            throw new AssertionError("Expected: " + expected + ", but got: " + actual);
        }
    }

    static void assertTrue(boolean actual) {
        if (!actual) {
            throw new AssertionError("Expected: true, but got false");
        }
    }
}