diff --git a/compiler/testData/codegen/regressions/kt1800.kt b/compiler/testData/codegen/regressions/kt1800.kt new file mode 100644 index 00000000000..5fd36ba0939 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt1800.kt @@ -0,0 +1,32 @@ +//KT-1800 error/NonExistentClass generated on runtime +package i + +import java.util.Collection +import java.util.ArrayList + +public class User(val firstName: String, + val lastName: String, + val age: Int) { + fun toString() = "$firstName $lastName, age $age" +} + +public fun > Collection.min(): T? { + var minValue: T? = null + for(value in this) { + if (minValue == null || value.compareTo(minValue!!) < 0) { + minValue = value + } + } + return minValue +} + +fun box() : String { + val users = arrayList( + User("John", "Doe", 30), + User("Jane", "Doe", 27)) + + val ages = users.map { it.age } + + val minAge = ages.min() + return if (minAge == 27) "OK" else "fail" +} diff --git a/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.kt b/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.kt index 1d8cd306fe0..a734ef489e4 100644 --- a/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.kt +++ b/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.kt @@ -42,7 +42,7 @@ fun main(args : Array) { val b = fooT2()(1) b : Int - fooT2()(1) // : Any? + fooT2()(1) // : Any? 1() 1{} @@ -79,4 +79,4 @@ fun test() { {}() 1?.{Int.() -> 1}() 1.{}() -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/arrayConstructor.jet b/compiler/testData/diagnostics/tests/inference/arrayConstructor.jet new file mode 100644 index 00000000000..bda02d27549 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/arrayConstructor.jet @@ -0,0 +1,12 @@ +package b + +fun bar() { + val a1 = Array(1, {(i: Int) -> i}) + val a2 = Array(1, {(i: Int) -> "$i"}) + val a3 = Array(1, {it}) + + a1 : Array + a2 : Array + a3 : Array +} + diff --git a/compiler/testData/diagnostics/tests/inference/dependOnExpectedType.jet b/compiler/testData/diagnostics/tests/inference/dependOnExpectedType.jet new file mode 100644 index 00000000000..4198f557e87 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/dependOnExpectedType.jet @@ -0,0 +1,51 @@ +package h +//+JDK +import java.util.* + +fun id(t: T) : T = t + +fun id1(t: T) = t + +fun elem(t: List): R = t.get(0) + +fun elemAndList(r: R, t: List): R = t.get(0) + +fun both(t1: T, t2: T) : T = t1 + +fun test1() { + val a = elem(list(2)) + val b = id(elem(list(2))) + val c = id(id1(id(id1(list(33))))) + a : Int + b : Int + c : List + + val d : ArrayList = newList() + val e : ArrayList = id(newList()) + val f : ArrayList = id(id1(id(id1(newList())))) + + d : List + e : List + f : List + + val g = elemAndList("", newList()) + val h = elemAndList(1, newList()) + + g : String + h : Long + + val i = both(1, "") + val j = both(id(1), id("")) + i : Comparable + j : Comparable +} + +fun list(value: T) : ArrayList { + val list = ArrayList() + list.add(value) + return list +} + +fun newList() : ArrayList { + return ArrayList() +} diff --git a/compiler/testData/diagnostics/tests/inference/listConstructor.jet b/compiler/testData/diagnostics/tests/inference/listConstructor.jet new file mode 100644 index 00000000000..d3c3ee5a622 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/listConstructor.jet @@ -0,0 +1,28 @@ +package a +//+JDK +import java.util.* + +fun cons(x: A, xs: List): List = xs + +fun nil(): List = arrayList() + +fun test() { + val xs = cons(1, nil()) + val xs1 = cons("", nil()) + val xs2 = cons(1, nil()) + + xs : List + xs1 : List + xs2 : List +} + + +// --------------------- +// copy from kotlin util + +fun arrayList(vararg values: T) : ArrayList = values.toCollection(ArrayList(values.size)) + +fun > Array.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} diff --git a/compiler/testData/diagnostics/tests/inference/mapFunction.jet b/compiler/testData/diagnostics/tests/inference/mapFunction.jet new file mode 100644 index 00000000000..74bac13367c --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/mapFunction.jet @@ -0,0 +1,28 @@ +package a + +//+JDK +import java.util.* + +fun foo() { + val v = array(1, 2, 3) + + val u = v map { it * 2 } + + u : List +} + + +// --------------------- +// copy from kotlin util + +fun array(vararg t : T) : Array = t + +fun Array.map(transform : (T) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), transform) +} + +fun > Array.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt1029.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt1029.jet new file mode 100644 index 00000000000..04a6f3880ab --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt1029.jet @@ -0,0 +1,15 @@ +//KT-1029 Wrong type inference +package i + +public fun from(yielder: ()->Iterable) : Iterable { +} + +public fun Iterable.where(predicate : (T)->Boolean) : ()->Iterable { +} + +fun a() { + val x = 0..200 + val odd = from (x where {it%2==0}) // I believe it should infer here + + odd : Iterable +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt1031.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt1031.jet new file mode 100644 index 00000000000..10bd8f10801 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt1031.jet @@ -0,0 +1,19 @@ +//KT-1031 Can't infer type of `it` with two lambdas +package i + +import java.util.ArrayList + +public fun Iterable.where(predicate : (TItem)->Boolean) : ()->Iterable { +} + +public fun select(yielder: ()->Iterable, selector : (TItem)->TResult) : ()->Iterable { +} + +fun a() { + val x = 0..200 + val z = x where { (i: Int) -> i % 2 == 0 } + val yielder = select(x where { it%2==0 }, { it.toString() }) + + z : () -> Iterable + yielder : () -> Iterable +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt1558.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt1558.jet new file mode 100644 index 00000000000..cee9259d894 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt1558.jet @@ -0,0 +1,24 @@ +//KT-1558 Exception while analyzing +package j + +//+JDK +import java.util.List + + +fun testArrays(val ci: List, val cii: List) { + val c1: Array = cii.sure().toArray(Array) + + val c2: Array = ci.toArray(Array()) + + val c3 = Array() + + val c4 = ci.toArray(Array()) + + val c5 = ci.toArray(Array()) + + c1 : Array + c2 : Array + c3 : Array + c4 : Array + c5 : Array +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt1944.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt1944.jet new file mode 100644 index 00000000000..9c8df897074 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt1944.jet @@ -0,0 +1,14 @@ +//KT-1944 Inference fails on run() +package j + +class P { + var x : Int = 0 + private set + + fun foo() { + val r = run {x = 5} // ERROR + r : Unit + } +} + +fun run(f: () -> T) : T = f() diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2179.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt2179.jet new file mode 100644 index 00000000000..c3284ec0909 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2179.jet @@ -0,0 +1,44 @@ +//KT-2179 Nested function literal breaks compiler +package i + +//+JDK +import java.util.* + +fun test() { + val sample1: List> = arrayList(arrayList(1, 7, null, 8)) + + //breaks compiler + val sample2 = sample1.map({it.map({it})}) + sample2 : List> + + //breaks compiler + val sample3 = sample1.map({row -> row.map({column -> column})}) + sample3 : List> + + //doesn't break compiler + val identity: (Int?) -> Int? = {column -> column} + val sample4 = sample1.map({row -> row.map(identity)}) + sample4 : List> +} + +//------------ + +fun arrayList(vararg values: T) : ArrayList = values.toCollection(ArrayList(values.size)) + +fun java.util.Collection.map(transform : (T) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), transform) +} + +fun > java.util.Collection.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +fun > Array.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +val Collection<*>.size : Int + get() = size() diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2294.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt2294.jet new file mode 100644 index 00000000000..aaf50e72945 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2294.jet @@ -0,0 +1,16 @@ +//KT-2294 Type inference infers DONT_CARE instead of correct type +package a + +public fun foo(array: Array): Array = array + +public fun test() +{ + val x = foo(array(1, 2, 3, 4, 5)) // Should infer type 'Int' + // ^--- public final fun array(vararg t : DONT_CARE) : jet.Array defined in Kotlin + // ^--- public final fun foo(items t : jet.Array) : jet.Array defined in root package + x : Array +} + +//-------------------- + +fun array(vararg t : T) : Array = t diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2320.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt2320.jet new file mode 100644 index 00000000000..d4f539a6032 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2320.jet @@ -0,0 +1,13 @@ +//KT-2320 failure of complex case of type inference +package i + +trait NotMap + +trait Entry { + fun getValue(): B +} + + +fun NotMap.mapValuesOriginal(ff: (Entry) -> R): NotMap = throw Exception() + +fun NotMap.mapValuesOnly(f: (B) -> C) = mapValuesOriginal { e -> f(e.getValue()) } diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2324.jet b/compiler/testData/diagnostics/tests/inference/regressions/kt2324.jet new file mode 100644 index 00000000000..04f892021ac --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2324.jet @@ -0,0 +1,33 @@ +//KT-2324 Can't resolve generic by type of function result +package i + +//+JDK +import java.util.* + +fun someFunction(list: List, transform: (T) -> K): List { + val result = arrayList() + for (i in list) { + result.add(transform(i)) + } + return result +} + +fun testSomeFunction() { + val result1 = someFunction(arrayList(1, 2), {it : Int}) //type of result1 is List + assertEquals(1, result1.get(0)); //OK + + val result2 = someFunction(arrayList(1, 2), {it}) // type of result2 is List + result2 : List + assertEquals(1, result2.get(0)); //resolved to error element +} + +//--------------------------------- +fun assertEquals(expected: Any?, actual: Any?, message: String = "") { +} + +fun arrayList(vararg values: T) : ArrayList = values.toCollection(ArrayList(values.size)) + +fun > Array.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} diff --git a/compiler/testData/diagnostics/tests/regressions/kt1489_1728.kt b/compiler/testData/diagnostics/tests/regressions/kt1489_1728.kt index cc3e7c771d2..1b707dee2d3 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt1489_1728.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt1489_1728.kt @@ -18,7 +18,7 @@ class C { fun p() : Resource? = null fun bar() { - foo(p()) { + foo(p()) { } }