Reproduce issue 345248270
Bug: b/345248270
Change-Id: I280b32f1e323bc6061f928e3da6370ff9ad41440
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/Regress345248270ConstClassTest.java b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/Regress345248270ConstClassTest.java
new file mode 100644
index 0000000..dcd81ac
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/Regress345248270ConstClassTest.java
@@ -0,0 +1,86 @@
+// Copyright (c) 2024, 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.effectivelytrivialphioptimization;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+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.ir.optimize.effectivelytrivialphioptimization.b345248270.I;
+import com.android.tools.r8.ir.optimize.effectivelytrivialphioptimization.b345248270.PublicAccessor;
+import com.android.tools.r8.utils.StringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class Regress345248270ConstClassTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ private static final String EXPECTED_OUTPUT = StringUtils.lines("Hello, world!");
+
+ @Test
+ public void testD8() throws Exception {
+ parameters.assumeDexRuntime();
+ testForD8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addProgramClasses(
+ I.class, PublicAccessor.class, PublicAccessor.getPackagePrivateImplementationClass())
+ .setMinApi(parameters.getApiLevel())
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ try {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addProgramClasses(
+ I.class, PublicAccessor.class, PublicAccessor.getPackagePrivateImplementationClass())
+ .addKeepMainRule(TestClass.class)
+ .setMinApi(parameters.getApiLevel())
+ .enableNeverClassInliningAnnotations()
+ .enableNoAccessModificationAnnotationsForMembers()
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ fail("Expected exception");
+ } catch (CompilationFailedException e) {
+ // TODO(b/345248270): Should not hit an assertion.
+ assertTrue(e.getCause() instanceof AssertionError);
+ return;
+ }
+ fail("Expected CompilationFailedException");
+ }
+
+ static class TestClass {
+ public static Class<?> test() {
+ Class<?> r = null;
+ if (System.currentTimeMillis() > 0) {
+ r = PublicAccessor.getPackagePrivateImplementationClass();
+ } else {
+ System.out.println("Do something");
+ r = PublicAccessor.getPackagePrivateImplementationClass();
+ }
+ return r;
+ }
+
+ public static void main(String[] args) {
+ test();
+ System.out.println("Hello, world!");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/Regress345248270Test.java b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/Regress345248270Test.java
new file mode 100644
index 0000000..aa916ba
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/Regress345248270Test.java
@@ -0,0 +1,86 @@
+// Copyright (c) 2024, 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.effectivelytrivialphioptimization;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+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.ir.optimize.effectivelytrivialphioptimization.b345248270.I;
+import com.android.tools.r8.ir.optimize.effectivelytrivialphioptimization.b345248270.PublicAccessor;
+import com.android.tools.r8.utils.StringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class Regress345248270Test extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ private static final String EXPECTED_OUTPUT = StringUtils.lines("Hello, world!");
+
+ @Test
+ public void testD8() throws Exception {
+ parameters.assumeDexRuntime();
+ testForD8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addProgramClasses(
+ I.class, PublicAccessor.class, PublicAccessor.getPackagePrivateImplementationClass())
+ .setMinApi(parameters.getApiLevel())
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ try {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addProgramClasses(
+ I.class, PublicAccessor.class, PublicAccessor.getPackagePrivateImplementationClass())
+ .addKeepMainRule(TestClass.class)
+ .setMinApi(parameters.getApiLevel())
+ .enableNeverClassInliningAnnotations()
+ .enableNoAccessModificationAnnotationsForMembers()
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ fail("Expected exception");
+ } catch (CompilationFailedException e) {
+ // TODO(b/345248270): Should not hit an assertion.
+ assertTrue(e.getCause() instanceof AssertionError);
+ return;
+ }
+ fail("Expected CompilationFailedException");
+ }
+
+ static class TestClass {
+ public static I test() {
+ I r = null;
+ if (System.currentTimeMillis() > 0) {
+ r = PublicAccessor.getPackagePrivateImplementation();
+ } else {
+ System.out.println("Do something");
+ r = PublicAccessor.getPackagePrivateImplementation();
+ }
+ return r;
+ }
+
+ public static void main(String[] args) {
+ test();
+ System.out.println("Hello, world!");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/I.java b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/I.java
new file mode 100644
index 0000000..ea07f32
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/I.java
@@ -0,0 +1,6 @@
+// Copyright (c) 2024, 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.effectivelytrivialphioptimization.b345248270;
+
+public interface I {}
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/PackagePrivateImplementation.java b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/PackagePrivateImplementation.java
new file mode 100644
index 0000000..7d615db
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/PackagePrivateImplementation.java
@@ -0,0 +1,12 @@
+// Copyright (c) 2024, 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.effectivelytrivialphioptimization.b345248270;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NoAccessModification;
+
+@NeverClassInline
+class PackagePrivateImplementation implements I {
+ @NoAccessModification static final I NULL = new PackagePrivateImplementation();
+}
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/PublicAccessor.java b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/PublicAccessor.java
new file mode 100644
index 0000000..52ab243
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/effectivelytrivialphioptimization/b345248270/PublicAccessor.java
@@ -0,0 +1,14 @@
+// Copyright (c) 2024, 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.effectivelytrivialphioptimization.b345248270;
+
+public class PublicAccessor {
+ public static I getPackagePrivateImplementation() {
+ return PackagePrivateImplementation.NULL;
+ }
+
+ public static Class<?> getPackagePrivateImplementationClass() {
+ return PackagePrivateImplementation.class;
+ }
+}