[NI] Discard DefNotNull types inside invariant positions
#KT-30297 Fixed #KT-32168 Fixed #KT-27722 Fixed (actually, it was fixed with addition of DefNotNullTypes, and now test was added to save this behavior) #KT-32345 Fixed
This commit is contained in:
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
|
||||
|
||||
class Inv<T>(val x: T?)
|
||||
|
||||
fun <K> create(y: K) = Inv(y)
|
||||
fun <K> createPrivate(y: K) = Inv(y)
|
||||
|
||||
fun takeInvInt(i: Inv<Int>) {}
|
||||
|
||||
fun <S> test(i: Int, s: S) {
|
||||
val a = Inv(s)
|
||||
|
||||
<!DEBUG_INFO_EXPRESSION_TYPE("Inv<S>")!>a<!>
|
||||
|
||||
val b = create(i)
|
||||
|
||||
<!DEBUG_INFO_EXPRESSION_TYPE("Inv<kotlin.Int>")!>b<!>
|
||||
|
||||
val c = createPrivate(i)
|
||||
|
||||
<!DEBUG_INFO_EXPRESSION_TYPE("Inv<kotlin.Int>")!>c<!>
|
||||
|
||||
takeInvInt(create(i))
|
||||
}
|
||||
compiler/testData/diagnostics/tests/inference/constraints/definitelyNotNullTypeInvariantPosition.txt
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ K> create(/*0*/ y: K): Inv<K>
|
||||
public fun </*0*/ K> createPrivate(/*0*/ y: K): Inv<K>
|
||||
public fun takeInvInt(/*0*/ i: Inv<kotlin.Int>): kotlin.Unit
|
||||
public fun </*0*/ S> test(/*0*/ i: kotlin.Int, /*1*/ s: S): kotlin.Unit
|
||||
|
||||
public final class Inv</*0*/ T> {
|
||||
public constructor Inv</*0*/ T>(/*0*/ x: T?)
|
||||
public final val x: T?
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Foo {
|
||||
var test: String by refreshOnUpdate("str")
|
||||
|
||||
fun <T> refreshOnUpdate(initialValue: T) = RefreshDelegate(initialValue)
|
||||
|
||||
class RefreshDelegate<T>(initialValue: T?) {
|
||||
operator fun getValue(thisRef: Foo, property: KProperty<*>): T = TODO()
|
||||
|
||||
operator fun setValue(thisRef: Foo, property: KProperty<*>, value: T) {}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package
|
||||
|
||||
public final class Foo {
|
||||
public constructor Foo()
|
||||
public final var test: kotlin.String
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public final fun </*0*/ T> refreshOnUpdate(/*0*/ initialValue: T): Foo.RefreshDelegate<T>
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public final class RefreshDelegate</*0*/ T> {
|
||||
public constructor RefreshDelegate</*0*/ T>(/*0*/ initialValue: T?)
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final operator fun getValue(/*0*/ thisRef: Foo, /*1*/ property: kotlin.reflect.KProperty<*>): T
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public final operator fun setValue(/*0*/ thisRef: Foo, /*1*/ property: kotlin.reflect.KProperty<*>, /*2*/ value: T): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// !WITH_NEW_INFERENCE
|
||||
|
||||
fun <T> foo(resources: List<T>) {
|
||||
resources.map { runCatching { it } }.<!OI;TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>mapNotNull<!> { it.getOrNull() }
|
||||
}
|
||||
|
||||
fun <T: Any> bar(resources: List<T>) {
|
||||
resources.map { runCatching { it } }.mapNotNull { it.getOrNull() }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T : kotlin.Any> bar(/*0*/ resources: kotlin.collections.List<T>): kotlin.Unit
|
||||
public fun </*0*/ T> foo(/*0*/ resources: kotlin.collections.List<T>): kotlin.Unit
|
||||
@@ -0,0 +1,49 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
|
||||
class CleanupTestExample {
|
||||
val cleanUpBlocks: MutableList<Pair<Any, (Any) -> Unit>> = mutableListOf()
|
||||
|
||||
class CleaningDelegate<T : Any?>(
|
||||
initialValue: T? = null,
|
||||
val cleanupBlocks: MutableList<Pair<Any, (Any) -> Unit>>,
|
||||
val block: (T) -> Unit
|
||||
) : ReadWriteProperty<Any?, T> {
|
||||
private var value: T? = initialValue
|
||||
|
||||
init {
|
||||
addCleanupBlock(initialValue)
|
||||
}
|
||||
|
||||
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
|
||||
return value ?: throw IllegalStateException("Property ${property.name} should be initialized before get.")
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
||||
addCleanupBlock(value)
|
||||
this.value = value
|
||||
}
|
||||
|
||||
fun addCleanupBlock(value: T?) {
|
||||
if (value != null) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
cleanupBlocks.add((<!DEBUG_INFO_SMARTCAST!>value<!> to block) as Pair<Any, (Any) -> Unit>)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
data class TestHolder(val num: Int)
|
||||
|
||||
fun <T : Any?> cleanup(initialValue: T? = null, block: (T) -> Unit) = CleaningDelegate(initialValue, cleanUpBlocks, block)
|
||||
|
||||
fun testWithCleanup() {
|
||||
val testHolder = TestHolder(1)
|
||||
|
||||
var thing: TestHolder by CleaningDelegate(testHolder, cleanupBlocks = cleanUpBlocks, block = { println("cleaning up $it") })
|
||||
var thing2: TestHolder by cleanup(testHolder) { println("cleaning up $it") }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package
|
||||
|
||||
public final class CleanupTestExample {
|
||||
public constructor CleanupTestExample()
|
||||
public final val cleanUpBlocks: kotlin.collections.MutableList<kotlin.Pair<kotlin.Any, (kotlin.Any) -> kotlin.Unit>>
|
||||
public final fun </*0*/ T> cleanup(/*0*/ initialValue: T? = ..., /*1*/ block: (T) -> kotlin.Unit): CleanupTestExample.CleaningDelegate<T>
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public final fun testWithCleanup(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public final class CleaningDelegate</*0*/ T> : kotlin.properties.ReadWriteProperty<kotlin.Any?, T> {
|
||||
public constructor CleaningDelegate</*0*/ T>(/*0*/ initialValue: T? = ..., /*1*/ cleanupBlocks: kotlin.collections.MutableList<kotlin.Pair<kotlin.Any, (kotlin.Any) -> kotlin.Unit>>, /*2*/ block: (T) -> kotlin.Unit)
|
||||
public final val block: (T) -> kotlin.Unit
|
||||
public final val cleanupBlocks: kotlin.collections.MutableList<kotlin.Pair<kotlin.Any, (kotlin.Any) -> kotlin.Unit>>
|
||||
private final var value: T?
|
||||
public final fun addCleanupBlock(/*0*/ value: T?): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>): T
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
@kotlin.Suppress(names = {"UNCHECKED_CAST"}) public open override /*1*/ fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>, /*2*/ value: T): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final data class TestHolder {
|
||||
public constructor TestHolder(/*0*/ num: kotlin.Int)
|
||||
public final val num: kotlin.Int
|
||||
public final operator /*synthesized*/ fun component1(): kotlin.Int
|
||||
public final /*synthesized*/ fun copy(/*0*/ num: kotlin.Int = ...): CleanupTestExample.TestHolder
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user