Check other lattice element type before casting.
Otherwise, e.g., if other is range (as shown in b/132897042), meet will
raise NPE (since range's type conversion returns null).
Bug: 132897042
Change-Id: I6236b27422c0601881d90beb85708e84376064c1
diff --git a/src/main/java/com/android/tools/r8/ir/analysis/constant/ConstLatticeElement.java b/src/main/java/com/android/tools/r8/ir/analysis/constant/ConstLatticeElement.java
index 65cbc0b..982f883 100644
--- a/src/main/java/com/android/tools/r8/ir/analysis/constant/ConstLatticeElement.java
+++ b/src/main/java/com/android/tools/r8/ir/analysis/constant/ConstLatticeElement.java
@@ -20,8 +20,10 @@
if (other.isBottom()) {
return other;
}
- if (value.identicalNonValueNonPositionParts(other.asConst().value)) {
- return this;
+ if (other.isConst()) {
+ if (value.identicalNonValueNonPositionParts(other.asConst().value)) {
+ return this;
+ }
}
return Bottom.getInstance();
}
diff --git a/src/test/java/com/android/tools/r8/ir/analysis/constant/B132897042.java b/src/test/java/com/android/tools/r8/ir/analysis/constant/B132897042.java
new file mode 100644
index 0000000..0a6106c
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/analysis/constant/B132897042.java
@@ -0,0 +1,66 @@
+// Copyright (c) 2019, 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.analysis.constant;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.StringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class B132897042 extends TestBase {
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimes().build();
+ }
+
+ private final TestParameters parameters;
+
+ public B132897042(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addLibraryClasses(LibClass.class)
+ .addLibraryFiles(runtimeJar(parameters))
+ .addProgramClasses(TestClass.class)
+ .addKeepRules(StringUtils.lines(
+ "-assumevalues class" + LibClass.class.getName() + " {",
+ " static int SDK_INT return 1..28;",
+ "}"
+ ))
+ .noTreeShaking()
+ .setMinApi(parameters.getRuntime())
+ .compile()
+ .assertNoMessages();
+ }
+
+ static class LibClass {
+ static int SDK_INT;
+ }
+
+ static class TestClass {
+ static void foo() {
+ String v = "0";
+ int x;
+ switch (Integer.parseInt(v)) {
+ case 0:
+ x = LibClass.SDK_INT;
+ break;
+ default:
+ x = 1;
+ }
+ if (x > 12) {
+ System.out.println(x);
+ }
+ }
+ }
+
+}