From 3d2f5f4bc14d34fd1e727f58c545a07202e84d89 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 18 Nov 2020 16:57:13 +0300 Subject: [PATCH] KT-42018 explicitly cast inline class values in safe-as --- .../codegen/ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../kotlin/backend/jvm/lower/TypeOperatorLowering.kt | 8 +++++++- .../testData/codegen/box/inlineClasses/UIntSafeAsInt.kt | 6 ++++++ .../kotlin/codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../kotlin/codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../es6/semantics/IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ .../js/test/semantics/JsCodegenBoxTestGenerated.java | 5 +++++ .../wasm/semantics/IrCodegenBoxWasmTestGenerated.java | 5 +++++ 10 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index f99781bf3e9..b7a9c69695a 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -14182,6 +14182,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt"); diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt index 6b7c69ddab9..e272e123bfa 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrValueSymbol import org.jetbrains.kotlin.ir.types.* +import org.jetbrains.kotlin.ir.util.isInlined import org.jetbrains.kotlin.ir.util.render import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid @@ -125,10 +126,15 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil IrStatementOrigin.SAFE_CALL, irType = context.irBuiltIns.anyNType ) { valueSymbol -> + val thenPart = + if (valueSymbol.owner.type.isInlined()) + lowerCast(irGet(valueSymbol.owner), expression.typeOperand) + else + irGet(valueSymbol.owner) irIfThenElse( expression.type, lowerInstanceOf(irGet(valueSymbol.owner), expression.typeOperand.makeNotNull()), - irGet(valueSymbol.owner), + thenPart, irNull(expression.type) ) } diff --git a/compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt b/compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt new file mode 100644 index 00000000000..c1d311545bf --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +// KJS_WITH_FULL_RUNTIME + +fun testUIntSafeAsInt(x: UInt) = x as? Int + +fun box(): String = if (testUIntSafeAsInt(1U) != null) "fail" else "OK" \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a2d7fefb626..aeb7c5cf9b2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -15582,6 +15582,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 8d36b92445c..06ce5e9fda9 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -15582,6 +15582,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index feacb8248f8..3afc9182e33 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -14182,6 +14182,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index b29caf2ef46..80951a4b7fa 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -12182,6 +12182,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 108f1e3c519..61fc3d19434 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -12182,6 +12182,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index a9223f8868c..4f712e2fd20 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -12247,6 +12247,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index f5453b2e20f..308270614c8 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -6702,6 +6702,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/inlineClasses/typeChecksForInlineClasses.kt"); } + @TestMetadata("UIntSafeAsInt.kt") + public void testUIntSafeAsInt() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt"); + } + @TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt") public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");