summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--jack-tests/tests/com/android/jack/cast/CastTests.java12
-rw-r--r--jack-tests/tests/com/android/jack/cast/explicit002/dx/Tests.java37
-rw-r--r--jack-tests/tests/com/android/jack/cast/explicit002/jack/Explicit002.java27
-rw-r--r--jack/src/com/android/jack/transformations/cast/UselessCastRemover.java14
-rw-r--r--jack/tests/com/android/jack/transformations/cast/UselessCastRemoverTest.java2
5 files changed, 87 insertions, 5 deletions
diff --git a/jack-tests/tests/com/android/jack/cast/CastTests.java b/jack-tests/tests/com/android/jack/cast/CastTests.java
index 6bccf9a1..74016987 100644
--- a/jack-tests/tests/com/android/jack/cast/CastTests.java
+++ b/jack-tests/tests/com/android/jack/cast/CastTests.java
@@ -35,6 +35,10 @@ public class CastTests extends RuntimeTest {
AbstractTestTools.getTestRootDir("com.android.jack.cast.explicit001"),
"com.android.jack.cast.explicit001.dx.Tests");
+ private RuntimeTestInfo EXPLICIT002 = new RuntimeTestInfo(
+ AbstractTestTools.getTestRootDir("com.android.jack.cast.explicit002"),
+ "com.android.jack.cast.explicit002.dx.Tests");
+
private RuntimeTestInfo IMPLICIT001 = new RuntimeTestInfo(
AbstractTestTools.getTestRootDir("com.android.jack.cast.implicit001"),
"com.android.jack.cast.implicit001.dx.Tests");
@@ -63,6 +67,7 @@ public class CastTests extends RuntimeTest {
public static void setUpClass() {
CastTests.class.getClassLoader().setDefaultAssertionStatus(true);
}
+
@Test
@Category(RuntimeRegressionTest.class)
public void explicit001() throws Exception {
@@ -71,6 +76,12 @@ public class CastTests extends RuntimeTest {
@Test
@Category(RuntimeRegressionTest.class)
+ public void explicit002() throws Exception {
+ new RuntimeTestHelper(EXPLICIT002).compileAndRunTest();
+ }
+
+ @Test
+ @Category(RuntimeRegressionTest.class)
public void implicit001() throws Exception {
new RuntimeTestHelper(IMPLICIT001).compileAndRunTest();
}
@@ -129,6 +140,7 @@ public class CastTests extends RuntimeTest {
@Override
protected void fillRtTestInfos() {
rtTestInfos.add(EXPLICIT001);
+ rtTestInfos.add(EXPLICIT002);
rtTestInfos.add(IMPLICIT001);
rtTestInfos.add(IMPLICIT002);
rtTestInfos.add(IMPLICIT003);
diff --git a/jack-tests/tests/com/android/jack/cast/explicit002/dx/Tests.java b/jack-tests/tests/com/android/jack/cast/explicit002/dx/Tests.java
new file mode 100644
index 00000000..4ce0161b
--- /dev/null
+++ b/jack-tests/tests/com/android/jack/cast/explicit002/dx/Tests.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.jack.cast.explicit002.dx;
+
+import com.android.jack.cast.explicit002.jack.Explicit002;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests about casting of null.
+ */
+public class Tests {
+
+ @Test
+ public void test1() {
+ try {
+ Explicit002.castWithNull();
+ Assert.fail();
+ } catch (NullPointerException e) {
+ }
+ }
+}
diff --git a/jack-tests/tests/com/android/jack/cast/explicit002/jack/Explicit002.java b/jack-tests/tests/com/android/jack/cast/explicit002/jack/Explicit002.java
new file mode 100644
index 00000000..b4ea00d7
--- /dev/null
+++ b/jack-tests/tests/com/android/jack/cast/explicit002/jack/Explicit002.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.jack.cast.explicit002.jack;
+
+public class Explicit002 {
+
+ public static void castWithNull() {
+ useInt(((int[]) null)[0]);
+ }
+
+ public static void useInt(int i ) {
+ }
+}
diff --git a/jack/src/com/android/jack/transformations/cast/UselessCastRemover.java b/jack/src/com/android/jack/transformations/cast/UselessCastRemover.java
index f50fc1f6..42e9c4ea 100644
--- a/jack/src/com/android/jack/transformations/cast/UselessCastRemover.java
+++ b/jack/src/com/android/jack/transformations/cast/UselessCastRemover.java
@@ -19,7 +19,9 @@ package com.android.jack.transformations.cast;
import com.android.jack.Options;
import com.android.jack.ir.ast.JCastOperation;
import com.android.jack.ir.ast.JDynamicCastOperation;
+import com.android.jack.ir.ast.JExpression;
import com.android.jack.ir.ast.JMethod;
+import com.android.jack.ir.ast.JNullLiteral;
import com.android.jack.ir.ast.JReferenceType;
import com.android.jack.ir.ast.JType;
import com.android.jack.ir.ast.JVisitor;
@@ -57,10 +59,14 @@ public class UselessCastRemover implements RunnableSchedulable<JMethod> {
@Override
public void endVisit(@Nonnull JCastOperation cast) {
JType destType = cast.getCastType();
- JType srcType = cast.getExpr().getType();
- if (srcType instanceof JReferenceType && destType instanceof JReferenceType) {
- if (((JReferenceType) srcType).canBeSafelyUpcast((JReferenceType) destType)) {
- request.append(new Replace(cast, cast.getExpr()));
+ JExpression castedExpr = cast.getExpr();
+ JType srcType = castedExpr.getType();
+ // Do not remove cast of 'null' expression otherwise type is lost
+ if (!(castedExpr instanceof JNullLiteral)) {
+ if (srcType instanceof JReferenceType && destType instanceof JReferenceType) {
+ if (((JReferenceType) srcType).canBeSafelyUpcast((JReferenceType) destType)) {
+ request.append(new Replace(cast, castedExpr));
+ }
}
}
super.endVisit(cast);
diff --git a/jack/tests/com/android/jack/transformations/cast/UselessCastRemoverTest.java b/jack/tests/com/android/jack/transformations/cast/UselessCastRemoverTest.java
index 62d9540f..da5db0ce 100644
--- a/jack/tests/com/android/jack/transformations/cast/UselessCastRemoverTest.java
+++ b/jack/tests/com/android/jack/transformations/cast/UselessCastRemoverTest.java
@@ -85,7 +85,7 @@ public class UselessCastRemoverTest {
public void castNullToString() throws Exception {
final String methodSignature = "castNullToString()Ljava/lang/String;";
- buildMethodAndCheckUselessCastRemover(CAST, methodSignature, true);
+ buildMethodAndCheckUselessCastRemover(CAST, methodSignature, false);
}
@Test