From bdf623f711037955d98d58eaafb45547497d3c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pap=20L=C5=91rinc?= Date: Mon, 18 Sep 2017 17:51:26 +0300 Subject: [PATCH] Prohibit step size of min value, as it doesn't have a negated counterpart #KT-17176 --- core/builtins/src/kotlin/Progressions.kt | 3 +++ generators/builtins/progressions.kt | 9 ++++++++- libraries/stdlib/test/ranges/RangeTest.kt | 8 +++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/core/builtins/src/kotlin/Progressions.kt b/core/builtins/src/kotlin/Progressions.kt index 7dc89552325..0fc54d47814 100644 --- a/core/builtins/src/kotlin/Progressions.kt +++ b/core/builtins/src/kotlin/Progressions.kt @@ -21,6 +21,7 @@ public open class CharProgression ) : Iterable { init { if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero") + else if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse") } /** @@ -75,6 +76,7 @@ public open class IntProgression ) : Iterable { init { if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero") + else if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse") } /** @@ -129,6 +131,7 @@ public open class LongProgression ) : Iterable { init { if (step == 0L) throw kotlin.IllegalArgumentException("Step must be non-zero") + else if (step == Long.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse") } /** diff --git a/generators/builtins/progressions.kt b/generators/builtins/progressions.kt index 5358a58c1f5..c56f9a611a8 100644 --- a/generators/builtins/progressions.kt +++ b/generators/builtins/progressions.kt @@ -35,7 +35,13 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) { LONG -> "0L" else -> "0" } - val checkZero = "if (step == $zero) throw kotlin.IllegalArgumentException(\"Step must be non-zero\")" + val checkZero = """if (step == $zero) throw kotlin.IllegalArgumentException("Step must be non-zero")""" + + val minValue = when (kind) { + LONG -> "Long.MIN_VALUE" + else -> "Int.MIN_VALUE" + } + val checkMin = """else if (step == $minValue) throw kotlin.IllegalArgumentException("Step must have an inverse")""" val hashCode = "=\n" + when (kind) { CHAR -> @@ -60,6 +66,7 @@ public open class $progression ) : Iterable<$t> { init { $checkZero + $checkMin } /** diff --git a/libraries/stdlib/test/ranges/RangeTest.kt b/libraries/stdlib/test/ranges/RangeTest.kt index f2c72cf372d..bec49780399 100644 --- a/libraries/stdlib/test/ranges/RangeTest.kt +++ b/libraries/stdlib/test/ranges/RangeTest.kt @@ -380,4 +380,10 @@ public class RangeTest { assertFailsWithIllegalArgument { 0L downTo -5L step -2L } assertFailsWithIllegalArgument { 'z' downTo 'a' step -2 } } -} \ No newline at end of file + + @Test fun stepSizeIsTooLow() { + assertFailsWithIllegalArgument { CharProgression.fromClosedRange('a', 'b', Int.MIN_VALUE) } + assertFailsWithIllegalArgument { IntProgression.fromClosedRange(0, 1, Int.MIN_VALUE) } + assertFailsWithIllegalArgument { LongProgression.fromClosedRange(0, 1, Long.MIN_VALUE) } + } +}