diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/CollectionLiteralResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/CollectionLiteralResolver.kt index 20a4c91e102..5bdeb355f45 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/CollectionLiteralResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/CollectionLiteralResolver.kt @@ -74,16 +74,15 @@ class CollectionLiteralResolver(val module: ModuleDescriptor, val callResolver: ): KotlinTypeInfo { val call = CallMaker.makeCallForCollectionLiteral(expression) val callName = getArrayFunctionCallName(context.expectedType) - val functionDescriptor = getFunctionDescriptorForCollectionLiteral(expression, callName) - if (functionDescriptor == null) { + val functionDescriptors = getFunctionDescriptorForCollectionLiteral(expression, callName) + if (functionDescriptors.isEmpty()) { context.trace.report(MISSING_STDLIB.on( expression, "Collection literal call '$callName()' is unresolved")) return noTypeInfo(context) } - val resolutionResults = callResolver.resolveCollectionLiteralCallWithGivenDescriptor(context, expression, call, functionDescriptor) + val resolutionResults = callResolver.resolveCollectionLiteralCallWithGivenDescriptor(context, expression, call, functionDescriptors) - // No single result after resolving one candidate? if (!resolutionResults.isSingleResult) { return noTypeInfo(context) } @@ -95,9 +94,9 @@ class CollectionLiteralResolver(val module: ModuleDescriptor, val callResolver: private fun getFunctionDescriptorForCollectionLiteral( expression: KtCollectionLiteralExpression, callName: Name - ): SimpleFunctionDescriptor? { + ): Collection { val memberScopeOfKotlinPackage = module.getPackage(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME).memberScope - return memberScopeOfKotlinPackage.getContributedFunctions(callName, KotlinLookupLocation(expression)).singleOrNull() + return memberScopeOfKotlinPackage.getContributedFunctions(callName, KotlinLookupLocation(expression)) } private fun checkSupportsArrayLiterals(expression: KtCollectionLiteralExpression, context: ExpressionTypingContext) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java index 3a3ab36ef03..310584ede3b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.resolve.calls; import com.intellij.psi.PsiElement; import kotlin.Pair; +import kotlin.collections.CollectionsKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.builtins.FunctionTypesKt; @@ -247,14 +248,19 @@ public class CallResolver { @NotNull ExpressionTypingContext context, @NotNull KtCollectionLiteralExpression expression, @NotNull Call call, - @NotNull FunctionDescriptor functionDescriptor + @NotNull Collection functionDescriptors ) { BasicCallResolutionContext callResolutionContext = BasicCallResolutionContext.create(context, call, CheckArgumentTypesMode.CHECK_VALUE_ARGUMENTS); - ResolutionCandidate candidate = ResolutionCandidate.create( - call, functionDescriptor, null, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null); + List> candidates = CollectionsKt.map(functionDescriptors, descriptor -> + ResolutionCandidate.create( + call, + descriptor, + null, + ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, + null)); return computeTasksFromCandidatesAndResolvedCall( - callResolutionContext, Collections.singleton(candidate), TracingStrategyImpl.create(expression, call)); + callResolutionContext, candidates, TracingStrategyImpl.create(expression, call)); } @NotNull diff --git a/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/build.log.expected b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/build.log.expected new file mode 100644 index 00000000000..b36190b633b --- /dev/null +++ b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/build.log.expected @@ -0,0 +1,32 @@ +OUT: +Buildfile: [TestData]/build.xml + +build: + [mkdir] Created dir: [Temp]/classes + [javac] Compiling 2 source files to [Temp]/classes + [javac] Compiling [[TestData]] => [[Temp]/classes] + [javac] [TestData]/literals.kt:6:9: error: overload resolution ambiguity: + [javac] public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt + [javac] public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt + [javac] @AnnInt([1, 2]) + [javac] ^ + [javac] [TestData]/literals.kt:9:9: error: overload resolution ambiguity: + [javac] public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt + [javac] public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt + [javac] @AnnInt(intArrayOf(1, 2)) + [javac] ^ + [javac] [TestData]/myArrayOf.kt:3:1: error: conflicting overloads: public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt, public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt + [javac] public fun intArrayOf(vararg elements: Int): IntArray = TODO() + [javac] ^ + [javac] [TestData]/myArrayOf.kt:4:1: error: conflicting overloads: public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt, public fun intArrayOf(vararg elements: Int): IntArray defined in kotlin in file myArrayOf.kt + [javac] public fun intArrayOf(vararg elements: Int): IntArray = TODO() + [javac] ^ + +ERR: + +BUILD FAILED +[TestData]/build.xml:6: Compile failed; see the compiler error output for details. + +Total time: [time] + +Return code: 1 diff --git a/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/build.xml b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/build.xml new file mode 100644 index 00000000000..dfc95db6187 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/build.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/literals.kt b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/literals.kt new file mode 100644 index 00000000000..7010f455a21 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/literals.kt @@ -0,0 +1,15 @@ +package lit + +annotation class AnnInt(val a: IntArray) + +@Suppress("UNSUPPORTED_FEATURE") +@AnnInt([1, 2]) +fun foo() {} + +@AnnInt(intArrayOf(1, 2)) +fun bar() {} + +fun main(args: Array) { + foo() + bar() +} \ No newline at end of file diff --git a/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/myArrayOf.kt b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/myArrayOf.kt new file mode 100644 index 00000000000..b8a6327d432 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/myArrayOf.kt @@ -0,0 +1,4 @@ +package kotlin + +public fun intArrayOf(vararg elements: Int): IntArray = TODO() +public fun intArrayOf(vararg elements: Int): IntArray = TODO() \ No newline at end of file diff --git a/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/build.log.expected b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/build.log.expected new file mode 100644 index 00000000000..86ebaf82fbc --- /dev/null +++ b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/build.log.expected @@ -0,0 +1,17 @@ +OUT: +Buildfile: [TestData]/build.xml + +build: + [mkdir] Created dir: [Temp]/classes + [javac] Compiling 2 source files to [Temp]/classes + [javac] Compiling [[TestData]] => [[Temp]/classes] + [javac] [TestData]/myArrayOf.kt:3:30: warning: parameter 'elements' is never used + [javac] public fun intArrayOf(vararg elements: Int): IntArray = TODO() + [javac] ^ + [javac] Running javac... + [jar] Building jar: [Temp]/literals.jar + +BUILD SUCCESSFUL +Total time: [time] + +Return code: 0 diff --git a/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/build.xml b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/build.xml new file mode 100644 index 00000000000..dfc95db6187 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/build.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/literals.kt b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/literals.kt new file mode 100644 index 00000000000..7010f455a21 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/literals.kt @@ -0,0 +1,15 @@ +package lit + +annotation class AnnInt(val a: IntArray) + +@Suppress("UNSUPPORTED_FEATURE") +@AnnInt([1, 2]) +fun foo() {} + +@AnnInt(intArrayOf(1, 2)) +fun bar() {} + +fun main(args: Array) { + foo() + bar() +} \ No newline at end of file diff --git a/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/myArrayOf.kt b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/myArrayOf.kt new file mode 100644 index 00000000000..796af30ded5 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/myArrayOf.kt @@ -0,0 +1,3 @@ +package kotlin + +public fun intArrayOf(vararg elements: Int): IntArray = TODO() \ No newline at end of file diff --git a/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/build.log.expected b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/build.log.expected new file mode 100644 index 00000000000..805d02a9e6a --- /dev/null +++ b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/build.log.expected @@ -0,0 +1,22 @@ +OUT: +Buildfile: [TestData]/build.xml + +build: + [mkdir] Created dir: [Temp]/classes + [javac] Compiling 2 source files to [Temp]/classes + [javac] Compiling [[TestData]] => [[Temp]/classes] + [javac] [TestData]/literals.kt:6:9: error: type mismatch: inferred type is FloatArray but IntArray was expected + [javac] @AnnInt([1, 2]) + [javac] ^ + [javac] [TestData]/literals.kt:9:9: error: type mismatch: inferred type is FloatArray but IntArray was expected + [javac] @AnnInt(intArrayOf(1, 2)) + [javac] ^ + +ERR: + +BUILD FAILED +[TestData]/build.xml:6: Compile failed; see the compiler error output for details. + +Total time: [time] + +Return code: 1 diff --git a/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/build.xml b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/build.xml new file mode 100644 index 00000000000..dfc95db6187 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/build.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/literals.kt b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/literals.kt new file mode 100644 index 00000000000..7010f455a21 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/literals.kt @@ -0,0 +1,15 @@ +package lit + +annotation class AnnInt(val a: IntArray) + +@Suppress("UNSUPPORTED_FEATURE") +@AnnInt([1, 2]) +fun foo() {} + +@AnnInt(intArrayOf(1, 2)) +fun bar() {} + +fun main(args: Array) { + foo() + bar() +} \ No newline at end of file diff --git a/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/myArrayOf.kt b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/myArrayOf.kt new file mode 100644 index 00000000000..b8a8d3afda3 --- /dev/null +++ b/compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/myArrayOf.kt @@ -0,0 +1,3 @@ +package kotlin + +public fun intArrayOf(vararg elements: Int): FloatArray = TODO() \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskTestGenerated.java index a3d28f41344..20768ce7bdc 100644 --- a/compiler/tests/org/jetbrains/kotlin/integration/AntTaskTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/integration/AntTaskTestGenerated.java @@ -120,6 +120,12 @@ public class AntTaskTestGenerated extends AbstractAntTaskTest { doTest(fileName); } + @TestMetadata("overloadResolutionOnCollectionLiteral") + public void testOverloadResolutionOnCollectionLiteral() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/integration/ant/jvm/overloadResolutionOnCollectionLiteral/"); + doTest(fileName); + } + @TestMetadata("stdlibForJavacWithNoKotlin") public void testStdlibForJavacWithNoKotlin() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/integration/ant/jvm/stdlibForJavacWithNoKotlin/"); @@ -132,6 +138,12 @@ public class AntTaskTestGenerated extends AbstractAntTaskTest { doTest(fileName); } + @TestMetadata("twoStdlibForCollectionLiterals") + public void testTwoStdlibForCollectionLiterals() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/integration/ant/jvm/twoStdlibForCollectionLiterals/"); + doTest(fileName); + } + @TestMetadata("valWithInvoke") public void testValWithInvoke() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/integration/ant/jvm/valWithInvoke/"); @@ -161,4 +173,10 @@ public class AntTaskTestGenerated extends AbstractAntTaskTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/integration/ant/jvm/withKotlinNoJavaSources/"); doTest(fileName); } + + @TestMetadata("wrongCallForCollectionLiteral") + public void testWrongCallForCollectionLiteral() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/integration/ant/jvm/wrongCallForCollectionLiteral/"); + doTest(fileName); + } }