Don't optimize null checks for unsafe primitive type boxes as well

#KT-34829 Fixed
This commit is contained in:
Alexander Udalov
2019-11-08 18:54:33 +01:00
parent b80e157381
commit 228fdb4436
5 changed files with 37 additions and 2 deletions
@@ -3871,8 +3871,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
Type type = expressionType(exp);
StackValue argument = pregeneratedExpr != null ? pregeneratedExpr : gen(exp);
if (kotlinType == null ||
!AsmUtil.isPrimitive(type) && (TypeUtils.isNullableType(kotlinType) || !InlineClassesUtilsKt.isInlineClassType(kotlinType))) {
if (kotlinType == null || TypeUtils.isNullableType(kotlinType) || !InlineClassesUtilsKt.isInlineClassType(kotlinType)) {
return StackValue.compareWithNull(argument, (KtTokens.EQEQ == opToken || KtTokens.EQEQEQ == opToken) ? IFNONNULL : IFNULL);
} else {
// If exp is an unboxed inline class value the comparison is vacuous
@@ -0,0 +1,21 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM_IR
// FILE: Unsound.java
import test.Wrap;
public class Unsound {
public static <T> Wrap<T> get() {
return new Wrap<T>(null);
}
}
// FILE: 1.kt
package test
class Wrap<T>(val x: T)
// JVM IR generates bytecode that fails with NPE because it unwraps the value with `Number.intValue()`,
// whereas JVM generates a simple null check without unwrapping the box.
fun box(): String = if ((Unsound.get<Int>() as Wrap<Int>).x == null) "OK" else "Fail"
@@ -17925,6 +17925,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/platformTypes/unsafeNullCheck.kt");
}
@TestMetadata("unsafeNullCheckWithPrimitive.kt")
public void testUnsafeNullCheckWithPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/platformTypes/unsafeNullCheckWithPrimitive.kt");
}
@TestMetadata("compiler/testData/codegen/box/platformTypes/primitives")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -17925,6 +17925,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/platformTypes/unsafeNullCheck.kt");
}
@TestMetadata("unsafeNullCheckWithPrimitive.kt")
public void testUnsafeNullCheckWithPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/platformTypes/unsafeNullCheckWithPrimitive.kt");
}
@TestMetadata("compiler/testData/codegen/box/platformTypes/primitives")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -16485,6 +16485,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/platformTypes/unsafeNullCheck.kt");
}
@TestMetadata("unsafeNullCheckWithPrimitive.kt")
public void testUnsafeNullCheckWithPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/platformTypes/unsafeNullCheckWithPrimitive.kt");
}
@TestMetadata("compiler/testData/codegen/box/platformTypes/primitives")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)