Minor. Add more tests

This commit is contained in:
Ilmir Usmanov
2021-12-28 11:52:26 +01:00
parent c8817893d4
commit cece7120df
274 changed files with 20551 additions and 25 deletions
@@ -0,0 +1,122 @@
// KJS_WITH_FULL_RUNTIME
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(private val value: T) : Comparable<UInt<T>> {
companion object {
private const val INT_MASK = 0xffffffffL
}
fun asInt(): Int = value
fun toLong(): Long = value.toLong() and INT_MASK
override fun compareTo(other: UInt<T>): Int =
flip().compareTo(other.flip())
override fun toString(): String {
return toLong().toString()
}
private fun flip(): Int =
value xor Int.MIN_VALUE
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class UIntArray(private val intArray: IntArray) {
val size: Int get() = intArray.size
operator fun get(index: Int): UInt<Int> = UInt(intArray[index])
operator fun set(index: Int, value: UInt<Int>) {
intArray[index] = value.asInt()
}
operator fun iterator(): UIntIterator = UIntIterator(intArray.iterator())
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class UIntIterator(private val intIterator: IntIterator) : Iterator<UInt<Int>> {
override fun next(): UInt<Int> {
return UInt(intIterator.next())
}
override fun hasNext(): Boolean {
return intIterator.hasNext()
}
}
fun uIntArrayOf(vararg u: Int): UIntArray = UIntArray(u)
fun UIntArray.swap(i: Int, j: Int) {
this[j] = this[i].also { this[i] = this[j] }
}
fun UIntArray.quickSort() {
quickSort(0, size - 1)
}
private fun UIntArray.quickSort(l: Int, r: Int) {
if (l < r) {
val q = partition(l, r)
quickSort(l, q - 1)
quickSort(q + 1, r)
}
}
private fun UIntArray.partition(l: Int, r: Int): Int {
val m = this[(l + r) / 2]
var i = l
var j = r
while (i <= j) {
while (this[i] < m) i++
while (this[j] > m) j--
if (i <= j)
swap(i++, j--)
}
return i
}
fun check(array: UIntArray, resultAsInt: String, resultAsInner: String) {
val actualAsInt = StringBuilder()
val actualAsInner = StringBuilder()
for (n in array) {
actualAsInt.append("${n.asInt()} ")
actualAsInner.append(n.toString() + " ")
}
if (actualAsInt.toString() != resultAsInt) {
throw IllegalStateException("wrong result as int (actual): $actualAsInt ; expected: $resultAsInt")
}
if (actualAsInner.toString() != resultAsInner) {
throw IllegalStateException("wrong result as inner (actual): $actualAsInner ; expected: $resultAsInner")
}
}
fun box(): String {
val a1 = uIntArrayOf(1, 2, 3)
a1.quickSort()
check(a1, "1 2 3 ", "1 2 3 ")
val a2 = uIntArrayOf(-1)
a2.quickSort()
check(a2, "-1 ", "4294967295 ")
val a3 = uIntArrayOf(-1, 1, 0)
a3.quickSort()
check(a3, "0 1 -1 ", "0 1 4294967295 ")
val a4 = uIntArrayOf(-1, Int.MAX_VALUE)
a4.quickSort()
check(a4, "${Int.MAX_VALUE} -1 ", "2147483647 4294967295 ")
return "OK"
}
@@ -0,0 +1,20 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
@Target(AnnotationTarget.PROPERTY)
annotation class Anno
OPTIONAL_JVM_INLINE_ANNOTATION
value class Z<T: String>(val s: T)
class A {
@Anno
val Z<String>.r: String get() = s
}
fun box(): String {
with(A()) {
return Z("OK").r
}
}
@@ -0,0 +1,14 @@
// WITH_STDLIB
// IGNORE_BACKEND: JVM
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Int>(val x: T) {
fun f(): Int = super.hashCode()
}
fun box(): String {
val a = A(1).f()
return "OK"
}
@@ -0,0 +1,70 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcInt<T: Int>(val i: T) {
fun simple(): String = i.toString()
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcLong<T: Long>(val l: T) {
fun simple(): String = l.toString()
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcAny<T>(val a: T) {
fun simple(): String = a?.toString() ?: "null"
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcOverIc<T: IcLong<Long>>(val o: T) {
fun simple(): String = o.toString()
}
fun testUnboxed(i: IcInt<Int>, l: IcLong<Long>, a: IcAny<Int>, o: IcOverIc<IcLong<Long>>): String =
foo(i::simple) + foo(l::simple) + foo(a::simple) + foo(o::simple)
fun testBoxed(i: IcInt<Int>?, l: IcLong<Long>?, a: IcAny<Int>?, o: IcOverIc<IcLong<Long>>?): String =
foo(i!!::simple) + foo(l!!::simple) + foo(a!!::simple) + foo(o!!::simple)
fun testLocalVars(): String {
val i = IcInt(0)
val l = IcLong(1L)
val a = IcAny(2)
val o = IcOverIc(IcLong(3))
return foo(i::simple) + foo(l::simple) + foo(a::simple) + foo(o::simple)
}
val ip = IcInt(1)
val lp = IcLong(2L)
val ap = IcAny(3)
val op = IcOverIc(IcLong(4))
fun testGlobalProperties(): String =
foo(ip::simple) + foo(lp::simple) + foo(ap::simple) + foo(op::simple)
fun testCapturedVars(): String {
return IcInt(2).let { foo(it::simple) } +
IcLong(3).let { foo(it::simple) } +
IcAny(4).let { foo(it::simple) } +
IcOverIc(IcLong(5)).let { foo(it::simple) }
}
inline fun foo(init: () -> String): String = init()
fun box(): String {
val i = IcInt(3)
val l = IcLong(4)
val a = IcAny(5)
val o = IcOverIc(IcLong(6))
if (testUnboxed(i, l, a, o) != "345IcLong(l=6)") return "Fail 1 ${testUnboxed(i, l, a, o)}"
if (testBoxed(i, l, a, o) != "345IcLong(l=6)") return "Fail 2"
if (testLocalVars() != "012IcLong(l=3)") return "Fail 3"
if (testGlobalProperties() != "123IcLong(l=4)") return "Fail 4"
if (testCapturedVars() != "234IcLong(l=5)") return "Fail 5 ${testCapturedVars()}"
return "OK"
}
@@ -0,0 +1,26 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC<T: Int> private constructor(val i: T) {
@Suppress("SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS")
constructor() : this(0 as T) {
counter += 1
}
}
var counter = 0
fun <T> id(t: T) = t
fun box(): String {
val ic = IC<Int>()
if (counter != 1) return "FAIL 1: $counter"
counter = 0
id(ic)
if (counter != 0) return "FAIL 2: $counter"
return "OK"
}
@@ -0,0 +1,25 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC<T: Int>(val i: T) {
init {
counter += i
}
}
var counter = 0
fun <T> id(t: T) = `t`
fun box(): String {
val ic = IC(42)
if (counter != 42) return "FAIL 1: $counter"
counter = 0
id(ic)
if (counter != 0) return "FAIL 2: $counter"
return "OK"
}
@@ -0,0 +1,15 @@
// WITH_STDLIB
// IGNORE_BACKEND: JVM
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
abstract class C<T> {
fun foo(v: T?, x: (T) -> Any?) = v?.let { x(it) }
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class V<T>(val value: T)
class D : C<V<String>>()
fun box() = D().foo(V("OK")) { it.value } as String
@@ -0,0 +1,46 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class BoxT<T>(val boxed: T)
class BoxAny(val boxed: Any?)
class BoxFoo(val boxed: IFoo?)
interface IFoo
OPTIONAL_JVM_INLINE_ANNOTATION
value class Str<T: String>(val value: T) : IFoo
OPTIONAL_JVM_INLINE_ANNOTATION
value class Str2<T: Str<String>>(val value: T): IFoo
OPTIONAL_JVM_INLINE_ANNOTATION
value class StrArr(val value: Array<String>): IFoo
fun boxToTypeParameter(x: Str<String>?) = BoxT(x)
fun boxToNullableAny(x: Str<String>?) = BoxAny(x)
fun boxToNullableInterface(x: Str<String>?) = BoxFoo(x)
fun box2ToTypeParameter(x: Str2<Str<String>>?) = BoxT(x)
fun box2ToNullableAny(x: Str2<Str<String>>?) = BoxAny(x)
fun box2ToNullableInterface(x: Str2<Str<String>>?) = BoxFoo(x)
fun boxArrToTypeParameter(x: StrArr?) = BoxT(x)
fun boxArrToNullableAny(x: StrArr?) = BoxAny(x)
fun boxArrToNullableInterface(x: StrArr?) = BoxFoo(x)
fun box(): String {
if (boxToNullableAny(null).boxed != null) throw AssertionError()
if (boxToTypeParameter(null).boxed != null) throw AssertionError()
if (boxToNullableInterface(null).boxed != null) throw AssertionError()
if (box2ToNullableAny(null).boxed != null) throw AssertionError()
if (box2ToTypeParameter(null).boxed != null) throw AssertionError()
if (box2ToNullableInterface(null).boxed != null) throw AssertionError()
if (boxArrToNullableAny(null).boxed != null) throw AssertionError()
if (boxArrToTypeParameter(null).boxed != null) throw AssertionError()
if (boxArrToNullableInterface(null).boxed != null) throw AssertionError()
return "OK"
}
@@ -0,0 +1,24 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
class BoxT<T>(val boxed: T)
class BoxAny(val boxed: Any?)
class BoxFoo(val boxed: IFoo?)
interface IFoo
OPTIONAL_JVM_INLINE_ANNOTATION
value class I32<T: Int>(val value: T): IFoo
fun boxToTypeParameter(x: I32<Int>?) = BoxT(x)
fun boxToNullableAny(x: I32<Int>?) = BoxAny(x)
fun boxToNullableInterface(x: I32<Int>?) = BoxFoo(x)
fun box(): String {
if (boxToNullableAny(null).boxed != null) throw AssertionError()
if (boxToTypeParameter(null).boxed != null) throw AssertionError()
if (boxToNullableInterface(null).boxed != null) throw AssertionError()
return "OK"
}
@@ -0,0 +1,27 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Result<T>(val a: T)
fun box(): String {
val a = Result<Int>(1) // valueOf
val b = Result<String>("sample")
val c = Result<Result<Int>>(a)
val d = Result<Result<Int>>(Result<Int>(1)) // valueOf
if (a.a !is Int) throw AssertionError()
if (b.a !is String) throw AssertionError()
if (c.a !is Result<*>) throw AssertionError()
val ca = c.a as Result<*>
if (ca.a !is Int) throw AssertionError()
if (d.a !is Result<*>) throw AssertionError()
val da = d.a as Result<*>
if (da.a !is Int) throw AssertionError()
return "OK"
}
@@ -0,0 +1,36 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(private val value: T) {
fun asInt() = value
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class UIntArray(private val intArray: IntArray) {
operator fun get(index: Int): UInt<Int> = UInt<Int>(intArray[index])
operator fun set(index: Int, value: UInt<Int>) {
intArray[index] = value.asInt()
}
}
fun UIntArray.swap(i: Int, j: Int) {
this[j] = this[i].also { this[i] = this[j] }
}
fun uIntArrayOf(vararg elements: Int) = UIntArray(intArrayOf(*elements))
fun box(): String {
val a = uIntArrayOf(1, 2, 3, 4)
a.swap(0, 3)
a.swap(1, 2)
if (a[0].asInt() != 4) return "fail"
if (a[1].asInt() != 3) return "fail"
if (a[2].asInt() != 2) return "fail"
if (a[3].asInt() != 1) return "fail"
return "OK"
}
@@ -0,0 +1,74 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(private val value: T) {
operator fun plus(other: UInt<Int>): UInt<Int> = UInt<Int>(value + other.asValue())
fun asValue(): Int = value
}
val Int.u get() = UInt(this)
var global = 0.u
fun testInlined(x: UInt<Int>?, withAssert: Boolean) {
x?.myLet {
takeUInt(it)
takeUInt(x)
}
x?.myLet {
takeNullableUInt(it)
takeNullableUInt(x)
}
if (withAssert) {
x!!.myLet {
takeUInt(it)
takeUInt(x)
}
}
}
fun testNotInlined(x: UInt<Int>?) {
x?.myLet {
takeUInt(it)
takeUInt(x)
}
x?.myLet {
takeNullableUInt(it)
takeNullableUInt(x)
}
}
fun takeUInt(y: UInt<Int>) {
global += y
}
fun takeNullableUInt(y: UInt<Int>?) {
if (y != null) {
global += y
}
}
inline fun <T> T.myLet(f: (T) -> Unit) = f(this)
fun <T> T.nonInlineLet(f: (T) -> Unit) = f(this)
fun box(): String {
val u = 1.u
testInlined(u, true)
if (global.asValue() != 6) return "fail 1"
global = 0.u
testInlined(null, false)
if (global.asValue() != 0) return "fail 2"
global = 0.u
testNotInlined(u)
if (global.asValue() != 4) return "fail 3"
return "OK"
}
@@ -0,0 +1,22 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC<T: String>(val x: T)
interface I<T> {
fun foo(): T
}
interface II: I<IC<String>>
class A : I<IC<String>> {
override fun foo() = IC("O")
}
class B : II {
override fun foo() = IC("K")
}
fun box() = A().foo().x + B().foo().x
@@ -0,0 +1,37 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Result<T>(val a: T) {
fun getOrThrow(): T = a
}
abstract class ResultReceiver<T> {
abstract fun receive(result: Result<T>)
}
fun <T> ResultReceiver(f: (Result<T>) -> Unit): ResultReceiver<T> =
object : ResultReceiver<T>() {
override fun receive(result: Result<T>) {
f(result)
}
}
fun test() {
var invoked = false
val receiver = ResultReceiver<Int> { result ->
val intResult = result.getOrThrow()
invoked = true
}
receiver.receive(Result(42))
if (!invoked) {
throw RuntimeException("Fail")
}
}
fun box(): String {
test()
return "OK"
}
@@ -0,0 +1,37 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlinedComparable<T: Int>(val x: T) : Comparable<InlinedComparable<T>> {
override fun compareTo(other: InlinedComparable<T>): Int {
return x.compareTo(other.x)
}
}
fun <T> generic(c: Comparable<T>, element: T) = c.compareTo(element)
interface Base<T> {
fun Base<T>.foo(a: Base<T>, b: T): Base<T>
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlinedBase<T: Int>(val x: T) : Base<InlinedBase<T>> {
override fun Base<InlinedBase<T>>.foo(a: Base<InlinedBase<T>>, b: InlinedBase<T>): Base<InlinedBase<T>> {
return if (a is InlinedBase<*>) InlinedBase((a.x + b.x) as T) else this
}
fun double(): InlinedBase<T> {
return this.foo(this, this) as InlinedBase<T>
}
}
fun box(): String {
val a = InlinedComparable(42)
if (generic(a, a) != 0) return "Fail 1"
val b = InlinedBase(3)
if (b.double().x != 6) return "Fail 2"
return "OK"
}
@@ -0,0 +1,26 @@
// WITH_REFLECT
// FULL_JDK
// TARGET_BACKEND: JVM
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
// WITH_STDLIB
import java.lang.reflect.InvocationTargetException
OPTIONAL_JVM_INLINE_ANNOTATION
value class Simple<T: String>(val x: T) {
fun somethingWeird() {}
}
fun box(): String {
var s = ""
val name = "equals-impl0"
val specializedEquals =
Simple::class.java.getDeclaredMethod(name, String::class.java, String::class.java)
?: return "$name not found"
if (specializedEquals.invoke(null, "a", "b") as Boolean)
return "Fail"
return "OK"
}
@@ -0,0 +1,26 @@
// WITH_STDLIB
// TARGET_BACKEND: JVM
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class UIntArray(private val storage: IntArray) : Collection<UInt<Int>> {
public override val size: Int get() = storage.size
override operator fun iterator() = TODO()
override fun contains(element: UInt<Int>): Boolean = TODO()
override fun containsAll(elements: Collection<UInt<Int>>): Boolean = TODO()
override fun isEmpty(): Boolean = TODO()
}
fun calculate(u: UIntArray): Int {
return u.size
}
fun box(): String {
if (calculate(UIntArray(intArrayOf(1, 2, 3, 4))) != 4) return "Fail"
return "OK"
}
@@ -0,0 +1,21 @@
// WITH_STDLIB
// WITH_REFLECT
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: String>(val x: T) {
fun bar(f: Foo<T>, i: Int): Foo<String> = Foo(x + f.x + i)
}
fun box(): String {
val f = Foo("original")
val function1 = f::bar
val result1 = function1.invoke(Foo("+argument+"), 42)
if (result1.x != "original+argument+42") return "Fail first"
val result2 = Foo<String>::bar.invoke(Foo("explicit"), Foo("+argument2+"), 10)
if (result2.x != "explicit+argument2+10") return "Fail second"
return "OK"
}
@@ -0,0 +1,21 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: Any>(val x: T) {
fun bar() {}
}
fun <T: Any, K: Any> transform(f: Foo<T>): Foo<K> {
return when {
true -> f as Foo<K>
else -> TODO()
}
}
fun box(): String {
val f = Foo<Int>(42)
val t = transform<Int, Number>(f)
return if (t.x !is Number) "Fail" else "OK"
}
@@ -0,0 +1,45 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: Int>(val value: T)
fun <T> id(x: T): T = x
inline fun <T> inlinedId(x: T): T = x
fun <T> T.idExtension(): T = this
inline fun <T> T.inlinedIdExtension(): T = this
fun <T: Int> test(f: Foo<T>) {
inlinedId(f) // box
inlinedId(f).idExtension() // box
f.inlinedIdExtension() // box
val a = inlinedId(f).idExtension() // box unbox
val b = inlinedId(f).inlinedIdExtension() // box unbox
}
fun box(): String {
val f = Foo(11)
id(inlinedId(f))
inlinedId(id(f))
inlinedId(f) // box
inlinedId(f).idExtension() // box
f.inlinedIdExtension() // box
val a = inlinedId(f).idExtension() // box unbox
val b = inlinedId(f).inlinedIdExtension() // box unbox
if (a.value != 11) return "fail 1"
if (b.value != 11) return "fail 2"
if (inlinedId(Foo(10)).value != 10) return "fail 3"
if (Foo(20).inlinedIdExtension().value != 20) return "fail 4"
return "OK"
}
@@ -0,0 +1,52 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class WithPrimitive<T: Int>(val a: T)
fun <T: Int> takeWithPrimitive(a: WithPrimitive<T>) {}
OPTIONAL_JVM_INLINE_ANNOTATION
value class WithReference<T: Any>(val a: T)
fun <T: Any> takeWithReference(a: WithReference<T>) {}
OPTIONAL_JVM_INLINE_ANNOTATION
value class WithNullableReference<T>(val a: T)
fun <T> takeWithNullableReference(a: WithNullableReference<T>) {}
fun <T: Int> foo(a: WithPrimitive<T>?, b: WithPrimitive<T>) {
takeWithPrimitive(a!!) // unbox
takeWithPrimitive(a) // unbox
takeWithPrimitive(b!!)
}
fun <T: Any, T2: Any> bar(a: WithReference<T>?, b: WithReference<T2>) {
takeWithReference(a!!)
takeWithReference(a)
takeWithReference(b!!)
}
fun <T, R> baz(a: WithNullableReference<T>?, b: WithNullableReference<R>) {
takeWithNullableReference(a!!) // unbox
takeWithNullableReference(a) // unbox
takeWithNullableReference(a!!) // unbox
takeWithNullableReference(b!!)
}
fun box(): String {
val a1 = WithPrimitive(1)
val b1 = WithPrimitive(2)
foo(a1, b1)
val a2 = WithReference("")
bar(a2, a2)
val a3 = WithNullableReference("test")
val a4 = WithNullableReference(123)
baz(a3, a4)
return "OK"
}
@@ -0,0 +1,51 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC<T: Int>(val x: T)
interface I<T> {
fun foo(t: T): T
}
interface I2: I<IC<Int>>
open class A<T> {
fun foo(t: T): T =
if (t is IC<*>)
IC(20 + t.x) as T
else
t
}
open class B: A<IC<Int>>()
class C: I2, B()
fun box(): String {
val ic = IC(10)
val i: I<IC<Int>> = C()
val i2: I2 = C()
val a: A<IC<Int>> = C()
val b: B = C()
val c: C = C()
val fooI = i.foo(ic).x
if (fooI != 30) return "Fail I"
// Test calling abstract fake override methods
// with signature specialized by inline class
val fooI2 = i2.foo(ic).x
if (fooI2 != 30) return "Fail I2"
val fooA = a.foo(ic).x
if (fooA != 30) return "Fail A"
val fooB = b.foo(ic).x
if (fooB != 30) return "Fail B"
val resC = c.foo(ic).x
if (resC != 30) return "Fail C"
return "OK"
}
@@ -0,0 +1,32 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class ULong<T: Long>(val l: T)
fun nonLocal(): ULong<Long>? {
val u1 = ULong(1)
run {
return u1 // box
}
ULong(-1)
}
fun foo(): Boolean = true
fun labeled(): ULong<Long>? {
val u = ULong(2)
return run {
if (foo()) return@run u
ULong(-1) // box
}
}
fun box(): String {
if (nonLocal()!!.l != 1L) return "fail"
if (labeled()!!.l != 2L) return "fail"
return "OK"
}
@@ -0,0 +1,38 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: Int>(val a: T) {
fun member(): String = ""
fun asResult() = a
}
fun <T> id(x: T): T = x
fun <T> T.idExtension(): T = this
fun <T: Int> Foo<T>.extension() {}
fun <T: Int> test(f: Foo<T>): String {
id(f) // box
id(f).idExtension() // box
id(f).member() // box unbox
id(f).extension() // box unbox
val a = id(f) // box unbox
val b = id(f).idExtension() // box unbox
if (a.asResult() != 10) return "fail a"
if (b.asResult() != 10) return "fail b"
return "OK"
}
fun box(): String {
val f = Foo(10)
return test(f)
}
@@ -0,0 +1,26 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineNotNullPrimitive<T: Int>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineNotNullReference<T: String>(val y: T)
fun <A, T: Int> testNotNullPrimitive(a: Any, b: A, c: InlineNotNullPrimitive<T>, d: InlineNotNullPrimitive<T>?) {}
fun <A, T: String> testNotNullReference(a: Any, b: A, c: InlineNotNullReference<T>, d: InlineNotNullReference<T>?) {}
fun test(a: InlineNotNullPrimitive<Int>, b: InlineNotNullReference<String>) {
testNotNullPrimitive(a, a, a, a) // 3 box
testNotNullReference(b, b, b, b) // 2 box
}
fun box(): String {
val a = InlineNotNullPrimitive(10)
val b = InlineNotNullReference("some")
test(a, b)
return "OK"
}
@@ -0,0 +1,61 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineNotNullPrimitive<T: Int>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineNullablePrimitive<T: Int?>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineNotNullReference<T: Any>(val a: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineNullableReference<T>(val a: T)
fun <T: Int> test1(a: InlineNotNullPrimitive<T>) {
val a0 = a
val a1: Any = a // box
val a2: Any? = a // box
val a3: InlineNotNullPrimitive<T> = a
val a4: InlineNotNullPrimitive<T>? = a // box
}
fun <T: Int?> test2(b: InlineNullablePrimitive<T>) {
val b0 = b
val b1: Any = b // box
val b2: Any? = b // box
val b3: InlineNullablePrimitive<T> = b
val b4: InlineNullablePrimitive<T>? = b // box
}
fun <T: Any> test3(c: InlineNotNullReference<T>) {
val c0 = c
val c1: Any = c // box
val c2: Any? = c // box
val c3: InlineNotNullReference<T> = c
val c4: InlineNotNullReference<T>? = c
}
fun <T> test4(d: InlineNullableReference<T>) {
val d0 = d
val d1: Any = d // box
val d2: Any? = d // box
val d3: InlineNullableReference<T> = d
val d4: InlineNullableReference<T>? = d // box
}
fun box(): String {
val a = InlineNotNullPrimitive(1)
val b = InlineNullablePrimitive(1)
val c = InlineNotNullReference("some")
val d = InlineNullableReference("other")
test1(a)
test2(b)
test3(c)
test4(d)
return "OK"
}
@@ -0,0 +1,35 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC<T: Int>(val x: T)
abstract class A<T> {
var t: T? = null
final fun foo(): T = t!!
}
class B: A<IC<Int>>()
interface I {
fun foo(): IC<Int>
}
class B2: A<IC<Int>>(), I
fun box(): String {
val b = B()
b.t = IC(10)
if (b.foo() != IC(10)) return "Fail 1"
val b2 = B2()
b2.t = IC(10)
if (b2.foo() != IC(10)) return "Fail 2"
val b2i: I = b2
if (b2i.foo() != IC(10)) return "Fail 3"
return "OK"
}
@@ -0,0 +1,27 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: Int>(val x: T) {
fun empty() = ""
fun withParam(a: String) = a
fun withInlineClassParam(f: Foo<T>) = f.toString()
fun test(): String {
val a = empty()
val b = withParam("hello")
val c = withInlineClassParam(this)
return a + b + c
}
override fun toString(): String {
return x.toString()
}
}
fun box(): String {
val f = Foo(12)
return if (f.test() != "hello12") "fail" else "OK"
return "OK"
}
@@ -0,0 +1,22 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(val s: T)
fun <T: Int> test(a1: Any, a2: UInt<T>?, a3: Any?, a4: Any?): Int {
val b1 = a1 as UInt<T>
val b2 = a2 as UInt<T>
val b3 = (a3 as UInt<T>?) as UInt<T>
val b4 = (a4 as? UInt<T>) as UInt<T>
return b1.s + b2.s + b3.s + b4.s
}
fun box(): String {
val u1 = UInt(1)
val u2 = UInt(2)
if (test(u1, u2, u1, u2) != 6) return "fail"
return "OK"
}
@@ -0,0 +1,58 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(val u: T) {
override fun toString(): String {
return "UInt: $u"
}
}
fun Any.isUInt(): Boolean = this is UInt<*>
fun Any.notIsUInt(): Boolean = this !is UInt<*>
inline fun <reified T> Any?.instanceOf(): Boolean = this is T
fun UInt<Int>.extension(): String = "OK:"
fun foo(x: UInt<Int>?): String {
if (x is UInt<*>) {
return x.extension() + x.toString()
}
return "fail"
}
fun bar(x: UInt<Int>?): String {
if (x is Any) {
return x.extension()
}
return "fail"
}
fun box(): String {
val u = UInt(12)
if (!u.isUInt()) return "fail"
if (u.notIsUInt()) return "fail"
if (1.isUInt()) return "fail"
if (!1.notIsUInt()) return "fail"
if (!u.instanceOf<UInt<Int>>()) return "fail"
if (1.instanceOf<UInt<Int>>()) return "fail"
val nullableUInt: UInt<Int>? = UInt(10)
if (!nullableUInt.instanceOf<UInt<Int>>()) return "fail"
val nullAsUInt: UInt<Int>? = null
if (nullAsUInt.instanceOf<UInt<Int>>()) return "fail"
if (!nullAsUInt.instanceOf<UInt<Int>?>()) return "fail"
if (foo(u) != "OK:UInt: 12") return "fail"
if (bar(u) != "OK:") return "fail"
return "OK"
}
@@ -0,0 +1,30 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(val value: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class ULong<T: Long>(val value: T)
fun foo(u: UInt<Int>, f: (UInt<Int>) -> ULong<Long>): ULong<Long> = f(u)
inline fun inlinedFoo(u: UInt<Int>, f: (UInt<Int>) -> ULong<Long>): ULong<Long> = f(u)
fun mapUIntToULong(u: UInt<Int>): ULong<Long> = ULong(u.value.toLong())
fun box(): String {
val u = UInt(123)
val l1 = foo(u) {
mapUIntToULong(it)
}
if (l1.value != 123L) return "fail"
val l2 = inlinedFoo(UInt(10)) {
mapUIntToULong(it)
}
if (l2.value != 10L) return "fail"
return "OK"
}
@@ -0,0 +1,43 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Result<T>(val a: T) {
fun typed(): T = a
}
fun <T> takeResult(r: Result<T>) {}
fun takeResultOfInt(r: Result<Int>) {}
fun takeInt(i: Int) {}
fun box(): String {
val asInt = Result<Int>(19)
val asString = Result<String>("sample")
val asResult = Result<Result<Int>>(asInt)
val asResultCtor = Result<Result<Int>>(Result<Int>(10))
takeResult(asInt)
takeResult(asString)
takeResult(asResult)
takeResult(asResultCtor)
takeResultOfInt(asInt)
takeInt(asInt.typed())
val unboxedInt = asInt.typed()
val unboxedString = asString.typed()
val unboxedResult = asResult.typed()
val unboxedAsCtor = asResultCtor.typed()
if (unboxedInt != 19) return "fail 1"
if (unboxedString != "sample") return "fail 2"
if (unboxedResult.typed() != 19) return "fail 3"
if (unboxedAsCtor.typed() != 10) return "fail 4"
if (asResult.typed().typed() != 19) return "fail 5"
if (asResultCtor.typed().typed() != 10) return "fail 6"
return "OK"
}
@@ -0,0 +1,22 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
var result = "Fail"
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: String>(val value: T) {
init {
class B {
init {
result = value
}
}
B()
}
}
fun box(): String {
A("OK")
return result
}
@@ -0,0 +1,82 @@
// WITH_STDLIB
// WITH_REFLECT
// TARGET_BACKEND: JVM
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
package root
import kotlin.reflect.KClass
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcInt<T: Int>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcLong<T: Long>(val l: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcAny<T>(val a: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcOverIc<T: IcLong<Long>>(val o: T)
fun check(c: KClass<*>, s: String) {
if (c.toString() != s) error("Fail, expected: $s, actual: $c")
}
fun check(actual: String?, expected: String) {
if (actual != expected) error("Fail, expected: $expected, actual: $actual")
}
inline fun <reified T> reifiedCheck(asString: String, simpleName: String) {
check(T::class, asString)
check(T::class.simpleName, simpleName)
}
fun box(): String {
val i = IcInt(0)
val l = IcLong(0)
val a = IcAny("foo")
val o = IcOverIc(IcLong(0))
check(i::class, "class root.IcInt")
check(l::class, "class root.IcLong")
check(a::class, "class root.IcAny")
check(o::class, "class root.IcOverIc")
check(1u::class, "class kotlin.UInt")
check(i::class.simpleName, "IcInt")
check(l::class.simpleName, "IcLong")
check(a::class.simpleName, "IcAny")
check(o::class.simpleName, "IcOverIc")
check(1u::class.simpleName, "UInt")
reifiedCheck<IcInt<Int>>("class root.IcInt", "IcInt")
reifiedCheck<IcLong<Long>>("class root.IcLong", "IcLong")
reifiedCheck<IcAny<Any?>>("class root.IcAny", "IcAny")
reifiedCheck<IcOverIc<IcLong<Long>>>("class root.IcOverIc", "IcOverIc")
reifiedCheck<UInt>("class kotlin.UInt", "UInt")
val arrI = arrayOf(i)
check(arrI[0]::class, "class root.IcInt")
val arrL = arrayOf(l)
check(arrL[0]::class, "class root.IcLong")
val arrA = arrayOf(a)
check(arrA[0]::class, "class root.IcAny")
val arrO = arrayOf(o)
check(arrO[0]::class, "class root.IcOverIc")
val arrU = arrayOf(1u)
check(arrU[0]::class, "class kotlin.UInt")
check(IcInt::class, "class root.IcInt")
check(IcLong::class, "class root.IcLong")
check(IcAny::class, "class root.IcAny")
check(IcOverIc::class, "class root.IcOverIc")
check(UInt::class, "class kotlin.UInt")
return "OK"
}
@@ -0,0 +1,19 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: Int>(val x: T) : Comparable<Foo<T>> {
override fun compareTo(other: Foo<T>): Int {
return 10
}
}
fun box(): String {
val f1 = Foo(42)
val ff1: Comparable<Foo<Int>> = f1
if (ff1.compareTo(f1) != 10) return "Fail"
return "OK"
}
@@ -0,0 +1,18 @@
// WITH_STDLIB
// KJS_FULL_RUNTIME
// SKIP_MANGLE_VERIFICATION
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
interface I {
companion object {
val default: IC<String> by lazy(::IC)
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC<T: String>(val ok: T = "OK" as T) : I
fun box(): String {
return I.default.ok
}
@@ -0,0 +1,27 @@
// TARGET_BACKEND: JVM
// WITH_STDLIB
// FULL_JDK
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
import java.lang.reflect.Modifier
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC1<T: Int> public constructor(val i: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC11<T: Int> internal constructor(val i: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC2<T: Int> private constructor(val i: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC4<T: Int> protected constructor(val i: T)
fun box(): String {
if (!Modifier.isPublic(IC1::class.java.declaredMethods.single { it.name == "constructor-impl" }.modifiers)) return "FAIL 1"
if (!Modifier.isPublic(IC11::class.java.declaredMethods.single { it.name == "constructor-impl" }.modifiers)) return "FAIL 1"
if (!Modifier.isPrivate(IC2::class.java.declaredMethods.single { it.name == "constructor-impl" }.modifiers)) return "FAIL 2"
if (!Modifier.isProtected(IC4::class.java.declaredMethods.single { it.name == "constructor-impl" }.modifiers)) return "FAIL 4"
return "OK"
}
@@ -0,0 +1,22 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
interface Base {
fun result(): Int
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Inlined<T: Int>(val x: T) : Base {
override fun result(): Int = x
}
fun foo(b: Boolean): Base {
return if (b) Inlined(0) else Inlined(1)
}
fun box(): String {
if (foo(true).result() != 0) return "Fail 1"
if (foo(false).result() != 1) return "Fail 2"
return "OK"
}
@@ -0,0 +1,34 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class AsInt<T: Int>(val value: T) {
override fun toString(): String {
return "asInt: ${value.toString()}"
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class AsAny<T: Any>(val value: T) {
override fun toString(): String {
return "asAny: ${value.toString()}"
}
}
fun takeAny(a: Any): String = a.toString()
fun getInt(): Int = 10
fun <T> id(x: T) = x
fun box(): String {
if (takeAny(AsInt(123)) != "asInt: 123") return "fail"
if (takeAny(AsAny(321)) != "asAny: 321") return "fail"
if (takeAny(AsInt(getInt())) != "asInt: 10") return "fail"
if (takeAny(AsInt(id(20))) != "asInt: 20") return "fail"
if (takeAny(AsAny(id(30))) != "asAny: 30") return "fail"
return "OK"
}
@@ -0,0 +1,37 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Result<T>(val a: T) {
fun getOrThrow(): T = a
}
abstract class ResultReceiver<T> {
abstract fun receive(result: Result<T>)
}
inline fun <T> ResultReceiver(crossinline f: (Result<T>) -> Unit): ResultReceiver<T> =
object : ResultReceiver<T>() {
override fun receive(result: Result<T>) {
f(result)
}
}
fun test() {
var invoked = false
val receiver = ResultReceiver<String> { result ->
val intResult = result.getOrThrow()
invoked = true
}
receiver.receive(Result("42"))
if (!invoked) {
throw RuntimeException("Fail")
}
}
fun box(): String {
test()
return "OK"
}
@@ -0,0 +1,59 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: Int>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class FooRef<T: String>(val y: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class FooLong<T: Long>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class FooDouble<T: Double>(val y: T)
fun box(): String {
val f = Foo(42)
if (f.toString() != "Foo(x=42)") return "Fail 1: $f"
if (!f.equals(f)) return "Fail 2"
val g = Foo(43)
if (f.equals(g)) return "Fail 3"
if (42.hashCode() != f.hashCode()) return "Fail 4"
val fRef = FooRef("42")
if (fRef.toString() != "FooRef(y=42)") return "Fail 5: $fRef"
if (!fRef.equals(fRef)) return "Fail 6"
val gRef = FooRef("43")
if (fRef.equals(gRef)) return "Fail 7"
if ("42".hashCode() != fRef.hashCode()) return "Fail 8"
val fLong = FooLong(42)
if (fLong.toString() != "FooLong(x=42)") return "Fail 9: $fLong"
if (!fLong.equals(fLong)) return "Fail 10"
val gLong = FooLong(43)
if (fLong.equals(gLong)) return "Fail 11"
if (42L.hashCode() != fLong.hashCode()) return "Fail 12"
val fDouble = FooDouble(42.1)
if (fDouble.toString() != "FooDouble(y=42.1)") return "Fail 13: $fDouble"
if (!fDouble.equals(fDouble)) return "Fail 14"
val gDouble = FooDouble(43.0)
if (fDouble.equals(gDouble)) return "Fail 15"
if (42.1.hashCode() != fDouble.hashCode()) return "Fail 16"
return "OK"
}
@@ -0,0 +1,18 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
interface IFoo<T> {
fun foo(x: T): String = "O"
fun T.bar(): String = "K"
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class L<T: Long>(val x: T) : IFoo<L<T>>
fun box(): String {
val z = L(0L)
return with(z) {
foo(z) + z.bar()
}
}
@@ -0,0 +1,11 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
// FILE: a.kt
fun box() = A(0).f()
// FILE: b.kt
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Int>(val i: T)
fun <T: Int> A<T>.f(xs: Array<String> = Array<String>(1) { "OK" }) = xs[i]
@@ -0,0 +1,27 @@
// WITH_STDLIB
// IGNORE_LIGHT_ANALYSIS
// IGNORE_BACKEND: JVM
// LANGUAGE: +InlineClassImplementationByDelegation, +GenericInlineClassParameter
interface I {
fun o(k: String = "K"): String = "O$k"
}
inline class IC<T: I>(val i: T): I by i
fun box(): String {
val i = object : I {}
val ic1 = IC(i)
var res = ic1.o()
if (res != "OK") return "FAIL 1: $res"
res = ic1.o("KK")
if (res != "OKK") return "FAIL 2: $res"
val ic2: I = IC(i)
res = ic2.o()
if (res != "OK") return "FAIL 3: $res"
res = ic2.o("KK")
if (res != "OKK") return "FAIL 4: $res"
return "OK"
}
@@ -0,0 +1,19 @@
// WITH_STDLIB
// IGNORE_LIGHT_ANALYSIS
// IGNORE_BACKEND: JVM
// LANGUAGE: +InlineClassImplementationByDelegation, +GenericInlineClassParameter
interface I {
fun ok(): String = "OK"
}
inline class IC<T: I>(val i: T): I by i
fun box(): String {
val i = object : I {}
var res = IC(i).ok()
if (res != "OK") return "FAIL: $res"
val ic: I = IC(i)
res = ic.ok()
return res
}
@@ -0,0 +1,21 @@
// WITH_STDLIB
// IGNORE_LIGHT_ANALYSIS
// IGNORE_BACKEND: JVM
// LANGUAGE: +InlineClassImplementationByDelegation, +GenericInlineClassParameter
interface I {
fun ok(): String
}
inline class IC<T: I>(val i: T): I by i
fun box(): String {
val i = object : I {
override fun ok(): String = "OK"
}
var res = IC(i).ok()
if (res != "OK") return "FAIL: $res"
val ic: I = IC(i)
res = ic.ok()
return res
}
@@ -0,0 +1,19 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class UInt<T: Int>(private val data: T) {
fun result(): String = if (data == 1) "OK" else "fail"
}
fun f(): UInt<Int> {
val unull = UInt(1) ?: null
return nonNull(unull)
}
fun <T: Int> nonNull(u: UInt<T>?) = u!!
fun box(): String {
return f().result()
}
@@ -0,0 +1,13 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Foo<T: String>(val s: T) {
fun asResult(): String = s
}
fun box(): String {
val a = Foo("OK")
return a.asResult()
}
@@ -0,0 +1,55 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Inner<T: String>(val w: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Inner<String>>(val x: T)
fun <T: Inner<String>> isNullVacuousLeft(s: A<T>) = s == null
fun <T: Inner<String>> isNullVacuousRight(s: A<T>) = null == s
fun <T: Inner<String>> isNullLeft(s: A<T>?) = s == null
fun <T: Inner<String>> isNullRight(s: A<T>?) = null == s
fun <T: Inner<String>> isEqualSame(s: A<T>, t: A<T>) = s == t
fun <T: Inner<String>> isEqualAnyLeft(s: A<T>, t: Any?) = s == t
fun <T: Inner<String>> isEqualAnyRight(s: Any?, t: A<T>) = s == t
fun <T: Inner<String>> isEqualSameNullable(s: A<T>?, t: A<T>?) = s == t
fun <T: Inner<String>> isEqualAnyNullableLeft(s: A<T>?, t: Any?) = s == t
fun <T: Inner<String>> isEqualAnyNullableRight(s: Any?, t: A<T>?) = s == t
fun box(): String {
if (isNullVacuousLeft(A(Inner("")))) return "Fail 1"
if (isNullVacuousRight(A(Inner("")))) return "Fail 2"
if (isNullLeft(A(Inner("")))) return "Fail 3"
if (isNullRight(A(Inner("")))) return "Fail 4"
if (!isNullLeft<Inner<String>>(null)) return "Fail 5"
if (!isNullRight<Inner<String>>(null)) return "Fail 6"
if (!isEqualSame(A(Inner("")), A(Inner("")))) return "Fail 7"
if (isEqualSame(A(Inner("")), A(Inner("a")))) return "Fail 8"
if (isEqualAnyLeft(A(Inner("")), Inner(""))) return "Fail 9"
if (isEqualAnyLeft(A(Inner("")), null)) return "Fail 10"
if (!isEqualAnyLeft(A(Inner("")), A(Inner("")))) return "Fail 11"
if (isEqualAnyRight(Inner(""), A(Inner("")))) return "Fail 12"
if (isEqualAnyRight(null, A(Inner("")))) return "Fail 13"
if (!isEqualAnyRight(A(Inner("")), A(Inner("")))) return "Fail 14"
if (!isEqualSameNullable<Inner<String>>(null, null)) return "Fail 15"
if (!isEqualSameNullable(A(Inner("")), A(Inner("")))) return "Fail 16"
if (isEqualSameNullable(null, A(Inner("")))) return "Fail 17"
if (isEqualSameNullable(A(Inner("")), null)) return "Fail 18"
if (isEqualSameNullable(A(Inner("")), A(Inner("a")))) return "Fail 19"
if (!isEqualAnyNullableLeft<Inner<String>>(null, null)) return "Fail 20"
if (!isEqualAnyNullableLeft(A(Inner("")), A(Inner("")))) return "Fail 21"
if (isEqualAnyNullableLeft(A(Inner("")), "")) return "Fail 22"
if (isEqualAnyNullableLeft<Inner<String>>(null, Inner(""))) return "Fail 23"
if (isEqualAnyNullableLeft(A(Inner("")), null)) return "Fail 24"
if (isEqualAnyNullableLeft(A(Inner("")), A(Inner("a")))) return "Fail 25"
if (!isEqualAnyNullableRight<Inner<String>>(null, null)) return "Fail 26"
if (!isEqualAnyNullableRight(A(Inner("a")), A(Inner("a")))) return "Fail 27"
if (isEqualAnyNullableRight(Inner(""), A(Inner("")))) return "Fail 28"
if (isEqualAnyNullableRight<Inner<String>>(Inner(""), null)) return "Fail 29"
if (isEqualAnyNullableRight(null, A(Inner("")))) return "Fail 30"
if (isEqualAnyNullableRight(A(Inner("a")), A(Inner("b")))) return "Fail 31"
return "OK"
}
@@ -0,0 +1,17 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: String>(val a: T)
fun <T: String> isEqualNA(x: A<T>?, y: A<T>) = x == y
fun <T: String> isEqualAN(x: A<T>, y: A<T>?) = x == y
fun box(): String {
if (isEqualNA(null, A(""))) return "Fail 1"
if (isEqualAN(A(""), null)) return "Fail 2"
if (!isEqualNA(A(""), A(""))) return "Fail 3"
if (!isEqualAN(A(""), A(""))) return "Fail 4"
return "OK"
}
@@ -0,0 +1,54 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Inner<T: String>(val w: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Inner<String>>(val x: T)
fun <T: Inner<String>> isNotNullVacuousLeft(s: A<T>) = s != null
fun <T: Inner<String>> isNotNullVacuousRight(s: A<T>) = null != s
fun <T: Inner<String>> isNotNullLeft(s: A<T>?) = s != null
fun <T: Inner<String>> isNotNullRight(s: A<T>?) = null != s
fun <T: Inner<String>> isNotEqualSame(s: A<T>, t: A<T>) = s != t
fun <T: Inner<String>> isNotEqualAnyLeft(s: A<T>, t: Any?) = s != t
fun <T: Inner<String>> isNotEqualAnyRight(s: Any?, t: A<T>) = s != t
fun <T: Inner<String>> isNotEqualSameNullable(s: A<T>?, t: A<T>?) = s != t
fun <T: Inner<String>> isNotEqualAnyNullableLeft(s: A<T>?, t: Any?) = s != t
fun <T: Inner<String>> isNotEqualAnyNullableRight(s: Any?, t: A<T>?) = s != t
fun box(): String {
if (!isNotNullVacuousLeft(A(Inner("")))) return "Fail 1"
if (!isNotNullVacuousRight(A(Inner("")))) return "Fail 2"
if (!isNotNullLeft(A(Inner("")))) return "Fail 3"
if (!isNotNullRight(A(Inner("")))) return "Fail 4"
if (isNotNullLeft<Inner<String>>(null)) return "Fail 5"
if (isNotNullRight<Inner<String>>(null)) return "Fail 6"
if (isNotEqualSame(A(Inner("")), A(Inner("")))) return "Fail 7"
if (!isNotEqualSame(A(Inner("")), A(Inner("a")))) return "Fail 8"
if (!isNotEqualAnyLeft(A(Inner("")), Inner(""))) return "Fail 9"
if (!isNotEqualAnyLeft(A(Inner("")), null)) return "Fail 10"
if (isNotEqualAnyLeft(A(Inner("")), A(Inner("")))) return "Fail 11"
if (!isNotEqualAnyRight(Inner(""), A(Inner("")))) return "Fail 12"
if (!isNotEqualAnyRight(null, A(Inner("")))) return "Fail 13"
if (isNotEqualAnyRight(A(Inner("")), A(Inner("")))) return "Fail 14"
if (isNotEqualSameNullable<Inner<String>>(null, null)) return "Fail 15"
if (isNotEqualSameNullable(A(Inner("")), A(Inner("")))) return "Fail 16"
if (!isNotEqualSameNullable(null, A(Inner("")))) return "Fail 17"
if (!isNotEqualSameNullable(A(Inner("")), null)) return "Fail 18"
if (!isNotEqualSameNullable(A(Inner("")), A(Inner("a")))) return "Fail 19"
if (isNotEqualAnyNullableLeft<Inner<String>>(null, null)) return "Fail 20"
if (isNotEqualAnyNullableLeft(A(Inner("")), A(Inner("")))) return "Fail 21"
if (!isNotEqualAnyNullableLeft(A(Inner("")), "")) return "Fail 22"
if (!isNotEqualAnyNullableLeft<Inner<String>>(null, Inner(""))) return "Fail 23"
if (!isNotEqualAnyNullableLeft(A(Inner("")), null)) return "Fail 24"
if (!isNotEqualAnyNullableLeft(A(Inner("")), A(Inner("a")))) return "Fail 25"
if (isNotEqualAnyNullableRight<Inner<String>>(null, null)) return "Fail 26"
if (isNotEqualAnyNullableRight(A(Inner("a")), A(Inner("a")))) return "Fail 27"
if (!isNotEqualAnyNullableRight(Inner(""), A(Inner("")))) return "Fail 28"
if (!isNotEqualAnyNullableRight<Inner<String>>(Inner(""), null)) return "Fail 29"
if (!isNotEqualAnyNullableRight(null, A(Inner("")))) return "Fail 30"
if (!isNotEqualAnyNullableRight(A(Inner("a")), A(Inner("b")))) return "Fail 31"
return "OK"
}
@@ -0,0 +1,60 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: String>(val x: T)
fun <T: String> isNotNullVacuousLeft(s: A<T>) = s != null
fun <T: String> isNotNullVacuousRight(s: A<T>) = null != s
fun <T: String> isNotNullLeft(s: A<T>?) = s != null
fun <T: String> isNotNullRight(s: A<T>?) = null != s
fun <T: String> isNotEqualSame(s: A<T>, t: A<T>) = s != t
fun <T: String> isNotEqualAnyLeft(s: A<T>, t: Any?) = s != t
fun <T: String> isNotEqualAnyRight(s: Any?, t: A<T>) = s != t
fun <T: String> isNotEqualSameNullable(s: A<T>?, t: A<T>?) = s != t
fun <T: String> isNotEqualAnyNullableLeft(s: A<T>?, t: Any?) = s != t
fun <T: String> isNotEqualAnyNullableRight(s: Any?, t: A<T>?) = s != t
fun <T: String> isNotEqualNullableUnboxedLeft(s: A<T>, t: A<T>?) = s != t
fun <T: String> isNotEqualNullableUnboxedRight(s: A<T>?, t: A<T>) = s != t
fun box(): String {
if (!isNotNullVacuousLeft(A(""))) return "Fail 1"
if (!isNotNullVacuousRight(A(""))) return "Fail 2"
if (!isNotNullLeft(A(""))) return "Fail 3"
if (!isNotNullRight(A(""))) return "Fail 4"
if (isNotNullLeft<String>(null)) return "Fail 5"
if (isNotNullRight<String>(null)) return "Fail 6"
if (isNotEqualSame(A(""), A(""))) return "Fail 7"
if (!isNotEqualSame(A("a"), A("b"))) return "Fail 8"
if (!isNotEqualAnyLeft(A(""), "")) return "Fail 9"
if (!isNotEqualAnyLeft(A(""), null)) return "Fail 10"
if (isNotEqualAnyLeft(A(""), A(""))) return "Fail 11"
if (!isNotEqualAnyRight("", A(""))) return "Fail 12"
if (!isNotEqualAnyRight(null, A(""))) return "Fail 13"
if (isNotEqualAnyRight(A(""), A(""))) return "Fail 14"
if (isNotEqualSameNullable<String>(null, null)) return "Fail 15"
if (isNotEqualSameNullable(A(""), A(""))) return "Fail 16"
if (!isNotEqualSameNullable(null, A(""))) return "Fail 17"
if (!isNotEqualSameNullable(A(""), null)) return "Fail 18"
if (!isNotEqualSameNullable(A(""), A("a"))) return "Fail 19"
if (isNotEqualAnyNullableLeft<String>(null, null)) return "Fail 20"
if (isNotEqualAnyNullableLeft(A(""), A(""))) return "Fail 21"
if (!isNotEqualAnyNullableLeft(A(""), "")) return "Fail 22"
if (!isNotEqualAnyNullableLeft<String>(null, "")) return "Fail 23"
if (!isNotEqualAnyNullableLeft(A(""), null)) return "Fail 24"
if (!isNotEqualAnyNullableLeft(A(""), A("a"))) return "Fail 25"
if (isNotEqualAnyNullableRight<String>(null, null)) return "Fail 26"
if (isNotEqualAnyNullableRight(A(""), A(""))) return "Fail 27"
if (!isNotEqualAnyNullableRight("", A(""))) return "Fail 28"
if (!isNotEqualAnyNullableRight<String>("", null)) return "Fail 29"
if (!isNotEqualAnyNullableRight(null, A(""))) return "Fail 30"
if (!isNotEqualAnyNullableRight(A(""), A("a"))) return "Fail 31"
if (!isNotEqualNullableUnboxedLeft(A(""), A("a"))) return "Fail 32"
if (isNotEqualNullableUnboxedLeft(A(""), A(""))) return "Fail 33"
if (!isNotEqualNullableUnboxedRight(A(""), A("a"))) return "Fail 34"
if (isNotEqualNullableUnboxedRight(A("a"), A("a"))) return "Fail 35"
if (!isNotEqualNullableUnboxedLeft(A(""), null)) return "Fail 36"
if (!isNotEqualNullableUnboxedRight(null, A(""))) return "Fail 37"
return "OK"
}
@@ -0,0 +1,76 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T>(val x: T)
fun <T> isNotNullVacuousLeft(s: A<T>) = s != null
fun <T> isNotNullVacuousRight(s: A<T>) = null != s
fun <T> isNotNullLeft(s: A<T>?) = s != null
fun <T> isNotNullRight(s: A<T>?) = null != s
fun <T> isNotEqualSame(s: A<T>, t: A<T>) = s != t
fun <T> isNotEqualAnyLeft(s: A<T>, t: Any?) = s != t
fun <T> isNotEqualAnyRight(s: Any?, t: A<T>) = s != t
fun <T> isNotEqualSameNullable(s: A<T>?, t: A<T>?) = s != t
fun <T> isNotEqualAnyNullableLeft(s: A<T>?, t: Any?) = s != t
fun <T> isNotEqualAnyNullableRight(s: Any?, t: A<T>?) = s != t
fun <T> isNotEqualNullableUnboxedLeft(s: A<T>, t: A<T>?) = s != t
fun <T> isNotEqualNullableUnboxedRight(s: A<T>?, t: A<T>) = s != t
fun box(): String {
if (!isNotNullVacuousLeft(A(0))) return "Fail 1"
if (!isNotNullVacuousRight(A(0))) return "Fail 2"
if (!isNotNullLeft(A(0))) return "Fail 3"
if (!isNotNullRight(A(0))) return "Fail 4"
if (isNotNullLeft<Any?>(null)) return "Fail 5"
if (isNotNullRight<Any?>(null)) return "Fail 6"
if (isNotEqualSame(A(0), A(0))) return "Fail 7"
if (!isNotEqualSame(A(0), A(1))) return "Fail 8"
if (!isNotEqualAnyLeft(A(0), 0)) return "Fail 9"
if (!isNotEqualAnyLeft(A(0), null)) return "Fail 10"
if (isNotEqualAnyLeft(A(0), A(0))) return "Fail 11"
if (!isNotEqualAnyRight(0, A(0))) return "Fail 12"
if (!isNotEqualAnyRight(null, A(0))) return "Fail 13"
if (isNotEqualAnyRight(A(0), A(0))) return "Fail 14"
if (isNotEqualSameNullable<Any?>(null, null)) return "Fail 15"
if (isNotEqualSameNullable(A(0), A(0))) return "Fail 16"
if (!isNotEqualSameNullable(null, A(0))) return "Fail 17"
if (!isNotEqualSameNullable(A(0), null)) return "Fail 18"
if (!isNotEqualSameNullable(A(0), A(1))) return "Fail 19"
if (isNotEqualAnyNullableLeft<Any?>(null, null)) return "Fail 20"
if (isNotEqualAnyNullableLeft(A(0), A(0))) return "Fail 21"
if (!isNotEqualAnyNullableLeft(A(0), 0)) return "Fail 22"
if (!isNotEqualAnyNullableLeft<Any?>(null, 0)) return "Fail 23"
if (!isNotEqualAnyNullableLeft(A(0), null)) return "Fail 24"
if (!isNotEqualAnyNullableLeft(A(0), A(1))) return "Fail 25"
if (isNotEqualAnyNullableRight<Any?>(null, null)) return "Fail 26"
if (isNotEqualAnyNullableRight(A(0), A(0))) return "Fail 27"
if (!isNotEqualAnyNullableRight(0, A(0))) return "Fail 28"
if (!isNotEqualAnyNullableRight<Any?>(0, null)) return "Fail 29"
if (!isNotEqualAnyNullableRight(null, A(0))) return "Fail 30"
if (!isNotEqualAnyNullableRight(A(0), A(1))) return "Fail 31"
if (!isNotNullVacuousLeft(A(null))) return "Fail 32"
if (!isNotNullVacuousRight(A(null))) return "Fail 33"
if (!isNotNullLeft(A(null))) return "Fail 34"
if (!isNotNullRight(A(null))) return "Fail 35"
if (!isNotEqualAnyLeft(A(null), null)) return "Fail 36"
if (!isNotEqualAnyRight(null, A(null))) return "Fail 37"
if (!isNotEqualAnyNullableLeft(A(null), null)) return "Fail 38"
if (!isNotEqualAnyNullableRight(null, A(null))) return "Fail 39"
if (!isNotEqualSameNullable(A(null), null)) return "Fail 42"
if (!isNotEqualSameNullable(null, A(null))) return "Fail 43"
if (!isNotEqualNullableUnboxedLeft(A(0), A(1))) return "Fail 44"
if (isNotEqualNullableUnboxedLeft(A(0), A(0))) return "Fail 45"
if (!isNotEqualNullableUnboxedRight(A(0), A(1))) return "Fail 46"
if (isNotEqualNullableUnboxedRight(A(1), A(1))) return "Fail 47"
if (!isNotEqualNullableUnboxedLeft(A(0), null)) return "Fail 48"
if (!isNotEqualNullableUnboxedRight(null, A(1))) return "Fail 49"
if (!isNotEqualNullableUnboxedRight(null, A(null))) return "Fail 50"
if (!isNotEqualNullableUnboxedLeft(A(null), null)) return "Fail 51"
return "OK"
}
@@ -0,0 +1,60 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Int>(val x: T)
fun <T: Int> isNotNullVacuousLeft(s: A<T>) = s != null
fun <T: Int> isNotNullVacuousRight(s: A<T>) = null != s
fun <T: Int> isNotNullLeft(s: A<T>?) = s != null
fun <T: Int> isNotNullRight(s: A<T>?) = null != s
fun <T: Int> isNotEqualSame(s: A<T>, t: A<T>) = s != t
fun <T: Int> isNotEqualAnyLeft(s: A<T>, t: Any?) = s != t
fun <T: Int> isNotEqualAnyRight(s: Any?, t: A<T>) = s != t
fun <T: Int> isNotEqualSameNullable(s: A<T>?, t: A<T>?) = s != t
fun <T: Int> isNotEqualAnyNullableLeft(s: A<T>?, t: Any?) = s != t
fun <T: Int> isNotEqualAnyNullableRight(s: Any?, t: A<T>?) = s != t
fun <T: Int> isNotEqualNullableUnboxedLeft(s: A<T>, t: A<T>?) = s != t
fun <T: Int> isNotEqualNullableUnboxedRight(s: A<T>?, t: A<T>) = s != t
fun box(): String {
if (!isNotNullVacuousLeft(A(0))) return "Fail 1"
if (!isNotNullVacuousRight(A(0))) return "Fail 2"
if (!isNotNullLeft(A(0))) return "Fail 3"
if (!isNotNullRight(A(0))) return "Fail 4"
if (isNotNullLeft<Int>(null)) return "Fail 5"
if (isNotNullRight<Int>(null)) return "Fail 6"
if (isNotEqualSame(A(0), A(0))) return "Fail 7"
if (!isNotEqualSame(A(0), A(1))) return "Fail 8"
if (!isNotEqualAnyLeft(A(0), 0)) return "Fail 9"
if (!isNotEqualAnyLeft(A(0), null)) return "Fail 10"
if (isNotEqualAnyLeft(A(0), A(0))) return "Fail 11"
if (!isNotEqualAnyRight(0, A(0))) return "Fail 12"
if (!isNotEqualAnyRight(null, A(0))) return "Fail 13"
if (isNotEqualAnyRight(A(0), A(0))) return "Fail 14"
if (isNotEqualSameNullable<Int>(null, null)) return "Fail 15"
if (isNotEqualSameNullable(A(0), A(0))) return "Fail 16"
if (!isNotEqualSameNullable(null, A(0))) return "Fail 17"
if (!isNotEqualSameNullable(A(0), null)) return "Fail 18"
if (!isNotEqualSameNullable(A(0), A(1))) return "Fail 19"
if (isNotEqualAnyNullableLeft<Int>(null, null)) return "Fail 20"
if (isNotEqualAnyNullableLeft(A(0), A(0))) return "Fail 21"
if (!isNotEqualAnyNullableLeft(A(0), 0)) return "Fail 22"
if (!isNotEqualAnyNullableLeft<Int>(null, 0)) return "Fail 23"
if (!isNotEqualAnyNullableLeft(A(0), null)) return "Fail 24"
if (!isNotEqualAnyNullableLeft(A(0), A(1))) return "Fail 25"
if (isNotEqualAnyNullableRight<Int>(null, null)) return "Fail 26"
if (isNotEqualAnyNullableRight(A(0), A(0))) return "Fail 27"
if (!isNotEqualAnyNullableRight(0, A(0))) return "Fail 28"
if (!isNotEqualAnyNullableRight<Int>(0, null)) return "Fail 29"
if (!isNotEqualAnyNullableRight(null, A(0))) return "Fail 30"
if (!isNotEqualAnyNullableRight(A(0), A(1))) return "Fail 31"
if (!isNotEqualNullableUnboxedLeft(A(0), A(1))) return "Fail 32"
if (isNotEqualNullableUnboxedLeft(A(0), A(0))) return "Fail 33"
if (!isNotEqualNullableUnboxedRight(A(0), A(1))) return "Fail 34"
if (isNotEqualNullableUnboxedRight(A(1), A(1))) return "Fail 35"
if (!isNotEqualNullableUnboxedLeft(A(0), null)) return "Fail 36"
if (!isNotEqualNullableUnboxedRight(null, A(1))) return "Fail 37"
return "OK"
}
@@ -0,0 +1,60 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: String>(val x: T)
fun <T: String> isNullVacuousLeft(s: A<T>) = s == null
fun <T: String> isNullVacuousRight(s: A<T>) = null == s
fun <T: String> isNullLeft(s: A<T>?) = s == null
fun <T: String> isNullRight(s: A<T>?) = null == s
fun <T: String> isEqualSame(s: A<T>, t: A<T>) = s == t
fun <T: String> isEqualAnyLeft(s: A<T>, t: Any?) = s == t
fun <T: String> isEqualAnyRight(s: Any?, t: A<T>) = s == t
fun <T: String> isEqualSameNullable(s: A<T>?, t: A<T>?) = s == t
fun <T: String> isEqualAnyNullableLeft(s: A<T>?, t: Any?) = s == t
fun <T: String> isEqualAnyNullableRight(s: Any?, t: A<T>?) = s == t
fun <T: String> isEqualNullableUnboxedLeft(s: A<T>, t: A<T>?) = s == t
fun <T: String> isEqualNullableUnboxedRight(s: A<T>?, t: A<T>) = s == t
fun box(): String {
if (isNullVacuousLeft(A(""))) return "Fail 1"
if (isNullVacuousRight(A(""))) return "Fail 2"
if (isNullLeft(A(""))) return "Fail 3"
if (isNullRight(A(""))) return "Fail 4"
if (!isNullLeft<String>(null)) return "Fail 5"
if (!isNullRight<String>(null)) return "Fail 6"
if (!isEqualSame(A(""), A(""))) return "Fail 7"
if (isEqualSame(A("a"), A("b"))) return "Fail 8"
if (isEqualAnyLeft(A(""), "")) return "Fail 9"
if (isEqualAnyLeft(A(""), null)) return "Fail 10"
if (!isEqualAnyLeft(A(""), A(""))) return "Fail 11"
if (isEqualAnyRight("", A(""))) return "Fail 12"
if (isEqualAnyRight(null, A(""))) return "Fail 13"
if (!isEqualAnyRight(A(""), A(""))) return "Fail 14"
if (!isEqualSameNullable<String>(null, null)) return "Fail 15"
if (!isEqualSameNullable(A(""), A(""))) return "Fail 16"
if (isEqualSameNullable(null, A(""))) return "Fail 17"
if (isEqualSameNullable(A(""), null)) return "Fail 18"
if (isEqualSameNullable(A(""), A("a"))) return "Fail 19"
if (!isEqualAnyNullableLeft<String>(null, null)) return "Fail 20"
if (!isEqualAnyNullableLeft(A(""), A(""))) return "Fail 21"
if (isEqualAnyNullableLeft(A(""), "")) return "Fail 22"
if (isEqualAnyNullableLeft<String>(null, "")) return "Fail 23"
if (isEqualAnyNullableLeft(A(""), null)) return "Fail 24"
if (isEqualAnyNullableLeft(A(""), A("a"))) return "Fail 25"
if (!isEqualAnyNullableRight<String>(null, null)) return "Fail 26"
if (!isEqualAnyNullableRight(A(""), A(""))) return "Fail 27"
if (isEqualAnyNullableRight("", A(""))) return "Fail 28"
if (isEqualAnyNullableRight<String>("", null)) return "Fail 29"
if (isEqualAnyNullableRight(null, A(""))) return "Fail 30"
if (isEqualAnyNullableRight(A(""), A("a"))) return "Fail 31"
if (isEqualNullableUnboxedLeft(A(""), A("a"))) return "Fail 32"
if (!isEqualNullableUnboxedLeft(A(""), A(""))) return "Fail 33"
if (isEqualNullableUnboxedRight(A(""), A("a"))) return "Fail 34"
if (!isEqualNullableUnboxedRight(A("a"), A("a"))) return "Fail 35"
if (isEqualNullableUnboxedLeft(A(""), null)) return "Fail 36"
if (isEqualNullableUnboxedRight(null, A(""))) return "Fail 37"
return "OK"
}
@@ -0,0 +1,76 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T>(val x: T)
fun <T> isNullVacuousLeft(s: A<T>) = s == null
fun <T> isNullVacuousRight(s: A<T>) = null == s
fun <T> isNullLeft(s: A<T>?) = s == null
fun <T> isNullRight(s: A<T>?) = null == s
fun <T> isEqualSame(s: A<T>, t: A<T>) = s == t
fun <T> isEqualAnyLeft(s: A<T>, t: Any?) = s == t
fun <T> isEqualAnyRight(s: Any?, t: A<T>) = s == t
fun <T> isEqualSameNullable(s: A<T>?, t: A<T>?) = s == t
fun <T> isEqualAnyNullableLeft(s: A<T>?, t: Any?) = s == t
fun <T> isEqualAnyNullableRight(s: Any?, t: A<T>?) = s == t
fun <T> isEqualNullableUnboxedLeft(s: A<T>, t: A<T>?) = s == t
fun <T> isEqualNullableUnboxedRight(s: A<T>?, t: A<T>) = s == t
fun box(): String {
if (isNullVacuousLeft(A(0))) return "Fail 1"
if (isNullVacuousRight(A(0))) return "Fail 2"
if (isNullLeft(A(0))) return "Fail 3"
if (isNullRight(A(0))) return "Fail 4"
if (!isNullLeft<Any?>(null)) return "Fail 5"
if (!isNullRight<Any?>(null)) return "Fail 6"
if (!isEqualSame(A(0), A(0))) return "Fail 7"
if (isEqualSame(A(0), A(1))) return "Fail 8"
if (isEqualAnyLeft(A(0), 0)) return "Fail 9"
if (isEqualAnyLeft(A(0), null)) return "Fail 10"
if (!isEqualAnyLeft(A(0), A(0))) return "Fail 11"
if (isEqualAnyRight(0, A(0))) return "Fail 12"
if (isEqualAnyRight(null, A(0))) return "Fail 13"
if (!isEqualAnyRight(A(0), A(0))) return "Fail 14"
if (!isEqualSameNullable<Any?>(null, null)) return "Fail 15"
if (!isEqualSameNullable(A(0), A(0))) return "Fail 16"
if (isEqualSameNullable(null, A(0))) return "Fail 17"
if (isEqualSameNullable(A(0), null)) return "Fail 18"
if (isEqualSameNullable(A(0), A(1))) return "Fail 19"
if (!isEqualAnyNullableLeft<Any?>(null, null)) return "Fail 20"
if (!isEqualAnyNullableLeft(A(0), A(0))) return "Fail 21"
if (isEqualAnyNullableLeft(A(0), 0)) return "Fail 22"
if (isEqualAnyNullableLeft<Any?>(null, 0)) return "Fail 23"
if (isEqualAnyNullableLeft(A(0), null)) return "Fail 24"
if (isEqualAnyNullableLeft(A(0), A(1))) return "Fail 25"
if (!isEqualAnyNullableRight<Any?>(null, null)) return "Fail 26"
if (!isEqualAnyNullableRight(A(0), A(0))) return "Fail 27"
if (isEqualAnyNullableRight(0, A(0))) return "Fail 28"
if (isEqualAnyNullableRight<Any?>(0, null)) return "Fail 29"
if (isEqualAnyNullableRight(null, A(0))) return "Fail 30"
if (isEqualAnyNullableRight(A(0), A(1))) return "Fail 31"
if (isNullVacuousLeft(A(null))) return "Fail 32"
if (isNullVacuousRight(A(null))) return "Fail 33"
if (isNullLeft(A(null))) return "Fail 34"
if (isNullRight(A(null))) return "Fail 35"
if (isEqualAnyLeft(A(null), null)) return "Fail 36"
if (isEqualAnyRight(null, A(null))) return "Fail 37"
if (isEqualAnyNullableLeft(A(null), null)) return "Fail 38"
if (isEqualAnyNullableRight(null, A(null))) return "Fail 39"
if (isEqualSameNullable(A(null), null)) return "Fail 42"
if (isEqualSameNullable(null, A(null))) return "Fail 43"
if (isEqualNullableUnboxedLeft(A(0), A(1))) return "Fail 44"
if (!isEqualNullableUnboxedLeft(A(0), A(0))) return "Fail 45"
if (isEqualNullableUnboxedRight(A(0), A(1))) return "Fail 46"
if (!isEqualNullableUnboxedRight(A(1), A(1))) return "Fail 47"
if (isEqualNullableUnboxedLeft(A(0), null)) return "Fail 48"
if (isEqualNullableUnboxedRight(null, A(1))) return "Fail 49"
if (isEqualNullableUnboxedRight(null, A(null))) return "Fail 50"
if (isEqualNullableUnboxedLeft(A(null), null)) return "Fail 51"
return "OK"
}
@@ -0,0 +1,60 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Int>(val x: T)
fun <T: Int> isNullVacuousLeft(s: A<T>) = s == null
fun <T: Int> isNullVacuousRight(s: A<T>) = null == s
fun <T: Int> isNullLeft(s: A<T>?) = s == null
fun <T: Int> isNullRight(s: A<T>?) = null == s
fun <T: Int> isEqualSame(s: A<T>, t: A<T>) = s == t
fun <T: Int> isEqualAnyLeft(s: A<T>, t: Any?) = s == t
fun <T: Int> isEqualAnyRight(s: Any?, t: A<T>) = s == t
fun <T: Int> isEqualSameNullable(s: A<T>?, t: A<T>?) = s == t
fun <T: Int> isEqualAnyNullableLeft(s: A<T>?, t: Any?) = s == t
fun <T: Int> isEqualAnyNullableRight(s: Any?, t: A<T>?) = s == t
fun <T: Int> isEqualNullableUnboxedLeft(s: A<T>, t: A<T>?) = s == t
fun <T: Int> isEqualNullableUnboxedRight(s: A<T>?, t: A<T>) = s == t
fun box(): String {
if (isNullVacuousLeft(A(0))) return "Fail 1"
if (isNullVacuousRight(A(0))) return "Fail 2"
if (isNullLeft(A(0))) return "Fail 3"
if (isNullRight(A(0))) return "Fail 4"
if (!isNullLeft<Int>(null)) return "Fail 5"
if (!isNullRight<Int>(null)) return "Fail 6"
if (!isEqualSame(A(0), A(0))) return "Fail 7"
if (isEqualSame(A(0), A(1))) return "Fail 8"
if (isEqualAnyLeft(A(0), 0)) return "Fail 9"
if (isEqualAnyLeft(A(0), null)) return "Fail 10"
if (!isEqualAnyLeft(A(0), A(0))) return "Fail 11"
if (isEqualAnyRight(0, A(0))) return "Fail 12"
if (isEqualAnyRight(null, A(0))) return "Fail 13"
if (!isEqualAnyRight(A(0), A(0))) return "Fail 14"
if (!isEqualSameNullable<Int>(null, null)) return "Fail 15"
if (!isEqualSameNullable(A(0), A(0))) return "Fail 16"
if (isEqualSameNullable(null, A(0))) return "Fail 17"
if (isEqualSameNullable(A(0), null)) return "Fail 18"
if (isEqualSameNullable(A(0), A(1))) return "Fail 19"
if (!isEqualAnyNullableLeft<Int>(null, null)) return "Fail 20"
if (!isEqualAnyNullableLeft(A(0), A(0))) return "Fail 21"
if (isEqualAnyNullableLeft(A(0), 0)) return "Fail 22"
if (isEqualAnyNullableLeft<Int>(null, 0)) return "Fail 23"
if (isEqualAnyNullableLeft(A(0), null)) return "Fail 24"
if (isEqualAnyNullableLeft(A(0), A(1))) return "Fail 25"
if (!isEqualAnyNullableRight<Int>(null, null)) return "Fail 26"
if (!isEqualAnyNullableRight(A(0), A(0))) return "Fail 27"
if (isEqualAnyNullableRight(0, A(0))) return "Fail 28"
if (isEqualAnyNullableRight<Int>(0, null)) return "Fail 29"
if (isEqualAnyNullableRight(null, A(0))) return "Fail 30"
if (isEqualAnyNullableRight(A(0), A(1))) return "Fail 31"
if (isEqualNullableUnboxedLeft(A(0), A(1))) return "Fail 32"
if (!isEqualNullableUnboxedLeft(A(0), A(0))) return "Fail 33"
if (isEqualNullableUnboxedRight(A(0), A(1))) return "Fail 34"
if (!isEqualNullableUnboxedRight(A(1), A(1))) return "Fail 35"
if (isEqualNullableUnboxedLeft(A(0), null)) return "Fail 36"
if (isEqualNullableUnboxedRight(null, A(1))) return "Fail 37"
return "OK"
}
@@ -0,0 +1,53 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class X<T: String>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class Y<T: Number>(val y: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class NX<T: String?>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class NY<T: Number?>(val y: T)
fun testNotNull(x: X<String>?, y: Y<Number>?) {
val xs = listOf<Any?>(x)
val ys = listOf<Any?>(y)
if (!xs.contains(y)) throw AssertionError()
if (xs[0] != ys[0]) throw AssertionError()
if (xs[0] !== ys[0]) throw AssertionError()
}
fun testNullable(x: NX<String?>?, y: NY<Number?>?) {
val xs = listOf<Any?>(x)
val ys = listOf<Any?>(y)
if (xs.contains(y)) throw AssertionError()
if (xs[0] == ys[0]) throw AssertionError()
if (xs[0] === ys[0]) throw AssertionError()
}
fun testNullsAsNullable(x: NX<String?>?, y: NY<Number?>?) {
val xs = listOf<Any?>(x)
val ys = listOf<Any?>(y)
if (!xs.contains(y)) throw AssertionError()
if (xs[0] != ys[0]) throw AssertionError()
if (xs[0] !== ys[0]) throw AssertionError()
}
fun box(): String {
testNotNull(null, null)
testNullable(NX(null), NY(null))
testNullable(NX(null), null)
testNullable(null, NY(null))
testNullsAsNullable(null, null)
return "OK"
}
@@ -0,0 +1,17 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: String>(val x: T)
class B {
override fun equals(other: Any?) = true
}
fun box(): String {
val x: Any? = B()
val y: A<String> = A("")
if (x != y) return "Fail"
return "OK"
}
@@ -0,0 +1,38 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Inner<T: Int>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Inner<Int>>(val x: T)
var i = 0
fun set1(): A<Inner<Int>> {
i = 1
return A(Inner(0))
}
fun test1(n: Int): A<Inner<Int>> {
if (i != 1)
throw IllegalStateException("Fail $n")
i = 0
return A(Inner(0))
}
fun set1Boxed(): Any? = set1()
fun test1Boxed(n: Int): Any? = test1(n)
fun box(): String {
try {
set1() == test1(1)
set1Boxed() == test1(2)
set1() == test1Boxed(3)
set1Boxed() == test1Boxed(4)
} catch (e: IllegalStateException) {
return e.message ?: "Fail no message"
}
return "OK"
}
@@ -0,0 +1,35 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: String>(val x: T = "" as T)
var i = 0
fun set1(): A<String> {
i = 1
return A()
}
fun test1(n: Int): A<String> {
if (i != 1)
throw IllegalStateException("Fail $n")
i = 0
return A()
}
fun set1Boxed(): Any? = set1()
fun test1Boxed(n: Int): Any? = test1(n)
fun box(): String {
try {
set1() == test1(1)
set1Boxed() == test1(2)
set1() == test1Boxed(3)
set1Boxed() == test1Boxed(4)
} catch (e: IllegalStateException) {
return e.message ?: "Fail no message"
}
return "OK"
}
@@ -0,0 +1,35 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T>(val x: T? = null)
var i = 0
fun set1(): A<Any?> {
i = 1
return A()
}
fun test1(n: Int): A<Any?> {
if (i != 1)
throw IllegalStateException("Fail $n")
i = 0
return A()
}
fun set1Boxed(): Any? = set1()
fun test1Boxed(n: Int): Any? = test1(n)
fun box(): String {
try {
set1() == test1(1)
set1Boxed() == test1(2)
set1() == test1Boxed(3)
set1Boxed() == test1Boxed(4)
} catch (e: IllegalStateException) {
return e.message ?: "Fail no message"
}
return "OK"
}
@@ -0,0 +1,35 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: Int>(val x: T = 0 as T)
var i = 0
fun set1(): A<Int> {
i = 1
return A()
}
fun test1(n: Int): A<Int> {
if (i != 1)
throw IllegalStateException("Fail $n")
i = 0
return A()
}
fun set1Boxed(): Any? = set1()
fun test1Boxed(n: Int): Any? = test1(n)
fun box(): String {
try {
set1() == test1(1)
set1Boxed() == test1(2)
set1() == test1Boxed(3)
set1Boxed() == test1Boxed(4)
} catch (e: IllegalStateException) {
return e.message ?: "Fail no message"
}
return "OK"
}
@@ -0,0 +1,37 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcAny<T>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcInt<T: Int>(val x: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class IcLong<T: Long>(val x: T)
fun <T> id(x: T) = x
fun box(): String {
if (IcInt(42) == id(IcInt(24))) return "Error 1"
if (IcInt(42) != id(IcInt(42))) return "Error 2"
if (id(IcInt(42)) == IcInt(24)) return "Error 3"
if (id(IcInt(42)) != IcInt(42)) return "Error 4"
if (id(IcInt(42)) == id(IcInt(24))) return "Error 5"
if (id(IcInt(42)) != id(IcInt(42))) return "Error 6"
if (IcLong(42) == id(IcLong(24))) return "Error 2, 1"
if (IcLong(42) != id(IcLong(42))) return "Error 2, 2"
if (id(IcLong(42)) == IcLong(24)) return "Error 2, 3"
if (id(IcLong(42)) != IcLong(42)) return "Error 2, 4"
if (id(IcLong(42)) == id(IcLong(24))) return "Error 2, 5"
if (id(IcLong(42)) != id(IcLong(42))) return "Error 2, 6"
if (IcAny(42) == id(IcAny(24))) return "Error 3, 1"
if (IcAny(42) != id(IcAny(42))) return "Error 3, 2"
if (id(IcAny(42)) == IcAny(24)) return "Error 3, 3"
if (id(IcAny(42)) != IcAny(42)) return "Error 3, 4"
if (id(IcAny(42)) == id(IcAny(24))) return "Error 3, 5"
if (id(IcAny(42)) != id(IcAny(42))) return "Error 3, 6"
return "OK"
}
@@ -0,0 +1,12 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
fun <T> T.runExt(fn: T.() -> String) = fn()
OPTIONAL_JVM_INLINE_ANNOTATION
value class R<T: String>(private val r: T) {
fun test() = runExt { r }
}
fun box() = R("OK").test()
@@ -0,0 +1,12 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
fun <T> T.runExt(fn: T.() -> String) = fn()
OPTIONAL_JVM_INLINE_ANNOTATION
value class R<T: Int>(private val r: T) {
fun test() = runExt { "OK" }
}
fun box() = R(0).test()
@@ -0,0 +1,14 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Z<T: String>(val s: T) {
val Int.s: Int get() = 42
}
fun box(): String {
if (Z("a").toString() == "Z(s=a)")
return "OK"
return "Fail"
}
@@ -0,0 +1,22 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Result<T>(val isSuccess: T)
fun interface ResultHandler<T> {
fun onResult(result: Result<T>)
}
fun doSmth(resultHandler: ResultHandler<Boolean>) {
resultHandler.onResult(Result(true))
}
fun box(): String {
var res = "FAIL"
doSmth { result ->
res = if (result.isSuccess) "OK" else "FAIL 1"
}
return res
}
@@ -0,0 +1,20 @@
// IGNORE_BACKEND: JVM
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class A<T: String>(val value: T)
fun interface B {
fun f(x: A<String>): A<String>
}
inline fun g(unit: Unit = Unit, b: B): A<String> {
return b.f(A("Fail"))
}
fun box(): String {
val b = { _ : A<*> -> A("O") }
return g(b = b).value + g { A("K") }.value
}
@@ -0,0 +1,20 @@
// WITH_STDLIB
// IGNORE_BACKEND: JVM
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Result<T>(val isSuccess: T)
fun interface ResultHandler<T> {
fun onResult(): Result<T>
}
fun doSmth(resultHandler: ResultHandler<Boolean>): Result<Boolean> {
return resultHandler.onResult()
}
fun box(): String {
var res = doSmth { Result(true) }
return if (res.isSuccess) "OK" else "FAIL"
}
@@ -0,0 +1,11 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
inline fun <T: Int> new(init: (Z<T>) -> Unit): Z<T> = Z(42) as Z<T>
OPTIONAL_JVM_INLINE_ANNOTATION
value class Z<T: Int>(val value: T)
fun box(): String =
if (new(fun(z: Z<Int>) {}).value == 42) "OK" else "Fail"
@@ -0,0 +1,15 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
fun foo(s: S<String>): String {
val anon = object {
fun bar() = s.string
}
return anon.bar()
}
fun box() = foo(S("OK"))
@@ -0,0 +1,59 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Id<T: String>(val id: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class Name<T: String>(val name: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class Password<T: String>(val password: T)
fun Id<String>.test() {
if (id != "OK") throw AssertionError()
}
fun Id<String>?.test() {
if (this != null) throw AssertionError()
}
fun Name<String>.test() {
if (name != "OK") throw AssertionError()
}
fun test(password: Password<String>) {
if (password.password != "OK") throw AssertionError()
}
class Outer {
fun Id<String>.testExn() {
if (id != "OK") throw AssertionError()
}
fun Name<String>.testExn() {
if (name != "OK") throw AssertionError()
}
fun testExn(password: Password<String>) {
if (password.password != "OK") throw AssertionError()
}
fun testExns() {
Id("OK").testExn()
Name("OK").testExn()
testExn(Password("OK"))
}
}
fun box(): String {
Id("OK").test()
null.test()
Name("OK").test()
test(Password("OK"))
Outer().testExns()
return "OK"
}
@@ -0,0 +1,21 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Id<T: String>(val id: T)
fun test(id: Id<String>) {
if (id.id != "OK") throw AssertionError()
}
fun test(id: Id<String>?) {
if (id != null) throw AssertionError()
}
fun box(): String {
test(Id("OK"))
test(null)
return "OK"
}
@@ -0,0 +1,27 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S1<T: String>(val s1: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class S2<T: String>(val s2: T)
object X1
object X2
fun <T> test(s1: S1<String>, x: T) {
if (s1.s1 != "OK" && x != X1) throw AssertionError()
}
fun <T> test(s2: S2<String>, x: T) {
if (s2.s2 != "OK" && x != X2) throw AssertionError()
}
fun box(): String {
test(S1("OK"), X1)
test(S2("OK"), X2)
return "OK"
}
@@ -0,0 +1,26 @@
// WITH_STDLIB
// TARGET_BACKEND: JVM
// WITH_REFLECT
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.javaMethod
import kotlin.test.*
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineClass1<T: String>(val s: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class InlineClass2<T: Number>(val n: T)
fun <T : InlineClass1<String>, U : InlineClass2<Number>> foo(t: T, u: U) {}
fun box(): String {
val fooRef: (InlineClass1<String>, InlineClass2<Number>) -> Unit = ::foo
val fooMethod = (fooRef as KFunction<*>).javaMethod!!
assertEquals("[T, U]", fooMethod.genericParameterTypes.asList().toString())
return "OK"
}
@@ -0,0 +1,15 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
fun foo(s: S<String>): String {
class Local {
fun bar() = s.string
}
return Local().bar()
}
fun box() = foo(S("OK"))
@@ -0,0 +1,92 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Id<T: String>(val id: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class Name<T: String>(val name: T)
interface IA {
fun fromInterface(id: Id<String>)
fun fromInterface(name: Name<String>)
fun fromBoth(id: Id<String>)
fun fromBoth(name: Name<String>)
fun withDefaultImpl(id: Id<String>) {
if (id.id != "OK") throw AssertionError()
}
fun withDefaultImpl(name: Name<String>) {
if (name.name != "OK") throw AssertionError()
}
}
abstract class Base {
abstract fun fromClass(id: Id<String>)
abstract fun fromClass(name: Name<String>)
abstract fun fromBoth(id: Id<String>)
abstract fun fromBoth(name: Name<String>)
}
class C : Base(), IA {
override fun fromInterface(id: Id<String>) {
if (id.id != "OK") throw AssertionError()
}
override fun fromInterface(name: Name<String>) {
if (name.name != "OK") throw AssertionError()
}
override fun fromClass(id: Id<String>) {
if (id.id != "OK") throw AssertionError()
}
override fun fromClass(name: Name<String>) {
if (name.name != "OK") throw AssertionError()
}
override fun fromBoth(id: Id<String>) {
if (id.id != "OK") throw AssertionError()
}
override fun fromBoth(name: Name<String>) {
if (name.name != "OK") throw AssertionError()
}
}
fun testIA(a: IA) {
a.fromInterface(Id("OK"))
a.fromInterface(Name("OK"))
a.fromBoth(Id("OK"))
a.fromBoth(Name("OK"))
a.withDefaultImpl(Id("OK"))
a.withDefaultImpl(Name("OK"))
}
fun testBase(b: Base) {
b.fromClass(Id("OK"))
b.fromClass(Name("OK"))
b.fromBoth(Id("OK"))
b.fromBoth(Name("OK"))
}
fun testC(c: C) {
c.withDefaultImpl(Id("OK"))
c.withDefaultImpl(Name("OK"))
}
fun box(): String {
testIA(C())
testBase(C())
testC(C())
return "OK"
}
@@ -0,0 +1,32 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Id<T: String>(val id: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class Name<T: String>(val name: T)
OPTIONAL_JVM_INLINE_ANNOTATION
value class Password<T: String>(val password: T)
fun test(id: Id<String>) {
if (id.id != "OK") throw AssertionError()
}
fun test(name: Name<String>) {
if (name.name != "OK") throw AssertionError()
}
fun test(password: Password<String>) {
if (password.password != "OK") throw AssertionError()
}
fun box(): String {
test(Id("OK"))
test(Name("OK"))
test(Password("OK"))
return "OK"
}
@@ -0,0 +1,39 @@
// IGNORE_BACKEND: JS, JS_IR, WASM
// IGNORE_BACKEND: JS_IR_ES6
// FULL_JDK
// WITH_STDLIB
// WASM_MUTE_REASON: IGNORED_IN_JS
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Id<T: String>(val id: T)
fun throws() {
throw RuntimeException()
}
fun test(id: Id<String>) {
throws()
}
fun foo() {
test(Id("id"))
}
fun box(): String {
val stackTrace = try {
foo()
throw AssertionError()
} catch (e: RuntimeException) {
e.stackTrace
}
for (entry in stackTrace) {
if (entry.methodName.startsWith("test")) {
return "OK"
}
}
throw AssertionError(stackTrace.asList().toString())
}
@@ -0,0 +1,21 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Id<T: String>(val id: T)
fun test(id: Id<String>, str: String) {
if (id.id != "OK" && str != "1") throw AssertionError()
}
fun test(str: String, id: Id<String>) {
if (id.id != "OK" && str != "2") throw AssertionError()
}
fun box(): String {
test(Id("OK"), "1")
test("2", Id("OK"))
return "OK"
}
@@ -0,0 +1,26 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
abstract class GenericBase<T> {
abstract fun foo(x: T): T
}
interface IFoo {
fun foo(x: String): String
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Str<T: String>(val str: T)
class Derived : GenericBase<Str<String>>(), IFoo {
override fun foo(x: Str<String>): Str<String> = x
override fun foo(x: String): String = x
}
fun box(): String {
if (Derived().foo(Str("OK")).str != "OK") throw AssertionError()
if (Derived().foo("OK") != "OK") throw AssertionError()
return "OK"
}
@@ -0,0 +1,16 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
abstract class GenericBase<T> {
abstract fun foo(x: T): T
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Str<T: String>(val str: T)
class Derived : GenericBase<Str<String>>() {
override fun foo(x: Str<String>): Str<String> = x
}
fun box() = Derived().foo(Str("OK")).str
@@ -0,0 +1,16 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class Str<T: String>(val string: T)
class C {
var s = Str("")
}
fun box(): String {
val x = C()
x.s = Str("OK")
return x.s.string
}
@@ -0,0 +1,19 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
import kotlin.test.*
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
fun foo(s: S<String>) = s
fun box(): String {
val fooRef = ::foo
assertEquals("abc", fooRef.invoke(S("abc")).string)
assertEquals("foo", fooRef.name)
return "OK"
}
@@ -0,0 +1,32 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
import kotlin.test.*
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
fun test(s: S<String>) {
class Local
val localKClass = Local::class
val localJClass = localKClass.java
val kName = localKClass.simpleName
// See https://youtrack.jetbrains.com/issue/KT-29413
// assertEquals("Local", kName)
if (kName != "Local" && kName != "test\$Local") throw AssertionError("Fail KClass: $kName")
assertTrue { localJClass.isLocalClass }
val jName = localJClass.simpleName
if (jName != "Local" && jName != "test\$Local") throw AssertionError("Fail java.lang.Class: $jName")
}
fun box(): String {
test(S(""))
return "OK"
}
@@ -0,0 +1,23 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
import kotlin.test.*
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
var prop = S("")
fun box(): String {
val propRef = ::prop
assertEquals(S(""), propRef.get())
propRef.set(S("abc"))
assertEquals(S("abc"), propRef.get())
assertEquals("prop", propRef.name)
return "OK"
}
@@ -0,0 +1,16 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
class Outer {
private fun foo(s: S<String>) = s.string
inner class Inner(val string: String) {
fun bar() = foo(S(string))
}
}
fun box(): String = Outer().Inner("OK").bar()
@@ -0,0 +1,20 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
class Outer {
private var pr = S("")
inner class Inner() {
fun updateOuter(string: String): String {
pr = S(string)
return pr.string
}
}
}
fun box(): String =
Outer().Inner().updateOuter("OK")
@@ -0,0 +1,14 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
// FILE: 1.kt
fun box(): String = X(Z("OK")).z.result
// FILE: 2.kt
OPTIONAL_JVM_INLINE_ANNOTATION
value class Z<T: String>(val result: T)
class X(val z: Z<String>)
@@ -0,0 +1,14 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
// FILE: 2.kt
fun box(): String = X(Z("OK")).z.result
// FILE: 1.kt
OPTIONAL_JVM_INLINE_ANNOTATION
value class Z<T: String>(val result: T)
class X(val z: Z<String>)
@@ -0,0 +1,12 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
class Test(val x: S<String>, val y: S<String> = S("K")) {
val test = x.string + y.string
}
fun box() = Test(S("O")).test
@@ -0,0 +1,12 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
abstract class Base(val x: S<String>)
class Test(x: S<String>) : Base(x)
fun box() = Test(S("OK")).x.string
@@ -0,0 +1,14 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
abstract class Base(val x: S<String>)
class Test : Base {
constructor() : super(S("OK"))
}
fun box() = Test().x.string
@@ -0,0 +1,14 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
class Test(val x: S<String>, val y: S<String>) {
constructor(x: S<String>) : this(x, S("K"))
val test = x.string + y.string
}
fun box() = Test(S("O")).test
@@ -0,0 +1,12 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
enum class Test(val s: S<String>) {
OK(S("OK"))
}
fun box() = Test.OK.s.string
@@ -0,0 +1,14 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
class Outer(val s1: S<String>) {
inner class Inner(val s2: S<String>) {
val test = s1.string + s2.string
}
}
fun box() = Outer(S("O")).Inner(S("K")).test
@@ -0,0 +1,10 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
class Test(val s: S<String>)
fun box() = Test(S("OK")).s.string
@@ -0,0 +1,14 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +GenericInlineClassParameter
OPTIONAL_JVM_INLINE_ANNOTATION
value class S<T: String>(val string: T)
class Outer private constructor(val s: S<String>) {
class Nested {
fun test(s: S<String>) = Outer(s)
}
}
fun box() = Outer.Nested().test(S("OK")).s.string

Some files were not shown because too many files have changed in this diff Show More