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:
Dmitry Petrov
2016-01-26 21:20:24 +03:00
parent e3e463ef70
commit 9db3440e72
28 changed files with 498 additions and 18 deletions
@@ -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[""] }
@@ -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!
}
@@ -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!! } }
@@ -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?