Fix illegal override of final method caused by class merging
Change-Id: I43832bae3b39e94f6e20b6dc5a167d437b899ef6
diff --git a/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java b/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java
index efcbb27..4992877 100644
--- a/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java
+++ b/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java
@@ -131,8 +131,10 @@
if (result.isBridge() && Iterables.any(allFlags, flags -> !flags.isBridge())) {
result.unsetBridge();
}
- if (result.isFinal() && Iterables.any(allFlags, flags -> !flags.isFinal())) {
- result.unsetFinal();
+ if (result.isFinal()) {
+ if (methods.size() < group.size() || Iterables.any(allFlags, flags -> !flags.isFinal())) {
+ result.unsetFinal();
+ }
}
if (result.isSynthetic() && Iterables.any(allFlags, flags -> !flags.isSynthetic())) {
result.unsetSynthetic();
diff --git a/src/test/java/com/android/tools/r8/classmerging/horizontal/NonFinalOverrideOfFinalMethodNonTrivialMergeTest.java b/src/test/java/com/android/tools/r8/classmerging/horizontal/NonFinalOverrideOfFinalMethodNonTrivialMergeTest.java
new file mode 100644
index 0000000..5e95a1e
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/classmerging/horizontal/NonFinalOverrideOfFinalMethodNonTrivialMergeTest.java
@@ -0,0 +1,103 @@
+// 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.classmerging.horizontal;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+import org.junit.Test;
+import org.junit.runners.Parameterized;
+
+public class NonFinalOverrideOfFinalMethodNonTrivialMergeTest
+ extends HorizontalClassMergingTestBase {
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public NonFinalOverrideOfFinalMethodNonTrivialMergeTest(TestParameters parameters) {
+ super(parameters);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addKeepMainRule(Main.class)
+ .enableInliningAnnotations()
+ .enableNeverClassInliningAnnotations()
+ .enableNoVerticalClassMergingAnnotations()
+ .setMinApi(parameters.getApiLevel())
+ .addHorizontallyMergedClassesInspector(
+ inspector -> inspector.assertIsCompleteMergeGroup(A.class, B.class, C.class))
+ .compile()
+ .inspect(
+ inspector -> {
+ ClassSubject classSubject = inspector.clazz(A.class);
+ assertThat(classSubject, isPresent());
+
+ MethodSubject methodSubject = classSubject.uniqueMethodWithName("foo$bridge");
+ assertThat(methodSubject, isPresent());
+ assertFalse(methodSubject.isFinal());
+ })
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("A", "B", "C", "CSub");
+ }
+
+ public static class Main {
+ public static void main(String[] args) {
+ new A().foo();
+ new B().foo();
+ new C().bar();
+ new CSub().foo();
+ }
+ }
+
+ @NeverClassInline
+ public static class A {
+
+ @NeverInline
+ public final void foo() {
+ System.out.println("A");
+ }
+ }
+
+ @NeverClassInline
+ public static class B {
+
+ @NeverInline
+ public final void foo() {
+ System.out.println("B");
+ }
+ }
+
+ @NeverClassInline
+ @NoVerticalClassMerging
+ public static class C {
+
+ @NeverInline
+ public void bar() {
+ System.out.println("C");
+ }
+ }
+
+ @NeverClassInline
+ public static class CSub extends C {
+
+ @NeverInline
+ public void foo() {
+ System.out.println("CSub");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/classmerging/horizontal/NonFinalOverrideOfFinalMethodTest.java b/src/test/java/com/android/tools/r8/classmerging/horizontal/NonFinalOverrideOfFinalMethodTest.java
index 110f7f6..6287174 100644
--- a/src/test/java/com/android/tools/r8/classmerging/horizontal/NonFinalOverrideOfFinalMethodTest.java
+++ b/src/test/java/com/android/tools/r8/classmerging/horizontal/NonFinalOverrideOfFinalMethodTest.java
@@ -43,12 +43,12 @@
.compile()
.inspect(
inspector -> {
- ClassSubject aClassSubject = inspector.clazz(A.class);
- assertThat(aClassSubject, isPresent());
+ ClassSubject classSubject = inspector.clazz(A.class);
+ assertThat(classSubject, isPresent());
- MethodSubject synchronizedMethodSubject = aClassSubject.uniqueMethodWithName("foo");
- assertThat(synchronizedMethodSubject, isPresent());
- assertFalse(synchronizedMethodSubject.isFinal());
+ MethodSubject methodSubject = classSubject.uniqueMethodWithName("foo");
+ assertThat(methodSubject, isPresent());
+ assertFalse(methodSubject.isFinal());
})
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutputLines("A", "B", "BSub");