[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:
committed by
Space Team
parent
eaef7122f6
commit
69a7bf7f68
+11
@@ -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()!!
|
||||
}
|
||||
+9
@@ -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) {}
|
||||
}
|
||||
+9
@@ -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) {}
|
||||
}
|
||||
Vendored
-8
@@ -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<!>
|
||||
}
|
||||
Vendored
+1
-1
@@ -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
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
|
||||
}
|
||||
}
|
||||
Vendored
-34
@@ -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()
|
||||
}
|
||||
}
|
||||
Vendored
+1
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// Issue: KT-37621
|
||||
|
||||
class Inv<T>
|
||||
|
||||
Reference in New Issue
Block a user