Reproduce inconsistency between handling of abstract/interface methods
This adds two tests showing that the tracing of inherited abstract methods and default interface methods is different.
Bug: b/389039340
Change-Id: Icb369ac1360e29f0b656c6af220dc28e1adcb9f7
diff --git a/src/test/java/com/android/tools/r8/partial/PartialCompilationWithDefaultInterfaceMethodTest.java b/src/test/java/com/android/tools/r8/partial/PartialCompilationWithDefaultInterfaceMethodTest.java
new file mode 100644
index 0000000..ee97ab3
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/partial/PartialCompilationWithDefaultInterfaceMethodTest.java
@@ -0,0 +1,68 @@
+// Copyright (c) 2025, 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.partial;
+
+import static org.junit.Assume.assumeTrue;
+
+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 PartialCompilationWithDefaultInterfaceMethodTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withDexRuntimesAndAllApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ // TODO(b/388763735): Enable for all API levels.
+ assumeTrue(parameters.canUseDefaultAndStaticInterfaceMethods());
+ testForR8Partial(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addR8IncludedClasses(I.class, J.class)
+ .addR8ExcludedClasses(Main.class, A.class)
+ .setMinApi(parameters)
+ .compile()
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("Hello, world!");
+ }
+
+ // D8
+
+ static class Main {
+
+ public static void main(String[] args) {
+ I i = new A();
+ i.m();
+ }
+ }
+
+ static class A implements J {}
+
+ // R8
+
+ interface I {
+
+ void m();
+ }
+
+ interface J extends I {
+
+ @Override
+ default void m() {
+ System.out.println("Hello, world!");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/partial/PartialCompilationWithNonAbstractMethodOnAbstractClassTest.java b/src/test/java/com/android/tools/r8/partial/PartialCompilationWithNonAbstractMethodOnAbstractClassTest.java
new file mode 100644
index 0000000..a1b0a57
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/partial/PartialCompilationWithNonAbstractMethodOnAbstractClassTest.java
@@ -0,0 +1,68 @@
+// Copyright (c) 2025, 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.partial;
+
+import static org.junit.Assume.assumeTrue;
+
+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 PartialCompilationWithNonAbstractMethodOnAbstractClassTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withDexRuntimesAndAllApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ // TODO(b/388763735): Enable for all API levels.
+ assumeTrue(parameters.canUseDefaultAndStaticInterfaceMethods());
+ testForR8Partial(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addR8IncludedClasses(A.class, B.class)
+ .addR8ExcludedClasses(Main.class, C.class)
+ .setMinApi(parameters)
+ .compile()
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("Hello, world!");
+ }
+
+ // D8
+
+ static class Main {
+
+ public static void main(String[] args) {
+ A a = new C();
+ a.m();
+ }
+ }
+
+ static class C extends B {}
+
+ // R8
+
+ abstract static class A {
+
+ public abstract void m();
+ }
+
+ abstract static class B extends A {
+
+ @Override
+ public void m() {
+ System.out.println("Hello, world!");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/shaking/AbstractMethodWithOpenHierarchyTest.java b/src/test/java/com/android/tools/r8/shaking/AbstractMethodWithOpenHierarchyTest.java
new file mode 100644
index 0000000..8de48b5
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/AbstractMethodWithOpenHierarchyTest.java
@@ -0,0 +1,65 @@
+// Copyright (c) 2025, 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.shaking;
+
+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 AbstractMethodWithOpenHierarchyTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(A.class, B.class)
+ .addKeepClassAndMembersRules(A.class)
+ .addKeepClassAndDefaultConstructor(B.class)
+ .setMinApi(parameters)
+ .compile()
+ .addRunClasspathClasses(Main.class, C.class)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("Hello, world!");
+ }
+
+ // D8
+
+ static class Main {
+
+ public static void main(String[] args) {
+ A a = new C();
+ a.m();
+ }
+ }
+
+ static class C extends B {}
+
+ // R8
+
+ abstract static class A {
+
+ public abstract void m();
+ }
+
+ abstract static class B extends A {
+
+ @Override
+ public void m() {
+ System.out.println("Hello, world!");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/shaking/DefaultInterfaceMethodWithOpenHierarchyTest.java b/src/test/java/com/android/tools/r8/shaking/DefaultInterfaceMethodWithOpenHierarchyTest.java
new file mode 100644
index 0000000..8e592d1
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/DefaultInterfaceMethodWithOpenHierarchyTest.java
@@ -0,0 +1,64 @@
+// Copyright (c) 2025, 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.shaking;
+
+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 DefaultInterfaceMethodWithOpenHierarchyTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(I.class, J.class)
+ .addKeepClassAndMembersRules(I.class)
+ .addKeepClassRules(J.class)
+ .setMinApi(parameters)
+ .compile()
+ .addRunClasspathClasses(Main.class, A.class)
+ .run(parameters.getRuntime(), Main.class)
+ .assertFailureWithErrorThatThrows(AbstractMethodError.class);
+ }
+
+ // D8
+
+ static class Main {
+
+ public static void main(String[] args) {
+ I i = new A();
+ i.m();
+ }
+ }
+
+ static class A implements J {}
+
+ // R8
+
+ interface I {
+
+ void m();
+ }
+
+ interface J extends I {
+
+ default void m() {
+ System.out.println("Hello, world!");
+ }
+ }
+}