diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/Callable.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/Callable.kt index a6f3ab50718..b8bc3de8286 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/Callable.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/Callable.kt @@ -36,7 +36,8 @@ interface Callable { fun isStaticCall(): Boolean fun invokeMethodWithArguments(resolvedCall: ResolvedCall<*>, receiver: StackValue, codegen: ExpressionCodegen): StackValue { - return StackValue.functionCall(returnType, resolvedCall.resultingDescriptor.returnType) { + // it's important to use unsubstituted return type here to unbox value if it comes from type variable + return StackValue.functionCall(returnType, resolvedCall.resultingDescriptor.original.returnType) { codegen.invokeMethodWithArguments(this, resolvedCall, receiver) } } diff --git a/compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt b/compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt new file mode 100644 index 00000000000..6aac12ec4ed --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt @@ -0,0 +1,23 @@ +// !LANGUAGE: +InlineClasses + +inline class Result(val a: Any?) { + fun typed(): T = a as T +} + +fun box(): String { + val asInt = Result(19) + val asString = Result("sample") + val asResult = Result>(asInt) + + val unboxedInt = asInt.typed() + val unboxedString = asString.typed() + val unboxedResult = asResult.typed() + + if (unboxedInt != 19) return "fail" + if (unboxedString != "sample") return "fail" + if (unboxedResult.typed() != 19) return "fail" + + if (asResult.typed().typed() != 19) return "fail" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassFromParameterizedType.kt b/compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassFromParameterizedType.kt new file mode 100644 index 00000000000..70ac452df29 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassFromParameterizedType.kt @@ -0,0 +1,28 @@ +// !LANGUAGE: +InlineClasses + +inline class Result(val a: Any?) { + fun typed(): T = a as T +} + +fun materialize(): K = TODO() + +fun test(asInt: Result, asString: Result, asResult: Result>) { + val a1 = materialize>() // unbox + val a2 = materialize>>() // unbox + + val b1 = asInt.typed() // intValue + val b2 = asString.typed() + + val c1 = asResult.typed() // unbox + + materialize>() + asInt.typed() + asString.typed() + asResult.typed() +} + +// 0 INVOKESTATIC Result\$Erased.box +// 3 INVOKEVIRTUAL Result.unbox + +// 0 valueOf +// 1 intValue \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/commonSystemForInnerCalls.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/commonSystemForInnerCalls.txt new file mode 100644 index 00000000000..3d67005d3ee --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/commonSystemForInnerCalls.txt @@ -0,0 +1,5 @@ +package + +public fun bar(/*0*/ i: kotlin.Int): kotlin.Unit +public fun foo(): kotlin.Int +public fun test(): kotlin.Unit 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 3c547f71699..b75566047fb 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 @@ -10623,6 +10623,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("checkUnboxingResultFromTypeVariable.kt") + public void testCheckUnboxingResultFromTypeVariable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt"); + doTest(fileName); + } + @TestMetadata("computablePropertyInsideInlineClass.kt") public void testComputablePropertyInsideInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index ac95c722fa0..8d580a49710 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -10623,6 +10623,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("checkUnboxingResultFromTypeVariable.kt") + public void testCheckUnboxingResultFromTypeVariable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt"); + doTest(fileName); + } + @TestMetadata("computablePropertyInsideInlineClass.kt") public void testComputablePropertyInsideInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 79368b61011..6b66bc38c1b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -2079,6 +2079,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { doTest(fileName); } + @TestMetadata("unboxInlineClassFromParameterizedType.kt") + public void testUnboxInlineClassFromParameterizedType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassFromParameterizedType.kt"); + doTest(fileName); + } + @TestMetadata("unboxInlineClassesAfterSmartCasts.kt") public void testUnboxInlineClassesAfterSmartCasts() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassesAfterSmartCasts.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d2177ccdbdc..ea66373112f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -10623,6 +10623,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("checkUnboxingResultFromTypeVariable.kt") + public void testCheckUnboxingResultFromTypeVariable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt"); + doTest(fileName); + } + @TestMetadata("computablePropertyInsideInlineClass.kt") public void testComputablePropertyInsideInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.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 3a9b4e4a078..44407f1453c 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 @@ -11685,6 +11685,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("checkUnboxingResultFromTypeVariable.kt") + public void testCheckUnboxingResultFromTypeVariable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkUnboxingResultFromTypeVariable.kt"); + doTest(fileName); + } + @TestMetadata("computablePropertyInsideInlineClass.kt") public void testComputablePropertyInsideInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");