diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 71e9301b087..65b5353f9be 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -4322,29 +4322,29 @@ The "returned" value of try expression with no finally is either the last expres KtExpression left = expression.getLeft(); IElementType opToken = expression.getOperationReference().getReferencedNameElementType(); - KotlinType rightType = bindingContext.get(TYPE, expression.getRight()); - assert rightType != null; + KotlinType rightKotlinType = bindingContext.get(TYPE, expression.getRight()); + assert rightKotlinType != null; StackValue value = genQualified(receiver, left); - return StackValue.operation(boxType(asmType(rightType)), v -> { - value.put(boxType(value.type), null, v); + Type boxedRightType = boxType(typeMapper.mapTypeAsDeclaration(rightKotlinType)); + return StackValue.operation(boxedRightType, rightKotlinType, v -> { + value.put(boxType(value.type), value.kotlinType, v); if (value.type == Type.VOID_TYPE) { StackValue.putUnitInstance(v); } boolean safeAs = opToken == KtTokens.AS_SAFE; - Type type = boxType(asmType(rightType)); - if (TypeUtils.isReifiedTypeParameter(rightType)) { - putReifiedOperationMarkerIfTypeIsReifiedParameter(rightType, + if (TypeUtils.isReifiedTypeParameter(rightKotlinType)) { + putReifiedOperationMarkerIfTypeIsReifiedParameter(rightKotlinType, safeAs ? ReifiedTypeInliner.OperationKind.SAFE_AS : ReifiedTypeInliner.OperationKind.AS); - v.checkcast(type); + v.checkcast(boxedRightType); return Unit.INSTANCE; } - CodegenUtilKt.generateAsCast(v, rightType, type, safeAs); + CodegenUtilKt.generateAsCast(v, rightKotlinType, boxedRightType, safeAs); return Unit.INSTANCE; }); diff --git a/compiler/testData/codegen/box/inlineClasses/checkCastToInlineClass.kt b/compiler/testData/codegen/box/inlineClasses/checkCastToInlineClass.kt new file mode 100644 index 00000000000..52425992af8 --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/checkCastToInlineClass.kt @@ -0,0 +1,19 @@ +// !LANGUAGE: +InlineClasses + +inline class UInt(val s: Int) + +fun test(a1: Any, a2: UInt?, a3: Any?, a4: Any?): Int { + val b1 = a1 as UInt + val b2 = a2 as UInt + val b3 = (a3 as UInt?) as UInt + val b4 = (a4 as? UInt) as UInt + return b1.s + b2.s + b3.s + b4.s +} + +fun box(): String { + val u1 = UInt(1) + val u2 = UInt(2) + if (test(u1, u2, u1, u2) != 6) return "fail" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/inlineClasses/asCastForInlineClass.kt b/compiler/testData/codegen/bytecodeText/inlineClasses/asCastForInlineClass.kt new file mode 100644 index 00000000000..1c4c92166d1 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/inlineClasses/asCastForInlineClass.kt @@ -0,0 +1,17 @@ +// !LANGUAGE: +InlineClasses + +inline class UInt(val u: Int) + +fun test(a1: Any, a2: UInt?, a3: Any?, a4: Any?) { + val b1 = a1 as UInt // checkcast, unbox + val b2 = a2 as UInt // unbox + val b3 = a3 as UInt? // checkcast + val b4 = a4 as? UInt // instanceof, checkcast +} + +// 3 CHECKCAST UInt +// 2 INVOKEVIRTUAL UInt.unbox + +// 1 INSTANCEOF UInt + +// 0 intValue \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index ee2ce649937..298fcb20a2b 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -10449,6 +10449,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("checkCastToInlineClass.kt") + public void testCheckCastToInlineClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCastToInlineClass.kt"); + doTest(fileName); + } + @TestMetadata("checkForInstanceOfInlineClass.kt") public void testCheckForInstanceOfInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkForInstanceOfInlineClass.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 3e9fa04ba20..cec51997212 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -10449,6 +10449,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("checkCastToInlineClass.kt") + public void testCheckCastToInlineClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCastToInlineClass.kt"); + doTest(fileName); + } + @TestMetadata("checkForInstanceOfInlineClass.kt") public void testCheckForInstanceOfInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkForInstanceOfInlineClass.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 8cc6e915fdd..eb4e3cd06c0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -1914,6 +1914,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/bytecodeText/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("asCastForInlineClass.kt") + public void testAsCastForInlineClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/asCastForInlineClass.kt"); + doTest(fileName); + } + @TestMetadata("boxResultAfterConstructorCall.kt") public void testBoxResultAfterConstructorCall() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/boxResultAfterConstructorCall.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 55884cf1651..200042498dc 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -10449,6 +10449,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("checkCastToInlineClass.kt") + public void testCheckCastToInlineClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCastToInlineClass.kt"); + doTest(fileName); + } + @TestMetadata("checkForInstanceOfInlineClass.kt") public void testCheckForInstanceOfInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkForInstanceOfInlineClass.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 2c902f0d38e..b214990d491 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 @@ -11433,6 +11433,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("checkCastToInlineClass.kt") + public void testCheckCastToInlineClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCastToInlineClass.kt"); + doTest(fileName); + } + @TestMetadata("checkForInstanceOfInlineClass.kt") public void testCheckForInstanceOfInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkForInstanceOfInlineClass.kt");