Revert "Temporarily disable horizontal merging in D8"
This reverts commit cd91365aecf2c5f39b7d882ab74435f2655af014.
Bug: b/227791663
Change-Id: I22a9cbc6e8b09868ea7019ed01c036f894e4faad
diff --git a/src/main/java/com/android/tools/r8/D8Command.java b/src/main/java/com/android/tools/r8/D8Command.java
index c953853..acbd677 100644
--- a/src/main/java/com/android/tools/r8/D8Command.java
+++ b/src/main/java/com/android/tools/r8/D8Command.java
@@ -579,9 +579,7 @@
HorizontalClassMergerOptions horizontalClassMergerOptions =
internal.horizontalClassMergerOptions();
if (internal.isGeneratingDex()) {
- // TODO(b/227791663): Disable until fixed.
- horizontalClassMergerOptions.disable();
- // horizontalClassMergerOptions.setRestrictToSynthetics();
+ horizontalClassMergerOptions.setRestrictToSynthetics();
} else {
assert internal.isGeneratingClassFiles();
horizontalClassMergerOptions.disable();
diff --git a/src/main/java/com/android/tools/r8/horizontalclassmerging/PolicyScheduler.java b/src/main/java/com/android/tools/r8/horizontalclassmerging/PolicyScheduler.java
index 2ff1bfb..eb5b57e 100644
--- a/src/main/java/com/android/tools/r8/horizontalclassmerging/PolicyScheduler.java
+++ b/src/main/java/com/android/tools/r8/horizontalclassmerging/PolicyScheduler.java
@@ -44,7 +44,7 @@
import com.android.tools.r8.horizontalclassmerging.policies.NoVirtualMethodMerging;
import com.android.tools.r8.horizontalclassmerging.policies.NoWeakerAccessPrivileges;
import com.android.tools.r8.horizontalclassmerging.policies.NotMatchedByNoHorizontalClassMerging;
-import com.android.tools.r8.horizontalclassmerging.policies.OnlyClassesWithStaticDefinitions;
+import com.android.tools.r8.horizontalclassmerging.policies.OnlyClassesWithStaticDefinitionsAndNoClassInitializer;
import com.android.tools.r8.horizontalclassmerging.policies.OnlyDirectlyConnectedOrUnrelatedInterfaces;
import com.android.tools.r8.horizontalclassmerging.policies.PreserveMethodCharacteristics;
import com.android.tools.r8.horizontalclassmerging.policies.PreventClassMethodAndDefaultMethodCollisions;
@@ -137,7 +137,7 @@
ImmutableList.Builder<SingleClassPolicy> builder =
ImmutableList.<SingleClassPolicy>builder()
.add(new CheckSyntheticClasses(appView))
- .add(new OnlyClassesWithStaticDefinitions());
+ .add(new OnlyClassesWithStaticDefinitionsAndNoClassInitializer());
assert verifySingleClassPoliciesIrrelevantForMergingSyntheticsInD8(appView, mode, builder);
return builder.build();
}
diff --git a/src/main/java/com/android/tools/r8/horizontalclassmerging/policies/OnlyClassesWithStaticDefinitions.java b/src/main/java/com/android/tools/r8/horizontalclassmerging/policies/OnlyClassesWithStaticDefinitionsAndNoClassInitializer.java
similarity index 81%
rename from src/main/java/com/android/tools/r8/horizontalclassmerging/policies/OnlyClassesWithStaticDefinitions.java
rename to src/main/java/com/android/tools/r8/horizontalclassmerging/policies/OnlyClassesWithStaticDefinitionsAndNoClassInitializer.java
index 736ede0..8b6334a 100644
--- a/src/main/java/com/android/tools/r8/horizontalclassmerging/policies/OnlyClassesWithStaticDefinitions.java
+++ b/src/main/java/com/android/tools/r8/horizontalclassmerging/policies/OnlyClassesWithStaticDefinitionsAndNoClassInitializer.java
@@ -9,10 +9,13 @@
import com.google.common.collect.Iterables;
/** Prevent merging of classes that has non-static methods or fields. */
-public class OnlyClassesWithStaticDefinitions extends SingleClassPolicy {
+public class OnlyClassesWithStaticDefinitionsAndNoClassInitializer extends SingleClassPolicy {
@Override
public boolean canMerge(DexProgramClass program) {
+ if (program.hasClassInitializer()) {
+ return false;
+ }
return !Iterables.any(program.members(), member -> !member.isStatic());
}
diff --git a/src/test/java/com/android/tools/r8/classmerging/horizontal/HorizontalClassMergingInD8WithClInitOnCCTest.java b/src/test/java/com/android/tools/r8/classmerging/horizontal/HorizontalClassMergingInD8WithClInitOnCCTest.java
index c5a340c..4e955d0 100644
--- a/src/test/java/com/android/tools/r8/classmerging/horizontal/HorizontalClassMergingInD8WithClInitOnCCTest.java
+++ b/src/test/java/com/android/tools/r8/classmerging/horizontal/HorizontalClassMergingInD8WithClInitOnCCTest.java
@@ -4,10 +4,6 @@
package com.android.tools.r8.classmerging.horizontal;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assume.assumeTrue;
-
-import com.android.tools.r8.CompilationFailedException;
import com.android.tools.r8.CompilationMode;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
@@ -31,22 +27,17 @@
@Test
public void testD8() throws Exception {
- // TODO(b/227791663): Ensure we do not class merge classes with static initializers.
- assumeTrue(
- parameters.getApiLevel().isLessThan(TestBase.apiLevelWithStaticInterfaceMethodsSupport()));
- assertThrows(
- CompilationFailedException.class,
- () ->
- testForD8(parameters.getBackend())
- .addInnerClasses(getClass())
- .setMode(CompilationMode.RELEASE)
- .setMinApi(parameters.getApiLevel())
- .addOptionsModification(
- options -> {
- options.horizontalClassMergerOptions().enable();
- options.horizontalClassMergerOptions().setRestrictToSynthetics();
- })
- .compile());
+ testForD8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .setMode(CompilationMode.RELEASE)
+ .setMinApi(parameters.getApiLevel())
+ .addOptionsModification(
+ options -> {
+ options.horizontalClassMergerOptions().enable();
+ options.horizontalClassMergerOptions().setRestrictToSynthetics();
+ })
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("0", "1");
}
public interface A {