Test showing lost shrinking opportunity due to member rebinding.
Bug: 150445487
Bug: 141606642
Change-Id: I166ade64753f87fa9e72392ba35ee585b4032746
diff --git a/src/test/java/com/android/tools/r8/shaking/EventuallyNonTargetedMethodTest.java b/src/test/java/com/android/tools/r8/shaking/EventuallyNonTargetedMethodTest.java
new file mode 100644
index 0000000..741496d
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/EventuallyNonTargetedMethodTest.java
@@ -0,0 +1,101 @@
+// Copyright (c) 2020, 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 static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NeverMerge;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.StringUtils;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class EventuallyNonTargetedMethodTest extends TestBase {
+
+ static final String EXPECTED = StringUtils.lines("A::foo", "C::bar");
+
+ private final TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public EventuallyNonTargetedMethodTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .enableInliningAnnotations()
+ .enableMergeAnnotations()
+ .enableNeverClassInliningAnnotations()
+ .addInnerClasses(EventuallyNonTargetedMethodTest.class)
+ .addKeepMainRule(Main.class)
+ .setMinApi(parameters.getApiLevel())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED)
+ .inspect(this::checkIsFooPresent);
+ }
+
+ private void checkIsFooPresent(CodeInspector inspector) {
+ ClassSubject classSubject = inspector.clazz(C.class);
+ assertThat(classSubject, isPresent());
+ // TODO(b/150445487): Member rebinding will rewrite B::foo to A::foo causing C::foo to remain.
+ assertThat(classSubject.uniqueMethodWithName("foo"), isPresent());
+ }
+
+ @NeverMerge
+ private static class A {
+ @NeverInline
+ public void foo() {
+ System.out.println("A::foo");
+ }
+ }
+
+ @NeverMerge
+ @NeverClassInline
+ private static class B extends A {
+ // No override of foo, but B::foo will be the only target.
+ }
+
+ @NeverClassInline
+ private static class C extends A {
+
+ // Non-targeted override.
+ @Override
+ public void foo() {
+ System.out.println("C::foo");
+ }
+
+ @NeverInline
+ public void bar() {
+ System.out.println("C::bar");
+ }
+ }
+
+ private static class Main {
+ static boolean effectivelyFinalFalse = false;
+
+ public static void main(String[] args) {
+ new B().foo();
+ new C().bar();
+ if (effectivelyFinalFalse) {
+ // First round of tree shaking the below reference to A::foo will keep it live.
+ // This branch will then be removed and it should be possible to conclude C::foo dead.
+ new A().foo();
+ }
+ }
+ }
+}