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 64289f3c3e9..e8fdf34d09d 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 @@ -47119,6 +47119,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/sam/kt54600.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Test @TestMetadata("nonInlinedSamWrapper.kt") public void testNonInlinedSamWrapper() throws Exception { diff --git a/compiler/testData/codegen/box/sam/kt56188.kt b/compiler/testData/codegen/box/sam/kt56188.kt new file mode 100644 index 00000000000..919dfbb4d90 --- /dev/null +++ b/compiler/testData/codegen/box/sam/kt56188.kt @@ -0,0 +1,28 @@ +// IGNORE_BACKEND: JS + +fun interface Supplier { + fun get(): SupplierTP +} + +fun foo(t: FooTP): Supplier { + return Supplier { t } as Supplier +} + +// Let's do some enterprise-style programming shall we +fun interface SupplierInvoker> { + fun invokeSupplier(supplier: SupplierInvokerTP2): SupplierInvokerTP1 +} + +fun > bar(): SupplierInvoker { + return SupplierInvoker { supplier -> supplier.get() } as SupplierInvoker +} + +fun box(): String { + val result1 = foo("OK").get() + if (result1 != "OK") return result1 + + val result2 = bar>().invokeSupplier { "OK" } + if (result2 != "OK") return result2 + + return "OK" +} 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 fa6473bcf5e..22c7078cbe6 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 @@ -45133,6 +45133,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/sam/kt54600.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Test @TestMetadata("nonInlinedSamWrapper.kt") public void testNonInlinedSamWrapper() 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 740ba5061c9..3447cf75098 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 @@ -47119,6 +47119,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/sam/kt54600.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Test @TestMetadata("nonInlinedSamWrapper.kt") public void testNonInlinedSamWrapper() 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 fc156633214..67f8d0b1787 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -36416,6 +36416,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/sam/kt54600.kt"); } + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @TestMetadata("nonInlinedSamWrapper.kt") public void testNonInlinedSamWrapper() throws Exception { runTest("compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java index d2d83863a20..ebf28f9c169 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java @@ -33693,6 +33693,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/sam/kt17765.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestDataPath("$PROJECT_ROOT") diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java index 6f9393990d7..ed05dca1930 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java @@ -33873,6 +33873,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { runTest("compiler/testData/codegen/box/sam/kt17765.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestDataPath("$PROJECT_ROOT") diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index 5bd6a5dd424..807e9862e08 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -33873,6 +33873,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/sam/kt17765.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestDataPath("$PROJECT_ROOT") diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java index 2749bf32aad..63edaaf868e 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java @@ -33873,6 +33873,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes runTest("compiler/testData/codegen/box/sam/kt17765.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestDataPath("$PROJECT_ROOT") diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 8919af75bce..1d5dfaaa4e5 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -30321,6 +30321,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/sam/kt17765.kt"); } + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt index 9702bd6db15..62bb9c543a1 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.* import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl +import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* @@ -130,7 +131,7 @@ internal class FunctionReferenceLowering(val generationState: NativeGenerationSt return super.visitTypeOperator(expression) } reference.transformChildrenVoid() - return transformFunctionReference(reference, expression.typeOperand.erasure()) + return transformFunctionReference(reference, expression.typeOperand) } return super.visitTypeOperator(expression) } @@ -254,13 +255,18 @@ internal class FunctionReferenceLowering(val generationState: NativeGenerationSt typeParametersFromEnclosingScope.zip(functionReferenceClass.typeParameters).toMap() ) - private val functionParameterAndReturnTypes = (functionReference.type as IrSimpleType).arguments.map { - when (it) { - is IrTypeProjection -> typeParameterRemapper.remapType(it.type) - is IrStarProjection -> context.irBuiltIns.anyNType + private fun IrType.remappedTypeArguments(): List { + if (this !is IrSimpleType) return emptyList() + return arguments.mapIndexed { index, typeArgument -> + when (typeArgument) { + is IrTypeProjection -> typeParameterRemapper.remapType(typeArgument.type) + is IrStarProjection -> (classifier as IrClassSymbol).owner.typeParameters[index].defaultType.erasure() + } } } + private val functionParameterAndReturnTypes = functionReference.type.remappedTypeArguments() + private val functionParameterTypes = functionParameterAndReturnTypes.dropLast(1) private val functionReturnType = functionParameterAndReturnTypes.last() @@ -330,9 +336,10 @@ internal class FunctionReferenceLowering(val generationState: NativeGenerationSt val superTypes = mutableListOf(superClass) val transformedSuperMethod: IrSimpleFunction if (samSuperType != null) { - superTypes += samSuperType - val samSuperClass = samSuperType.classOrNull ?: error("Expected a class but was: ${samSuperType.render()}") - transformedSuperMethod = samSuperClass.functions.single { it.owner.modality == Modality.ABSTRACT }.owner + val remappedSuperType = (samSuperType.classOrNull ?: error("Expected a class but was: ${samSuperType.render()}")) + .typeWith(samSuperType.remappedTypeArguments()) + superTypes += remappedSuperType + transformedSuperMethod = remappedSuperType.classOrNull!!.functions.single { it.owner.modality == Modality.ABSTRACT }.owner } else { val numberOfParameters = unboundFunctionParameters.size if (isKSuspendFunction) { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java index da606b7fa74..9e82d21ce76 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java @@ -37399,6 +37399,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes runTest("compiler/testData/codegen/box/sam/kt17765.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestDataPath("$PROJECT_ROOT") diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index 1e23ff3fcd0..fac3a9f01d0 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -36938,6 +36938,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/sam/kt17765.kt"); } + @Test + @TestMetadata("kt56188.kt") + public void testKt56188() throws Exception { + runTest("compiler/testData/codegen/box/sam/kt56188.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestDataPath("$PROJECT_ROOT")