From 3dad0cfb058e49233e99c06964a5ecb885b3b047 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Thu, 9 Feb 2023 13:23:41 +0200 Subject: [PATCH] [IR] Fixed wrong IrCall type (to fix IR validation) --- .../kotlin/backend/common/lower/loops/HeaderProcessor.kt | 6 +++--- .../backend/common/lower/loops/IndexedGetLoopHeader.kt | 5 +++-- .../jetbrains/kotlin/backend/common/lower/loops/Utils.kt | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt index 5e34b2a855e..db2460621aa 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt @@ -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 } } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/IndexedGetLoopHeader.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/IndexedGetLoopHeader.kt index 02660818a76..19c12de4efb 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/IndexedGetLoopHeader.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/IndexedGetLoopHeader.kt @@ -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::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) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt index ccb27f92ca4..df26b9f0ae1 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt @@ -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) } } }