Skip to content

Commit 6e3e0ed

Browse files
udalovSpace Team
authored andcommitted
JVM: workaround inline class mapping issue in old backend
The real fix will be removal of `StackValue` and `StaticTypeMapperForOldBackend`, and it will be done as a part of KT-71197. This change is a relatively safe workaround for the issue. The problem is caused by 588361c, where `computeExpandedTypeForInlineClass` started to use more things from `TypeSystemContext`, namely `builtIns`, to be able to correctly map generic types based on inline classes over arrays. #KT-79547 Fixed
1 parent b389359 commit 6e3e0ed

File tree

10 files changed

+80
-4
lines changed

10 files changed

+80
-4
lines changed

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLBlackBoxTestGenerated.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.

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLReversedBlackBoxTestGenerated.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/backend/src/org/jetbrains/kotlin/codegen/state/StaticTypeMapperForOldBackend.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.jetbrains.kotlin.codegen.state
77

8+
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
89
import org.jetbrains.kotlin.codegen.signature.AsmTypeFactory
910
import org.jetbrains.kotlin.descriptors.ClassDescriptor
1011
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
@@ -18,6 +19,7 @@ import org.jetbrains.kotlin.types.KotlinType
1819
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
1920
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
2021
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
22+
import org.jetbrains.kotlin.types.typeUtil.builtIns
2123
import org.jetbrains.org.objectweb.asm.Type
2224

2325
// This class exists only because it's used to box/unbox inline classes in the static method StackValue.coerce in the old backend, which
@@ -26,7 +28,7 @@ object StaticTypeMapperForOldBackend : KotlinTypeMapperBase() {
2628
override val typeSystem: TypeSystemCommonBackendContext
2729
get() = SimpleClassicTypeSystemContext
2830

29-
private val staticTypeMappingConfiguration = object : TypeMappingConfiguration<Type> {
31+
private class Configuration(override val builtIns: KotlinBuiltIns) : TypeMappingConfiguration<Type> {
3032
override fun commonSupertype(types: Collection<KotlinType>): KotlinType {
3133
return CommonSupertypes.commonSupertype(types)
3234
}
@@ -51,7 +53,7 @@ object StaticTypeMapperForOldBackend : KotlinTypeMapperBase() {
5153
override fun mapClass(classifier: ClassifierDescriptor): Type = TODO("Should not be called")
5254

5355
override fun mapTypeCommon(type: KotlinTypeMarker, mode: TypeMappingMode): Type {
54-
return mapType(type as KotlinType, AsmTypeFactory, mode, staticTypeMappingConfiguration, null)
56+
return mapType(type as KotlinType, AsmTypeFactory, mode, Configuration(type.builtIns), null)
5557
}
5658

5759
private fun generateErrorMessageForErrorType(type: KotlinType, descriptor: DeclarationDescriptor): String {

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.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/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.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.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TARGET_BACKEND: JVM
2+
// WITH_STDLIB
3+
4+
@JvmInline
5+
public value class KoneMutableArray<Element>(internal val array: Array<Element>)
6+
7+
public inline fun <reified Element> KoneMutableArray(size: UInt, initializer: (UInt) -> Element): KoneMutableArray<Element> =
8+
KoneMutableArray(Array(size.toInt()) { initializer(it.toUInt()) })
9+
10+
@JvmInline
11+
public value class KoneArraySettableList<Element> @PublishedApi internal constructor(
12+
internal val data: KoneMutableArray<Any?>,
13+
)
14+
15+
public inline fun <Element> KoneArraySettableList(size: UInt, initializer: (index: UInt) -> Element): KoneArraySettableList<Element> =
16+
KoneArraySettableList(KoneMutableArray(size, initializer))
17+
18+
fun box(): String {
19+
val koneList = run {
20+
val list = listOf(1, 2, 3, 4)
21+
KoneArraySettableList(list.size.toUInt()) { list[it.toInt()] }
22+
}
23+
return "OK"
24+
}

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/JvmAbiConsistencyTestBoxGenerated.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-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/inlineScopes/FirBlackBoxCodegenTestWithInlineScopesGenerated.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.

core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/descriptorBasedTypeSignatureMapping.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import org.jetbrains.kotlin.descriptors.*
1212
import org.jetbrains.kotlin.name.SpecialNames
1313
import org.jetbrains.kotlin.resolve.isInlineClass
1414
import org.jetbrains.kotlin.types.*
15-
import org.jetbrains.kotlin.types.error.ErrorUtils
15+
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
1616
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
17+
import org.jetbrains.kotlin.types.error.ErrorUtils
1718
import org.jetbrains.kotlin.types.typeUtil.makeNullable
1819
import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
1920
import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
@@ -28,6 +29,9 @@ interface TypeMappingConfiguration<out T : Any> {
2829

2930
// returns null when type doesn't need to be preprocessed
3031
fun preprocessType(kotlinType: KotlinType): KotlinType? = null
32+
33+
val builtIns: KotlinBuiltIns?
34+
get() = null
3135
}
3236

3337
fun <T : Any> mapType(
@@ -117,7 +121,11 @@ fun <T : Any> mapType(
117121
descriptor is ClassDescriptor -> {
118122
// NB if inline class is recursive, it's ok to map it as wrapped
119123
if (descriptor.isInlineClass() && !mode.needInlineClassWrapping) {
120-
val expandedType = SimpleClassicTypeSystemContext.computeExpandedTypeForInlineClass(kotlinType) as KotlinType?
124+
val typeSystemContext = object : ClassicTypeSystemContext {
125+
override val builtIns: KotlinBuiltIns
126+
get() = typeMappingConfiguration.builtIns ?: super.builtIns
127+
}
128+
val expandedType = typeSystemContext.computeExpandedTypeForInlineClass(kotlinType) as KotlinType?
121129
if (expandedType != null) {
122130
return mapType(
123131
expandedType, factory, mode.wrapInlineClassesMode(), typeMappingConfiguration,

0 commit comments

Comments
 (0)