Add reproduction for generating inconsistent SSA in staticizer Bug: 171642432 Change-Id: I3ae6aa4fab87d96ddc1414803e2f3ec5d999dda7
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/ClassWithCompanion.java b/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/ClassWithCompanion.java new file mode 100644 index 0000000..f493863 --- /dev/null +++ b/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/ClassWithCompanion.java
@@ -0,0 +1,26 @@ +// 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.ir.optimize.staticizer.b171642432; + +import com.android.tools.r8.NeverClassInline; +import com.android.tools.r8.NeverInline; + +@NeverClassInline +public class ClassWithCompanion { + + public String url; + + @NeverClassInline + public static class Companion { + @NeverInline + ClassWithCompanion newInstance(String url) { + ClassWithCompanion classWithCompanion = new ClassWithCompanion(); + classWithCompanion.url = url; + return classWithCompanion; + } + } + + static Companion COMPANION_INSTANCE = new Companion(); +}
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/CompanionUser.java b/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/CompanionUser.java new file mode 100644 index 0000000..fd1d06c --- /dev/null +++ b/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/CompanionUser.java
@@ -0,0 +1,37 @@ +// 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.ir.optimize.staticizer.b171642432; + +import static com.android.tools.r8.ir.optimize.staticizer.b171642432.ClassWithCompanion.COMPANION_INSTANCE; + +import com.android.tools.r8.NeverInline; + +public class CompanionUser { + + private final String url; + + public CompanionUser(String url) { + this.url = url; + } + + public ClassWithCompanion getItem(int position) { + return (position == 0 + ? (url.contains("y.htm") + ? COMPANION_INSTANCE.newInstance(replace("y.htm", "ay.htm")) + : COMPANION_INSTANCE.newInstance(replace(".htm", "a.htm"))) + : (position == 1 + ? (url.contains("y.htm") + ? COMPANION_INSTANCE.newInstance(replace("y.htm", "by.htm")) + : COMPANION_INSTANCE.newInstance(replace(".htm", "b.htm"))) + : (url.contains("y.htm") + ? COMPANION_INSTANCE.newInstance(replace("y.htm", "cy.htm")) + : COMPANION_INSTANCE.newInstance(replace(".htm", "c.htm"))))); + } + + @NeverInline + public String replace(String target, String replacement) { + return url.replace(target, replacement); + } +}
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/CompanionWithPhisTest.java b/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/CompanionWithPhisTest.java new file mode 100644 index 0000000..a6948db --- /dev/null +++ b/src/test/java/com/android/tools/r8/ir/optimize/staticizer/b171642432/CompanionWithPhisTest.java
@@ -0,0 +1,70 @@ +// 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.ir.optimize.staticizer.b171642432; + +import static com.android.tools.r8.DiagnosticsMatcher.diagnosticException; +import static org.junit.Assert.assertThrows; + +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 org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +// This is a reproduction of b/171642432. +@RunWith(Parameterized.class) +public class CompanionWithPhisTest extends TestBase { + + private final TestParameters parameters; + private final String EXPECTED = "FooBarBaza.htm"; + + @Parameters(name = "{0}") + public static TestParametersCollection data() { + return getTestParameters().withDexRuntimes().withAllApiLevels().build(); + } + + public CompanionWithPhisTest(TestParameters parameters) { + this.parameters = parameters; + } + + @Test + public void testRuntime() throws Exception { + testForRuntime(parameters) + .addProgramClasses(Main.class, CompanionUser.class) + .addProgramClassesAndInnerClasses(ClassWithCompanion.class) + .run(parameters.getRuntime(), Main.class) + .assertSuccessWithOutputLines(EXPECTED); + } + + @Test + public void testR8() throws Exception { + assertThrows( + CompilationFailedException.class, + () -> { + testForR8(parameters.getBackend()) + .addProgramClasses(Main.class, CompanionUser.class) + .addProgramClassesAndInnerClasses(ClassWithCompanion.class) + .setMinApi(parameters.getApiLevel()) + .enableNeverClassInliningAnnotations() + .enableInliningAnnotations() + .addKeepMainRule(Main.class) + .compileWithExpectedDiagnostics( + diagnostics -> { + diagnostics.assertErrorsMatch(diagnosticException(AssertionError.class)); + }); + }); + } + + public static class Main { + + public static void main(String[] args) { + System.out.println( + new CompanionUser(args.length == 0 ? "FooBarBaz.htm" : args[0]).getItem(args.length).url); + } + } +}