Deprecate Char-to-Number conversions in stdlib (JVM and JS)

- Int.toChar was left non-deprecated because the replacement is not intrinsic yet.
- Number.toChar was left non-deprecated because otherwise the deprecation propagates to the override, Int.toChar.

KT-23451
This commit is contained in:
Ilya Gorbunov
2021-04-06 15:36:44 +03:00
parent b976cd812a
commit b64b96eee6
59 changed files with 273 additions and 169 deletions
+10 -2
View File
@@ -394,10 +394,14 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
}
private fun generateConversions(kind: PrimitiveType) {
fun isConversionDeprecated(otherKind: PrimitiveType): Boolean {
fun isFpToIntConversionDeprecated(otherKind: PrimitiveType): Boolean {
return kind in PrimitiveType.floatingPoint && otherKind in listOf(PrimitiveType.BYTE, PrimitiveType.SHORT)
}
fun isCharConversionDeprecated(otherKind: PrimitiveType): Boolean {
return kind != PrimitiveType.INT && otherKind == PrimitiveType.CHAR
}
val thisName = kind.capitalized
for (otherKind in PrimitiveType.exceptBoolean) {
val otherName = otherKind.capitalized
@@ -422,10 +426,14 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
}
out.println(doc)
if (isConversionDeprecated(otherKind)) {
if (isFpToIntConversionDeprecated(otherKind)) {
out.println(" @Deprecated(\"Unclear conversion. To achieve the same result convert to Int explicitly and then to $otherName.\", ReplaceWith(\"toInt().to$otherName()\"))")
out.println(" @DeprecatedSinceKotlin(warningSince = \"1.3\", errorSince = \"1.5\")")
}
if (isCharConversionDeprecated(otherKind)) {
out.println(" @Deprecated(\"Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.\", ReplaceWith(\"this.toInt().toChar()\"))")
out.println(" @DeprecatedSinceKotlin(warningSince = \"1.5\")")
}
out.println(" public override fun to$otherName(): $otherName")
}
+3 -3
View File
@@ -27,7 +27,7 @@ fun integerProgressionIterator(kind: ProgressionKind): String {
val incrementType = progressionIncrementType(kind)
val (toInt, toType) = when (kind) {
CHAR -> ".toInt()" to ".to$t()"
CHAR -> ".code" to ".toChar()"
else -> "" to ""
}
@@ -36,9 +36,9 @@ fun integerProgressionIterator(kind: ProgressionKind): String {
* @property step the number by which the value is incremented on each step.
*/
internal class ${t}ProgressionIterator(first: $t, last: $t, val step: $incrementType) : ${t}Iterator() {
private val finalElement = last$toInt
private val finalElement: $incrementType = last$toInt
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first$toInt else finalElement
private var next: $incrementType = if (hasNext) first$toInt else finalElement
override fun hasNext(): Boolean = hasNext
+10 -2
View File
@@ -42,12 +42,20 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
val hashCode = "=\n" + when (kind) {
CHAR ->
" if (isEmpty()) -1 else (31 * (31 * first.toInt() + last.toInt()) + step)"
" if (isEmpty()) -1 else (31 * (31 * first.code + last.code) + step)"
INT ->
" if (isEmpty()) -1 else (31 * (31 * first + last) + step)"
LONG ->
" if (isEmpty()) -1 else (31 * (31 * ${hashLong("first")} + ${hashLong("last")}) + ${hashLong("step")}).toInt()"
}
val elementToIncrement = when (kind) {
CHAR -> ".code"
else -> ""
}
val incrementToElement = when (kind) {
CHAR -> ".toChar()"
else -> ""
}
out.println(
"""/**
@@ -73,7 +81,7 @@ public open class $progression
/**
* The last element in the progression.
*/
public val last: $t = getProgressionLastElement(start.to$incrementType(), endInclusive.to$incrementType(), step).to$t()
public val last: $t = getProgressionLastElement(start$elementToIncrement, endInclusive$elementToIncrement, step)$incrementToElement
/**
* The step of the progression.
+1 -1
View File
@@ -39,7 +39,7 @@ class GenerateRanges(out: PrintWriter) : BuiltInsSourceGenerator(out) {
val hashCode = when (kind) {
CHAR -> "=\n" +
" if (isEmpty()) -1 else (31 * first.toInt() + last.toInt())"
" if (isEmpty()) -1 else (31 * first.code + last.code)"
INT -> "=\n" +
" if (isEmpty()) -1 else (31 * first + last)"
LONG -> "=\n" +