Prohibit MIN_VALUE step for unsigned progressions

This commit is contained in:
Ilya Gorbunov
2018-08-01 17:38:34 +03:00
parent 4e574cb017
commit 7dc6c3d7ce
4 changed files with 15 additions and 3 deletions
+5 -1
View File
@@ -341,6 +341,7 @@ class UnsignedRangeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn
val elementType = type.capitalized
val signedType = type.asSigned.capitalized
val stepType = signedType
val stepMinValue = "$stepType.MIN_VALUE"
override fun getPackage(): String = "kotlin.ranges"
@@ -393,7 +394,8 @@ internal constructor(
step: $stepType
) : Iterable<$elementType> {
init {
if (step == 0.to$stepType()) throw kotlin.IllegalArgumentException("Step must be non-zero")
if (step == 0.to$stepType()) throw kotlin.IllegalArgumentException("Step must be non-zero.")
if (step == $stepMinValue) throw kotlin.IllegalArgumentException("Step must be greater than $stepMinValue to avoid overflow on negation.")
}
/**
@@ -431,6 +433,8 @@ internal constructor(
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
* In order to go backwards the [step] must be negative.
*
* [step] must be greater than `$stepMinValue` and not equal to zero.
*/
public fun fromClosedRange(rangeStart: $elementType, rangeEnd: $elementType, step: $stepType): ${elementType}Progression = ${elementType}Progression(rangeStart, rangeEnd, step)
}