Reproduce NPE in vertical class merger in presence of missing classes
Bug: 191124967
Change-Id: I62d2c3e0089675cec0e03f5182e25b5d9dfd211d
diff --git a/src/test/java/com/android/tools/r8/classmerging/vertical/VerticalClassMergingWithMissingSuperClassTest.java b/src/test/java/com/android/tools/r8/classmerging/vertical/VerticalClassMergingWithMissingSuperClassTest.java
new file mode 100644
index 0000000..10c570a
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/classmerging/vertical/VerticalClassMergingWithMissingSuperClassTest.java
@@ -0,0 +1,79 @@
+// Copyright (c) 2021, 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.vertical;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.AssertUtils;
+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 VerticalClassMergingWithMissingSuperClassTest extends TestBase {
+
+ @Parameter public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ AssertUtils.assertFailsCompilation(
+ () ->
+ testForR8(parameters.getBackend())
+ .addProgramClasses(Main.class, A.class, B.class, C.class)
+ .addKeepMainRule(Main.class)
+ .addDontWarn(MissingClass.class)
+ .enableNoVerticalClassMergingAnnotations()
+ .setMinApi(parameters.getApiLevel())
+ .compile()
+ .inspect(inspector -> assertThat(inspector.clazz(B.class), not(isPresent())))
+ .addRunClasspathFiles(buildOnDexRuntime(parameters, MissingClass.class))
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("C", "A", "B"),
+ exception -> assertEquals(NullPointerException.class, exception.getCause().getClass()));
+ }
+
+ static class Main {
+ public static void main(String[] args) {
+ new C().m();
+ }
+ }
+
+ static class MissingClass {}
+
+ @NoVerticalClassMerging
+ static class A extends MissingClass {
+ void m() {
+ System.out.println("A");
+ }
+ }
+
+ static class B extends A {
+ @Override
+ void m() {
+ super.m();
+ System.out.println("B");
+ }
+ }
+
+ static class C extends B {
+ C() {
+ System.out.println("C");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/utils/codeinspector/AssertUtils.java b/src/test/java/com/android/tools/r8/utils/codeinspector/AssertUtils.java
new file mode 100644
index 0000000..174e734
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/utils/codeinspector/AssertUtils.java
@@ -0,0 +1,76 @@
+// 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.utils.codeinspector;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.utils.ThrowingAction;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.function.Consumer;
+
+public class AssertUtils {
+
+ public static void assertFailsCompilation(ThrowingAction<CompilationFailedException> action) {
+ try {
+ assertFailsCompilationIf(true, action);
+ return;
+ } catch (CompilationFailedException e) {
+ // Should have been caught
+ }
+ fail("Should have failed with a CompilationFailedException");
+ }
+
+ public static <E extends Throwable> void assertFailsCompilation(
+ ThrowingAction<E> action, Consumer<Throwable> consumer) throws E {
+ assertFailsCompilationIf(true, action, consumer);
+ }
+
+ public static <E extends Throwable> void assertFailsCompilationIf(
+ boolean condition, ThrowingAction<E> action) throws E {
+ assertFailsCompilationIf(condition, action, null);
+ }
+
+ public static <E extends Throwable> void assertFailsCompilationIf(
+ boolean condition, ThrowingAction<E> action, Consumer<Throwable> consumer) throws E {
+ assertThrowsIf(condition, CompilationFailedException.class, action, consumer);
+ }
+
+ public static <E extends Throwable> void assertThrowsIf(
+ boolean condition, Class<? extends Throwable> clazz, ThrowingAction<E> action) throws E {
+ assertThrowsIf(condition, clazz, action, null);
+ }
+
+ public static <E extends Throwable> void assertThrowsIf(
+ boolean condition,
+ Class<? extends Throwable> clazz,
+ ThrowingAction<E> action,
+ Consumer<Throwable> consumer)
+ throws E {
+ if (condition) {
+ try {
+ action.execute();
+ fail("Expected action to fail with an exception, but succeeded");
+ } catch (Throwable e) {
+ assertEquals(printStackTraceToString(e), clazz, e.getClass());
+ if (consumer != null) {
+ consumer.accept(e);
+ }
+ }
+ } else {
+ action.execute();
+ }
+ }
+
+ private static String printStackTraceToString(Throwable e) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (PrintStream ps = new PrintStream(baos)) {
+ e.printStackTrace(ps);
+ }
+ return baos.toString();
+ }
+}