|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
23 | 23 |
|
24 | 24 | /*
|
25 | 25 | * @test
|
26 |
| - * @bug 8214031 8214114 8236546 |
| 26 | + * @bug 8214031 8214114 8236546 8353565 |
27 | 27 | * @summary Verify switch expressions embedded in various statements work properly.
|
28 | 28 | * @compile ExpressionSwitchEmbedding.java
|
29 | 29 | * @run main ExpressionSwitchEmbedding
|
|
32 | 32 | public class ExpressionSwitchEmbedding {
|
33 | 33 | public static void main(String... args) {
|
34 | 34 | new ExpressionSwitchEmbedding().run();
|
| 35 | + new ExpressionSwitchEmbedding().runStackMapMergingTest(); |
35 | 36 | }
|
36 | 37 |
|
37 | 38 | private void run() {
|
@@ -330,6 +331,110 @@ yield switch (i) {
|
330 | 331 | }
|
331 | 332 | }
|
332 | 333 |
|
| 334 | + private void runStackMapMergingTest() { |
| 335 | + //JDK-8353565: verify that two types neither of which is a subtype of the other |
| 336 | + //can be merged while computing StackMaps. |
| 337 | + if (!(computeTypeAtMergePoint1(E.A, E.A) instanceof Impl1a)) { |
| 338 | + throw new AssertionError("Unexpected result"); |
| 339 | + } |
| 340 | + if (runMethodForInterfaceTypeAtMergePoint1(E.A, E.A) != 1) { |
| 341 | + throw new AssertionError("Unexpected result"); |
| 342 | + } |
| 343 | + if (runMethodForInterfaceTypeAtMergePoint2(E.A, E.A) != 2) { |
| 344 | + throw new AssertionError("Unexpected result"); |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 348 | + private Root computeTypeAtMergePoint1(E e1, E e2) { |
| 349 | + return (Root) switch (e1) { |
| 350 | + case A -> switch (e2) { |
| 351 | + case A -> new Impl1a(); |
| 352 | + case B -> new Impl1b(); |
| 353 | + case C -> new Impl1c(); |
| 354 | + }; |
| 355 | + case B -> switch (e2) { |
| 356 | + case A -> new Impl2a(); |
| 357 | + case B -> new Impl2b(); |
| 358 | + case C -> new Impl2c(); |
| 359 | + }; |
| 360 | + case C -> switch (e2) { |
| 361 | + case A -> new Impl3a(); |
| 362 | + case B -> new Impl3b(); |
| 363 | + case C -> new Impl3c(); |
| 364 | + }; |
| 365 | + }; |
| 366 | + } |
| 367 | + |
| 368 | + private int runMethodForInterfaceTypeAtMergePoint1(E e1, E e2) { |
| 369 | + return (switch (e1) { |
| 370 | + case A -> switch (e2) { |
| 371 | + case A -> new C1(); |
| 372 | + case B -> new C1(); |
| 373 | + case C -> new C1(); |
| 374 | + }; |
| 375 | + case B -> switch (e2) { |
| 376 | + case A -> new C2(); |
| 377 | + case B -> new C2(); |
| 378 | + case C -> new C2(); |
| 379 | + }; |
| 380 | + case C -> switch (e2) { |
| 381 | + case A -> new C3(); |
| 382 | + case B -> new C3(); |
| 383 | + case C -> new C3(); |
| 384 | + }; |
| 385 | + }).test1(); |
| 386 | + } |
| 387 | + |
| 388 | + private int runMethodForInterfaceTypeAtMergePoint2(E e1, E e2) { |
| 389 | + return (switch (e1) { |
| 390 | + case A -> switch (e2) { |
| 391 | + case A -> new C1(); |
| 392 | + case B -> new C1(); |
| 393 | + case C -> new C1(); |
| 394 | + }; |
| 395 | + case B -> switch (e2) { |
| 396 | + case A -> new C2(); |
| 397 | + case B -> new C2(); |
| 398 | + case C -> new C2(); |
| 399 | + }; |
| 400 | + case C -> switch (e2) { |
| 401 | + case A -> new C3(); |
| 402 | + case B -> new C3(); |
| 403 | + case C -> new C3(); |
| 404 | + }; |
| 405 | + }).test2(); |
| 406 | + } |
| 407 | + |
| 408 | + private static class Root {} |
| 409 | + private static class Base1 extends Root {} |
| 410 | + private static class Impl1a extends Base1 {} |
| 411 | + private static class Impl1b extends Base1 {} |
| 412 | + private static class Impl1c extends Base1 {} |
| 413 | + private static class Base2 extends Root {} |
| 414 | + private static class Impl2a extends Base2 {} |
| 415 | + private static class Impl2b extends Base2 {} |
| 416 | + private static class Impl2c extends Base2 {} |
| 417 | + private static class Base3 extends Root {} |
| 418 | + private static class Impl3a extends Base3 {} |
| 419 | + private static class Impl3b extends Base3 {} |
| 420 | + private static class Impl3c extends Base3 {} |
| 421 | + |
| 422 | + private static interface RootInterface1 { |
| 423 | + public default int test1() { |
| 424 | + return 1; |
| 425 | + } |
| 426 | + } |
| 427 | + private static interface RootInterface2 { |
| 428 | + public default int test2() { |
| 429 | + return 2; |
| 430 | + } |
| 431 | + } |
| 432 | + private static class C1 implements RootInterface1, RootInterface2 {} |
| 433 | + private static class C2 implements RootInterface1, RootInterface2 {} |
| 434 | + private static class C3 implements RootInterface1, RootInterface2 {} |
| 435 | + |
| 436 | + enum E {A, B, C;} |
| 437 | + |
333 | 438 | private void throwException() {
|
334 | 439 | throw new RuntimeException();
|
335 | 440 | }
|
|
0 commit comments