|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 | +/* |
| 26 | + * @test |
| 27 | + * @bug 8266615 |
| 28 | + * @summary C2 incorrectly folds subtype checks involving an interface array. |
| 29 | + * @run main/othervm -Xbatch |
| 30 | + * compiler.types.TestInterfaceArraySubtypeCheck |
| 31 | + */ |
| 32 | + |
| 33 | +package compiler.types; |
| 34 | + |
| 35 | +public class TestInterfaceArraySubtypeCheck { |
| 36 | + |
| 37 | + static interface MyInterface { } |
| 38 | + |
| 39 | + static class MyClassA { } |
| 40 | + |
| 41 | + static class MyClassB extends MyClassA implements MyInterface { } |
| 42 | + |
| 43 | + static MyInterface[] getMyInterfaceArray() { |
| 44 | + return new MyClassB[0]; |
| 45 | + } |
| 46 | + |
| 47 | + static MyInterface getMyInterface() { |
| 48 | + return new MyClassB(); |
| 49 | + } |
| 50 | + |
| 51 | + static MyClassA[] test1() { |
| 52 | + return (MyClassA[])getMyInterfaceArray(); |
| 53 | + } |
| 54 | + |
| 55 | + static void test2() { |
| 56 | + if (!(getMyInterfaceArray() instanceof MyClassA[])) { |
| 57 | + throw new RuntimeException("test2 failed"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static MyClassA test3() { |
| 62 | + return (MyClassA)getMyInterface(); |
| 63 | + } |
| 64 | + |
| 65 | + static void test4() { |
| 66 | + if (!(getMyInterface() instanceof MyClassA)) { |
| 67 | + throw new RuntimeException("test4 failed"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static void main(String[] args) { |
| 72 | + for (int i = 0; i < 50_000; ++i) { |
| 73 | + test1(); |
| 74 | + test2(); |
| 75 | + test3(); |
| 76 | + test4(); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments