IR: fix thisReceiver parameter type for function classes

Incorrect builder was used at line 269, which led to non-sensible type
in `IrClass.thisReceiver` for function types, such as
`SuspendFunction1<SuspendFunction1, SuspendFunction1>` in the linked
issue.

Avoid creating types manually completely to simplify this code and fix
the bug.

 #KT-49168 Fixed
This commit is contained in:
Alexander Udalov
2021-11-09 22:08:48 +01:00
parent 5b9268a108
commit 27cfcb9b3d
12 changed files with 86 additions and 23 deletions
@@ -9762,6 +9762,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/kt46813.kt");
}
@Test
@TestMetadata("kt49168.kt")
public void testKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@Test
@TestMetadata("lastExpressionIsLoop.kt")
public void testLastExpressionIsLoop() throws Exception {
@@ -32,7 +32,10 @@ import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeBuilder
import org.jetbrains.kotlin.ir.types.impl.buildSimpleType
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.referenceClassifier
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
@@ -54,7 +57,7 @@ class IrBuiltInsOverDescriptors(
get() =
synchronized(this) {
if (_functionFactory == null) {
_functionFactory = IrDescriptorBasedFunctionFactory(this, symbolTable)
_functionFactory = IrDescriptorBasedFunctionFactory(this, symbolTable, typeTranslator)
}
_functionFactory!!
}
@@ -85,6 +85,7 @@ abstract class IrAbstractDescriptorBasedFunctionFactory {
class IrDescriptorBasedFunctionFactory(
private val irBuiltIns: IrBuiltInsOverDescriptors,
private val symbolTable: SymbolTable,
private val typeTranslator: TypeTranslator,
getPackageFragment: ((PackageFragmentDescriptor) -> IrPackageFragment)? = null,
// Needed for JS and Wasm backends to "preload" interfaces that can referenced during lowerings
private val referenceFunctionsWhenKFunctionAreReferenced: Boolean = false,
@@ -257,29 +258,16 @@ class IrDescriptorBasedFunctionFactory(
}
}
private fun IrClass.createThisReceiver(descriptorFactory: FunctionDescriptorFactory): IrValueParameter {
val vDescriptor = descriptorFactory.classReceiverParameterDescriptor()
val vSymbol = IrValueParameterSymbolImpl(vDescriptor)
val type = with(IrSimpleTypeBuilder()) {
classifier = symbol
arguments = typeParameters.run {
val builder = IrSimpleTypeBuilder()
mapTo(ArrayList(size)) {
builder.classifier = it.symbol
buildTypeProjection()
}
}
buildSimpleType()
}
val vDeclaration = irFactory.createValueParameter(
offset, offset, classOrigin, vSymbol, SpecialNames.THIS, -1, type, null,
private fun createThisReceiver(descriptorFactory: FunctionDescriptorFactory): IrValueParameter {
val descriptor = descriptorFactory.classReceiverParameterDescriptor()
return irFactory.createValueParameter(
offset, offset, classOrigin, IrValueParameterSymbolImpl(descriptor), SpecialNames.THIS, -1,
typeTranslator.translateType(descriptor.type), null,
isCrossinline = false,
isNoinline = false,
isHidden = false,
isAssignable = false
)
return vDeclaration
}
private fun IrClass.createMembers(isK: Boolean, isSuspend: Boolean, descriptorFactory: FunctionDescriptorFactory) {
@@ -435,7 +423,7 @@ class IrDescriptorBasedFunctionFactory(
val r = klass.createTypeParameters(n, descriptorFactory)
klass.thisReceiver = klass.createThisReceiver(descriptorFactory).also { it.parent = klass }
klass.thisReceiver = createThisReceiver(descriptorFactory).also { it.parent = klass }
klass.superTypes = listOf(with(IrSimpleTypeBuilder()) {
classifier = baseClass
@@ -38,7 +38,6 @@ import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrModuleSeria
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerDesc
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerIr
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltInsOverDescriptors
@@ -56,7 +55,6 @@ import org.jetbrains.kotlin.konan.util.KlibMetadataFactories
import org.jetbrains.kotlin.library.*
import org.jetbrains.kotlin.library.impl.BuiltInsPlatform
import org.jetbrains.kotlin.library.impl.buildKotlinLibrary
import org.jetbrains.kotlin.library.resolver.KotlinResolvedLibrary
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.progress.IncrementalNextRoundException
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus
@@ -429,6 +427,7 @@ fun getIrModuleInfoForKlib(
irBuiltIns.functionFactory = IrDescriptorBasedFunctionFactory(
irBuiltIns,
symbolTable,
typeTranslator,
if (loadFunctionInterfacesIntoStdlib) getFunctionFactoryCallback(deserializedModuleFragments.first()) else null,
true
)
@@ -483,6 +482,7 @@ fun getIrModuleInfoForSourceFiles(
IrDescriptorBasedFunctionFactory(
irBuiltIns,
symbolTable,
psi2IrContext.typeTranslator,
if (loadFunctionInterfacesIntoStdlib) getFunctionFactoryCallback(deserializedModuleFragments.first()) else null,
true
)
+27
View File
@@ -0,0 +1,27 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: JVM
// IGNORE_LIGHT_ANALYSIS
import helpers.*
import kotlin.coroutines.*
fun interface Print {
suspend fun print(msg: String): String
}
object Context : Print by Print(::id)
fun id(x: String): String = x
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var result = "Fail"
builder {
result = Context.print("OK")
}
return result
}
@@ -9684,6 +9684,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/kt46813.kt");
}
@Test
@TestMetadata("kt49168.kt")
public void testKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@Test
@TestMetadata("lastExpressionIsLoop.kt")
public void testLastExpressionIsLoop() throws Exception {
@@ -9762,6 +9762,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/kt46813.kt");
}
@Test
@TestMetadata("kt49168.kt")
public void testKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@Test
@TestMetadata("lastExpressionIsLoop.kt")
public void testLastExpressionIsLoop() throws Exception {
@@ -7230,6 +7230,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/kt24135.kt");
}
@TestMetadata("kt49168.kt")
public void ignoreKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@TestMetadata("suspendFunctionAsSupertype.kt")
public void ignoreSuspendFunctionAsSupertype() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/suspendFunctionAsSupertype.kt");
@@ -6752,6 +6752,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/coroutines/kt46813.kt");
}
@TestMetadata("kt49168.kt")
public void testKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@TestMetadata("lastExpressionIsLoop.kt")
public void testLastExpressionIsLoop() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/lastExpressionIsLoop.kt");
@@ -5981,6 +5981,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/coroutines/kt46813.kt");
}
@TestMetadata("kt49168.kt")
public void testKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@TestMetadata("lastExpressionIsLoop.kt")
public void testLastExpressionIsLoop() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/lastExpressionIsLoop.kt");
@@ -6998,6 +6998,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/kt46813.kt");
}
@Test
@TestMetadata("kt49168.kt")
public void testKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@Test
@TestMetadata("lastExpressionIsLoop.kt")
public void testLastExpressionIsLoop() throws Exception {
@@ -6956,6 +6956,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/kt46813.kt");
}
@Test
@TestMetadata("kt49168.kt")
public void testKt49168() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/kt49168.kt");
}
@Test
@TestMetadata("lastExpressionIsLoop.kt")
public void testLastExpressionIsLoop() throws Exception {