Add reproduction for missing cast to array of interfaces
Bug: 188112948
Change-Id: I3f9decd9b030c9f334539eb2f6f8c757e374fed6
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/checkcast/CheckCastInterfaceArrayTest.java b/src/test/java/com/android/tools/r8/ir/optimize/checkcast/CheckCastInterfaceArrayTest.java
index a8ef7a1..4431c5a 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/checkcast/CheckCastInterfaceArrayTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/checkcast/CheckCastInterfaceArrayTest.java
@@ -15,7 +15,7 @@
@RunWith(Parameterized.class)
public class CheckCastInterfaceArrayTest extends TestBase {
- private static String EXPECTED = StringUtils.lines("A", "B");
+ private static final String EXPECTED = StringUtils.lines("A", "B");
private final TestParameters parameters;
@@ -30,18 +30,10 @@
@Test
public void testJvmAndD8() throws Exception {
- if (parameters.isCfRuntime()) {
- testForJvm()
- .addInnerClasses(CheckCastInterfaceArrayTest.class)
- .run(parameters.getRuntime(), TestClass.class)
- .assertSuccessWithOutput(EXPECTED);
- } else {
- testForD8()
- .addInnerClasses(CheckCastInterfaceArrayTest.class)
- .setMinApi(parameters.getApiLevel())
- .run(parameters.getRuntime(), TestClass.class)
- .assertSuccessWithOutput(EXPECTED);
- }
+ testForRuntime(parameters)
+ .addProgramClasses(I.class, A.class, B.class, TestClass.class)
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED);
}
@Test
@@ -58,6 +50,17 @@
.assertSuccessWithOutput(EXPECTED);
}
+ @Test
+ public void testJvmAndD8Throwing() throws Exception {
+ testForRuntime(parameters)
+ .addProgramClasses(I.class, A.class, B.class, TestClassError.class)
+ .run(parameters.getRuntime(), TestClassError.class)
+ .applyIf(
+ parameters.isDexRuntime(),
+ result -> result.assertFailureWithErrorThatThrows(VerifyError.class),
+ result -> result.assertSuccessWithOutput(EXPECTED));
+ }
+
interface I {
int id();
}
@@ -98,4 +101,21 @@
}
}
}
+
+ static class TestClassError {
+
+ public static I[] get(I kind) {
+ // Work around the ART bug by inserting an explicit check cast (fortunately javac keeps it).
+ return (kind.id() == A.id ? A.values() : B.values());
+ }
+
+ public static void main(String[] args) {
+ for (I i : get(A.A)) {
+ System.out.println(i);
+ }
+ for (I i : get(B.B)) {
+ System.out.println(i);
+ }
+ }
+ }
}
diff --git a/src/test/java/com/android/tools/r8/workaround/ExtendingInterfaceArrayCheckCastTest.java b/src/test/java/com/android/tools/r8/workaround/ExtendingInterfaceArrayCheckCastTest.java
new file mode 100644
index 0000000..eee79f0
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/workaround/ExtendingInterfaceArrayCheckCastTest.java
@@ -0,0 +1,78 @@
+// Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.workaround;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class ExtendingInterfaceArrayCheckCastTest extends TestBase {
+
+ private final TestParameters parameters;
+ private final String[] EXPECTED = new String[] {"Hello World!"};
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public ExtendingInterfaceArrayCheckCastTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addInnerClasses(getClass())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ public static class Utility {
+
+ public static <T extends Base<T> & I> void get(T enumType) {
+ // If using I[] as the local variable type, the compiler will automatically insert a checkcast
+ // and therefore circumvent b/188112948. Another option is to explicitly insert a checkcast.
+ I[] params = enumType.getParams();
+ tryGet(params);
+ }
+
+ // this will become void tryGet(I[] value) when compiled due to the extends.
+ public static <T extends I> void tryGet(T[] value) {
+ System.out.println("Hello World!");
+ }
+ }
+
+ public interface I {
+ void call();
+ }
+
+ public static class Base<T extends Base<T>> {
+
+ T[] getParams() {
+ return null;
+ }
+ }
+
+ public static class Foo extends Base<Foo> implements I {
+
+ @Override
+ public void call() {
+ System.out.println("I::Foo");
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ Utility.get(new Foo());
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/workaround/ExtendingInterfaceArrayMissingCheckCastTest.java b/src/test/java/com/android/tools/r8/workaround/ExtendingInterfaceArrayMissingCheckCastTest.java
new file mode 100644
index 0000000..971fac1
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/workaround/ExtendingInterfaceArrayMissingCheckCastTest.java
@@ -0,0 +1,82 @@
+// Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.workaround;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+// This is a reproduction of b/188112948.
+public class ExtendingInterfaceArrayMissingCheckCastTest extends TestBase {
+
+ private final TestParameters parameters;
+ private final String[] EXPECTED = new String[] {"Hello World!"};
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public ExtendingInterfaceArrayMissingCheckCastTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addInnerClasses(getClass())
+ .run(parameters.getRuntime(), Main.class)
+ .applyIf(
+ parameters.isDexRuntime(),
+ // TODO(b/188112948): This should not throw a verification error.
+ result -> result.assertFailureWithErrorThatThrows(VerifyError.class),
+ result -> result.assertSuccessWithOutputLines(EXPECTED));
+ }
+
+ public static class Utility {
+
+ public static <T extends Base<T> & I> void get(T enumType) {
+ // JDK 8 will add a checkcast [I such that the [Base array is cast to the correct type.
+ // JDK 9 and later will not add the checkcast.
+ tryGet(enumType.getParams());
+ }
+
+ // this will become void tryGet(I[] value) when compiled due to the extends.
+ public static <T extends I> void tryGet(T[] value) {
+ System.out.println("Hello World!");
+ }
+ }
+
+ public interface I {
+ void call();
+ }
+
+ public static class Base<T extends Base<T>> {
+
+ T[] getParams() {
+ return null;
+ }
+ }
+
+ public static class Foo extends Base<Foo> implements I {
+
+ @Override
+ public void call() {
+ System.out.println("I::Foo");
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ Utility.get(new Foo());
+ }
+ }
+}