[FIR, Tests] Add box tests to verify that disappeared DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE leads to executable code

#KT-59903


Merge-request: KT-MR-13423
Merged-by: Evgeniy Zhelenskiy <Evgeniy.Zhelenskiy@jetbrains.com>
This commit is contained in:
Evgeniy.Zhelenskiy
2023-12-08 13:18:59 +00:00
committed by Space Team
parent aff5b91da3
commit 9cef8a2133
25 changed files with 774 additions and 0 deletions
@@ -0,0 +1,20 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// IGNORE_BACKEND_K1: ANY
// WITH_STDLIB
object CommonCase {
interface Fas<D, E, R>
fun <D, E, R> delegate() : Fas<D, E, R> = object : Fas<D, E, R> {}
operator fun <D, E, R> Fas<D, E, R>.provideDelegate(host: D, p: Any?): Fas<D, E, R> = this
operator fun <D, E, R> Fas<D, E, R>.getValue(receiver: E, p: Any?): R = "OK" as R
val Long.test1: String by delegate()
val Long.test2: String by delegate<CommonCase, Long, String>()
}
fun box() = with(CommonCase) {
require(3L.test1 == "OK" && 3L.test2 == "OK")
"OK"
}
@@ -0,0 +1,19 @@
// IGNORE_BACKEND_K1: ANY
// WITH_STDLIB
object Inference2 {
interface Foo<T>
fun <T> delegate(): Foo<T> = object : Foo<T> {}
operator fun <T> Foo<T>.provideDelegate(host: T, p: Any?): Foo<T> = this
operator fun <T> Foo<T>.getValue(receiver: Inference2, p: Any?): String = "OK"
val test1: String by delegate() // same story like in Inference1
val test2: String by delegate<Inference2>()
}
fun box(): String {
require(Inference2.test1 == "OK" && Inference2.test2 == "OK")
return "OK"
}
@@ -0,0 +1,42 @@
FILE: notNullAssertionInLocalDelegated.kt
public final data class Ref<D> : R|kotlin/Any| {
public constructor<D>(t: R|D|): R|Ref<D>| {
super<R|kotlin/Any|>()
}
public final val t: R|D| = R|<local>/t|
public get(): R|D|
public final operator fun component1(): R|D|
public final fun copy(t: R|D| = this@R|/Ref|.R|/Ref.t|): R|Ref<D>|
}
public final class GenericDelegate<G> : R|kotlin/Any| {
public constructor<G>(value: R|G|): R|GenericDelegate<G>| {
super<R|kotlin/Any|>()
}
public final val value: R|G| = R|<local>/value|
public get(): R|G|
}
public final operator fun <V> R|Ref<V>|.provideDelegate(a: R|kotlin/Any?|, p: R|kotlin/reflect/KProperty<*>|): R|GenericDelegate<V>| {
^provideDelegate R|/GenericDelegate.GenericDelegate|<R|V|>(this@R|/provideDelegate|.R|SubstitutionOverride</Ref.t: R|V|>|)
}
public final operator fun <W> R|GenericDelegate<W>|.getValue(a: R|kotlin/Any?|, p: R|kotlin/reflect/KProperty<*>|): R|W| {
^getValue this@R|/getValue|.R|SubstitutionOverride</GenericDelegate.value: R|W|>|
}
public final fun <E> R|kotlin/collections/List<Ref<*>>|.getElement(i: R|kotlin/Int|): R|Ref<E>| {
^getElement (this@R|/getElement|.R|SubstitutionOverride<kotlin/collections/List.get: R|Ref<*>|>|(R|<local>/i|) as R|Ref<E>|)
}
@R|kotlin/Suppress|(names = vararg(String(UNNECESSARY_NOT_NULL_ASSERTION))) public final fun test(list: R|kotlin/collections/List<Ref<*>>|): R|kotlin/Unit| {
lval data: R|kotlin/String|by R|<local>/list|.R|/getElement|<R|kotlin/String|>(Int(0))!!.R|/provideDelegate|<R|kotlin/String|>(Null(null), ::R|<local>/data|)
R|kotlin/require|(==(R|<local>/data|, R|<local>/list|.R|SubstitutionOverride<kotlin/collections/List.get: R|Ref<*>|>|(Int(0)).R|SubstitutionOverride</Ref.t: R|CapturedType(*)|>|))
lval data2: R|kotlin/String|by R|<local>/list|.R|/getElement|<R|kotlin/String|>(Int(0)).R|/provideDelegate|<R|kotlin/String|>(Null(null), ::R|<local>/data2|)
R|kotlin/require|(==(R|<local>/data2|, R|<local>/list|.R|SubstitutionOverride<kotlin/collections/List.get: R|Ref<*>|>|(Int(0)).R|SubstitutionOverride</Ref.t: R|CapturedType(*)|>|))
}
public final fun box(): R|kotlin/String| {
R|/test|(R|kotlin/collections/listOf|<R|Ref<kotlin/String>|>(R|/Ref.Ref|<R|kotlin/String|>(String(q))))
^box String(OK)
}
@@ -0,0 +1,33 @@
// ISSUE: KT-58013
// WITH_STDLIB
// WITH_REFLECT
// FIR_DUMP
// IGNORE_BACKEND_K1: ANY
import kotlin.reflect.KProperty
data class Ref<D>(val t: D)
class GenericDelegate<G>(val value: G)
operator fun <V> Ref<V>.provideDelegate(a: Any?, p: KProperty<*>): GenericDelegate<V> = GenericDelegate(this.t)
operator fun <W> GenericDelegate<W>.getValue(a: Any?, p: KProperty<*>): W = this.value
fun <E> List<Ref<*>>.getElement(i: Int): Ref<E> = this[i] as Ref<E>
@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
fun test(list: List<Ref<*>>) {
val data: String by list.getElement(0)!!
require(data == list[0].t)
val data2: String by list.getElement(0)
require(data2 == list[0].t)
}
fun box(): String {
test(listOf(Ref("q")))
return "OK"
}
@@ -0,0 +1,40 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// ISSUE: KT-61077
// IGNORE_BACKEND_K1: ANY
// WITH_STDLIB
data class Delegate<T>(val data: T)
var counter = 0
operator fun Delegate<*>.getValue(receiver: Any?, p: Any): String = this.data.toString()
operator fun <T> Delegate<T>.setValue(receiver: Any?, p: Any, value: T) {
require(value == "OK")
counter++
}
operator fun <T> String.provideDelegate(receiver: Any?, p: Any) = Delegate<T>(this as T)
var test1: String by Delegate("OK1")
var test2: String by Delegate<String>("OK2")
var test3: String by "OK3"
var test4: String by "OK4".provideDelegate(null, "")
var test5: String by "OK5".provideDelegate<String>(null, "")
fun box(): String {
require(test1 == "OK1")
test1 = "OK"
require(test2 == "OK2")
test2 = "OK"
require(test3 == "OK3")
test3 = "OK"
require(test4 == "OK4")
test4 = "OK"
require(test5 == "OK5")
test5 = "OK"
require(counter == 5) { "Counter counter is $counter" }
return "OK"
}
@@ -0,0 +1,55 @@
// ISSUE: KT-58754
// IGNORE_BACKEND_K1: ANY
// WITH_STDLIB
fun foo(): Int = 1
fun bar(): Int = 2
class Test(b: Boolean) {
val test_1 by lazy {
val a = if (b) {
::foo
} else {
::bar
}
a
}
val test_2 by lazy {
val a = if (b) ::foo else ::bar
a
}
val test_3 by lazy {
val a = when {
b -> { ::foo }
else -> { ::bar }
}
a
}
val test_4 by lazy {
val a = when {
b -> ::foo
else -> ::bar
}
a
}
}
fun box(): String {
with(Test(b = false)) {
require(test_1() == bar())
require(test_2() == bar())
require(test_3() == bar())
require(test_4() == bar())
}
with(Test(b = true)) {
require(test_1() == foo())
require(test_2() == foo())
require(test_3() == foo())
require(test_4() == foo())
}
return "OK"
}