diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 7c07742b835..d5b0fd6c39e 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -16340,6 +16340,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @Test + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @Test @TestMetadata("kt785.kt") public void testKt785() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt index 409b368bc2c..605bbd1a4a5 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt @@ -9,15 +9,17 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.ir.isInlineClassType import org.jetbrains.kotlin.backend.jvm.ir.isInlineParameter import org.jetbrains.kotlin.backend.jvm.lower.inlineclasses.InlineClassAbi -import org.jetbrains.kotlin.backend.jvm.lower.suspendFunctionOriginal import org.jetbrains.kotlin.codegen.IrExpressionLambda import org.jetbrains.kotlin.codegen.JvmKotlinType import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.ValueKind import org.jetbrains.kotlin.codegen.inline.* import org.jetbrains.kotlin.codegen.state.GenerationState -import org.jetbrains.kotlin.ir.declarations.* -import org.jetbrains.kotlin.ir.descriptors.* +import org.jetbrains.kotlin.ir.declarations.IrConstructor +import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin +import org.jetbrains.kotlin.ir.declarations.IrFunction +import org.jetbrains.kotlin.ir.declarations.IrValueParameter +import org.jetbrains.kotlin.ir.descriptors.toIrBasedKotlinType import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrType @@ -195,16 +197,8 @@ class IrDefaultLambda( needReification: Boolean, sourceCompiler: IrSourceCompilerForInline ) : DefaultLambda(lambdaClassType, capturedArgs, irValueParameter.isCrossinline, offset, needReification, sourceCompiler) { - private val typeArguments: MutableList = - (irValueParameter.type as IrSimpleType).arguments.mapTo(mutableListOf()) { (it as IrTypeProjection).type }.apply { - // Suspend function references: `suspend (A) -> B` => `invoke(A, Continuation): Any?` - // TODO: default suspend lambdas are currently uninlinable due to having a state machine - if (irValueParameter.type.isSuspendFunction()) { - val context = sourceCompiler.codegen.context - set(size - 1, context.ir.symbols.continuationClass.typeWith(get(size - 1))) - add(context.irBuiltIns.anyNType) - } - } + + private val typeArguments: MutableList override val invokeMethodParameters: List get() = typeArguments.dropLast(1).map { it.toIrBasedKotlinType() } @@ -213,9 +207,28 @@ class IrDefaultLambda( get() = typeArguments.last().toIrBasedKotlinType() init { + val context = sourceCompiler.codegen.context + + typeArguments = + (irValueParameter.type as IrSimpleType).arguments + .mapTo(mutableListOf()) { + when (it) { + is IrTypeProjection -> it.type + else -> context.irBuiltIns.anyNType + } + } + .apply { + // Suspend function references: `suspend (A) -> B` => `invoke(A, Continuation): Any?` + // TODO: default suspend lambdas are currently uninlinable due to having a state machine + if (irValueParameter.type.isSuspendFunction()) { + set(size - 1, context.ir.symbols.continuationClass.typeWith(get(size - 1))) + add(context.irBuiltIns.anyNType) + } + } + val base = if (isPropertyReference) OperatorNameConventions.GET.asString() else OperatorNameConventions.INVOKE.asString() val name = InlineClassAbi.hashSuffix( - sourceCompiler.codegen.context.state.useOldManglingSchemeForFunctionsWithInlineClassesInSignatures, + context.state.useOldManglingSchemeForFunctionsWithInlineClassesInSignatures, typeArguments.dropLast(1), typeArguments.last().takeIf { it.isInlineClassType() } )?.let { "$base-$it" } ?: base @@ -223,7 +236,7 @@ class IrDefaultLambda( // it would be better to map to a non-erased signature if not a property reference. if (loadInvoke(sourceCompiler, base, Method(name, AsmTypes.OBJECT_TYPE, Array(typeArguments.size - 1) { AsmTypes.OBJECT_TYPE }))) { // If the loaded method is `invoke(Object, ...) -> Object`, then it expects boxed parameters and returns a boxed value. - typeArguments.replaceAll { sourceCompiler.codegen.context.irBuiltIns.anyNType } + typeArguments.replaceAll { context.irBuiltIns.anyNType } } } } diff --git a/compiler/testData/codegen/box/functions/kt47449.kt b/compiler/testData/codegen/box/functions/kt47449.kt new file mode 100644 index 00000000000..0cfeb255fb6 --- /dev/null +++ b/compiler/testData/codegen/box/functions/kt47449.kt @@ -0,0 +1,33 @@ +// IGNORE_BACKEND: JS +// IGNORE_BACKEND: WASM + +typealias EmptyFunctionResult = () -> T + +typealias LoggingFunctionType = (tag: String, message: String, throwable: Throwable?) -> T + +fun box(): String { + tryAndLog { + throw RuntimeException() + } + return "OK" +} + +inline fun tryAndLog( + title: String = "", + message: String = "", + logger: LoggingFunctionType<*> = L::error, + throwableAction: EmptyFunctionResult +): T? { + return try { + throwableAction() + } catch (e: Throwable) { + logger(title, message, e) + return null + } +} + +open class LLogger { + fun error(tag: String, message: String, exception: Throwable?): Unit {} +} + +object L : LLogger() diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 995266ebbef..fb455fe1901 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -16310,6 +16310,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @Test + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @Test @TestMetadata("kt785.kt") public void testKt785() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index a771a8b6e1c..b871627cb5a 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -16340,6 +16340,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @Test + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @Test @TestMetadata("kt785.kt") public void testKt785() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index e0a7ff20abd..7bcb83c34fe 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -13450,6 +13450,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @TestMetadata("kt785.kt") public void testKt785() throws Exception { runTest("compiler/testData/codegen/box/functions/kt785.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 8d08caeb8ef..05d0637cacb 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -11864,6 +11864,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @TestMetadata("kt785.kt") public void testKt785() throws Exception { runTest("compiler/testData/codegen/box/functions/kt785.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index b1f1ca2992f..09a50bbc603 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -11270,6 +11270,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @TestMetadata("kt785.kt") public void testKt785() throws Exception { runTest("compiler/testData/codegen/box/functions/kt785.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 84045a9e2fb..355ef4cd11d 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -11270,6 +11270,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @TestMetadata("kt785.kt") public void testKt785() throws Exception { runTest("compiler/testData/codegen/box/functions/kt785.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 903bb5ea0f5..b9f2f317168 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -5745,6 +5745,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/functions/kt395.kt"); } + @TestMetadata("kt47449.kt") + public void testKt47449() throws Exception { + runTest("compiler/testData/codegen/box/functions/kt47449.kt"); + } + @TestMetadata("kt785.kt") public void testKt785() throws Exception { runTest("compiler/testData/codegen/box/functions/kt785.kt");