diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index c32d2d2b760..96cfd7db7ff 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3871,8 +3871,7 @@ public class ExpressionCodegen extends KtVisitor 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 diff --git a/compiler/testData/codegen/box/platformTypes/unsafeNullCheckWithPrimitive.kt b/compiler/testData/codegen/box/platformTypes/unsafeNullCheckWithPrimitive.kt new file mode 100644 index 00000000000..3546afc1924 --- /dev/null +++ b/compiler/testData/codegen/box/platformTypes/unsafeNullCheckWithPrimitive.kt @@ -0,0 +1,21 @@ +// TARGET_BACKEND: JVM +// IGNORE_BACKEND: JVM_IR +// FILE: Unsound.java + +import test.Wrap; + +public class Unsound { + public static Wrap get() { + return new Wrap(null); + } +} + +// FILE: 1.kt + +package test + +class Wrap(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() as Wrap).x == null) "OK" else "Fail" diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 783eba95473..922a3238f0d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -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) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 8ce6d849ffa..ef7f12c8840 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -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) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 80ecc808ce5..0e25ecc6867 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -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)