Deprecate mixed Int/FP contains operator for ranges

#KT-22423
This commit is contained in:
Ilya Gorbunov
2018-09-10 03:34:29 +03:00
parent 68c5c0f639
commit 48add96e79
3 changed files with 49 additions and 17 deletions
@@ -120,6 +120,7 @@ public operator fun ClosedRange<Short>.contains(value: Byte): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("doubleRangeContains")
public operator fun ClosedRange<Double>.contains(value: Byte): Boolean {
return contains(value.toDouble())
@@ -128,6 +129,7 @@ public operator fun ClosedRange<Double>.contains(value: Byte): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("floatRangeContains")
public operator fun ClosedRange<Float>.contains(value: Byte): Boolean {
return contains(value.toFloat())
@@ -136,6 +138,7 @@ public operator fun ClosedRange<Float>.contains(value: Byte): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("intRangeContains")
public operator fun ClosedRange<Int>.contains(value: Double): Boolean {
return value.toIntExactOrNull().let { if (it != null) contains(it) else false }
@@ -144,6 +147,7 @@ public operator fun ClosedRange<Int>.contains(value: Double): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("longRangeContains")
public operator fun ClosedRange<Long>.contains(value: Double): Boolean {
return value.toLongExactOrNull().let { if (it != null) contains(it) else false }
@@ -152,6 +156,7 @@ public operator fun ClosedRange<Long>.contains(value: Double): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("byteRangeContains")
public operator fun ClosedRange<Byte>.contains(value: Double): Boolean {
return value.toByteExactOrNull().let { if (it != null) contains(it) else false }
@@ -160,6 +165,7 @@ public operator fun ClosedRange<Byte>.contains(value: Double): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("shortRangeContains")
public operator fun ClosedRange<Short>.contains(value: Double): Boolean {
return value.toShortExactOrNull().let { if (it != null) contains(it) else false }
@@ -176,6 +182,7 @@ public operator fun ClosedRange<Float>.contains(value: Double): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("intRangeContains")
public operator fun ClosedRange<Int>.contains(value: Float): Boolean {
return value.toIntExactOrNull().let { if (it != null) contains(it) else false }
@@ -184,6 +191,7 @@ public operator fun ClosedRange<Int>.contains(value: Float): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("longRangeContains")
public operator fun ClosedRange<Long>.contains(value: Float): Boolean {
return value.toLongExactOrNull().let { if (it != null) contains(it) else false }
@@ -192,6 +200,7 @@ public operator fun ClosedRange<Long>.contains(value: Float): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("byteRangeContains")
public operator fun ClosedRange<Byte>.contains(value: Float): Boolean {
return value.toByteExactOrNull().let { if (it != null) contains(it) else false }
@@ -200,6 +209,7 @@ public operator fun ClosedRange<Byte>.contains(value: Float): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("shortRangeContains")
public operator fun ClosedRange<Short>.contains(value: Float): Boolean {
return value.toShortExactOrNull().let { if (it != null) contains(it) else false }
@@ -240,6 +250,7 @@ public operator fun ClosedRange<Short>.contains(value: Int): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("doubleRangeContains")
public operator fun ClosedRange<Double>.contains(value: Int): Boolean {
return contains(value.toDouble())
@@ -248,6 +259,7 @@ public operator fun ClosedRange<Double>.contains(value: Int): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("floatRangeContains")
public operator fun ClosedRange<Float>.contains(value: Int): Boolean {
return contains(value.toFloat())
@@ -280,6 +292,7 @@ public operator fun ClosedRange<Short>.contains(value: Long): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("doubleRangeContains")
public operator fun ClosedRange<Double>.contains(value: Long): Boolean {
return contains(value.toDouble())
@@ -288,6 +301,7 @@ public operator fun ClosedRange<Double>.contains(value: Long): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("floatRangeContains")
public operator fun ClosedRange<Float>.contains(value: Long): Boolean {
return contains(value.toFloat())
@@ -320,6 +334,7 @@ public operator fun ClosedRange<Byte>.contains(value: Short): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("doubleRangeContains")
public operator fun ClosedRange<Double>.contains(value: Short): Boolean {
return contains(value.toDouble())
@@ -328,6 +343,7 @@ public operator fun ClosedRange<Double>.contains(value: Short): Boolean {
/**
* Checks if the specified [value] belongs to this range.
*/
@Deprecated("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.")
@kotlin.jvm.JvmName("floatRangeContains")
public operator fun ClosedRange<Float>.contains(value: Short): Boolean {
return contains(value.toFloat())
+30 -17
View File
@@ -31,8 +31,10 @@ public class RangeTest {
assertTrue(1.toShort() in range)
assertTrue(1.toByte() in range)
assertTrue(1.toLong() in range)
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
run @Suppress("DEPRECATION") {
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
}
assertFalse(Long.MAX_VALUE in range)
@@ -64,8 +66,10 @@ public class RangeTest {
assertTrue(1.toShort() in range)
assertTrue(1.toInt() in range)
assertTrue(1.toLong() in range)
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
run @Suppress("DEPRECATION") {
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
}
assertFalse(Long.MAX_VALUE in range)
@@ -98,8 +102,10 @@ public class RangeTest {
assertTrue(1.toByte() in range)
assertTrue(1.toInt() in range)
assertTrue(1.toLong() in range)
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
run @Suppress("DEPRECATION") {
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
}
assertFalse(Long.MAX_VALUE in range)
@@ -134,10 +140,13 @@ public class RangeTest {
assertTrue(1.toByte() in range)
assertTrue(1.toShort() in range)
assertTrue(1.toInt() in range)
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
run @Suppress("DEPRECATION") {
assertTrue(1.toFloat() in range)
assertTrue(1.toDouble() in range)
assertFalse(Double.MAX_VALUE in range)
}
assertFalse(Double.MAX_VALUE in range)
val openRange = 1L until 10L
assertTrue(9L in openRange)
@@ -194,10 +203,12 @@ public class RangeTest {
assertFalse(range.isEmpty())
assertTrue(1.toByte() in range)
assertTrue(1.toShort() in range)
assertTrue(1.toInt() in range)
assertTrue(1.toLong() in range)
run @Suppress("DEPRECATION") {
assertTrue(1.toByte() in range)
assertTrue(1.toShort() in range)
assertTrue(1.toInt() in range)
assertTrue(1.toLong() in range)
}
assertTrue(1.toFloat() in range)
val zeroRange = 0.0..-0.0
@@ -240,10 +251,12 @@ public class RangeTest {
assertFalse(range.isEmpty())
assertTrue(1.toByte() in range)
assertTrue(1.toShort() in range)
assertTrue(1.toInt() in range)
assertTrue(1.toLong() in range)
run @Suppress("DEPRECATION") {
assertTrue(1.toByte() in range)
assertTrue(1.toShort() in range)
assertTrue(1.toInt() in range)
assertTrue(1.toLong() in range)
}
assertTrue(1.toDouble() in range)
assertFalse(Double.MAX_VALUE in range)
@@ -160,6 +160,9 @@ object RangeOps : TemplateGroupBase() {
signature("contains(value: $itemType)")
check(rangeType.isNumeric() == itemType.isNumeric()) { "Required rangeType and itemType both to be numeric or both not, got: $rangeType, $itemType" }
if (rangeType.isIntegral() != itemType.isIntegral()) {
deprecate(Deprecation("This `contains` operation mixing integer and floating point arguments has ambiguous semantics and is going to be removed.", level = DeprecationLevel.WARNING))
}
platformName("${rangeType.name.decapitalize()}RangeContains")
returns("Boolean")
doc { "Checks if the specified [value] belongs to this range." }