IR: add an emptiness check to all unsigned until loops

Unlike signed integers, a larger unsigned type does not mean a lower
minimum value, so `x - 1` can overflow even if `x` is casted to a larger
type.

 #KT-42186 Fixed
This commit is contained in:
pyos
2020-10-05 15:20:57 +02:00
committed by Alexander Udalov
parent 4a03006162
commit adcbfc7b4c
10 changed files with 283 additions and 86 deletions

View File

@@ -1,36 +0,0 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun testUByteUntilUByte(a: UByte, b: UByte): Int {
var sum = 0
for (i in a until b) {
sum += i.toInt()
}
return sum
}
fun testUShortUntilUShort(a: UShort, b: UShort): Int {
var sum = 0
for (i in a until b) {
sum += i.toInt()
}
return sum
}
// For "until" progressions in JVM IR, there is typically a check that the range is not empty: upper bound != MIN_VALUE.
// However, this check is not needed when the upper bound is smaller than the range element type.
// Here are the available `until` extension functions with mixed bounds that return UIntRange:
//
// infix fun UByte.until(to: UByte): UIntRange // NO bound check needed
// infix fun UShort.until(to: UShort): UIntRange // NO bound check needed
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 2 IFGT
// 2 IFLE
// 4 IF
// 0 INVOKESTATIC kotlin/UInt.constructor-impl
// 0 INVOKE\w+ kotlin/UInt.(un)?box-impl