Fix type of IllegalArgumentException call for illegal step in StepHandler.

This commit is contained in:
Mark Punzalan
2019-11-04 16:31:54 -08:00
committed by max-kammerer
parent ca4784ffd3
commit f2289c216c
3 changed files with 21 additions and 13 deletions
@@ -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(
@@ -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())
@@ -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