[FIR] Add equality constraint from expected type for some synthetic function calls

This fixes some cases where we infer some type variable inside one
of the branches to Nothing instead of the expected type because Nothing
appeared in some other branch.

Specifically, we add an equality instead of a subtype constraint during
completion of calls to synthetic functions for if/when, try and !!.
We don't do it when the call contains a (possibly nested) elvis or is
inside the RHS of an assignment.
Otherwise, we would prevent some smart-casts.

#KT-65882 Fixed
This commit is contained in:
Kirill Rakhman
2024-02-21 16:06:47 +01:00
committed by Space Team
parent eaef7122f6
commit 69a7bf7f68
106 changed files with 1441 additions and 578 deletions
@@ -4,8 +4,8 @@ fun test(s: IntRange?) {
val a: Int = <!INITIALIZER_TYPE_MISMATCH!>s?.start<!>
val b: Int? = s?.start
val c: Int = s?.start ?: -11
val d: Int = <!TYPE_MISMATCH!>s?.start ?: "empty"<!>
val e: String = <!TYPE_MISMATCH!>s?.start ?: "empty"<!>
val d: Int = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>s?.start ?: "empty"<!>
val e: String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>s?.start ?: "empty"<!>
val f: Int = s?.endInclusive ?: b ?: 1
val g: Boolean? = e.startsWith("s")//?.length
}
@@ -6,7 +6,7 @@ fun dump(dumpStrategy: String) {
val k0: kotlin.reflect.KFunction0<String> = returnAdapter(<!ADAPTED_CALLABLE_REFERENCE_AGAINST_REFLECTION_TYPE!>::foo<!>) // Error: ADAPTED_CALLABLE_REFERENCE_AGAINST_REFLECTION_TYPE
val k1: kotlin.reflect.KFunction0<String> = <!INITIALIZER_TYPE_MISMATCH!>::foo<!>
// Should be error here, too
val k2: kotlin.reflect.KFunction0<String> = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>if (dumpStrategy == "KotlinLike") ::foo else ::bar<!>
val k2: kotlin.reflect.KFunction0<String> = <!INITIALIZER_TYPE_MISMATCH!>if (dumpStrategy == "KotlinLike") ::foo else ::bar<!>
val f0: Function0<String> = returnAdapter(<!ADAPTED_CALLABLE_REFERENCE_AGAINST_REFLECTION_TYPE!>::foo<!>)
val f1: Function0<String> = ::foo
@@ -29,11 +29,11 @@ val testSafeCall4: String? = J.m[""]?.let { it.toString() }
val testIf1: String = if (true) J.s else J.s
val testIf2: String? = if (true) J.s else J.s
val testIf3: String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>if (true) <!TYPE_MISMATCH!>J.m[""]<!> else <!TYPE_MISMATCH!>J.m[""]<!><!>
val testIf3: String = <!INITIALIZER_TYPE_MISMATCH!>if (true) <!TYPE_MISMATCH!>J.m[""]<!> else <!TYPE_MISMATCH!>J.m[""]<!><!>
val testIf4: String? = if (true) J.m[""] else J.m[""]
val testWhen1: String = when { else -> J.s }
val testWhen2: String? = when { else -> J.s }
val testWhen3: String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>when { else -> <!TYPE_MISMATCH!>J.m[""]<!> }<!>
val testWhen3: String = <!INITIALIZER_TYPE_MISMATCH!>when { else -> <!TYPE_MISMATCH!>J.m[""]<!> }<!>
val testWhen4: String? = when { else -> J.m[""] }
@@ -0,0 +1,11 @@
// FIR_IDENTICAL
// DIAGNOSTICS: -UNCHECKED_CAST
interface I
interface I2 : I
fun <T : I> I.cast(): T? = this as? T
fun foo(i: I): I2? {
return i.cast()!!
}
@@ -0,0 +1,9 @@
fun foo() {
var f: Int = <!INITIALIZER_TYPE_MISMATCH!>if (true) <!ARGUMENT_TYPE_MISMATCH!>{ x: Long -> }<!> else { x: Long -> }<!>
}
class A {
var x: Int
get(): Int = <!RETURN_TYPE_MISMATCH!>if (true) { <!ARGUMENT_TYPE_MISMATCH!>{42}<!> } else { <!ARGUMENT_TYPE_MISMATCH!>{24}<!> }<!>
set(i: Int) {}
}
@@ -0,0 +1,9 @@
fun foo() {
var f: Int = if (true) <!TYPE_MISMATCH!>{ x: Long -> }<!> else <!TYPE_MISMATCH!>{ x: Long -> }<!>
}
class A {
var x: Int
get(): Int = if (true) <!TYPE_MISMATCH!>{ {42} }<!> else <!TYPE_MISMATCH!>{ {24} }<!>
set(i: Int) {}
}
@@ -1,8 +0,0 @@
// ISSUE: KT-57649
open class A
abstract class B {
fun test(current: A): A? =
// K2 reports empty intersection here due to the smart cast from A to B, where A & B aren't compatible
<!INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION_WARNING("K; B?, A?; multiple incompatible classes; : B, A")!>if (current === this) current else null<!>
}
@@ -1,8 +1,8 @@
// FIR_IDENTICAL
// ISSUE: KT-57649
open class A
abstract class B {
fun test(current: A): A? =
// K2 reports empty intersection here due to the smart cast from A to B, where A & B aren't compatible
if (current === this) current else null
}
@@ -10,7 +10,7 @@ fun main() {
val x41: String.(String) -> String = if (true) {{ str: String, str2: String -> "this" }} else {{ str: String, str2: String -> "this" }}
val x42: String.(String) -> String = if (true) {{ str, <!CANNOT_INFER_PARAMETER_TYPE!>str2<!> -> "this" }} else {{ str, <!CANNOT_INFER_PARAMETER_TYPE!>str2<!> -> "this" }}
val x5: String.() -> String = if (true) {<!ARGUMENT_TYPE_MISMATCH!>{ <!CANNOT_INFER_PARAMETER_TYPE!>str<!> -> "this" }<!>} else {<!ARGUMENT_TYPE_MISMATCH!>{ <!CANNOT_INFER_PARAMETER_TYPE!>str<!> -> "this" }<!>}
val x6: String.() -> String = if (true) {<!ARGUMENT_TYPE_MISMATCH!>{ <!CANNOT_INFER_PARAMETER_TYPE!>str<!> -> "this" }<!>} else {{ "this" }}
val x6: String.() -> String = <!INITIALIZER_TYPE_MISMATCH!>if (true) {<!ARGUMENT_TYPE_MISMATCH!>{ <!CANNOT_INFER_PARAMETER_TYPE!>str<!> -> "this" }<!>} else {{ "this" }}<!>
val x7: String.() -> String = select({ -> this }, { -> this })
val x8: String.() -> String = select({ this }, { this })
}
@@ -1,62 +0,0 @@
// SKIP_TXT
interface Additional
interface A<T> : Additional
fun <U> aOf(): A<U> = TODO()
interface B<E>
fun <F> B<F>.convert(): A<F> = TODO()
fun foo1(x: B<String>): Any {
return if (x.hashCode() == 0) aOf() else x.convert()
}
fun foo2(x: B<String>): Additional {
return if (x.hashCode() == 0) aOf() else x.convert()
}
fun foo3(x: B<String>): Any {
return when {
x.hashCode() == 0 -> aOf()
else -> x.convert()
}
}
fun foo4(x: B<String>): Additional {
return when {
x.hashCode() == 0 -> aOf()
else -> x.convert()
}
}
fun foo5(x: B<String>): Any {
return if (x.hashCode() == 0) {
aOf()
} else {
x.convert()
}
}
fun foo6(x: B<String>): Additional {
return if (x.hashCode() == 0) {
aOf()
} else {
x.convert()
}
}
fun foo7(x: B<String>): Any {
return when {
x.hashCode() == 0 -> { aOf() }
else -> { x.convert() }
}
}
fun foo8(x: B<String>): Additional {
return when {
x.hashCode() == 0 -> { aOf() }
else -> { x.convert() }
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// SKIP_TXT
interface Additional
@@ -0,0 +1,24 @@
// FIR_IDENTICAL
interface Promise<T>
fun <T> materializePromise(): Promise<T> = TODO()
fun foo(x: Int, p1: Promise<Any?>, p2: Promise<Nothing?>): Promise<*> {
return if (x == 0) {
p1
} else {
if (x == 3) {
materializePromise()
} else {
p2
}
}
}
fun foo2(x: Int, p2: Promise<Nothing?>): Promise<*> {
return if (x == 3) {
<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>materializePromise<!>()
} else {
p2
}
}
@@ -1,34 +0,0 @@
// Issue: KT-37621
class Inv<T>
class In<in I>
class Out<out O>
inline fun <reified TB : Inv<TB>> invBound(): TB = TODO()
inline fun <reified IB : In<IB>> inBound(): IB = TODO()
inline fun <reified OB : Out<OB>> outBound(): OB = TODO()
inline fun <reified T : Inv<T>> testInv(): T {
return try {
invBound()
} catch (ex: Exception) {
throw Exception()
}
}
inline fun <reified T : In<T>> testIn(): T {
return try {
inBound()
} catch (ex: Exception) {
throw Exception()
}
}
// Unexpected behaviour
inline fun <reified T : Out<T>> testOut(): T {
return try {
<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>outBound<!>()
} catch (ex: Exception) {
throw Exception()
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// Issue: KT-37621
class Inv<T>
@@ -33,6 +33,6 @@ fun main() {
}
}
val f : String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>a<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!><!>
val f : String = <!INITIALIZER_TYPE_MISMATCH!>a<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!><!>
checkSubtype<String>(<!ARGUMENT_TYPE_MISMATCH!>a<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!><!>)
}
@@ -4,7 +4,7 @@ fun main() {
val a : Int? = null;
var v = 1
val b : String = <!INITIALIZER_TYPE_MISMATCH!>v<!>;
val f : String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>a!!<!>;
val f : String = <!INITIALIZER_TYPE_MISMATCH!>a!!<!>;
val g : String = <!INITIALIZER_TYPE_MISMATCH!>v++<!>;
val g1 : String = <!INITIALIZER_TYPE_MISMATCH!>++v<!>;
val h : String = <!INITIALIZER_TYPE_MISMATCH!>v--<!>;
+2 -2
View File
@@ -1,4 +1,4 @@
val test: Int = if (true) {
val test: Int = <!INITIALIZER_TYPE_MISMATCH!>if (true) {
<!TYPE_MISMATCH!>when (2) {
1 -> 1
else -> null
@@ -6,4 +6,4 @@ val test: Int = if (true) {
}
else {
2
}
}<!>
+3 -3
View File
@@ -10,7 +10,7 @@
*/
fun test1(): Int {
val x: String = <!TYPE_MISMATCH!>if (true) {
val x: String = <!INITIALIZER_TYPE_MISMATCH!>if (true) {
<!TYPE_MISMATCH, TYPE_MISMATCH!>when {
true -> <!TYPE_MISMATCH!>Any()<!>
else -> null
@@ -20,9 +20,9 @@ fun test1(): Int {
}
fun test2(): Int {
val x: String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH, TYPE_MISMATCH!><!TYPE_MISMATCH!>when {
val x: String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH, TYPE_MISMATCH!>when {
true -> <!TYPE_MISMATCH!>Any()<!>
else -> null
}<!> ?: return 0<!>
} ?: return 0<!>
return x.hashCode()
}
@@ -28,7 +28,7 @@ val test3: (String) -> Boolean =
}
val test4: (String) -> Boolean =
<!INITIALIZER_TYPE_MISMATCH!>when {
when {
true -> <!ARGUMENT_TYPE_MISMATCH!>{ s1, <!CANNOT_INFER_PARAMETER_TYPE!>s2<!> -> true }<!>
else -> null!!
}<!>
}
@@ -5,7 +5,7 @@ class ExcA : Exception()
class ExcB : Exception()
fun test2() {
val s: String? = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>try {
val s: String? = <!INITIALIZER_TYPE_MISMATCH!>try {
""
}
catch (e: ExcA) {