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 28d4f049cbf..77a550c4f02 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 @@ -329,15 +329,18 @@ internal class HeaderProcessor( ) } else null - val stepVariable = scope.createTemporaryVariable( - ensureNotNullable( - headerInfo.step.castIfNecessary( - headerInfo.progressionType.stepType(context.irBuiltIns), - headerInfo.progressionType.stepCastFunctionName - ) - ), - nameHint = "step" - ) + val stepVariable = headerInfo.progressionType.stepType(context.irBuiltIns).let { + scope.createTemporaryVariable( + ensureNotNullable( + headerInfo.step.castIfNecessary( + it, + headerInfo.progressionType.stepCastFunctionName + ) + ), + nameHint = "step", + irType = it + ) + } return when (headerInfo) { is IndexedGetHeaderInfo -> IndexedGetLoopHeader( diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt index 20bb5fae1d8..84ee703f9e5 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt @@ -261,7 +261,7 @@ internal class StepHandler( val stepGreaterFun = context.irBuiltIns.greaterFunByOperandType[stepType.toKotlinType()]!! val zeroStep = if (data == ProgressionType.LONG_PROGRESSION) irLong(0) else irInt(0) val throwIllegalStepExceptionCall = { - irCall(context.irBuiltIns.illegalArgumentExceptionSymbol, stepType).apply { + irCall(context.irBuiltIns.illegalArgumentExceptionSymbol).apply { val exceptionMessage = irConcat() exceptionMessage.addArgument(irString("Step must be positive, was: ")) exceptionMessage.addArgument(stepArgExpression.deepCopyWithSymbols()) 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 1f56dd0a257..ac5941143b4 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 @@ -15,16 +15,18 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.getClass +import org.jetbrains.kotlin.ir.types.isNothing import org.jetbrains.kotlin.ir.util.functions import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.util.OperatorNameConventions internal fun IrExpression.castIfNecessary(targetType: IrType, numberCastFunctionName: Name): IrExpression { - return if (type == targetType) { + // This expression's type could be Nothing from an exception throw. + return if (type == targetType || type.isNothing()) { this } else { - val function = type.getClass()!!.functions.first { it.name == numberCastFunctionName } - IrCallImpl(startOffset, endOffset, function.returnType, function.symbol) + val castFun = type.getClass()!!.functions.first { it.name == numberCastFunctionName } + IrCallImpl(startOffset, endOffset, castFun.returnType, castFun.symbol) .apply { dispatchReceiver = this@castIfNecessary } } } @@ -35,6 +37,9 @@ internal fun IrExpression.negate(): IrExpression { is Int -> IrConstImpl(startOffset, endOffset, type, IrConstKind.Int, -value) is Long -> IrConstImpl(startOffset, endOffset, type, IrConstKind.Long, -value) else -> { + // This expression's type could be Nothing from an exception throw, in which case the unary minus function will not exist. + if (type.isNothing()) return this + val unaryMinusFun = type.getClass()!!.functions.first { it.name == OperatorNameConventions.UNARY_MINUS } IrCallImpl(startOffset, endOffset, type, unaryMinusFun.symbol, unaryMinusFun.descriptor).apply { dispatchReceiver = this@negate