Skip to content

Commit b925404

Browse files
JSMonkSpace Team
authored and
Space Team
committed
[K/JS] Fix autoboxing for inlined function ^KT-60785 Fixed
1 parent 5801279 commit b925404

File tree

7 files changed

+77
-4
lines changed

7 files changed

+77
-4
lines changed

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private val inlineClassUsageLoweringPhase = makeBodyLoweringPhase(
727727
)
728728

729729
private val autoboxingTransformerPhase = makeBodyLoweringPhase(
730-
::AutoboxingTransformer,
730+
{ AutoboxingTransformer(it, shouldCalculateActualTypeForInlinedFunction = true) },
731731
name = "AutoboxingTransformer",
732732
description = "Insert box/unbox intrinsics"
733733
)

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ import org.jetbrains.kotlin.ir.util.render
3131

3232
// Copied and adapted from Kotlin/Native
3333

34-
abstract class AbstractValueUsageLowering(val context: JsCommonBackendContext) : AbstractValueUsageTransformer(context.irBuiltIns),
34+
abstract class AbstractValueUsageLowering(
35+
val context: JsCommonBackendContext,
36+
private val shouldCalculateActualTypeForInlinedFunction: Boolean = false
37+
) : AbstractValueUsageTransformer(context.irBuiltIns),
3538
BodyLoweringPass {
3639

3740
val icUtils = context.inlineClassesUtils
@@ -52,11 +55,19 @@ abstract class AbstractValueUsageLowering(val context: JsCommonBackendContext) :
5255

5356
abstract fun IrExpression.useExpressionAsType(actualType: IrType, expectedType: IrType): IrExpression
5457

55-
protected fun IrExpression.getActualType() = when (this) {
58+
protected fun IrExpression.getActualType(): IrType = when (this) {
5659
is IrConstructorCall -> symbol.owner.returnType
5760
is IrCall -> symbol.owner.realOverrideTarget.returnType
5861
is IrGetField -> this.symbol.owner.type
5962

63+
is IrInlinedFunctionBlock -> {
64+
if (shouldCalculateActualTypeForInlinedFunction) {
65+
inlineCall.getActualType()
66+
} else {
67+
this.type
68+
}
69+
}
70+
6071
is IrTypeOperatorCall -> {
6172
if (operator == IrTypeOperator.REINTERPRET_CAST) {
6273
this.typeOperand
@@ -120,7 +131,10 @@ abstract class AbstractValueUsageLowering(val context: JsCommonBackendContext) :
120131
)
121132
}
122133

123-
class AutoboxingTransformer(context: JsCommonBackendContext) : AbstractValueUsageLowering(context) {
134+
class AutoboxingTransformer(
135+
context: JsCommonBackendContext,
136+
shouldCalculateActualTypeForInlinedFunction: Boolean = false
137+
) : AbstractValueUsageLowering(context, shouldCalculateActualTypeForInlinedFunction) {
124138
private var processingReturnStack = mutableListOf<IrReturn>()
125139

126140
private fun IrExpression.useReturnableExpressionAsType(expectedType: IrType): IrExpression {

js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/BoxJsTestGenerated.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.

js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsBoxTestGenerated.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.

js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsES6TestGenerated.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.

js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// WITH_STDLIB
2+
// EXPECTED_REACHABLE_NODES: 1292
3+
// KT-60785
4+
5+
import kotlin.coroutines.*
6+
import kotlin.coroutines.intrinsics.*
7+
8+
value class SomeValue(val a: String) {
9+
override fun toString() = when (a) {
10+
"fa" -> "O"
11+
"il" -> "K"
12+
else -> ""
13+
}
14+
}
15+
16+
suspend fun foo() = mapOf(SomeValue("fa") to SomeValue("il"))
17+
18+
fun builder(c: suspend () -> Unit) {
19+
c.startCoroutine(object : Continuation<Unit> {
20+
override val context = EmptyCoroutineContext
21+
override fun resumeWith(result: Result<Unit>) {}
22+
})
23+
}
24+
25+
fun box(): String {
26+
var result = ""
27+
28+
builder {
29+
for ((k, v) in foo()) {
30+
result += "$k$v"
31+
}
32+
}
33+
34+
return result
35+
}

0 commit comments

Comments
 (0)