use data flow info in constraint system

intersect data flow value possible types
add intersection type (or exact type for the most cases) as a lower bound to constraint system

always get common super type for intersection type as a result
(avoid returning it, even in error messages)
This commit is contained in:
Svetlana Isakova
2013-07-29 16:32:37 +04:00
parent b91846eb5d
commit 27cff93ed8
7 changed files with 195 additions and 2 deletions
@@ -0,0 +1,56 @@
package a
fun <T> id(t: T): T = t
fun <T> two(u: T, <!UNUSED_PARAMETER!>v<!>: T): T = u
fun <T> three(<!UNUSED_PARAMETER!>a<!>: T, <!UNUSED_PARAMETER!>b<!>: T, c: T): T = c
trait A
trait B: A
trait C: A
fun test(a: A, b: B, c: C) {
if (a is B && a is C) {
val d: C = id(a)
val e: Any = id(a)
val f = id(a)
val g = two(a, b)
g: B
g: A
val h: Any = two(a, b)
val k = three(a, b, c)
k: A
<!TYPE_MISMATCH!>k<!>: B
val l: Int = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>three<!>(a, b, c)
use(d, e, f, g, h, k, l)
}
}
fun <T> foo(t: T, <!UNUSED_PARAMETER!>l<!>: MutableList<T>): T = t
fun testErrorMessages(a: A, ml: MutableList<String>) {
if (a is B && a is C) {
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>foo<!>(a, ml)
}
if(a is C) {
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>foo<!>(a, ml)
}
}
fun rr(s: String?) {
if (s != null) {
val l = arrayListOf("", s)
l: MutableList<String>
<!TYPE_MISMATCH!>l<!>: MutableList<String?>
}
}
//from library
fun arrayListOf<T>(vararg <!UNUSED_PARAMETER!>values<!>: T): MutableList<T> = throw Exception()
fun use(vararg a: Any) = a
@@ -0,0 +1,40 @@
//KT-1355 Type inference fails with autocast and generic function
//tests for Map.set
package a
import java.util.HashMap
fun foo(map: MutableMap<Int, String>, value: String?) {
if (value != null) {
map.put(1, value) //ok
map.set(1, value) //type inference failed
map[1] = value //type inference failed
}
}
//---------------------------
public data open class Tag(public var tagName: String) {
public val attributes: MutableMap<String, String> = HashMap<String, String>()
public val contents: MutableList<Tag> = arrayListOf()
public var id: String?
get() = attributes["id"]
set(value) {
if(value == null) {
attributes.remove("id")
}
else {
attributes["id"] = value<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
attributes["id"] = value
}
}
}
//from library
fun <K, V> MutableMap<K, V>.set(key : K, value : V) = this.put(key, value)
fun arrayListOf<T>(vararg <!UNUSED_PARAMETER!>values<!>: T): MutableList<T> = throw Exception()
@@ -0,0 +1,19 @@
//KT-2746 Do autocasts in inference
import java.util.HashMap
class C<T>(t :T)
fun test1(a: Any) {
if (a is String) {
val <!UNUSED_VARIABLE!>c<!>: C<String> = C(a)
}
}
fun f<T>(t :T): C<T> = C(t)
fun test2(a: Any) {
if (a is String) {
val <!UNUSED_VARIABLE!>c1<!>: C<String> = f(a)
}
}
@@ -0,0 +1,20 @@
//KT-2851 Type inference failed passing in not-null after smart-cast value in Pair
package a
fun main(args: Array<String>) {
val value: String? = ""
if (value != null) {
foo(Pair("val", value))
foo(Pair("val", value<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>))
foo(Pair<String, String>("val", value))
}
}
fun foo(<!UNUSED_PARAMETER!>map<!>: Pair<String, String>) {}
//from library
public class Pair<out A, out B> (
public val first: A,
public val second: B
)