Fix collection literals resolve in gradle-based projects

#KT-19441 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-08-06 18:42:09 +02:00
parent b24c1bf06c
commit 0b358fb693
15 changed files with 234 additions and 10 deletions
@@ -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<SimpleFunctionDescriptor> {
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) {
@@ -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<FunctionDescriptor> functionDescriptors
) {
BasicCallResolutionContext callResolutionContext = BasicCallResolutionContext.create(context, call, CheckArgumentTypesMode.CHECK_VALUE_ARGUMENTS);
ResolutionCandidate<FunctionDescriptor> candidate = ResolutionCandidate.create(
call, functionDescriptor, null, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
List<ResolutionCandidate<FunctionDescriptor>> 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
@@ -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
@@ -0,0 +1,25 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<mkdir dir="${temp}/classes"/>
<javac srcdir="${test.data}" destdir="${temp}/classes" includeantruntime="false">
<classpath>
<pathelement location="${kotlin.runtime.jar}"/>
</classpath>
<withKotlin>
<compilerarg value="-Xallow-kotlin-package"/>
</withKotlin>
</javac>
<jar destfile="${temp}/literals.jar">
<fileset dir="${temp}/classes"/>
</jar>
<java classname="lit.LiteralsKt" fork="true">
<classpath>
<pathelement location="${temp}/literals.jar"/>
<pathelement location="${kotlin.runtime.jar}"/>
</classpath>
</java>
</target>
</project>
@@ -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<String>) {
foo()
bar()
}
@@ -0,0 +1,4 @@
package kotlin
public fun intArrayOf(vararg elements: Int): IntArray = TODO()
public fun intArrayOf(vararg elements: Int): IntArray = TODO()
@@ -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
@@ -0,0 +1,25 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<mkdir dir="${temp}/classes"/>
<javac srcdir="${test.data}" destdir="${temp}/classes" includeantruntime="false">
<classpath>
<pathelement location="${kotlin.runtime.jar}"/>
</classpath>
<withKotlin>
<compilerarg value="-Xallow-kotlin-package"/>
</withKotlin>
</javac>
<jar destfile="${temp}/literals.jar">
<fileset dir="${temp}/classes"/>
</jar>
<java classname="lit.LiteralsKt" fork="true">
<classpath>
<pathelement location="${temp}/literals.jar"/>
<pathelement location="${kotlin.runtime.jar}"/>
</classpath>
</java>
</target>
</project>
@@ -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<String>) {
foo()
bar()
}
@@ -0,0 +1,3 @@
package kotlin
public fun intArrayOf(vararg elements: Int): IntArray = TODO()
@@ -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
@@ -0,0 +1,25 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<mkdir dir="${temp}/classes"/>
<javac srcdir="${test.data}" destdir="${temp}/classes" includeantruntime="false">
<classpath>
<pathelement location="${kotlin.runtime.jar}"/>
</classpath>
<withKotlin>
<compilerarg value="-Xallow-kotlin-package"/>
</withKotlin>
</javac>
<jar destfile="${temp}/literals.jar">
<fileset dir="${temp}/classes"/>
</jar>
<java classname="lit.LiteralsKt" fork="true">
<classpath>
<pathelement location="${temp}/literals.jar"/>
<pathelement location="${kotlin.runtime.jar}"/>
</classpath>
</java>
</target>
</project>
@@ -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<String>) {
foo()
bar()
}
@@ -0,0 +1,3 @@
package kotlin
public fun intArrayOf(vararg elements: Int): FloatArray = TODO()
@@ -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);
}
}