Add test for not rewriting invoke type for interface target
Bug: b/197490164
Change-Id: I6ec41bc134cb1c5eaf18f87cc0cfb58ef1afe6ea
diff --git a/src/test/java/com/android/tools/r8/bridgeremoval/BridgeWithInvokeSuperTest.java b/src/test/java/com/android/tools/r8/bridgeremoval/BridgeWithInvokeSuperTest.java
new file mode 100644
index 0000000..4aaa128
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/bridgeremoval/BridgeWithInvokeSuperTest.java
@@ -0,0 +1,117 @@
+// 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.bridgeremoval;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.SingleTestRunResult;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.ToolHelper.DexVm.Version;
+import com.android.tools.r8.graph.MethodAccessFlags;
+import com.android.tools.r8.transformers.ClassFileTransformer.MethodPredicate;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+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 BridgeWithInvokeSuperTest extends TestBase {
+
+ private static final String[] EXPECTED = new String[] {"I::foo"};
+
+ @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, I.class)
+ .addProgramClassFileData(getAWithBridgeAccessFlag())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(Main.class, I.class)
+ .addProgramClassFileData(getAWithBridgeAccessFlag())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepMainRule(Main.class)
+ .enableNoVerticalClassMergingAnnotations()
+ .enableInliningAnnotations()
+ .compile()
+ .inspect(
+ inspector -> {
+ // Check that we are removing the bridge if we support default methods.
+ if (parameters.canUseDefaultAndStaticInterfaceMethods()) {
+ ClassSubject A = inspector.clazz(A.class);
+ assertThat(A, isPresent());
+ MethodSubject fooMethod = A.uniqueMethodWithOriginalName("foo");
+ assertThat(fooMethod, not(isPresent()));
+ }
+ })
+ .run(parameters.getRuntime(), Main.class)
+ .apply(this::checkOutput);
+ }
+
+ private byte[] getAWithBridgeAccessFlag() throws Exception {
+ return transformer(A.class)
+ .setAccessFlags(MethodPredicate.onName("foo"), MethodAccessFlags::setBridge)
+ .transform();
+ }
+
+ private void checkOutput(SingleTestRunResult<?> result) {
+ if (parameters.canUseDefaultAndStaticInterfaceMethods()) {
+ // TODO(b/197490164): We should not produce invalid code.
+ if (parameters.isDexRuntime()
+ && parameters.getDexRuntimeVersion().isEqualTo(Version.V7_0_0)) {
+ result.assertFailureWithErrorThatThrows(CloneNotSupportedException.class);
+ } else {
+ result.assertFailureWithErrorThatThrows(IncompatibleClassChangeError.class);
+ }
+ } else {
+ result.assertSuccessWithOutputLines(EXPECTED);
+ }
+ }
+
+ @NoVerticalClassMerging
+ public interface I {
+
+ @NeverInline
+ default void foo() {
+ System.out.println("I::foo");
+ }
+ }
+
+ public static class A implements I {
+
+ @Override
+ @NeverInline
+ public void foo() {
+ I.super.foo();
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ new A().foo();
+ }
+ }
+}