[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"); 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 @Test
@TestMetadata("nonInlinedSamWrapper.kt") @TestMetadata("nonInlinedSamWrapper.kt")
public void testNonInlinedSamWrapper() throws Exception { 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"); 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 @Test
@TestMetadata("nonInlinedSamWrapper.kt") @TestMetadata("nonInlinedSamWrapper.kt")
public void testNonInlinedSamWrapper() throws Exception { public void testNonInlinedSamWrapper() throws Exception {
@@ -47119,6 +47119,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/sam/kt54600.kt"); 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 @Test
@TestMetadata("nonInlinedSamWrapper.kt") @TestMetadata("nonInlinedSamWrapper.kt")
public void testNonInlinedSamWrapper() throws Exception { public void testNonInlinedSamWrapper() throws Exception {
@@ -36416,6 +36416,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/sam/kt54600.kt"); 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") @TestMetadata("nonInlinedSamWrapper.kt")
public void testNonInlinedSamWrapper() throws Exception { public void testNonInlinedSamWrapper() throws Exception {
runTest("compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt"); 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"); 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 @Nested
@TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestMetadata("compiler/testData/codegen/box/sam/adapters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -33873,6 +33873,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/sam/kt17765.kt"); 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 @Nested
@TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestMetadata("compiler/testData/codegen/box/sam/adapters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -33873,6 +33873,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/sam/kt17765.kt"); 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 @Nested
@TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestMetadata("compiler/testData/codegen/box/sam/adapters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -33873,6 +33873,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/sam/kt17765.kt"); 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 @Nested
@TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestMetadata("compiler/testData/codegen/box/sam/adapters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -30321,6 +30321,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/sam/kt17765.kt"); 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") @TestMetadata("compiler/testData/codegen/box/sam/adapters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @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.declarations.*
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl 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.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
@@ -130,7 +131,7 @@ internal class FunctionReferenceLowering(val generationState: NativeGenerationSt
return super.visitTypeOperator(expression) return super.visitTypeOperator(expression)
} }
reference.transformChildrenVoid() reference.transformChildrenVoid()
return transformFunctionReference(reference, expression.typeOperand.erasure()) return transformFunctionReference(reference, expression.typeOperand)
} }
return super.visitTypeOperator(expression) return super.visitTypeOperator(expression)
} }
@@ -254,13 +255,18 @@ internal class FunctionReferenceLowering(val generationState: NativeGenerationSt
typeParametersFromEnclosingScope.zip(functionReferenceClass.typeParameters).toMap() typeParametersFromEnclosingScope.zip(functionReferenceClass.typeParameters).toMap()
) )
private val functionParameterAndReturnTypes = (functionReference.type as IrSimpleType).arguments.map { private fun IrType.remappedTypeArguments(): List<IrType> {
when (it) { if (this !is IrSimpleType) return emptyList()
is IrTypeProjection -> typeParameterRemapper.remapType(it.type) return arguments.mapIndexed { index, typeArgument ->
is IrStarProjection -> context.irBuiltIns.anyNType 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 functionParameterTypes = functionParameterAndReturnTypes.dropLast(1)
private val functionReturnType = functionParameterAndReturnTypes.last() private val functionReturnType = functionParameterAndReturnTypes.last()
@@ -330,9 +336,10 @@ internal class FunctionReferenceLowering(val generationState: NativeGenerationSt
val superTypes = mutableListOf(superClass) val superTypes = mutableListOf(superClass)
val transformedSuperMethod: IrSimpleFunction val transformedSuperMethod: IrSimpleFunction
if (samSuperType != null) { if (samSuperType != null) {
superTypes += samSuperType val remappedSuperType = (samSuperType.classOrNull ?: error("Expected a class but was: ${samSuperType.render()}"))
val samSuperClass = samSuperType.classOrNull ?: error("Expected a class but was: ${samSuperType.render()}") .typeWith(samSuperType.remappedTypeArguments())
transformedSuperMethod = samSuperClass.functions.single { it.owner.modality == Modality.ABSTRACT }.owner superTypes += remappedSuperType
transformedSuperMethod = remappedSuperType.classOrNull!!.functions.single { it.owner.modality == Modality.ABSTRACT }.owner
} else { } else {
val numberOfParameters = unboundFunctionParameters.size val numberOfParameters = unboundFunctionParameters.size
if (isKSuspendFunction) { if (isKSuspendFunction) {
@@ -37399,6 +37399,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes
runTest("compiler/testData/codegen/box/sam/kt17765.kt"); 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 @Nested
@TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestMetadata("compiler/testData/codegen/box/sam/adapters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -36938,6 +36938,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/sam/kt17765.kt"); 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 @Nested
@TestMetadata("compiler/testData/codegen/box/sam/adapters") @TestMetadata("compiler/testData/codegen/box/sam/adapters")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")