diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.kt new file mode 100644 index 00000000000..72afc526eaf --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.kt @@ -0,0 +1,6 @@ +fun test(f: (T) -> T?) { + doFun(f.ext()) +} + +fun Function1.ext(): Function0 = throw Exception() +fun doFun(f: () -> R?) = f diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.txt new file mode 100644 index 00000000000..e1ca5592c90 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.txt @@ -0,0 +1,5 @@ +package + +internal fun doFun(/*0*/ f: () -> R?): () -> R? +internal fun test(/*0*/ f: (T) -> T?): kotlin.Unit +internal fun ((E) -> E?).ext(): () -> E? diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.kt new file mode 100644 index 00000000000..56b7f5657a7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.kt @@ -0,0 +1,11 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +import java.util.* + +fun test(list: ArrayList, comparatorFun: (Int, Int) -> Int) { + sort(list, Comparator(comparatorFun)) +} + + +public fun sort(list: List, c: Comparator) { +} diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.txt new file mode 100644 index 00000000000..6085ec7dd70 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.txt @@ -0,0 +1,4 @@ +package + +public fun sort(/*0*/ list: kotlin.List, /*1*/ c: java.util.Comparator): kotlin.Unit +internal fun test(/*0*/ list: java.util.ArrayList, /*1*/ comparatorFun: (kotlin.Int, kotlin.Int) -> kotlin.Int): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/fixVariablesInRightOrder.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/fixVariablesInRightOrder.kt new file mode 100644 index 00000000000..72673750ba9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/fixVariablesInRightOrder.kt @@ -0,0 +1,17 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +class GenericClass + +public fun GenericClass>.foo() {} + +public fun bar(t: T, ext: GenericClass.() -> Unit) {} + +fun test() { + bar(mapOf(2 to 3)) { foo() } +} + +// from library +class Pair +fun mapOf(keyValuePair: Pair): Map = throw Exception() +fun A.to(that: B): Pair = throw Exception() + diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/fixVariablesInRightOrder.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/fixVariablesInRightOrder.txt new file mode 100644 index 00000000000..2f49bcdfb65 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/fixVariablesInRightOrder.txt @@ -0,0 +1,21 @@ +package + +public fun bar(/*0*/ t: T, /*1*/ ext: GenericClass.() -> kotlin.Unit): kotlin.Unit +internal fun mapOf(/*0*/ keyValuePair: Pair): kotlin.Map +internal fun test(): kotlin.Unit +public fun GenericClass>.foo(): kotlin.Unit +internal fun A.to(/*0*/ that: B): Pair + +internal final class GenericClass { + public constructor GenericClass() + 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 +} + +internal final class Pair { + public constructor Pair() + 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 +} diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/genericCandidateInGenericClass.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/genericCandidateInGenericClass.kt new file mode 100644 index 00000000000..3acf44efad9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/genericCandidateInGenericClass.kt @@ -0,0 +1,9 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +class GenericClass(val value: T) { + public fun foo

