Skip to content

Commit

Permalink
[K/JS] Fix autoboxing for inlined function ^KT-60785 Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
JSMonk authored and qodana-bot committed Aug 16, 2023
1 parent 5801279 commit b925404
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ private val inlineClassUsageLoweringPhase = makeBodyLoweringPhase(
)

private val autoboxingTransformerPhase = makeBodyLoweringPhase(
::AutoboxingTransformer,
{ AutoboxingTransformer(it, shouldCalculateActualTypeForInlinedFunction = true) },
name = "AutoboxingTransformer",
description = "Insert box/unbox intrinsics"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import org.jetbrains.kotlin.ir.util.render

// Copied and adapted from Kotlin/Native

abstract class AbstractValueUsageLowering(val context: JsCommonBackendContext) : AbstractValueUsageTransformer(context.irBuiltIns),
abstract class AbstractValueUsageLowering(
val context: JsCommonBackendContext,
private val shouldCalculateActualTypeForInlinedFunction: Boolean = false
) : AbstractValueUsageTransformer(context.irBuiltIns),
BodyLoweringPass {

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

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

protected fun IrExpression.getActualType() = when (this) {
protected fun IrExpression.getActualType(): IrType = when (this) {
is IrConstructorCall -> symbol.owner.returnType
is IrCall -> symbol.owner.realOverrideTarget.returnType
is IrGetField -> this.symbol.owner.type

is IrInlinedFunctionBlock -> {
if (shouldCalculateActualTypeForInlinedFunction) {
inlineCall.getActualType()
} else {
this.type
}
}

is IrTypeOperatorCall -> {
if (operator == IrTypeOperator.REINTERPRET_CAST) {
this.typeOperand
Expand Down Expand Up @@ -120,7 +131,10 @@ abstract class AbstractValueUsageLowering(val context: JsCommonBackendContext) :
)
}

class AutoboxingTransformer(context: JsCommonBackendContext) : AbstractValueUsageLowering(context) {
class AutoboxingTransformer(
context: JsCommonBackendContext,
shouldCalculateActualTypeForInlinedFunction: Boolean = false
) : AbstractValueUsageLowering(context, shouldCalculateActualTypeForInlinedFunction) {
private var processingReturnStack = mutableListOf<IrReturn>()

private fun IrExpression.useReturnableExpressionAsType(expectedType: IrType): IrExpression {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// WITH_STDLIB
// EXPECTED_REACHABLE_NODES: 1292
// KT-60785

import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*

value class SomeValue(val a: String) {
override fun toString() = when (a) {
"fa" -> "O"
"il" -> "K"
else -> ""
}
}

suspend fun foo() = mapOf(SomeValue("fa") to SomeValue("il"))

fun builder(c: suspend () -> Unit) {
c.startCoroutine(object : Continuation<Unit> {
override val context = EmptyCoroutineContext
override fun resumeWith(result: Result<Unit>) {}
})
}

fun box(): String {
var result = ""

builder {
for ((k, v) in foo()) {
result += "$k$v"
}
}

return result
}

0 comments on commit b925404

Please sign in to comment.