[K/N] Remap type args of SAM interfaces in FunctionReferenceLowering

#KT-56188 Fixed
This commit is contained in:
Sergej Jaskiewicz
2023-01-26 20:40:20 +01:00
committed by Space Team
parent 61fc3e99b1
commit bd18fd40ea
13 changed files with 107 additions and 8 deletions
@@ -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 {
+28
View File
@@ -0,0 +1,28 @@
// IGNORE_BACKEND: JS
fun interface Supplier<SupplierTP> {
fun get(): SupplierTP
}
fun <FooTP> foo(t: FooTP): Supplier<FooTP> {
return Supplier<FooTP> { t } as Supplier<FooTP>
}
// Let's do some enterprise-style programming shall we
fun interface SupplierInvoker<SupplierInvokerTP1, SupplierInvokerTP2: Supplier<SupplierInvokerTP1>> {
fun invokeSupplier(supplier: SupplierInvokerTP2): SupplierInvokerTP1
}
fun <BarTP1, BarTP2 : Supplier<BarTP1>> bar(): SupplierInvoker<BarTP1, BarTP2> {
return SupplierInvoker<BarTP1, BarTP2> { supplier -> supplier.get() } as SupplierInvoker<BarTP1, BarTP2>
}
fun box(): String {
val result1 = foo("OK").get()
if (result1 != "OK") return result1
val result2 = bar<String, Supplier<String>>().invokeSupplier { "OK" }
if (result2 != "OK") return result2
return "OK"
}
@@ -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 {
@@ -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 {
@@ -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");
@@ -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")
@@ -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")
@@ -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")
@@ -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")
@@ -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)
@@ -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<IrType> {
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) {
@@ -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")
@@ -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")