Data flow info: correct disjunction of a type with Nothing: Type U Nothing = Type #KT-9578 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-01-18 14:59:24 +03:00
parent 5d3186ac49
commit 9360bd49a6
6 changed files with 61 additions and 3 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.resolve.calls.smartcasts
import com.google.common.collect.*
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
@@ -251,6 +252,13 @@ internal class DelegatingDataFlowInfo @JvmOverloads constructor(
return DelegatingDataFlowInfo(this, ImmutableMap.copyOf(nullabilityMapBuilder), otherTypeInfo)
}
private fun Set<KotlinType>.containsNothing() = any { KotlinBuiltIns.isNothing(it) }
private fun Set<KotlinType>.intersect(other: Set<KotlinType>) =
if (other.containsNothing()) this
else if (this.containsNothing()) other
else Sets.intersection(this, other)
override fun or(otherInfo: DataFlowInfo): DataFlowInfo {
if (otherInfo === DataFlowInfo.EMPTY) return DataFlowInfo.EMPTY
if (this === DataFlowInfo.EMPTY) return DataFlowInfo.EMPTY
@@ -270,9 +278,7 @@ internal class DelegatingDataFlowInfo @JvmOverloads constructor(
val newTypeInfo = newTypeInfo()
for (key in Sets.intersection(myTypeInfo.keySet(), otherTypeInfo.keySet())) {
val thisTypes = myTypeInfo.get(key)
val otherTypes = otherTypeInfo.get(key)
newTypeInfo.putAll(key, Sets.intersection(thisTypes, otherTypes))
newTypeInfo.putAll(key, myTypeInfo[key].intersect(otherTypeInfo[key]))
}
if (nullabilityMapBuilder.isEmpty() && newTypeInfo.isEmpty) {
+15
View File
@@ -0,0 +1,15 @@
fun foo(s: Any): String {
val x = when (s) {
is String -> s
is Int -> "$s"
else -> return ""
}
val y: String = x
return y
}
fun box() = if (foo("OK") == "OK" && foo(42) == "42" && foo(true) == "") "OK" else "Fail"
@@ -0,0 +1,21 @@
fun foo(s: Any): String {
val x = when (s) {
is String -> s
is Int -> "$s"
else -> return ""
}
val y: String = <!DEBUG_INFO_SMARTCAST!>x<!> // should be Ok
return y
}
fun bar(s: Any): String {
val x = when (s) {
is String -> s <!USELESS_CAST!>as String<!> // meaningless
is Int -> "$s"
else -> return ""
}
val y: String = x // no error
return y
}
@@ -0,0 +1,4 @@
package
public fun bar(/*0*/ s: kotlin.Any): kotlin.String
public fun foo(/*0*/ s: kotlin.Any): kotlin.String
@@ -18617,6 +18617,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/When.kt");
doTest(fileName);
}
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/when/WhenTypeDisjunctions.kt");
doTest(fileName);
}
}
}
@@ -8389,6 +8389,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("typeDisjunction.kt")
public void testTypeDisjunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/typeDisjunction.kt");
doTest(fileName);
}
@TestMetadata("whenArgumentIsEvaluatedOnlyOnce.kt")
public void testWhenArgumentIsEvaluatedOnlyOnce() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/whenArgumentIsEvaluatedOnlyOnce.kt");