Also generate contains for meaningless combinations of types to prevent mistakenly using contains from an Iterable.

#KT-6361
This commit is contained in:
Ilya Gorbunov
2015-07-11 00:46:52 +03:00
parent b7fbb60db4
commit 1cc94f92e2
3 changed files with 75 additions and 4 deletions
+60
View File
@@ -45,6 +45,41 @@ public fun FloatRange.contains(item: Byte): Boolean {
return start <= item && item <= end
}
deprecated("The 'contains' operation for a range of Char and Byte item is not supported and should not be used.")
public fun CharRange.contains(item: Byte): Nothing {
throw UnsupportedOperationException()
}
deprecated("The 'contains' operation for a range of Int and Char item is not supported and should not be used.")
public fun IntRange.contains(item: Char): Nothing {
throw UnsupportedOperationException()
}
deprecated("The 'contains' operation for a range of Long and Char item is not supported and should not be used.")
public fun LongRange.contains(item: Char): Nothing {
throw UnsupportedOperationException()
}
deprecated("The 'contains' operation for a range of Byte and Char item is not supported and should not be used.")
public fun ByteRange.contains(item: Char): Nothing {
throw UnsupportedOperationException()
}
deprecated("The 'contains' operation for a range of Short and Char item is not supported and should not be used.")
public fun ShortRange.contains(item: Char): Nothing {
throw UnsupportedOperationException()
}
deprecated("The 'contains' operation for a range of Double and Char item is not supported and should not be used.")
public fun DoubleRange.contains(item: Char): Nothing {
throw UnsupportedOperationException()
}
deprecated("The 'contains' operation for a range of Float and Char item is not supported and should not be used.")
public fun FloatRange.contains(item: Char): Nothing {
throw UnsupportedOperationException()
}
/**
* Checks if the specified [item] belongs to this range.
*/
@@ -80,6 +115,11 @@ public fun FloatRange.contains(item: Double): Boolean {
return start <= item && item <= end
}
deprecated("The 'contains' operation for a range of Char and Double item is not supported and should not be used.")
public fun CharRange.contains(item: Double): Nothing {
throw UnsupportedOperationException()
}
/**
* Checks if the specified [item] belongs to this range.
*/
@@ -115,6 +155,11 @@ public fun DoubleRange.contains(item: Float): Boolean {
return start <= item && item <= end
}
deprecated("The 'contains' operation for a range of Char and Float item is not supported and should not be used.")
public fun CharRange.contains(item: Float): Nothing {
throw UnsupportedOperationException()
}
/**
* Checks if the specified [item] belongs to this range.
*/
@@ -150,6 +195,11 @@ public fun FloatRange.contains(item: Int): Boolean {
return start <= item && item <= end
}
deprecated("The 'contains' operation for a range of Char and Int item is not supported and should not be used.")
public fun CharRange.contains(item: Int): Nothing {
throw UnsupportedOperationException()
}
/**
* Checks if the specified [item] belongs to this range.
*/
@@ -185,6 +235,11 @@ public fun FloatRange.contains(item: Long): Boolean {
return start <= item && item <= end
}
deprecated("The 'contains' operation for a range of Char and Long item is not supported and should not be used.")
public fun CharRange.contains(item: Long): Nothing {
throw UnsupportedOperationException()
}
/**
* Checks if the specified [item] belongs to this range.
*/
@@ -220,6 +275,11 @@ public fun FloatRange.contains(item: Short): Boolean {
return start <= item && item <= end
}
deprecated("The 'contains' operation for a range of Char and Short item is not supported and should not be used.")
public fun CharRange.contains(item: Short): Nothing {
throw UnsupportedOperationException()
}
/**
* Returns a progression from this value down to the specified [to] value with the increment -1.
* The [to] value has to be less than this value.
@@ -54,6 +54,7 @@ enum class PrimitiveType {
}
fun PrimitiveType.isIntegral(): Boolean = this in PrimitiveType.integralPrimitives
fun PrimitiveType.isNumeric(): Boolean = this in PrimitiveType.numericPrimitives
class GenericFunction(val signature: String, val keyword: String = "fun") : Comparable<GenericFunction> {
@@ -120,12 +120,22 @@ fun ranges(): List<GenericFunction> {
fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(item: $itemType)") {
only(RangesOfPrimitives)
only(rangeType)
returns("Boolean")
doc { "Checks if the specified [item] belongs to this range." }
body { "return start <= item && item <= end" }
val meaningless = (rangeType.isNumeric() != itemType.isNumeric())
if (!meaningless) {
returns("Boolean")
doc { "Checks if the specified [item] belongs to this range." }
body { "return start <= item && item <= end" }
}
else {
returns("Nothing")
annotations("""deprecated("The 'contains' operation for a range of $rangeType and $itemType item is not supported and should not be used.")""")
body { """throw UnsupportedOperationException()""" }
}
}
templates addAll numericPermutations.filter { it.first != it.second }.map { contains(it.first, it.second) }
val allPermutations = (numericPrimitives + PrimitiveType.Char).let { it.flatMap { from -> it.map { to -> from to to } }}
templates addAll allPermutations.filter { it.first != it.second }.map { contains(it.first, it.second) }
return templates
}