Disable publicizing package private virtual methods
The current implementation because there is no check that subtypes do
not define members.
Bug: 150589374
Change-Id: Icf47dc3a8b8416fd375578cc267e9883aeeeda6a
diff --git a/src/main/java/com/android/tools/r8/optimize/ClassAndMemberPublicizer.java b/src/main/java/com/android/tools/r8/optimize/ClassAndMemberPublicizer.java
index 4816759..4269af8 100644
--- a/src/main/java/com/android/tools/r8/optimize/ClassAndMemberPublicizer.java
+++ b/src/main/java/com/android/tools/r8/optimize/ClassAndMemberPublicizer.java
@@ -110,13 +110,16 @@
if (accessFlags.isPublic()) {
return false;
}
- if (!accessFlags.isPrivate() || appView.dexItemFactory().isConstructor(encodedMethod.method)) {
- // TODO(b/150589374): This should check for dispatch targets or just abandon in
- // package-private.
+ if (accessFlags.isProtected() || appView.dexItemFactory().isConstructor(encodedMethod.method)) {
accessFlags.promoteToPublic();
return false;
}
-
+ // Package private virtual methods cannot be relaxed unless no subtypes defines the method.
+ // TODO(b/151971147): Measure the cost of not publicizing these.
+ if (accessFlags.isPackagePrivate() && !accessFlags.isStatic()) {
+ return false;
+ }
+ assert accessFlags.isPrivate() || accessFlags.isStatic();
if (!accessFlags.isStatic()) {
// If this method is mentioned in keep rules, do not transform (rule applications changed).
if (appView.appInfo().isPinned(encodedMethod.method)) {
diff --git a/src/test/java/com/android/tools/r8/accessrelaxation/PackagePrivateAccessRelaxationTest.java b/src/test/java/com/android/tools/r8/accessrelaxation/PackagePrivateAccessRelaxationTest.java
new file mode 100644
index 0000000..c600101
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/accessrelaxation/PackagePrivateAccessRelaxationTest.java
@@ -0,0 +1,68 @@
+// 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.accessrelaxation;
+
+import com.android.tools.r8.CompilationFailedException;
+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.accessrelaxation.packageprivate.package_a.A;
+import com.android.tools.r8.accessrelaxation.packageprivate.package_b.B;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class PackagePrivateAccessRelaxationTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters()
+ .withCfRuntimes()
+ .withDexRuntimesStartingFromIncluding(Version.V5_1_1)
+ .withAllApiLevels()
+ .build();
+ }
+
+ public PackagePrivateAccessRelaxationTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws ExecutionException, CompilationFailedException, IOException {
+ testForRuntime(parameters)
+ .addProgramClasses(A.class, B.class, Main.class)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("A.foo", "B.foo");
+ }
+
+ @Test
+ public void testR8() throws ExecutionException, CompilationFailedException, IOException {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(A.class, B.class, Main.class)
+ .addKeepMainRule(Main.class)
+ .addKeepRules("-allowaccessmodification")
+ .enableMergeAnnotations()
+ .enableInliningAnnotations()
+ .enableNeverClassInliningAnnotations()
+ .setMinApi(parameters.getApiLevel())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("A.foo", "B.foo");
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ new A().callFoo();
+ new B().foo();
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/accessrelaxation/packageprivate/package_a/A.java b/src/test/java/com/android/tools/r8/accessrelaxation/packageprivate/package_a/A.java
new file mode 100644
index 0000000..1d4eeea
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/accessrelaxation/packageprivate/package_a/A.java
@@ -0,0 +1,23 @@
+// 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.accessrelaxation.packageprivate.package_a;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NeverMerge;
+
+@NeverMerge
+@NeverClassInline
+public class A {
+
+ final void foo() {
+ System.out.println("A.foo");
+ }
+
+ @NeverInline
+ public void callFoo() {
+ foo();
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/accessrelaxation/packageprivate/package_b/B.java b/src/test/java/com/android/tools/r8/accessrelaxation/packageprivate/package_b/B.java
new file mode 100644
index 0000000..f369a58
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/accessrelaxation/packageprivate/package_b/B.java
@@ -0,0 +1,18 @@
+// 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.accessrelaxation.packageprivate.package_b;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.accessrelaxation.packageprivate.package_a.A;
+
+@NeverClassInline
+public class B extends A {
+
+ @NeverInline
+ public void foo() {
+ System.out.println("B.foo");
+ }
+}