[Native] Always cast expression to the expected type after inline

Right now, during the process of inlining, the compiler erases types.
Because of that, we can end up with some random type
(for example, `Any`) where the concrete type was
expected (for example, `Int`). Compiler must insert a cast in the
required places.

#KT-66017 Fixed
This commit is contained in:
Ivan Kylchik
2024-03-06 14:38:17 +01:00
committed by Space Team
parent 555cf56d6d
commit e1180adfbd
27 changed files with 438 additions and 3 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.types.isUnit
import org.jetbrains.kotlin.ir.util.isSuspend
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
data class TailSuspendCalls(val callSites: Set<IrCall>, val hasNotTailSuspendCalls: Boolean)
@@ -36,6 +37,14 @@ fun collectTailSuspendCalls(context: CommonBackendContext, irFunction: IrSimpleF
element.acceptChildren(this, VisitorState(data.insideTryBlock, isTailExpression = false))
}
override fun visitTypeOperator(expression: IrTypeOperatorCall, data: VisitorState) {
if (expression.operator == IrTypeOperator.IMPLICIT_CAST) {
expression.acceptChildren(this, data)
} else {
super.visitTypeOperator(expression, data)
}
}
override fun visitTry(aTry: IrTry, data: VisitorState) {
aTry.tryResult.accept(this, VisitorState(insideTryBlock = true, isTailExpression = false))
aTry.catches.forEach { it.result.accept(this, data) }
@@ -98,8 +107,12 @@ fun collectTailSuspendCalls(context: CommonBackendContext, irFunction: IrSimpleF
expression.acceptChildren(this, VisitorState(data.insideTryBlock, isTailExpression))
}
private fun IrExpression.isUnitRead(): Boolean =
this is IrGetObjectValue && symbol == context.irBuiltIns.unitClass
private fun IrExpression.isUnitRead(): Boolean {
if (this is IrTypeOperatorCall) {
return this.argument.isUnitRead()
}
return this is IrGetObjectValue && symbol == context.irBuiltIns.unitClass
}
private fun IrCall.isReturnIfSuspendedCall() =
symbol == context.ir.symbols.returnIfSuspended