Skip to content

Commit 27cfcb9

Browse files
committed
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
1 parent 5b9268a commit 27cfcb9

File tree

12 files changed

+86
-23
lines changed

12 files changed

+86
-23
lines changed

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltInsOverDescriptors.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import org.jetbrains.kotlin.ir.types.*
3232
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeBuilder
3333
import org.jetbrains.kotlin.ir.types.impl.buildSimpleType
3434
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
35-
import org.jetbrains.kotlin.ir.util.*
35+
import org.jetbrains.kotlin.ir.util.SymbolTable
36+
import org.jetbrains.kotlin.ir.util.TypeTranslator
37+
import org.jetbrains.kotlin.ir.util.functions
38+
import org.jetbrains.kotlin.ir.util.referenceClassifier
3639
import org.jetbrains.kotlin.name.FqName
3740
import org.jetbrains.kotlin.name.Name
3841
import org.jetbrains.kotlin.resolve.scopes.MemberScope
@@ -54,7 +57,7 @@ class IrBuiltInsOverDescriptors(
5457
get() =
5558
synchronized(this) {
5659
if (_functionFactory == null) {
57-
_functionFactory = IrDescriptorBasedFunctionFactory(this, symbolTable)
60+
_functionFactory = IrDescriptorBasedFunctionFactory(this, symbolTable, typeTranslator)
5861
}
5962
_functionFactory!!
6063
}

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrDescriptorBasedFunctionFactory.kt

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ abstract class IrAbstractDescriptorBasedFunctionFactory {
8585
class IrDescriptorBasedFunctionFactory(
8686
private val irBuiltIns: IrBuiltInsOverDescriptors,
8787
private val symbolTable: SymbolTable,
88+
private val typeTranslator: TypeTranslator,
8889
getPackageFragment: ((PackageFragmentDescriptor) -> IrPackageFragment)? = null,
8990
// Needed for JS and Wasm backends to "preload" interfaces that can referenced during lowerings
9091
private val referenceFunctionsWhenKFunctionAreReferenced: Boolean = false,
@@ -257,29 +258,16 @@ class IrDescriptorBasedFunctionFactory(
257258
}
258259
}
259260

260-
private fun IrClass.createThisReceiver(descriptorFactory: FunctionDescriptorFactory): IrValueParameter {
261-
val vDescriptor = descriptorFactory.classReceiverParameterDescriptor()
262-
val vSymbol = IrValueParameterSymbolImpl(vDescriptor)
263-
val type = with(IrSimpleTypeBuilder()) {
264-
classifier = symbol
265-
arguments = typeParameters.run {
266-
val builder = IrSimpleTypeBuilder()
267-
mapTo(ArrayList(size)) {
268-
builder.classifier = it.symbol
269-
buildTypeProjection()
270-
}
271-
}
272-
buildSimpleType()
273-
}
274-
val vDeclaration = irFactory.createValueParameter(
275-
offset, offset, classOrigin, vSymbol, SpecialNames.THIS, -1, type, null,
261+
private fun createThisReceiver(descriptorFactory: FunctionDescriptorFactory): IrValueParameter {
262+
val descriptor = descriptorFactory.classReceiverParameterDescriptor()
263+
return irFactory.createValueParameter(
264+
offset, offset, classOrigin, IrValueParameterSymbolImpl(descriptor), SpecialNames.THIS, -1,
265+
typeTranslator.translateType(descriptor.type), null,
276266
isCrossinline = false,
277267
isNoinline = false,
278268
isHidden = false,
279269
isAssignable = false
280270
)
281-
282-
return vDeclaration
283271
}
284272

285273
private fun IrClass.createMembers(isK: Boolean, isSuspend: Boolean, descriptorFactory: FunctionDescriptorFactory) {
@@ -435,7 +423,7 @@ class IrDescriptorBasedFunctionFactory(
435423

436424
val r = klass.createTypeParameters(n, descriptorFactory)
437425

438-
klass.thisReceiver = klass.createThisReceiver(descriptorFactory).also { it.parent = klass }
426+
klass.thisReceiver = createThisReceiver(descriptorFactory).also { it.parent = klass }
439427

440428
klass.superTypes = listOf(with(IrSimpleTypeBuilder()) {
441429
classifier = baseClass

compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrModuleSeria
3838
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerDesc
3939
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerIr
4040
import org.jetbrains.kotlin.ir.declarations.IrFactory
41-
import org.jetbrains.kotlin.ir.declarations.IrFile
4241
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
4342
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
4443
import org.jetbrains.kotlin.ir.descriptors.IrBuiltInsOverDescriptors
@@ -56,7 +55,6 @@ import org.jetbrains.kotlin.konan.util.KlibMetadataFactories
5655
import org.jetbrains.kotlin.library.*
5756
import org.jetbrains.kotlin.library.impl.BuiltInsPlatform
5857
import org.jetbrains.kotlin.library.impl.buildKotlinLibrary
59-
import org.jetbrains.kotlin.library.resolver.KotlinResolvedLibrary
6058
import org.jetbrains.kotlin.metadata.ProtoBuf
6159
import org.jetbrains.kotlin.progress.IncrementalNextRoundException
6260
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus
@@ -429,6 +427,7 @@ fun getIrModuleInfoForKlib(
429427
irBuiltIns.functionFactory = IrDescriptorBasedFunctionFactory(
430428
irBuiltIns,
431429
symbolTable,
430+
typeTranslator,
432431
if (loadFunctionInterfacesIntoStdlib) getFunctionFactoryCallback(deserializedModuleFragments.first()) else null,
433432
true
434433
)
@@ -483,6 +482,7 @@ fun getIrModuleInfoForSourceFiles(
483482
IrDescriptorBasedFunctionFactory(
484483
irBuiltIns,
485484
symbolTable,
485+
psi2IrContext.typeTranslator,
486486
if (loadFunctionInterfacesIntoStdlib) getFunctionFactoryCallback(deserializedModuleFragments.first()) else null,
487487
true
488488
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// WITH_RUNTIME
2+
// WITH_COROUTINES
3+
// IGNORE_BACKEND: JVM
4+
// IGNORE_LIGHT_ANALYSIS
5+
6+
import helpers.*
7+
import kotlin.coroutines.*
8+
9+
fun interface Print {
10+
suspend fun print(msg: String): String
11+
}
12+
13+
object Context : Print by Print(::id)
14+
15+
fun id(x: String): String = x
16+
17+
fun builder(c: suspend () -> Unit) {
18+
c.startCoroutine(EmptyContinuation)
19+
}
20+
21+
fun box(): String {
22+
var result = "Fail"
23+
builder {
24+
result = Context.print("OK")
25+
}
26+
return result
27+
}

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)