Make unsigned types experimental (with warning if not opted-in)

This commit is contained in:
Ilya Gorbunov
2018-07-03 14:56:26 +03:00
parent 71292bd681
commit b5fabf3f72
15 changed files with 78 additions and 0 deletions
+9
View File
@@ -43,6 +43,7 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
out.println("@Suppress(\"NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS\")")
out.println("@SinceKotlin(\"1.3\")")
out.println("@ExperimentalUnsignedTypes")
out.println("public inline class $className internal constructor(private val data: $storageType) : Comparable<$className> {")
out.println()
out.println(""" companion object {
@@ -198,6 +199,7 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
val otherSigned = otherType.asSigned.capitalized
val thisSigned = type.asSigned.capitalized
out.println("@SinceKotlin(\"1.3\")")
out.println("@ExperimentalUnsignedTypes")
out.print("public fun $otherSigned.to$className(): $className = ")
out.println(when {
otherType < type -> "$className(this.to$thisSigned() and ${otherType.mask})"
@@ -241,6 +243,7 @@ class UnsignedIteratorsGenerator(out: PrintWriter) : BuiltInsSourceGenerator(out
val s = type.capitalized
out.println("/** An iterator over a sequence of values of type `$s`. */")
out.println("@SinceKotlin(\"1.3\")")
out.println("@ExperimentalUnsignedTypes")
out.println("public abstract class ${s}Iterator : Iterator<$s> {")
// TODO: Sort modifiers
out.println(" override final fun next() = next$s()")
@@ -261,6 +264,7 @@ class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn
val storageArrayType = storageElementType + "Array"
override fun generateBody() {
out.println("@SinceKotlin(\"1.3\")")
out.println("@ExperimentalUnsignedTypes")
out.println("public inline class $arrayType")
out.println("@Suppress(\"NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS\")")
out.println("@PublishedApi")
@@ -299,11 +303,13 @@ class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn
// TODO: Make inline constructor, like in ByteArray
out.println()
out.println("@SinceKotlin(\"1.3\")")
out.println("@ExperimentalUnsignedTypes")
out.println("""public inline fun $arrayType(size: Int, init: (Int) -> $elementType): $arrayType {
return $arrayType($storageArrayType(size) { index -> init(index).to$storageElementType() })
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
// TODO: @kotlin.internal.InlineOnly
public inline fun $arrayTypeOf(vararg elements: $elementType): $arrayType = elements"""
)
@@ -330,6 +336,7 @@ import kotlin.internal.*
* A range of values of type `$elementType`.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public class ${elementType}Range(start: $elementType, endInclusive: $elementType) : ${elementType}Progression(start, endInclusive, 1), ClosedRange<${elementType}> {
override val start: $elementType get() = first
override val endInclusive: $elementType get() = last
@@ -357,6 +364,7 @@ public class ${elementType}Range(start: $elementType, endInclusive: $elementType
* A progression of values of type `$elementType`.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public open class ${elementType}Progression
internal constructor(
start: $elementType,
@@ -413,6 +421,7 @@ internal constructor(
* @property step the number by which the value is incremented on each step.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
private class ${elementType}ProgressionIterator(first: $elementType, last: $elementType, step: $stepType) : ${elementType}Iterator() {
private val finalElement = last
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
+2
View File
@@ -67,6 +67,8 @@ compileUnsignedKotlinCommon {
languageVersion = "1.3"
apiVersion = "1.3"
freeCompilerArgs += [
"-Xuse-experimental=kotlin.Experimental",
"-Xuse-experimental=kotlin.ExperimentalUnsignedTypes",
"-XXLanguage:+InlineClasses"
]
}
+2
View File
@@ -212,6 +212,8 @@ compileUnsignedKotlin {
languageVersion = "1.3"
apiVersion = "1.3"
freeCompilerArgs += [
"-Xuse-experimental=kotlin.Experimental",
"-Xuse-experimental=kotlin.ExperimentalUnsignedTypes",
"-XXLanguage:+InlineClasses"
]
}
@@ -11,6 +11,7 @@ import kotlin.experimental.*
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class UByte internal constructor(private val data: Byte) : Comparable<UByte> {
companion object {
@@ -130,10 +131,14 @@ public inline class UByte internal constructor(private val data: Byte) : Compara
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Byte.toUByte(): UByte = UByte(this)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Short.toUByte(): UByte = UByte(this.toByte())
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Int.toUByte(): UByte = UByte(this.toByte())
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Long.toUByte(): UByte = UByte(this.toByte())
@@ -8,6 +8,7 @@
package kotlin
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class UByteArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
@@ -41,10 +42,12 @@ internal constructor(private val storage: ByteArray) : Collection<UByte> {
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline fun UByteArray(size: Int, init: (Int) -> UByte): UByteArray {
return UByteArray(ByteArray(size) { index -> init(index).toByte() })
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
// TODO: @kotlin.internal.InlineOnly
public inline fun ubyteArrayOf(vararg elements: UByte): UByteArray = elements
@@ -11,6 +11,7 @@ import kotlin.experimental.*
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class UInt internal constructor(private val data: Int) : Comparable<UInt> {
companion object {
@@ -134,10 +135,14 @@ 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)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Short.toUInt(): UInt = UInt(this.toInt() and 0xFFFF)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Int.toUInt(): UInt = UInt(this)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Long.toUInt(): UInt = UInt(this.toInt())
@@ -8,6 +8,7 @@
package kotlin
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class UIntArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
@@ -41,10 +42,12 @@ internal constructor(private val storage: IntArray) : Collection<UInt> {
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline fun UIntArray(size: Int, init: (Int) -> UInt): UIntArray {
return UIntArray(IntArray(size) { index -> init(index).toInt() })
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
// TODO: @kotlin.internal.InlineOnly
public inline fun uintArrayOf(vararg elements: UInt): UIntArray = elements
@@ -15,6 +15,7 @@ import kotlin.internal.*
* A range of values of type `UInt`.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public class UIntRange(start: UInt, endInclusive: UInt) : UIntProgression(start, endInclusive, 1), ClosedRange<UInt> {
override val start: UInt get() = first
override val endInclusive: UInt get() = last
@@ -42,6 +43,7 @@ public class UIntRange(start: UInt, endInclusive: UInt) : UIntProgression(start,
* A progression of values of type `UInt`.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public open class UIntProgression
internal constructor(
start: UInt,
@@ -98,6 +100,7 @@ internal constructor(
* @property step the number by which the value is incremented on each step.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
private class UIntProgressionIterator(first: UInt, last: UInt, step: Int) : UIntIterator() {
private val finalElement = last
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
@@ -9,6 +9,7 @@ package kotlin.collections
/** An iterator over a sequence of values of type `UByte`. */
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public abstract class UByteIterator : Iterator<UByte> {
override final fun next() = nextUByte()
@@ -18,6 +19,7 @@ public abstract class UByteIterator : Iterator<UByte> {
/** An iterator over a sequence of values of type `UShort`. */
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public abstract class UShortIterator : Iterator<UShort> {
override final fun next() = nextUShort()
@@ -27,6 +29,7 @@ public abstract class UShortIterator : Iterator<UShort> {
/** An iterator over a sequence of values of type `UInt`. */
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public abstract class UIntIterator : Iterator<UInt> {
override final fun next() = nextUInt()
@@ -36,6 +39,7 @@ public abstract class UIntIterator : Iterator<UInt> {
/** An iterator over a sequence of values of type `ULong`. */
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public abstract class ULongIterator : Iterator<ULong> {
override final fun next() = nextULong()
@@ -11,6 +11,7 @@ import kotlin.experimental.*
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class ULong internal constructor(private val data: Long) : Comparable<ULong> {
companion object {
@@ -134,10 +135,14 @@ 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)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Short.toULong(): ULong = ULong(this.toLong() and 0xFFFF)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Int.toULong(): ULong = ULong(this.toLong() and 0xFFFF_FFFF)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Long.toULong(): ULong = ULong(this)
@@ -8,6 +8,7 @@
package kotlin
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class ULongArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
@@ -41,10 +42,12 @@ internal constructor(private val storage: LongArray) : Collection<ULong> {
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline fun ULongArray(size: Int, init: (Int) -> ULong): ULongArray {
return ULongArray(LongArray(size) { index -> init(index).toLong() })
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
// TODO: @kotlin.internal.InlineOnly
public inline fun ulongArrayOf(vararg elements: ULong): ULongArray = elements
@@ -15,6 +15,7 @@ import kotlin.internal.*
* A range of values of type `ULong`.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public class ULongRange(start: ULong, endInclusive: ULong) : ULongProgression(start, endInclusive, 1), ClosedRange<ULong> {
override val start: ULong get() = first
override val endInclusive: ULong get() = last
@@ -42,6 +43,7 @@ public class ULongRange(start: ULong, endInclusive: ULong) : ULongProgression(st
* A progression of values of type `ULong`.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public open class ULongProgression
internal constructor(
start: ULong,
@@ -98,6 +100,7 @@ internal constructor(
* @property step the number by which the value is incremented on each step.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
private class ULongProgressionIterator(first: ULong, last: ULong, step: Long) : ULongIterator() {
private val finalElement = last
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
@@ -11,6 +11,7 @@ import kotlin.experimental.*
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class UShort internal constructor(private val data: Short) : Comparable<UShort> {
companion object {
@@ -130,10 +131,14 @@ 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)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Short.toUShort(): UShort = UShort(this)
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Int.toUShort(): UShort = UShort(this.toShort())
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Long.toUShort(): UShort = UShort(this.toShort())
@@ -8,6 +8,7 @@
package kotlin
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline class UShortArray
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@PublishedApi
@@ -41,10 +42,12 @@ internal constructor(private val storage: ShortArray) : Collection<UShort> {
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline fun UShortArray(size: Int, init: (Int) -> UShort): UShortArray {
return UShortArray(ShortArray(size) { index -> init(index).toShort() })
}
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
// TODO: @kotlin.internal.InlineOnly
public inline fun ushortArrayOf(vararg elements: UShort): UShortArray = elements
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
package kotlin
import kotlin.annotation.AnnotationTarget.*
import kotlin.internal.RequireKotlin
import kotlin.internal.RequireKotlinVersionKind
/**
* Marks the API that is dependent on the experimental unsigned types, including those types themselves.
*
* Usages of such API will be reported as warnings unless an explicit opt-in with the [UseExperimental] annotation
* or the `-Xuse-experimental=kotlin.ExperimentalUnsignedTypes` compiler option is done.
*/
@Experimental(level = Experimental.Level.WARNING)
@Target(CLASS, ANNOTATION_CLASS, PROPERTY, FIELD, LOCAL_VARIABLE, VALUE_PARAMETER, CONSTRUCTOR, FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER, TYPEALIAS)
@Retention(AnnotationRetention.BINARY)
@RequireKotlin("1.2.50", versionKind = RequireKotlinVersionKind.COMPILER_VERSION)
public annotation class ExperimentalUnsignedTypes