Change signed to unsigned widening conversions to sign-extending
e.g. `Byte.toUInt()` now returns `UInt.MAX_VALUE` for Byte value of -1 instead of 0xFFu. #KT-26594 Fixed
This commit is contained in:
@@ -199,7 +199,11 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
|
||||
for (otherType in UnsignedType.values()) {
|
||||
val name = otherType.capitalized
|
||||
out.print(" public fun to$name(): $name = ")
|
||||
out.println(if (type == otherType) "this" else "data.to${otherType.capitalized}()")
|
||||
out.println(when {
|
||||
otherType > type -> "data.to${otherType.capitalized}() and ${type.mask}u"
|
||||
otherType == type -> "this"
|
||||
else -> "data.to${otherType.capitalized}()"
|
||||
})
|
||||
}
|
||||
out.println()
|
||||
}
|
||||
@@ -212,7 +216,6 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
|
||||
out.println("@ExperimentalUnsignedTypes")
|
||||
out.print("public fun $otherSigned.to$className(): $className = ")
|
||||
out.println(when {
|
||||
otherType < type -> "$className(this.to$thisSigned() and ${otherType.mask})"
|
||||
otherType == type -> "$className(this)"
|
||||
else -> "$className(this.to$thisSigned())"
|
||||
})
|
||||
|
||||
@@ -132,9 +132,9 @@ public inline class UByte internal constructor(private val data: Byte) : Compara
|
||||
public fun toLong(): Long = data.toLong() and 0xFF
|
||||
|
||||
public fun toUByte(): UByte = this
|
||||
public fun toUShort(): UShort = data.toUShort()
|
||||
public fun toUInt(): UInt = data.toUInt()
|
||||
public fun toULong(): ULong = data.toULong()
|
||||
public fun toUShort(): UShort = data.toUShort() and 0xFFu
|
||||
public fun toUInt(): UInt = data.toUInt() and 0xFFu
|
||||
public fun toULong(): ULong = data.toULong() and 0xFFu
|
||||
|
||||
public override fun toString(): String = toInt().toString()
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ public inline class UInt internal constructor(private val data: Int) : Comparabl
|
||||
public fun toUByte(): UByte = data.toUByte()
|
||||
public fun toUShort(): UShort = data.toUShort()
|
||||
public fun toUInt(): UInt = this
|
||||
public fun toULong(): ULong = data.toULong()
|
||||
public fun toULong(): ULong = data.toULong() and 0xFFFF_FFFFu
|
||||
|
||||
public override fun toString(): String = toLong().toString()
|
||||
|
||||
@@ -146,10 +146,10 @@ public inline class UInt internal constructor(private val data: Int) : Comparabl
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Byte.toUInt(): UInt = UInt(this.toInt() and 0xFF)
|
||||
public fun Byte.toUInt(): UInt = UInt(this.toInt())
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Short.toUInt(): UInt = UInt(this.toInt() and 0xFFFF)
|
||||
public fun Short.toUInt(): UInt = UInt(this.toInt())
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Int.toUInt(): UInt = UInt(this)
|
||||
|
||||
@@ -146,13 +146,13 @@ public inline class ULong internal constructor(private val data: Long) : Compara
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Byte.toULong(): ULong = ULong(this.toLong() and 0xFF)
|
||||
public fun Byte.toULong(): ULong = ULong(this.toLong())
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Short.toULong(): ULong = ULong(this.toLong() and 0xFFFF)
|
||||
public fun Short.toULong(): ULong = ULong(this.toLong())
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Int.toULong(): ULong = ULong(this.toLong() and 0xFFFF_FFFF)
|
||||
public fun Int.toULong(): ULong = ULong(this.toLong())
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Long.toULong(): ULong = ULong(this)
|
||||
|
||||
@@ -133,8 +133,8 @@ public inline class UShort internal constructor(private val data: Short) : Compa
|
||||
|
||||
public fun toUByte(): UByte = data.toUByte()
|
||||
public fun toUShort(): UShort = this
|
||||
public fun toUInt(): UInt = data.toUInt()
|
||||
public fun toULong(): ULong = data.toULong()
|
||||
public fun toUInt(): UInt = data.toUInt() and 0xFFFFu
|
||||
public fun toULong(): ULong = data.toULong() and 0xFFFFu
|
||||
|
||||
public override fun toString(): String = toInt().toString()
|
||||
|
||||
@@ -142,7 +142,7 @@ public inline class UShort internal constructor(private val data: Short) : Compa
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Byte.toUShort(): UShort = UShort(this.toShort() and 0xFF)
|
||||
public fun Byte.toUShort(): UShort = UShort(this.toShort())
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Short.toUShort(): UShort = UShort(this)
|
||||
|
||||
Reference in New Issue
Block a user