(extension: T.() -> P) {} +} + +public fun GenericClass>.bar() { + foo( { listIterator() }) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/genericCandidateInGenericClass.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/genericCandidateInGenericClass.txt new file mode 100644 index 00000000000..b0c56b38ada --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/genericCandidateInGenericClass.txt @@ -0,0 +1,12 @@ +package + +public fun GenericClass>.bar(): kotlin.Unit + +internal final class GenericClass { + public constructor GenericClass(/*0*/ value: T) + internal final val value: T + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(/*0*/ extension: T.() -> P): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/inferenceWithUpperBoundsInLambda.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/inferenceWithUpperBoundsInLambda.kt new file mode 100644 index 00000000000..7ab48bdff17 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/inferenceWithUpperBoundsInLambda.kt @@ -0,0 +1,13 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +import java.util.* + +interface Foo +class Bar(val list: MutableList) {} + +fun test(map: MutableMap>) { + + map.getOrPut1("", { Bar(ArrayList()) }) +} + +fun MutableMap.getOrPut1(key: K, defaultValue: () -> V): V = throw Exception() diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/inferenceWithUpperBoundsInLambda.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/inferenceWithUpperBoundsInLambda.txt new file mode 100644 index 00000000000..be4b7ba4273 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/inferenceWithUpperBoundsInLambda.txt @@ -0,0 +1,18 @@ +package + +internal fun test(/*0*/ map: kotlin.MutableMap>): kotlin.Unit +internal fun kotlin.MutableMap.getOrPut1(/*0*/ key: K, /*1*/ defaultValue: () -> V): V + +internal final class Bar { + public constructor Bar(/*0*/ list: kotlin.MutableList) + internal final val list: kotlin.MutableList + 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 +} + +internal interface Foo { + 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 +} diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/kt3372toCollection.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/kt3372toCollection.kt new file mode 100644 index 00000000000..ea1f2c3b8de --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/kt3372toCollection.kt @@ -0,0 +1,12 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +// !CHECK_TYPE +// KT-3372 Use upper bound in type argument inference + +import java.util.HashSet + +fun > Iterable.toCollection(result: C) : C = throw Exception() + +fun test(list: List) { + val set = list.toCollection(HashSet()) + set checkType { _>() } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/kt3372toCollection.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/kt3372toCollection.txt new file mode 100644 index 00000000000..2f6681972e4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/kt3372toCollection.txt @@ -0,0 +1,4 @@ +package + +internal fun test(/*0*/ list: kotlin.List): kotlin.Unit +internal fun > kotlin.Iterable.toCollection(/*0*/ result: C): C diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/nestedLambdas.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/nestedLambdas.kt new file mode 100644 index 00000000000..636000acb4c --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/nestedLambdas.kt @@ -0,0 +1,10 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +// !CHECK_TYPE + +fun Collection.map(transform : (T) -> R) : List = throw Exception() + +fun test(list: List>) { + + val list1 = list.map { it.map { "$it" } } + list1 checkType { _>>() } +} diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/nestedLambdas.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/nestedLambdas.txt new file mode 100644 index 00000000000..225cd969604 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/nestedLambdas.txt @@ -0,0 +1,4 @@ +package + +internal fun test(/*0*/ list: kotlin.List>): kotlin.Unit +internal fun kotlin.Collection.map(/*0*/ transform: (T) -> R): kotlin.List diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/theSameFunctionInArgs.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/theSameFunctionInArgs.kt new file mode 100644 index 00000000000..65b81903685 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/theSameFunctionInArgs.kt @@ -0,0 +1,8 @@ +// !CHECK_TYPE + +fun arrayOf(vararg t : T) : Array = t as Array + +fun test() { + val array = arrayOf(arrayOf(1)) + array checkType { _>>() } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/theSameFunctionInArgs.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/theSameFunctionInArgs.txt new file mode 100644 index 00000000000..e4c746ca220 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/theSameFunctionInArgs.txt @@ -0,0 +1,4 @@ +package + +internal fun arrayOf(/*0*/ vararg t: T /*kotlin.Array*/): kotlin.Array +internal fun test(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/resolve/ambiguityWithTwoCorrespondingFunctionTypes.kt b/compiler/testData/diagnostics/tests/resolve/ambiguityWithTwoCorrespondingFunctionTypes.kt new file mode 100644 index 00000000000..984dbdb2c74 --- /dev/null +++ b/compiler/testData/diagnostics/tests/resolve/ambiguityWithTwoCorrespondingFunctionTypes.kt @@ -0,0 +1,11 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun foo(a: Any, f: ()->Int) = f() +fun foo(a: Any, f: (Any)->Int) = f(a) +fun foo(i: Int, f: Int.()->Int) = i.f() + +fun test1() { + foo(1) { -> + this + } +} diff --git a/compiler/testData/diagnostics/tests/resolve/ambiguityWithTwoCorrespondingFunctionTypes.txt b/compiler/testData/diagnostics/tests/resolve/ambiguityWithTwoCorrespondingFunctionTypes.txt new file mode 100644 index 00000000000..8fc9fe6c93c --- /dev/null +++ b/compiler/testData/diagnostics/tests/resolve/ambiguityWithTwoCorrespondingFunctionTypes.txt @@ -0,0 +1,6 @@ +package + +internal fun foo(/*0*/ a: kotlin.Any, /*1*/ f: () -> kotlin.Int): kotlin.Int +internal fun foo(/*0*/ a: kotlin.Any, /*1*/ f: (kotlin.Any) -> kotlin.Int): kotlin.Int +internal fun foo(/*0*/ i: kotlin.Int, /*1*/ f: kotlin.Int.() -> kotlin.Int): kotlin.Int +internal fun test1(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdas.kt b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdas.kt new file mode 100644 index 00000000000..b6e7f1bc6dd --- /dev/null +++ b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdas.kt @@ -0,0 +1,8 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +// !CHECK_TYPE + +fun foo(first: () -> T, second: (T) -> R): R = throw Exception() +fun test() { + val r = foo( { 4 }, { "${it + 1}" } ) + r checkType { _() } +} diff --git a/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdas.txt b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdas.txt new file mode 100644 index 00000000000..452f937be9e --- /dev/null +++ b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdas.txt @@ -0,0 +1,4 @@ +package + +internal fun foo(/*0*/ first: () -> T, /*1*/ second: (T) -> R): R +internal fun test(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.kt b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.kt new file mode 100644 index 00000000000..6c2ba39d404 --- /dev/null +++ b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.kt @@ -0,0 +1,7 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun foo(f: () -> Collection, p: (T) -> Boolean): Collection = throw Exception() + +fun emptyList(): List = throw Exception() + +fun test(): Collection = foo({ emptyList() }, { x -> x > 0 }) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.txt b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.txt new file mode 100644 index 00000000000..3ddf0a9d9ab --- /dev/null +++ b/compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.txt @@ -0,0 +1,5 @@ +package + +internal fun emptyList(): kotlin.List +internal fun foo(/*0*/ f: () -> kotlin.Collection, /*1*/ p: (T) -> kotlin.Boolean): kotlin.Collection +internal fun test(): kotlin.Collection diff --git a/compiler/testData/diagnostics/tests/resolve/resolveWithSpecifiedFunctionLiteralWithId.kt b/compiler/testData/diagnostics/tests/resolve/resolveWithSpecifiedFunctionLiteralWithId.kt index bb0bd0b58f5..ad2dc2764be 100644 --- a/compiler/testData/diagnostics/tests/resolve/resolveWithSpecifiedFunctionLiteralWithId.kt +++ b/compiler/testData/diagnostics/tests/resolve/resolveWithSpecifiedFunctionLiteralWithId.kt @@ -10,26 +10,26 @@ fun foo(s: String, a: Any) = s + a fun foo(a: Any, s: String) = s + a fun foo(i: Int, j: Int) = i + j fun foo(a: Any, i: Int) = "$a$i" -fun foo(f: (Int)->Int, i: Int) = f(i) -fun foo(f: (String)->Int, s: String) = f(s) -fun foo(f: (Any)->Int, a: Any) = f(a) -fun foo(s: String, f: (String)->Int) = f(s) -fun foo(a: Any, f: (Any)->Int) = f(a) +fun foo(f: (Int) -> Int, i: Int) = f(i) +fun foo(f: (String) -> Int, s: String) = f(s) +fun foo(f: (Any) -> Int, a: Any) = f(a) +fun foo(s: String, f: (String) -> Int) = f(s) +fun foo(a: Any, f: (Any) -> Int) = f(a) //appropriate function -fun foo(i: Int, f: (Int)->Int) = f(i) +fun foo(i: Int, f: (Int) -> Int) = f(i) fun id(t: T) = t fun test() { - foo(1, id { (x1: Int):Int -> - foo(2, id { (x2: Int): Int -> - foo(3, id { (x3: Int): Int -> - foo(4, id { (x4: Int): Int -> - foo(5, id { (x5: Int): Int -> - x1 + x2 + x3 + x4 + x5 + A.iii - }) - }) - }) - }) - }) + foo(1, id(fun(x1: Int) = + foo(2, id(fun(x2: Int) = + foo(3, id(fun(x3: Int) = + foo(4, id(fun(x4: Int) = + foo(5, id(fun(x5: Int) = + x1 + x2 + x3 + x4 + x5 + A.iii + )) + )) + )) + )) + )) } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 9827f921490..52722331fd9 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -6696,6 +6696,63 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { } } + @TestMetadata("compiler/testData/diagnostics/tests/inference/commonSystem") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class CommonSystem extends AbstractJetDiagnosticsTest { + public void testAllFilesPresentInCommonSystem() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/inference/commonSystem"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("boundOnNullableVariable.kt") + public void testBoundOnNullableVariable() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/boundOnNullableVariable.kt"); + doTest(fileName); + } + + @TestMetadata("dontCaptureTypeVariable.kt") + public void testDontCaptureTypeVariable() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/dontCaptureTypeVariable.kt"); + doTest(fileName); + } + + @TestMetadata("fixVariablesInRightOrder.kt") + public void testFixVariablesInRightOrder() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/fixVariablesInRightOrder.kt"); + doTest(fileName); + } + + @TestMetadata("genericCandidateInGenericClass.kt") + public void testGenericCandidateInGenericClass() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/genericCandidateInGenericClass.kt"); + doTest(fileName); + } + + @TestMetadata("inferenceWithUpperBoundsInLambda.kt") + public void testInferenceWithUpperBoundsInLambda() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/inferenceWithUpperBoundsInLambda.kt"); + doTest(fileName); + } + + @TestMetadata("kt3372toCollection.kt") + public void testKt3372toCollection() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/kt3372toCollection.kt"); + doTest(fileName); + } + + @TestMetadata("nestedLambdas.kt") + public void testNestedLambdas() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/nestedLambdas.kt"); + doTest(fileName); + } + + @TestMetadata("theSameFunctionInArgs.kt") + public void testTheSameFunctionInArgs() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/commonSystem/theSameFunctionInArgs.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/diagnostics/tests/inference/constraints") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -11085,12 +11142,30 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("ambiguityWithTwoCorrespondingFunctionTypes.kt") + public void testAmbiguityWithTwoCorrespondingFunctionTypes() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/ambiguityWithTwoCorrespondingFunctionTypes.kt"); + doTest(fileName); + } + @TestMetadata("incompleteConstructorInvocation.kt") public void testIncompleteConstructorInvocation() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/incompleteConstructorInvocation.kt"); doTest(fileName); } + @TestMetadata("inferenceInLinkedLambdas.kt") + public void testInferenceInLinkedLambdas() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdas.kt"); + doTest(fileName); + } + + @TestMetadata("inferenceInLinkedLambdasDependentOnExpectedType.kt") + public void testInferenceInLinkedLambdasDependentOnExpectedType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.kt"); + doTest(fileName); + } + @TestMetadata("objectLiteralAsArgument.kt") public void testObjectLiteralAsArgument() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/objectLiteralAsArgument.kt");