Added captured types and approximation

CapturedType captures type projection while solving the constraint system.
During the substitution type containing captured types is approximated
to get rid of captured types and (for simple cases) replace them with corresponding type projections.

Note that Array<Array< CapturedType(out Int) >> is (over)approximated by Array<out<Array<out Int>>

See 'Mixed-site variance' by Ross Tate for details.

 #KT-2570 Fixed
 #KT-2872 Fixed
 #KT-3213 Fixed
This commit is contained in:
Svetlana Isakova
2014-09-23 15:29:47 +04:00
parent e798b7c6d1
commit cd359a046c
36 changed files with 798 additions and 114 deletions
@@ -0,0 +1,29 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_VARIABLE
// FILE: A.java
public class A {
public static <T> Class<T> foo(Class<T> clazz) {
return clazz;
}
public static <T> Class<Class<T>> bar(Class<T> clazz) {
throw new Exception();
}
}
// FILE: b.kt
fun test1(clazz: Class<out Int>) {
val foo0: Class<out Int> = A.foo(clazz)
val foo1 = A.foo(clazz)
foo1 checkType { it: _< Class<out Int> > }
// should be ok
foo1 checkType { <!TYPE_MISMATCH!>it<!>: _< Class<out Int?> > }
}
fun tes2t(clazz: Class<in Int>) {
val foo0: Class<out Class<in Int>> = A.bar(clazz)
val foo1 = A.bar(clazz)
foo1 checkType { it: _< Class<out Class<in Int>> > }
// should be ok
foo1 checkType { <!TYPE_MISMATCH!>it<!>: _< Class<out Class<in Int?>> > }
}
@@ -0,0 +1,15 @@
package
internal fun tes2t(/*0*/ clazz: java.lang.Class<in kotlin.Int>): kotlin.Unit
internal fun test1(/*0*/ clazz: java.lang.Class<out kotlin.Int>): kotlin.Unit
public open class A {
public constructor A()
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 open fun </*0*/ T> bar(/*0*/ clazz: java.lang.Class<T!>!): java.lang.Class<java.lang.Class<T!>!>!
public open fun </*0*/ T> foo(/*0*/ clazz: java.lang.Class<T!>!): java.lang.Class<T!>!
}
@@ -0,0 +1,16 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// !CHECK_TYPE
fun <T> foo(array: Array<T>): Array<T> = array
fun test1(a1: Array<out Int>) {
val b1: Array<out Int> = foo(a1)
val c1 = foo(a1)
c1 checkType { it : _<Array<out Int>> }
}
fun test2(a2: Array<in Int>) {
val b2: Array<in Int> = foo(a2)
val c2 = foo(a2)
c2 checkType { it : _<Array<in Int>> }
}
@@ -0,0 +1,5 @@
package
internal fun </*0*/ T> foo(/*0*/ array: kotlin.Array<T>): kotlin.Array<T>
internal fun test1(/*0*/ a1: kotlin.Array<out kotlin.Int>): kotlin.Unit
internal fun test2(/*0*/ a2: kotlin.Array<in kotlin.Int>): kotlin.Unit
@@ -0,0 +1,16 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// !CHECK_TYPE
fun <T> foo(a: Array<T>): Array<Array<T>> = throw Exception()
fun test1(a1: Array<out Int>) {
val b1: Array<out Array<out Int>> = foo(a1)
val c1 = foo(a1)
c1 checkType { it : _<Array<out Array<out Int>>> }
}
fun test2(a2: Array<in Int>) {
val b2: Array<out Array<in Int>> = foo(a2)
val c2 = foo(a2)
c2 checkType { it : _<Array<out Array<in Int>>> }
}
@@ -0,0 +1,5 @@
package
internal fun </*0*/ T> foo(/*0*/ a: kotlin.Array<T>): kotlin.Array<kotlin.Array<T>>
internal fun test1(/*0*/ a1: kotlin.Array<out kotlin.Int>): kotlin.Unit
internal fun test2(/*0*/ a2: kotlin.Array<in kotlin.Int>): kotlin.Unit
@@ -0,0 +1,9 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_VARIABLE
fun <T> foo(l: MutableList<T>): MutableList<T> = l
fun test(l: MutableList<out Int>) {
val a: MutableList<out Int> = foo(l)
val b = foo(l)
b checkType { it: _< MutableList<out Int> > }
}
@@ -0,0 +1,4 @@
package
internal fun </*0*/ T> foo(/*0*/ l: kotlin.MutableList<T>): kotlin.MutableList<T>
internal fun test(/*0*/ l: kotlin.MutableList<out kotlin.Int>): kotlin.Unit
@@ -0,0 +1,6 @@
fun <T> Array<T>.foo() {}
fun test(array: Array<out Int>) {
array.foo()
<!TYPE_MISMATCH!>array<!>.foo<<!PROJECTION_ON_NON_CLASS_TYPE_ARGUMENT!>out<!> Int>()
}
@@ -0,0 +1,4 @@
package
internal fun test(/*0*/ array: kotlin.Array<out kotlin.Int>): kotlin.Unit
internal fun </*0*/ T> kotlin.Array<T>.foo(): kotlin.Unit