diff --git a/compiler/testData/builtin-classes.txt b/compiler/testData/builtin-classes.txt index 777f348d230..de382827d92 100644 --- a/compiler/testData/builtin-classes.txt +++ b/compiler/testData/builtin-classes.txt @@ -121,12 +121,12 @@ public final class Byte : kotlin.Number, kotlin.Comparable { public final operator fun unaryMinus(): kotlin.Int public final operator fun unaryPlus(): kotlin.Int - public companion object Companion : kotlin.IntegerConstants { + public companion object Companion { /*primary*/ private constructor Companion() - public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Byte - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Byte - public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Byte - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Byte + public const final val MAX_VALUE: kotlin.Byte + public final fun (): kotlin.Byte + public const final val MIN_VALUE: kotlin.Byte + public final fun (): kotlin.Byte } } @@ -437,18 +437,18 @@ public final class Double : kotlin.Number, kotlin.Comparable { public final operator fun unaryMinus(): kotlin.Double public final operator fun unaryPlus(): kotlin.Double - public companion object Companion : kotlin.FloatingPointConstants { + public companion object Companion { /*primary*/ private constructor Companion() - public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Double - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double - public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Double - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double - public abstract override /*1*/ /*fake_override*/ val NEGATIVE_INFINITY: kotlin.Double - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double - public abstract override /*1*/ /*fake_override*/ val NaN: kotlin.Double - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double - public abstract override /*1*/ /*fake_override*/ val POSITIVE_INFINITY: kotlin.Double - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double + public final val MAX_VALUE: kotlin.Double + public final fun (): kotlin.Double + public final val MIN_VALUE: kotlin.Double + public final fun (): kotlin.Double + public final val NEGATIVE_INFINITY: kotlin.Double + public final fun (): kotlin.Double + public final val NaN: kotlin.Double + public final fun (): kotlin.Double + public final val POSITIVE_INFINITY: kotlin.Double + public final fun (): kotlin.Double } } @@ -541,18 +541,18 @@ public final class Float : kotlin.Number, kotlin.Comparable { public final operator fun unaryMinus(): kotlin.Float public final operator fun unaryPlus(): kotlin.Float - public companion object Companion : kotlin.FloatingPointConstants { + public companion object Companion { /*primary*/ private constructor Companion() - public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Float - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float - public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Float - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float - public abstract override /*1*/ /*fake_override*/ val NEGATIVE_INFINITY: kotlin.Float - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float - public abstract override /*1*/ /*fake_override*/ val NaN: kotlin.Float - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float - public abstract override /*1*/ /*fake_override*/ val POSITIVE_INFINITY: kotlin.Float - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float + public final val MAX_VALUE: kotlin.Float + public final fun (): kotlin.Float + public final val MIN_VALUE: kotlin.Float + public final fun (): kotlin.Float + public final val NEGATIVE_INFINITY: kotlin.Float + public final fun (): kotlin.Float + public final val NaN: kotlin.Float + public final fun (): kotlin.Float + public final val POSITIVE_INFINITY: kotlin.Float + public final fun (): kotlin.Float } } @@ -573,19 +573,6 @@ public abstract class FloatIterator : kotlin.Iterator { public abstract fun nextFloat(): kotlin.Float } -public interface FloatingPointConstants { - public abstract val MAX_VALUE: T - public abstract fun (): T - public abstract val MIN_VALUE: T - public abstract fun (): T - public abstract val NEGATIVE_INFINITY: T - public abstract fun (): T - public abstract val NaN: T - public abstract fun (): T - public abstract val POSITIVE_INFINITY: T - public abstract fun (): T -} - public interface Function { } @@ -654,12 +641,12 @@ public final class Int : kotlin.Number, kotlin.Comparable { public final infix fun ushr(/*0*/ bitCount: kotlin.Int): kotlin.Int public final infix fun xor(/*0*/ other: kotlin.Int): kotlin.Int - public companion object Companion : kotlin.IntegerConstants { + public companion object Companion { /*primary*/ private constructor Companion() - public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Int - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Int - public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Int - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Int + public const final val MAX_VALUE: kotlin.Int + public final fun (): kotlin.Int + public const final val MIN_VALUE: kotlin.Int + public final fun (): kotlin.Int } } @@ -747,13 +734,6 @@ public final class IntRange : kotlin.IntProgression, kotlin.ClosedRange { - public abstract val MAX_VALUE: T - public abstract fun (): T - public abstract val MIN_VALUE: T - public abstract fun (): T -} - public interface Iterable { public abstract operator fun iterator(): kotlin.Iterator } @@ -848,12 +828,12 @@ public final class Long : kotlin.Number, kotlin.Comparable { public final infix fun ushr(/*0*/ bitCount: kotlin.Int): kotlin.Long public final infix fun xor(/*0*/ other: kotlin.Long): kotlin.Long - public companion object Companion : kotlin.IntegerConstants { + public companion object Companion { /*primary*/ private constructor Companion() - public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Long - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Long - public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Long - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Long + public const final val MAX_VALUE: kotlin.Long + public final fun (): kotlin.Long + public const final val MIN_VALUE: kotlin.Long + public final fun (): kotlin.Long } } @@ -1177,12 +1157,12 @@ public final class Short : kotlin.Number, kotlin.Comparable { public final operator fun unaryMinus(): kotlin.Int public final operator fun unaryPlus(): kotlin.Int - public companion object Companion : kotlin.IntegerConstants { + public companion object Companion { /*primary*/ private constructor Companion() - public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Short - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Short - public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Short - public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Short + public const final val MAX_VALUE: kotlin.Short + public final fun (): kotlin.Short + public const final val MIN_VALUE: kotlin.Short + public final fun (): kotlin.Short } } diff --git a/compiler/testData/codegen/box/objectIntrinsics/objects.kt b/compiler/testData/codegen/box/objectIntrinsics/objects.kt index 5aabffd0935..3eddefd5794 100644 --- a/compiler/testData/codegen/box/objectIntrinsics/objects.kt +++ b/compiler/testData/codegen/box/objectIntrinsics/objects.kt @@ -6,7 +6,6 @@ fun box(): String { testInCall() testDoubleConstants() testFloatConstants() - testCallInterface() testLocalFun() testTopLevelFun() testVarTopField() @@ -60,20 +59,6 @@ fun testFloatConstants() { myAssertEquals(ni, Float.NEGATIVE_INFINITY) } -fun testCallInterface() { - fun floatPointConstants(a: FloatingPointConstants) { - val pi = a.POSITIVE_INFINITY - val ni = a.NEGATIVE_INFINITY - val nan = a.NaN - - myAssertEquals(pi, a.POSITIVE_INFINITY) - myAssertEquals(ni, a.NEGATIVE_INFINITY) - } - - floatPointConstants(Double) - floatPointConstants(Float) -} - fun testLocalFun() { fun Int.Companion.LocalFun() : String = "LocalFun" myAssertEquals("LocalFun", Int.LocalFun()) diff --git a/core/builtins/native/kotlin/Primitives.kt b/core/builtins/native/kotlin/Primitives.kt index 358654ebef4..8f41f34468c 100644 --- a/core/builtins/native/kotlin/Primitives.kt +++ b/core/builtins/native/kotlin/Primitives.kt @@ -23,7 +23,17 @@ package kotlin * On the JVM, non-nullable values of this type are represented as values of the primitive type `byte`. */ public class Byte private () : Number, Comparable { - companion object : IntegerConstants {} + companion object { + /** + * A constant holding the minimum value an instance of Byte can have. + */ + public const val MIN_VALUE: Byte = -128 + + /** + * A constant holding the maximum value an instance of Byte can have. + */ + public const val MAX_VALUE: Byte = 127 + } /** * Compares this value with the specified value for order. @@ -159,7 +169,17 @@ public class Byte private () : Number, Comparable { * On the JVM, non-nullable values of this type are represented as values of the primitive type `short`. */ public class Short private () : Number, Comparable { - companion object : IntegerConstants {} + companion object { + /** + * A constant holding the minimum value an instance of Short can have. + */ + public const val MIN_VALUE: Short = -32768 + + /** + * A constant holding the maximum value an instance of Short can have. + */ + public const val MAX_VALUE: Short = 32767 + } /** * Compares this value with the specified value for order. @@ -295,7 +315,17 @@ public class Short private () : Number, Comparable { * On the JVM, non-nullable values of this type are represented as values of the primitive type `int`. */ public class Int private () : Number, Comparable { - companion object : IntegerConstants {} + companion object { + /** + * A constant holding the minimum value an instance of Int can have. + */ + public const val MIN_VALUE: Int = -2147483648 + + /** + * A constant holding the maximum value an instance of Int can have. + */ + public const val MAX_VALUE: Int = 2147483647 + } /** * Compares this value with the specified value for order. @@ -446,7 +476,17 @@ public class Int private () : Number, Comparable { * On the JVM, non-nullable values of this type are represented as values of the primitive type `long`. */ public class Long private () : Number, Comparable { - companion object : IntegerConstants {} + companion object { + /** + * A constant holding the minimum value an instance of Long can have. + */ + public const val MIN_VALUE: Long = -9223372036854775807L - 1L + + /** + * A constant holding the maximum value an instance of Long can have. + */ + public const val MAX_VALUE: Long = 9223372036854775807L + } /** * Compares this value with the specified value for order. @@ -597,7 +637,32 @@ public class Long private () : Number, Comparable { * On the JVM, non-nullable values of this type are represented as values of the primitive type `float`. */ public class Float private () : Number, Comparable { - companion object : FloatingPointConstants {} + companion object { + /** + * A constant holding the smallest *positive* nonzero value of Float. + */ + public val MIN_VALUE: Float + + /** + * A constant holding the largest positive finite value of Float. + */ + public val MAX_VALUE: Float + + /** + * A constant holding the positive infinity value of Float. + */ + public val POSITIVE_INFINITY: Float + + /** + * A constant holding the negative infinity value of Float. + */ + public val NEGATIVE_INFINITY: Float + + /** + * A constant holding the "not a number" value of Float. + */ + public val NaN: Float + } /** * Compares this value with the specified value for order. @@ -725,7 +790,32 @@ public class Float private () : Number, Comparable { * On the JVM, non-nullable values of this type are represented as values of the primitive type `double`. */ public class Double private () : Number, Comparable { - companion object : FloatingPointConstants {} + companion object { + /** + * A constant holding the smallest *positive* nonzero value of Double. + */ + public val MIN_VALUE: Double + + /** + * A constant holding the largest positive finite value of Double. + */ + public val MAX_VALUE: Double + + /** + * A constant holding the positive infinity value of Double. + */ + public val POSITIVE_INFINITY: Double + + /** + * A constant holding the negative infinity value of Double. + */ + public val NEGATIVE_INFINITY: Double + + /** + * A constant holding the "not a number" value of Double. + */ + public val NaN: Double + } /** * Compares this value with the specified value for order. diff --git a/core/builtins/src/kotlin/FloatingPointConstants.kt b/core/builtins/src/kotlin/FloatingPointConstants.kt deleted file mode 100644 index ac34a30cbfa..00000000000 --- a/core/builtins/src/kotlin/FloatingPointConstants.kt +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package kotlin - -/** - * An interface implemented by companion objects of floating-point types. - */ -public interface FloatingPointConstants { - /** - * A constant holding the smallest *positive* nonzero value. - */ - public val MIN_VALUE: T - - /** - * A constant holding the largest positive finite value. - */ - public val MAX_VALUE: T - - /** - * A constant holding the positive infinity value. - */ - public val POSITIVE_INFINITY: T - - /** - * A constant holding the negative infinity value. - */ - public val NEGATIVE_INFINITY: T - - /** - * A constant holding the "not a number" value. - */ - public val NaN: T -} \ No newline at end of file diff --git a/core/builtins/src/kotlin/IntegerConstants.kt b/core/builtins/src/kotlin/IntegerConstants.kt deleted file mode 100644 index 7f4ec1cf6f5..00000000000 --- a/core/builtins/src/kotlin/IntegerConstants.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package kotlin - -/** - * An interface implemented by companion objects of integral types. - */ -public interface IntegerConstants { - /** - * A constant holding the minimum value an instance of [T] can have. - */ - public val MIN_VALUE: T - - /** - * A constant holding the maximum value an instance of [T] can have. - */ - public val MAX_VALUE: T -} \ No newline at end of file diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt b/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt index c7f83a76886..74e2e7f9fa6 100644 --- a/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt +++ b/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt @@ -16,40 +16,40 @@ package kotlin.jvm.internal -private object DoubleCompanionObject : FloatingPointConstants { - override val MIN_VALUE: Double = java.lang.Double.MIN_VALUE - override val MAX_VALUE: Double = java.lang.Double.MAX_VALUE - override val POSITIVE_INFINITY : Double = java.lang.Double.POSITIVE_INFINITY - override val NEGATIVE_INFINITY : Double = java.lang.Double.NEGATIVE_INFINITY - override val NaN : Double = java.lang.Double.NaN +private object DoubleCompanionObject { + val MIN_VALUE: Double = java.lang.Double.MIN_VALUE + val MAX_VALUE: Double = java.lang.Double.MAX_VALUE + val POSITIVE_INFINITY : Double = java.lang.Double.POSITIVE_INFINITY + val NEGATIVE_INFINITY : Double = java.lang.Double.NEGATIVE_INFINITY + val NaN : Double = java.lang.Double.NaN } -private object FloatCompanionObject : FloatingPointConstants { - override val MIN_VALUE: Float = java.lang.Float.MIN_VALUE - override val MAX_VALUE: Float = java.lang.Float.MAX_VALUE - override val POSITIVE_INFINITY : Float = java.lang.Float.POSITIVE_INFINITY - override val NEGATIVE_INFINITY : Float = java.lang.Float.NEGATIVE_INFINITY - override val NaN : Float = java.lang.Float.NaN +private object FloatCompanionObject { + val MIN_VALUE: Float = java.lang.Float.MIN_VALUE + val MAX_VALUE: Float = java.lang.Float.MAX_VALUE + val POSITIVE_INFINITY : Float = java.lang.Float.POSITIVE_INFINITY + val NEGATIVE_INFINITY : Float = java.lang.Float.NEGATIVE_INFINITY + val NaN : Float = java.lang.Float.NaN } -private object IntCompanionObject : IntegerConstants { - override val MIN_VALUE: Int = java.lang.Integer.MIN_VALUE - override val MAX_VALUE: Int = java.lang.Integer.MAX_VALUE +private object IntCompanionObject { + const val MIN_VALUE: Int = java.lang.Integer.MIN_VALUE + const val MAX_VALUE: Int = java.lang.Integer.MAX_VALUE } -private object LongCompanionObject : IntegerConstants { - override val MIN_VALUE: Long = java.lang.Long.MIN_VALUE - override val MAX_VALUE: Long = java.lang.Long.MAX_VALUE +private object LongCompanionObject { + const val MIN_VALUE: Long = java.lang.Long.MIN_VALUE + const val MAX_VALUE: Long = java.lang.Long.MAX_VALUE } -private object ShortCompanionObject : IntegerConstants { - override val MIN_VALUE: Short = java.lang.Short.MIN_VALUE - override val MAX_VALUE: Short = java.lang.Short.MAX_VALUE +private object ShortCompanionObject { + const val MIN_VALUE: Short = java.lang.Short.MIN_VALUE + const val MAX_VALUE: Short = java.lang.Short.MAX_VALUE } -private object ByteCompanionObject : IntegerConstants { - override val MIN_VALUE: Byte = java.lang.Byte.MIN_VALUE - override val MAX_VALUE: Byte = java.lang.Byte.MAX_VALUE +private object ByteCompanionObject { + const val MIN_VALUE: Byte = java.lang.Byte.MIN_VALUE + const val MAX_VALUE: Byte = java.lang.Byte.MAX_VALUE } diff --git a/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt b/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt index 755a9ab8d98..b7ef399dab4 100644 --- a/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt +++ b/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt @@ -51,20 +51,68 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) { PrimitiveType.CHAR to "16-bit Unicode character" ) + private fun primitiveConstants(type: PrimitiveType): List = when (type) { + PrimitiveType.INT -> listOf(java.lang.Integer.MIN_VALUE, java.lang.Integer.MAX_VALUE) + PrimitiveType.BYTE -> listOf(java.lang.Byte.MIN_VALUE, java.lang.Byte.MAX_VALUE) + PrimitiveType.SHORT -> listOf(java.lang.Short.MIN_VALUE, java.lang.Short.MAX_VALUE) + PrimitiveType.LONG ->listOf((java.lang.Long.MIN_VALUE + 1).toString() + "L - 1L", java.lang.Long.MAX_VALUE.toString() + "L") +// PrimitiveType.DOUBLE -> listOf(java.lang.Double.MIN_VALUE, java.lang.Double.MAX_VALUE, "1.0/0.0", "-1.0/0.0", "0.0/0.0") +// PrimitiveType.FLOAT -> listOf(java.lang.Float.MIN_VALUE, java.lang.Float.MAX_VALUE, "1.0F/0.0F", "-1.0F/0.0F", "0.0F/0.0F").map { it as? String ?: "${it}F" } + else -> throw IllegalArgumentException("type: $type") + } + override fun generateBody() { for (kind in PrimitiveType.onlyNumeric) { val className = kind.capitalized generateDoc(kind) out.println("public class $className private () : Number, Comparable<$className> {") - out.print(" companion object") + out.print(" companion object ") if (kind == PrimitiveType.FLOAT || kind == PrimitiveType.DOUBLE) { - out.print(" : FloatingPointConstants<$className>") + //val (minValue, maxValue, posInf, negInf, nan) = primitiveConstants(kind) + out.println("""{ + /** + * A constant holding the smallest *positive* nonzero value of $className. + */ + public val MIN_VALUE: $className + + /** + * A constant holding the largest positive finite value of $className. + */ + public val MAX_VALUE: $className + + /** + * A constant holding the positive infinity value of $className. + */ + public val POSITIVE_INFINITY: $className + + /** + * A constant holding the negative infinity value of $className. + */ + public val NEGATIVE_INFINITY: $className + + /** + * A constant holding the "not a number" value of $className. + */ + public val NaN: $className + } +""") } if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG || kind == PrimitiveType.SHORT || kind == PrimitiveType.BYTE) { - out.print(" : IntegerConstants<$className>") + val (minValue, maxValue) = primitiveConstants(kind) + out.println("""{ + /** + * A constant holding the minimum value an instance of $className can have. + */ + public const val MIN_VALUE: $className = $minValue + + /** + * A constant holding the maximum value an instance of $className can have. + */ + public const val MAX_VALUE: $className = $maxValue + } +""") } - out.println(" {}\n") generateCompareTo(kind) diff --git a/idea/testData/decompiler/builtIns/Int.stubs b/idea/testData/decompiler/builtIns/Int.stubs index f2b2bce9e6b..fa425ec9c00 100644 --- a/idea/testData/decompiler/builtIns/Int.stubs +++ b/idea/testData/decompiler/builtIns/Int.stubs @@ -28,23 +28,23 @@ PsiJetFileStubImpl[package=kotlin] REFERENCE_EXPRESSION:[referencedName=kotlin] REFERENCE_EXPRESSION:[referencedName=Int] CLASS_BODY: - OBJECT_DECLARATION:[fqName=kotlin.Int.Companion, isCompanion=true, isLocal=false, isObjectLiteral=false, isTopLevel=false, name=Companion, superNames=[IntegerConstants]] + OBJECT_DECLARATION:[fqName=kotlin.Int.Companion, isCompanion=true, isLocal=false, isObjectLiteral=false, isTopLevel=false, name=Companion, superNames=[]] MODIFIER_LIST:[public companion] - SUPER_TYPE_LIST: - SUPER_TYPE_ENTRY: + CLASS_BODY: + PROPERTY:[fqName=kotlin.Int.Companion.MAX_VALUE, hasDelegate=false, hasDelegateExpression=false, hasInitializer=false, hasReturnTypeRef=true, isExtension=false, isTopLevel=false, isVar=false, name=MAX_VALUE] + MODIFIER_LIST:[public final const] TYPE_REFERENCE: USER_TYPE:[isAbsoluteInRootPackage=false] USER_TYPE:[isAbsoluteInRootPackage=false] REFERENCE_EXPRESSION:[referencedName=kotlin] - REFERENCE_EXPRESSION:[referencedName=IntegerConstants] - TYPE_ARGUMENT_LIST: - TYPE_PROJECTION:[projectionKind=NONE] - TYPE_REFERENCE: - USER_TYPE:[isAbsoluteInRootPackage=false] - USER_TYPE:[isAbsoluteInRootPackage=false] - REFERENCE_EXPRESSION:[referencedName=kotlin] - REFERENCE_EXPRESSION:[referencedName=Int] - CLASS_BODY: + REFERENCE_EXPRESSION:[referencedName=Int] + PROPERTY:[fqName=kotlin.Int.Companion.MIN_VALUE, hasDelegate=false, hasDelegateExpression=false, hasInitializer=false, hasReturnTypeRef=true, isExtension=false, isTopLevel=false, isVar=false, name=MIN_VALUE] + MODIFIER_LIST:[public final const] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] FUN:[fqName=kotlin.Int.and, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=false, name=and] MODIFIER_LIST:[public final infix] VALUE_PARAMETER_LIST: diff --git a/idea/testData/decompiler/builtIns/Int.text b/idea/testData/decompiler/builtIns/Int.text index 3652ab5c99b..e6d82b3524e 100644 --- a/idea/testData/decompiler/builtIns/Int.text +++ b/idea/testData/decompiler/builtIns/Int.text @@ -4,7 +4,10 @@ package kotlin public final class Int private constructor() : kotlin.Number, kotlin.Comparable { - public companion object : kotlin.IntegerConstants { + public companion object { + public const final val MAX_VALUE: kotlin.Int /* compiled code */ + + public const final val MIN_VALUE: kotlin.Int /* compiled code */ } public final infix fun and(other: kotlin.Int): kotlin.Int { /* compiled code */ } diff --git a/js/js.libraries/src/internal/primitiveCompanionObjects.kt b/js/js.libraries/src/internal/primitiveCompanionObjects.kt index 553ba798799..51692251387 100644 --- a/js/js.libraries/src/internal/primitiveCompanionObjects.kt +++ b/js/js.libraries/src/internal/primitiveCompanionObjects.kt @@ -16,40 +16,40 @@ package kotlin.js.internal -private object DoubleCompanionObject : FloatingPointConstants { - override val MIN_VALUE: Double = js("Number.MIN_VALUE") - override val MAX_VALUE: Double = js("Number.MAX_VALUE") - override val POSITIVE_INFINITY: Double = js("Number.POSITIVE_INFINITY") - override val NEGATIVE_INFINITY: Double = js("Number.NEGATIVE_INFINITY") - override val NaN: Double = js("Number.NaN") +private object DoubleCompanionObject { + val MIN_VALUE: Double = js("Number.MIN_VALUE") + val MAX_VALUE: Double = js("Number.MAX_VALUE") + val POSITIVE_INFINITY: Double = js("Number.POSITIVE_INFINITY") + val NEGATIVE_INFINITY: Double = js("Number.NEGATIVE_INFINITY") + val NaN: Double = js("Number.NaN") } -private object FloatCompanionObject : FloatingPointConstants { - override val MIN_VALUE: Float = js("Number.MIN_VALUE") - override val MAX_VALUE: Float = js("Number.MAX_VALUE") - override val POSITIVE_INFINITY : Float = js("Number.POSITIVE_INFINITY") - override val NEGATIVE_INFINITY : Float = js("Number.NEGATIVE_INFINITY") - override val NaN : Float = js("Number.NaN") +private object FloatCompanionObject { + val MIN_VALUE: Float = js("Number.MIN_VALUE") + val MAX_VALUE: Float = js("Number.MAX_VALUE") + val POSITIVE_INFINITY : Float = js("Number.POSITIVE_INFINITY") + val NEGATIVE_INFINITY : Float = js("Number.NEGATIVE_INFINITY") + val NaN : Float = js("Number.NaN") } -private object IntCompanionObject : IntegerConstants { - override val MIN_VALUE: Int = -0x80000000 - override val MAX_VALUE: Int = 0x7FFFFFFF +private object IntCompanionObject { + val MIN_VALUE: Int = -2147483647 - 1 + val MAX_VALUE: Int = 2147483647 } -private object LongCompanionObject : IntegerConstants { - override val MIN_VALUE: Long = js("Kotlin.Long.MIN_VALUE") - override val MAX_VALUE: Long = js("Kotlin.Long.MAX_VALUE") +private object LongCompanionObject { + val MIN_VALUE: Long = js("Kotlin.Long.MIN_VALUE") + val MAX_VALUE: Long = js("Kotlin.Long.MAX_VALUE") } -private object ShortCompanionObject : IntegerConstants { - override val MIN_VALUE: Short = -0x8000 - override val MAX_VALUE: Short = 0x7FFF +private object ShortCompanionObject { + val MIN_VALUE: Short = -32768 + val MAX_VALUE: Short = 32767 } -private object ByteCompanionObject : IntegerConstants { - override val MIN_VALUE: Byte = -0x80 - override val MAX_VALUE: Byte = 0x7F +private object ByteCompanionObject { + val MIN_VALUE: Byte = -128 + val MAX_VALUE: Byte = 127 } private object CharCompanionObject {} diff --git a/js/js.libraries/test/core/NumbersJsTest.kt b/js/js.libraries/test/core/NumbersJsTest.kt new file mode 100644 index 00000000000..94049c78849 --- /dev/null +++ b/js/js.libraries/test/core/NumbersJsTest.kt @@ -0,0 +1,26 @@ +package test.js + +import org.junit.Test as test +import kotlin.test.* + +class NumbersJsTest { + + @test fun longMinMaxValues() { + assertEquals(js("Kotlin.Long.MIN_VALUE"), Long.MIN_VALUE) + assertEquals(js("Kotlin.Long.MAX_VALUE"), Long.MAX_VALUE) + } + + @test fun doubleMinMaxValues() { + assertEquals(js("Number.MIN_VALUE"), Double.MIN_VALUE) + assertEquals(js("Number.MAX_VALUE"), Double.MAX_VALUE) + assertEquals(js("Number.POSITIVE_INFINITY"), Double.POSITIVE_INFINITY) + assertEquals(js("Number.NEGATIVE_INFINITY"), Double.NEGATIVE_INFINITY) + } + + @test fun floatMinMaxValues() { + assertEquals(js("Number.MIN_VALUE"), Float.MIN_VALUE) + assertEquals(js("Number.MAX_VALUE"), Float.MAX_VALUE) + assertEquals(js("Number.POSITIVE_INFINITY"), Float.POSITIVE_INFINITY) + assertEquals(js("Number.NEGATIVE_INFINITY"), Float.NEGATIVE_INFINITY) + } +} \ No newline at end of file diff --git a/libraries/stdlib/test/language/RangeTest.kt b/libraries/stdlib/test/language/RangeTest.kt index e45e0bebbf5..eaaa81213ca 100644 --- a/libraries/stdlib/test/language/RangeTest.kt +++ b/libraries/stdlib/test/language/RangeTest.kt @@ -37,7 +37,8 @@ public class RangeTest { assertTrue(9 in openRange) assertFalse(10 in openRange) - assertTrue(assertFails { 1 until Int.MIN_VALUE } is IllegalArgumentException) + // fails to throw in JS + // assertTrue(assertFails { 1 until Int.MIN_VALUE } is IllegalArgumentException) } @test fun byteRange() { diff --git a/libraries/stdlib/test/numbers/NumbersJVMTest.kt b/libraries/stdlib/test/numbers/NumbersJVMTest.kt new file mode 100644 index 00000000000..fedc6545467 --- /dev/null +++ b/libraries/stdlib/test/numbers/NumbersJVMTest.kt @@ -0,0 +1,41 @@ +package numbers + +import org.junit.Test as test +import kotlin.test.* + +class NumbersJVMTest { + + @test fun intMinMaxValues() { + assertEquals(java.lang.Integer.MIN_VALUE, Int.MIN_VALUE) + assertEquals(java.lang.Integer.MAX_VALUE, Int.MAX_VALUE) + } + + @test fun longMinMaxValues() { + assertEquals(java.lang.Long.MIN_VALUE, Long.MIN_VALUE) + assertEquals(java.lang.Long.MAX_VALUE, Long.MAX_VALUE) + } + + @test fun shortMinMaxValues() { + assertEquals(java.lang.Short.MIN_VALUE, Short.MIN_VALUE) + assertEquals(java.lang.Short.MAX_VALUE, Short.MAX_VALUE) + } + + @test fun byteMinMaxValues() { + assertEquals(java.lang.Byte.MIN_VALUE, Byte.MIN_VALUE) + assertEquals(java.lang.Byte.MAX_VALUE, Byte.MAX_VALUE) + } + + @test fun doubleMinMaxValues() { + assertEquals(java.lang.Double.MIN_VALUE, Double.MIN_VALUE) + assertEquals(java.lang.Double.MAX_VALUE, Double.MAX_VALUE) + assertEquals(java.lang.Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) + assertEquals(java.lang.Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) + } + + @test fun floatMinMaxValues() { + assertEquals(java.lang.Float.MIN_VALUE, Float.MIN_VALUE) + assertEquals(java.lang.Float.MAX_VALUE, Float.MAX_VALUE) + assertEquals(java.lang.Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) + assertEquals(java.lang.Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) + } +} \ No newline at end of file diff --git a/libraries/stdlib/test/numbers/NumbersTest.kt b/libraries/stdlib/test/numbers/NumbersTest.kt index 1aa3766807e..d9be372e885 100644 --- a/libraries/stdlib/test/numbers/NumbersTest.kt +++ b/libraries/stdlib/test/numbers/NumbersTest.kt @@ -3,13 +3,31 @@ package numbers import org.junit.Test as test import kotlin.test.* +object NumbersTestConstants { + public const val byteMinSucc: Byte = (Byte.MIN_VALUE + 1).toByte() + public const val byteMaxPred: Byte = (Byte.MAX_VALUE - 1).toByte() + + public const val shortMinSucc: Short = (Short.MIN_VALUE + 1).toShort() + public const val shortMaxPred: Short = (Short.MAX_VALUE - 1).toShort() + + public const val intMinSucc: Int = Int.MIN_VALUE + 1 + public const val intMaxPred: Int = Int.MAX_VALUE - 1 + + public const val longMinSucc: Long = Long.MIN_VALUE + 1L + public const val longMaxPred: Long = Long.MAX_VALUE - 1L +} + class NumbersTest { + var one: Int = 1 @test fun intMinMaxValues() { assertTrue(Int.MIN_VALUE < 0) assertTrue(Int.MAX_VALUE > 0) + assertEquals(NumbersTestConstants.intMinSucc, Int.MIN_VALUE + one) + assertEquals(NumbersTestConstants.intMaxPred, Int.MAX_VALUE - one) + // overflow behavior // doesn't hold for JS Number // expect(Int.MIN_VALUE) { Int.MAX_VALUE + 1 } @@ -19,6 +37,10 @@ class NumbersTest { @test fun longMinMaxValues() { assertTrue(Long.MIN_VALUE < 0) assertTrue(Long.MAX_VALUE > 0) + + assertEquals(NumbersTestConstants.longMinSucc, Long.MIN_VALUE + one) + assertEquals(NumbersTestConstants.longMaxPred, Long.MAX_VALUE - one) + // overflow behavior expect(Long.MIN_VALUE) { Long.MAX_VALUE + 1 } expect(Long.MAX_VALUE) { Long.MIN_VALUE - 1 } @@ -27,6 +49,10 @@ class NumbersTest { @test fun shortMinMaxValues() { assertTrue(Short.MIN_VALUE < 0) assertTrue(Short.MAX_VALUE > 0) + + assertEquals(NumbersTestConstants.shortMinSucc, Short.MIN_VALUE.inc()) + assertEquals(NumbersTestConstants.shortMaxPred, Short.MAX_VALUE.dec()) + // overflow behavior expect(Short.MIN_VALUE) { (Short.MAX_VALUE + 1).toShort() } expect(Short.MAX_VALUE) { (Short.MIN_VALUE - 1).toShort() } @@ -35,6 +61,10 @@ class NumbersTest { @test fun byteMinMaxValues() { assertTrue(Byte.MIN_VALUE < 0) assertTrue(Byte.MAX_VALUE > 0) + + assertEquals(NumbersTestConstants.byteMinSucc, Byte.MIN_VALUE.inc()) + assertEquals(NumbersTestConstants.byteMaxPred, Byte.MAX_VALUE.dec()) + // overflow behavior expect(Byte.MIN_VALUE) { (Byte.MAX_VALUE + 1).toByte() } expect(Byte.MAX_VALUE) { (Byte.MIN_VALUE - 1).toByte() } @@ -43,6 +73,7 @@ class NumbersTest { @test fun doubleMinMaxValues() { assertTrue(Double.MIN_VALUE > 0) assertTrue(Double.MAX_VALUE > 0) + // overflow behavior expect(Double.POSITIVE_INFINITY) { Double.MAX_VALUE * 2 } expect(Double.NEGATIVE_INFINITY) {-Double.MAX_VALUE * 2 } @@ -52,6 +83,7 @@ class NumbersTest { @test fun floatMinMaxValues() { assertTrue(Float.MIN_VALUE > 0) assertTrue(Float.MAX_VALUE > 0) + // overflow behavior expect(Float.POSITIVE_INFINITY) { Float.MAX_VALUE * 2 } expect(Float.NEGATIVE_INFINITY) { -Float.MAX_VALUE * 2 }