Fix type inference issues for 'if' and 'when'.
Use 'expectedType' (when present) as an explicit type argument for a special construct call. Unfortunately, this approach can't be used for elvis due to other elvis-related inference hacks. Fixes KT-10807, KT-10811. This also affects KT-6189: now we can infer proper type for 'if'. If type inference for special call failed, and we found no type errors in sub-expressions, report TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT error. This (and the hack above) fixes KT-10809: code no longer compiles.
This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
// FILE: J.java
|
||||
import java.util.*;
|
||||
|
||||
public class J {
|
||||
public static String s = null;
|
||||
public static Map<String, String> m = null;
|
||||
}
|
||||
|
||||
// FILE: k.kt
|
||||
|
||||
val testImplicitExclExcl1: String = J.s
|
||||
val testImplicitExclExcl2: String? = J.s
|
||||
|
||||
val testImplicitExclExcl3: String = <!TYPE_MISMATCH!>J.m[""]<!>
|
||||
val testImplicitExclExcl4: String? = J.m[""]
|
||||
|
||||
val testExclExcl1: String = J.s!!
|
||||
val testExclExcl2: String? = J.s!!
|
||||
|
||||
val testExclExcl3: String = J.m[""]!!
|
||||
val testExclExcl4: String? = J.m[""]!!
|
||||
|
||||
val testSafeCall1: String = <!TYPE_MISMATCH!>J.s?.let { it }<!>
|
||||
val testSafeCall2: String? = J.s?.let { it }
|
||||
|
||||
val testSafeCall3: String = <!TYPE_MISMATCH!>J.m[""]?.let { it }<!>
|
||||
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 = 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 = when { else -> <!TYPE_MISMATCH!>J.m[""]<!> }
|
||||
val testWhen4: String? = when { else -> J.m[""] }
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
package
|
||||
|
||||
public val testExclExcl1: kotlin.String
|
||||
public val testExclExcl2: kotlin.String?
|
||||
public val testExclExcl3: kotlin.String
|
||||
public val testExclExcl4: kotlin.String?
|
||||
public val testIf1: kotlin.String
|
||||
public val testIf2: kotlin.String?
|
||||
public val testIf3: kotlin.String
|
||||
public val testIf4: kotlin.String?
|
||||
public val testImplicitExclExcl1: kotlin.String
|
||||
public val testImplicitExclExcl2: kotlin.String?
|
||||
public val testImplicitExclExcl3: kotlin.String
|
||||
public val testImplicitExclExcl4: kotlin.String?
|
||||
public val testSafeCall1: kotlin.String
|
||||
public val testSafeCall2: kotlin.String?
|
||||
public val testSafeCall3: kotlin.String
|
||||
public val testSafeCall4: kotlin.String?
|
||||
public val testWhen1: kotlin.String
|
||||
public val testWhen2: kotlin.String?
|
||||
public val testWhen3: kotlin.String
|
||||
public val testWhen4: kotlin.String?
|
||||
|
||||
public open class J {
|
||||
public constructor J()
|
||||
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
|
||||
|
||||
// Static members
|
||||
public final var m: kotlin.collections.(Mutable)Map<kotlin.String!, kotlin.String!>!
|
||||
public final var s: kotlin.String!
|
||||
}
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
val ns: String? = null
|
||||
|
||||
val testElvis1: String? = ns ?: ""
|
||||
val testElvis2: String = run { ns ?: "" }
|
||||
val testElvis3: String? = run { ns ?: "" }
|
||||
|
||||
val testIf1: String? = if (true) "" else ""
|
||||
val testIf2: String? = run { if (true) "" else "" }
|
||||
val testIf3: String? = if (true) run { "" } else ""
|
||||
val testIf4: String? = run { run { if (true) "" else "" } }
|
||||
val testIf5: String? = run { if (true) run { "" } else "" }
|
||||
|
||||
val testWhen1: String? = when { else -> "" }
|
||||
val testWhen2: String? = run { when { else -> "" } }
|
||||
val testWhen3: String? = when { else -> run { "" } }
|
||||
val testWhen4: String? = run { run { when { else -> "" } } }
|
||||
val testWhen5: String? = run { when { else -> run { "" } } }
|
||||
|
||||
val testExcl1: String? = run { ns!! }
|
||||
val testExcl2: String? = run { run { ns!! } }
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
package
|
||||
|
||||
public val ns: kotlin.String? = null
|
||||
public val testElvis1: kotlin.String?
|
||||
public val testElvis2: kotlin.String
|
||||
public val testElvis3: kotlin.String?
|
||||
public val testExcl1: kotlin.String?
|
||||
public val testExcl2: kotlin.String?
|
||||
public val testIf1: kotlin.String?
|
||||
public val testIf2: kotlin.String?
|
||||
public val testIf3: kotlin.String?
|
||||
public val testIf4: kotlin.String?
|
||||
public val testIf5: kotlin.String?
|
||||
public val testWhen1: kotlin.String?
|
||||
public val testWhen2: kotlin.String?
|
||||
public val testWhen3: kotlin.String?
|
||||
public val testWhen4: kotlin.String?
|
||||
public val testWhen5: kotlin.String?
|
||||
+2
-2
@@ -181,8 +181,8 @@ fun returnFunctionLiteral(a: Any?): Function0<Int> {
|
||||
else return { -> 1 }
|
||||
}
|
||||
|
||||
fun returnFunctionLiteralDoesntWork(a: Any?): Function0<Int> =
|
||||
if (a is Int) <!TYPE_MISMATCH!>{ -> a }<!>
|
||||
fun returnFunctionLiteralExpressionBody(a: Any?): Function0<Int> =
|
||||
if (a is Int) { -> <!DEBUG_INFO_SMARTCAST!>a<!> }
|
||||
else { -> 1 }
|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ public fun illegalWhenBody(/*0*/ a: kotlin.Any): kotlin.Int
|
||||
public fun mergeSmartCasts(/*0*/ a: kotlin.Any?): kotlin.Unit
|
||||
public fun returnFunctionLiteral(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun returnFunctionLiteralBlock(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun returnFunctionLiteralDoesntWork(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun returnFunctionLiteralExpressionBody(/*0*/ a: kotlin.Any?): () -> kotlin.Int
|
||||
public fun toInt(/*0*/ i: kotlin.Int?): kotlin.Int
|
||||
public fun vars(/*0*/ a: kotlin.Any?): kotlin.Unit
|
||||
|
||||
|
||||
+15
-3
@@ -8,9 +8,21 @@ interface D: A, B
|
||||
interface E: A, B
|
||||
|
||||
fun foo(c: C?, d: D?, e: E?) {
|
||||
val a: A? = <!TYPE_MISMATCH!>c ?: d<!> ?: e
|
||||
val test1: A? = <!TYPE_MISMATCH!>c ?: d<!> ?: e
|
||||
|
||||
val b: B? = if (false) <!TYPE_MISMATCH!>if (true) c else d<!> else e
|
||||
val test2: B? = if (false) if (true) c else d else e
|
||||
|
||||
//outer elvis operator and if-expression have error types
|
||||
val test3: A? = when {
|
||||
true -> c
|
||||
else -> when {
|
||||
true -> d
|
||||
else -> e
|
||||
}
|
||||
}
|
||||
|
||||
val test4: B? = when (1) {
|
||||
1 -> c
|
||||
2 -> d
|
||||
else -> e
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -DEBUG_INFO_SMARTCAST
|
||||
|
||||
interface Data
|
||||
interface Item
|
||||
class FlagData(val value: Boolean) : Data
|
||||
class ListData<T : Item>(val list: List<T>) : Data
|
||||
|
||||
fun <T> listOf(vararg items: T): List<T> = null!!
|
||||
|
||||
fun test1(o: Any) = <!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>when<!> (o) {
|
||||
is List<*> ->
|
||||
ListData(listOf())
|
||||
is Int -> when {
|
||||
o < 0 ->
|
||||
FlagData(true)
|
||||
else ->
|
||||
null
|
||||
}
|
||||
else ->
|
||||
null
|
||||
}
|
||||
|
||||
fun test1x(o: Any): Data? = when (o) {
|
||||
is List<*> ->
|
||||
ListData(listOf())
|
||||
is Int -> when {
|
||||
o < 0 ->
|
||||
FlagData(true)
|
||||
else ->
|
||||
null
|
||||
}
|
||||
else ->
|
||||
null
|
||||
}
|
||||
|
||||
fun test2() =
|
||||
<!TYPE_INFERENCE_FAILED_ON_SPECIAL_CONSTRUCT!>if<!> (true)
|
||||
ListData(listOf())
|
||||
else
|
||||
FlagData(true)
|
||||
|
||||
fun test2x(): Data =
|
||||
if (true) ListData(listOf()) else FlagData(true)
|
||||
|
||||
fun test2y(): Any =
|
||||
if (true) ListData(listOf()) else FlagData(true)
|
||||
|
||||
fun test2z(): Any =
|
||||
run { if (true) ListData(listOf()) else FlagData(true) }
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T> listOf(/*0*/ vararg items: T /*kotlin.Array<out T>*/): kotlin.collections.List<T>
|
||||
public fun test1(/*0*/ o: kotlin.Any): ???
|
||||
public fun test1x(/*0*/ o: kotlin.Any): Data?
|
||||
public fun test2(): ???
|
||||
public fun test2x(): Data
|
||||
public fun test2y(): kotlin.Any
|
||||
public fun test2z(): kotlin.Any
|
||||
|
||||
public interface Data {
|
||||
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
|
||||
}
|
||||
|
||||
public final class FlagData : Data {
|
||||
public constructor FlagData(/*0*/ value: kotlin.Boolean)
|
||||
public final val value: kotlin.Boolean
|
||||
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
|
||||
}
|
||||
|
||||
public interface Item {
|
||||
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
|
||||
}
|
||||
|
||||
public final class ListData</*0*/ T : Item> : Data {
|
||||
public constructor ListData</*0*/ T : Item>(/*0*/ list: kotlin.collections.List<T>)
|
||||
public final val list: kotlin.collections.List<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
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
interface Maybe<T>
|
||||
class Some<T>(val value: T) : Maybe<T>
|
||||
class None<T> : Maybe<T>
|
||||
|
||||
fun <T> none() : None<T> = TODO()
|
||||
|
||||
fun test1() : Maybe<String?> = if (true) none() else Some("")
|
||||
|
||||
fun test2() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some("")
|
||||
}
|
||||
|
||||
fun test3() : Maybe<String?> = when {
|
||||
true -> none()
|
||||
else -> Some<String?>("")
|
||||
}
|
||||
|
||||
fun test4() : Maybe<String?> {
|
||||
when ("") {
|
||||
"a" -> return none()
|
||||
else -> return Some<String?>("")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T> none(): None<T>
|
||||
public fun test1(): Maybe<kotlin.String?>
|
||||
public fun test2(): Maybe<kotlin.String?>
|
||||
public fun test3(): Maybe<kotlin.String?>
|
||||
public fun test4(): Maybe<kotlin.String?>
|
||||
|
||||
public interface Maybe</*0*/ 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
|
||||
}
|
||||
|
||||
public final class None</*0*/ T> : Maybe<T> {
|
||||
public constructor None</*0*/ 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
|
||||
}
|
||||
|
||||
public final class Some</*0*/ T> : Maybe<T> {
|
||||
public constructor Some</*0*/ T>(/*0*/ value: T)
|
||||
public final val value: 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
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
val test1: (String) -> Boolean =
|
||||
when {
|
||||
true -> {{ true }}
|
||||
else -> {{ false }}
|
||||
}
|
||||
|
||||
val test2: (String) -> Boolean =
|
||||
when {
|
||||
true -> {{ true }}
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
val test3: (String) -> Boolean =
|
||||
when {
|
||||
true -> { s -> true }
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
val test4: (String) -> Boolean =
|
||||
when {
|
||||
true -> { <!EXPECTED_PARAMETERS_NUMBER_MISMATCH!>s1, <!CANNOT_INFER_PARAMETER_TYPE!>s2<!><!> -> true }
|
||||
else -> null!!
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package
|
||||
|
||||
public val test1: (kotlin.String) -> kotlin.Boolean
|
||||
public val test2: (kotlin.String) -> kotlin.Boolean
|
||||
public val test3: (kotlin.String) -> kotlin.Boolean
|
||||
public val test4: (kotlin.String) -> kotlin.Boolean
|
||||
Reference in New Issue
Block a user