Additional tests for having collisions for normalized methods
Bug: b/258720808
Change-Id: I85bc203c594bcbadabd89c94c79147b8e1778a9e
diff --git a/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationDuplicateMethodTest.java b/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationDuplicateMethodTest.java
new file mode 100644
index 0000000..899ca4d
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationDuplicateMethodTest.java
@@ -0,0 +1,98 @@
+// 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.optimize.proto;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThrows;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoHorizontalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.references.Reference;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class ProtoNormalizationDuplicateMethodTest extends TestBase {
+
+ private final String[] EXPECTED = new String[] {"Base::foo-7Calling Sub::foo8"};
+
+ @Parameter() public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addInnerClasses(getClass())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ // TODO(b/258720808): We should not cause collision with an existing method.
+ assertThrows(
+ CompilationFailedException.class,
+ () -> {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepMainRule(Main.class)
+ .addKeepMethodRules(
+ Reference.methodFromMethod(
+ B.class.getDeclaredMethod("foo$1", int.class, int.class, String.class)))
+ .enableInliningAnnotations()
+ .enableNoHorizontalClassMergingAnnotations()
+ .compileWithExpectedDiagnostics(
+ diagnostics ->
+ diagnostics.assertErrorMessageThatMatches(
+ containsString("Duplicate method")));
+ });
+ }
+
+ @NoHorizontalClassMerging
+ public static class B {
+
+ @NeverInline
+ public void foo(int i, String s, int j) {
+ System.out.println("Base::foo-" + i + s + j);
+ }
+
+ @NeverInline
+ public void foo$1(int i, int j, String s) {
+ throw new RuntimeException("Should never be called: " + i + j + s);
+ }
+ }
+
+ @NoHorizontalClassMerging
+ public static class A {
+
+ @NeverInline
+ public void foo(String s, int i, int j) {
+ System.out.println("B-" + i + s + j);
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ if (System.currentTimeMillis() == 0) {
+ new A().foo("Calling Sub::foo", 3, 4);
+ new B().foo$1(5, 6, "Calling Sub::foo");
+ }
+ new B().foo(7, "Calling Sub::foo", 8);
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationIntroduceCollisionTest.java b/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationIntroduceCollisionTest.java
new file mode 100644
index 0000000..f4b8ccd
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationIntroduceCollisionTest.java
@@ -0,0 +1,84 @@
+// 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.optimize.proto;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoVerticalClassMerging;
+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.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class ProtoNormalizationIntroduceCollisionTest extends TestBase {
+
+ private final String[] EXPECTED = new String[] {"Base::foo-42Calling B::foo1337"};
+ private final String[] R8_EXPECTED = new String[] {"Sub::foo-Calling B::foo421337"};
+
+ @Parameter() public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addProgramClasses(Main.class, Base.class, Sub.class)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepMainRule(Main.class)
+ .addKeepClassAndMembersRules(Base.class)
+ .enableInliningAnnotations()
+ .enableNoVerticalClassMergingAnnotations()
+ .run(parameters.getRuntime(), Main.class)
+ // TODO(b/258720808): We should produce the expected result.
+ .assertSuccessWithOutputLines(R8_EXPECTED);
+ }
+
+ public static class Base {
+
+ @NeverInline
+ public void foo(int i, int j, String s) {
+ System.out.println("Base::foo-" + i + s + j);
+ }
+ }
+
+ @NoVerticalClassMerging
+ public static class Sub extends Base {
+
+ @NeverInline
+ public void foo(String s, int i, int j) {
+ System.out.println("Sub::foo-" + s + i + j);
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ if (System.currentTimeMillis() == 0) {
+ callFoo(new Base());
+ new Sub().foo("Calling Sub::foo", 1, 2);
+ }
+ callFoo(new Sub());
+ }
+
+ public static void callFoo(Base b) {
+ b.foo(42, 1337, "Calling B::foo");
+ }
+ }
+}