Don't remove casts to arrays of interface types

All Dalvik and Art runtimes can reject code with VerifyError if when
joining arrays of interface types.

Bug: b/223424356
Change-Id: If47782d76b706152f6e14b1238114dc89bc23119
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
index 868ef79..1f84bcf 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
@@ -1530,6 +1530,17 @@
       }
     }
 
+    // If casting to an array of an interface type elimination may lead to verification errors.
+    // See b/132420510 and b/223424356.
+    if (options.canHaveIncorrectJoinForArrayOfInterfacesBug()) {
+      if (castType.isArrayType()) {
+        DexType baseType = castType.toBaseType(dexItemFactory);
+        if (baseType.isClassType() && baseType.isInterface(appViewWithLiveness)) {
+          return RemoveCheckCastInstructionIfTrivialResult.NO_REMOVALS;
+        }
+      }
+    }
+
     TypeElement inTypeLattice = inValue.getType();
     TypeElement outTypeLattice = outValue.getType();
     TypeElement castTypeLattice =
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/checkcast/ArrayInterfaceArrayCheckCastTest.java b/src/test/java/com/android/tools/r8/ir/optimize/checkcast/ArrayInterfaceArrayCheckCastTest.java
index b643a2c..7e8b480 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/checkcast/ArrayInterfaceArrayCheckCastTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/checkcast/ArrayInterfaceArrayCheckCastTest.java
@@ -49,10 +49,7 @@
         .setMinApi(parameters.getApiLevel())
         .allowStdoutMessages()
         .run(parameters.getRuntime(), TestClass.class)
-        .applyIf(
-            parameters.isCfRuntime(),
-            r -> r.assertSuccessWithOutput(EXPECTED_OUTPUT),
-            r -> r.assertFailureWithErrorThatThrows(VerifyError.class));
+        .assertSuccessWithOutput(EXPECTED_OUTPUT);
   }
 
   // See b/223424356 (and basically the code back from b/69826014#comment3).
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/checkcast/InterfaceArrayCheckCastTest.java b/src/test/java/com/android/tools/r8/ir/optimize/checkcast/InterfaceArrayCheckCastTest.java
index 70f79da..2203de3 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/checkcast/InterfaceArrayCheckCastTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/checkcast/InterfaceArrayCheckCastTest.java
@@ -49,10 +49,7 @@
         .setMinApi(parameters.getApiLevel())
         .allowStdoutMessages()
         .run(parameters.getRuntime(), TestClass.class)
-        .applyIf(
-            parameters.isCfRuntime(),
-            r -> r.assertSuccessWithOutput(EXPECTED_OUTPUT),
-            r -> r.assertFailureWithErrorThatThrows(VerifyError.class));
+        .assertSuccessWithOutput(EXPECTED_OUTPUT);
   }
 
   // See b/223424356 (and basically the code back from b/69826014#comment3).