[IR] Fixed wrong IrCall type (to fix IR validation)

This commit is contained in:
Igor Chevdar
2023-02-09 13:23:41 +02:00
committed by Space Team
parent 5e6aa649ed
commit 3dad0cfb05
3 changed files with 8 additions and 7 deletions
@@ -60,17 +60,17 @@ fun IrStatement.isInductionVariable(context: CommonBackendContext) =
origin == context.inductionVariableOrigin &&
name.asString() == inductionVariableName
internal class InitializerCallReplacer(private val replacementCall: IrCall) : IrElementTransformerVoid() {
internal class InitializerCallReplacer(private val replacement: IrExpression) : IrElementTransformerVoid() {
var initializerCall: IrCall? = null
override fun visitCall(expression: IrCall): IrCall {
override fun visitCall(expression: IrCall): IrExpression {
if (initializerCall != null) {
throw IllegalStateException(
"Multiple initializer calls found. First: ${initializerCall!!.render()}\nSecond: ${expression.render()}"
)
}
initializerCall = expression
return replacementCall
return replacement
}
}
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrLoop
import org.jetbrains.kotlin.ir.expressions.impl.IrWhileLoopImpl
import org.jetbrains.kotlin.ir.util.implicitCastIfNeededTo
class IndexedGetLoopHeader(
headerInfo: IndexedGetHeaderInfo,
@@ -38,10 +39,10 @@ class IndexedGetLoopHeader(
val indexedGetFun = with(headerInfo.expressionHandler) { headerInfo.objectVariable.type.getFunction }
// Making sure that expression type has type of the variable when it exists.
// Return type of get function can be a type parameter (for example Array<T>::get) which is not a subtype of loopVariable type.
val get = irCall(indexedGetFun.symbol, type = loopVariable?.type ?: indexedGetFun.returnType).apply {
val get = irCall(indexedGetFun.symbol, indexedGetFun.returnType).apply {
dispatchReceiver = irGet(headerInfo.objectVariable)
putValueArgument(0, irGet(inductionVariable))
}
}.implicitCastIfNeededTo(loopVariable?.type ?: indexedGetFun.returnType)
// The call could be wrapped in an IMPLICIT_NOTNULL type-cast (see comment in ForLoopsLowering.gatherLoopVariableInfo()).
// Find and replace the call to preserve any type-casts.
loopVariable?.initializer = loopVariable?.initializer?.transform(InitializerCallReplacer(get), null)
@@ -33,13 +33,13 @@ internal fun IrExpression.negate(): IrExpression {
it.valueParameters.isEmpty()
}
IrCallImpl(
startOffset, endOffset, type,
startOffset, endOffset, unaryMinusFun.returnType,
unaryMinusFun.symbol,
valueArgumentsCount = 0,
typeArgumentsCount = 0
).apply {
dispatchReceiver = this@negate
}
}.implicitCastIfNeededTo(type)
}
}
}