Add test for invalid lookup on array types in memberrebinding
Bug: b/233048876
Change-Id: Ib0c1314d79be08e0285b6c1d30dc75572a3d5d3b
diff --git a/src/test/java/com/android/tools/r8/memberrebinding/MemberRebindingArrayTest.java b/src/test/java/com/android/tools/r8/memberrebinding/MemberRebindingArrayTest.java
new file mode 100644
index 0000000..8e0f6e8
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/memberrebinding/MemberRebindingArrayTest.java
@@ -0,0 +1,82 @@
+// Copyright (c) 2022, 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.memberrebinding;
+
+import static org.hamcrest.core.StringContains.containsString;
+import static org.junit.Assert.assertThrows;
+
+import com.android.tools.r8.CompilationFailedException;
+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;
+
+@RunWith(Parameterized.class)
+public class MemberRebindingArrayTest extends TestBase {
+
+ private final TestParameters parameters;
+ private final String[] EXPECTED = new String[] {"hashCode", "equals"};
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public MemberRebindingArrayTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addProgramClassFileData(getMainWithRewrittenEqualsAndHashCode())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ // TODO(b/233048876): We should not fail for arrays.
+ assertThrows(
+ CompilationFailedException.class,
+ () -> {
+ testForR8(parameters.getBackend())
+ .addProgramClassFileData(getMainWithRewrittenEqualsAndHashCode())
+ .addKeepMainRule(Main.class)
+ .setMinApi(parameters.getApiLevel())
+ .addOptionsModification(
+ options -> options.apiModelingOptions().disableApiCallerIdentification())
+ .compileWithExpectedDiagnostics(
+ diagnostics ->
+ diagnostics.assertErrorMessageThatMatches(
+ containsString("Cannot lookup definition for type: java.lang.String[]")));
+ });
+ }
+
+ private byte[] getMainWithRewrittenEqualsAndHashCode() throws Exception {
+ return transformer(Main.class)
+ .transformMethodInsnInMethod(
+ "main",
+ (opcode, owner, name, descriptor, isInterface, visitor) -> {
+ if (owner.equals(binaryName(Object.class))) {
+ visitor.visitMethodInsn(
+ opcode, "[" + descriptor(String.class), name, descriptor, isInterface);
+ } else {
+ visitor.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
+ }
+ })
+ .transform();
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ System.out.println(args.hashCode() != 0 ? "hashCode" : "error_hashCode");
+ System.out.println(args.equals(args.clone()) ? "error_equals" : "equals");
+ }
+ }
+}