From bc2a8555a3f281e120a502aba58be666438fbd85 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Wed, 7 Jun 2017 16:17:29 +0300 Subject: [PATCH] [NI] Resolve collection literals arguments as postponed ones --- .../tower/KotlinResolutionCallbacksImpl.kt | 12 ++++++++++++ .../resolve/calls/tower/NewCallArguments.kt | 11 +++++++++++ .../resolve/calls/tower/PSICallResolver.kt | 5 +++++ .../components/CheckArgumentsResolutionPart.kt | 11 +++++++++++ .../calls/components/ExternalComponents.kt | 1 + .../calls/components/KotlinCallCompleter.kt | 4 ++++ .../calls/inference/ConstraintSystemBuilder.kt | 4 +++- .../calls/inference/model/ConstraintStorage.kt | 9 ++++----- .../model/MutableConstraintStorage.kt | 8 +++----- .../inference/model/NewConstraintSystemImpl.kt | 18 +++++++++++++----- .../resolve/calls/model/KotlinCallArguments.kt | 1 + .../calls/model/ResolvedCallElements.kt | 11 ++++++++++- 12 files changed, 78 insertions(+), 17 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt index a7fda827304..cb8c3a24a53 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt @@ -194,4 +194,16 @@ class KotlinResolutionCallbacksImpl( doubleColonExpressionResolver.checkReferenceIsToAllowedMember(callableCandidate.candidate, topLevelCallContext.trace, callableReferenceExpression) } + + override fun completeCollectionLiteralCalls(collectionLiteralArgument: ResolvedCollectionLiteralArgument) { + val psiCallArgument = collectionLiteralArgument.argument.psiCallArgument as CollectionLiteralKotlinCallArgumentImpl + val context = psiCallArgument.outerCallContext + + val actualContext = context + .replaceBindingTrace(trace) + .replaceExpectedType(collectionLiteralArgument.expectedType) + .replaceContextDependency(ContextDependency.INDEPENDENT) + + expressionTypingServices.getTypeInfo(psiCallArgument.collectionLiteralExpression, actualContext) + } } \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt index 7823fa0d993..c8eee6d2d95 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt @@ -115,6 +115,17 @@ class CallableReferenceKotlinCallArgumentImpl( override val rhsName: Name ) : CallableReferenceKotlinCallArgument, PSIKotlinCallArgument() +class CollectionLiteralKotlinCallArgumentImpl( + override val valueArgument: ValueArgument, + override val argumentName: Name?, + override val dataFlowInfoBeforeThisArgument: DataFlowInfo, + override val dataFlowInfoAfterThisArgument: DataFlowInfo, + val collectionLiteralExpression: KtCollectionLiteralExpression, + val outerCallContext: BasicCallResolutionContext +) : CollectionLiteralKotlinCallArgument, PSIKotlinCallArgument() { + override val isSpread: Boolean get() = valueArgument.getSpreadElement() != null +} + class SubKotlinCallArgumentImpl( override val valueArgument: ValueArgument, override val dataFlowInfoBeforeThisArgument: DataFlowInfo, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt index 81621e23baa..f42d6a6ca90 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt @@ -510,6 +510,11 @@ class PSICallResolver( return lambdaArgument } + if (ktExpression is KtCollectionLiteralExpression) { + return CollectionLiteralKotlinCallArgumentImpl( + valueArgument, argumentName, startDataFlowInfo, startDataFlowInfo, ktExpression, outerCallContext) + } + val context = outerCallContext.replaceContextDependency(ContextDependency.DEPENDENT) .replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE).replaceDataFlowInfo(startDataFlowInfo) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CheckArgumentsResolutionPart.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CheckArgumentsResolutionPart.kt index 55975b80fb6..fa4cf8295cc 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CheckArgumentsResolutionPart.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CheckArgumentsResolutionPart.kt @@ -78,6 +78,7 @@ internal object CheckArguments : ResolutionPart { checkCallableExpectedType(csBuilder, argument, expectedType) } } + is CollectionLiteralKotlinCallArgument -> processCollectionLiteralArgument(kotlinCall, csBuilder, argument, expectedType) else -> error("Incorrect argument type: $argument, ${argument.javaClass.canonicalName}.") } } @@ -180,6 +181,16 @@ internal object CheckArguments : ResolutionPart { return null } + + fun processCollectionLiteralArgument( + kotlinCall: KotlinCall, + csBuilder: ConstraintSystemBuilder, + collectionLiteralArgument: CollectionLiteralKotlinCallArgument, + expectedType: UnwrappedType + ): KotlinCallDiagnostic? { + csBuilder.addCollectionLiteralArgument(ResolvedCollectionLiteralArgument(kotlinCall, collectionLiteralArgument, expectedType)) + return null + } } internal fun checkExpressionArgument( diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt index 0e75637a7fe..cc0e77f0ffb 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt @@ -39,4 +39,5 @@ interface KotlinResolutionCallbacks { fun completeCallableReference(callableReferenceArgument: ResolvedCallableReferenceArgument, resultTypeParameters: List) + fun completeCollectionLiteralCalls(collectionLiteralArgument: ResolvedCollectionLiteralArgument) } \ No newline at end of file diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt index 38bb1c30bfd..e0f1a1c1f52 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt @@ -52,6 +52,7 @@ class KotlinCallCompleter( fun buildResultingSubstitutor(): NewTypeSubstitutor val lambdaArguments: List val callableReferenceArguments: List + val collectionLiteralArguments: List // type can be proper if it not contains not fixed type variables fun canBeProper(type: UnwrappedType): Boolean @@ -121,6 +122,9 @@ class KotlinCallCompleter( c.callableReferenceArguments.forEach { resolutionCallbacks.completeCallableReference(it, it.myTypeVariables.map { currentSubstitutor.safeSubstitute(it.defaultType) }) } + c.collectionLiteralArguments.forEach { + resolutionCallbacks.completeCollectionLiteralCalls(it) + } return ResolvedKotlinCall.CompletedResolvedKotlinCall(completedCall, competedCalls, c.lambdaArguments) } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt index f49af82457b..07b2d55cb74 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstituto import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable import org.jetbrains.kotlin.resolve.calls.model.ResolvedCallableReferenceArgument +import org.jetbrains.kotlin.resolve.calls.model.ResolvedCollectionLiteralArgument import org.jetbrains.kotlin.resolve.calls.model.ResolvedKotlinCall import org.jetbrains.kotlin.resolve.calls.model.ResolvedLambdaArgument import org.jetbrains.kotlin.types.TypeConstructor @@ -41,6 +42,7 @@ interface ConstraintSystemBuilder : ConstraintSystemOperation { fun addInnerCall(innerCall: ResolvedKotlinCall.OnlyResolvedKotlinCall) fun addLambdaArgument(resolvedLambdaArgument: ResolvedLambdaArgument) fun addCallableReferenceArgument(resolvedCallableReferenceArgument: ResolvedCallableReferenceArgument) + fun addCollectionLiteralArgument(collectionLiteralArgument: ResolvedCollectionLiteralArgument) // if runOperations return true, then this operation will be applied, and function return true fun runTransaction(runOperations: ConstraintSystemOperation.() -> Boolean): Boolean diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt index 82f443059ac..58fa552be50 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,7 @@ package org.jetbrains.kotlin.resolve.calls.inference.model import org.jetbrains.kotlin.resolve.calls.inference.substitute -import org.jetbrains.kotlin.resolve.calls.model.ResolvedKotlinCall -import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic -import org.jetbrains.kotlin.resolve.calls.model.ResolvedCallableReferenceArgument -import org.jetbrains.kotlin.resolve.calls.model.ResolvedLambdaArgument +import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.types.TypeConstructor import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.UnwrappedType @@ -57,6 +54,7 @@ interface ConstraintStorage { val fixedTypeVariables: Map val lambdaArguments: List val callableReferenceArguments: List + val collectionLiteralArguments: List val innerCalls: List object Empty : ConstraintStorage { @@ -68,6 +66,7 @@ interface ConstraintStorage { override val fixedTypeVariables: Map get() = emptyMap() override val lambdaArguments: List get() = emptyList() override val callableReferenceArguments: List get() = emptyList() + override val collectionLiteralArguments: List get() = emptyList() override val innerCalls: List get() = emptyList() } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt index 8499d23c144..dbf93ffc467 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,7 @@ package org.jetbrains.kotlin.resolve.calls.inference.model import org.jetbrains.kotlin.resolve.calls.inference.trimToSize -import org.jetbrains.kotlin.resolve.calls.model.ResolvedKotlinCall -import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic -import org.jetbrains.kotlin.resolve.calls.model.ResolvedCallableReferenceArgument -import org.jetbrains.kotlin.resolve.calls.model.ResolvedLambdaArgument +import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.types.TypeConstructor import org.jetbrains.kotlin.types.UnwrappedType import java.util.* @@ -86,5 +83,6 @@ internal class MutableConstraintStorage : ConstraintStorage { override val fixedTypeVariables: MutableMap = LinkedHashMap() override val lambdaArguments: MutableList = ArrayList() override val callableReferenceArguments: MutableList = ArrayList() + override val collectionLiteralArguments: MutableList = ArrayList() override val innerCalls: MutableList = ArrayList() } \ No newline at end of file diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index c50e22f3316..6795e36147d 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,7 @@ package org.jetbrains.kotlin.resolve.calls.inference.model import org.jetbrains.kotlin.resolve.calls.components.KotlinCallCompleter import org.jetbrains.kotlin.resolve.calls.inference.* import org.jetbrains.kotlin.resolve.calls.inference.components.* -import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic -import org.jetbrains.kotlin.resolve.calls.model.ResolvedCallableReferenceArgument -import org.jetbrains.kotlin.resolve.calls.model.ResolvedKotlinCall -import org.jetbrains.kotlin.resolve.calls.model.ResolvedLambdaArgument +import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.tower.isSuccess import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.TypeConstructor @@ -156,6 +153,11 @@ class NewConstraintSystemImpl(val constraintInjector: ConstraintInjector, val re storage.callableReferenceArguments.add(resolvedCallableReferenceArgument) } + override fun addCollectionLiteralArgument(collectionLiteralArgument: ResolvedCollectionLiteralArgument) { + checkState(State.BUILDING, State.COMPLETION) + storage.collectionLiteralArguments.add(collectionLiteralArgument) + } + private fun getVariablesForFixation(): Map { val fixedVariables = LinkedHashMap() @@ -256,6 +258,12 @@ class NewConstraintSystemImpl(val constraintInjector: ConstraintInjector, val re return storage.callableReferenceArguments } + override val collectionLiteralArguments: List + get() { + checkState(State.COMPLETION) + return storage.collectionLiteralArguments + } + // KotlinCallCompleter.Context override fun asResultTypeResolverContext() = apply { checkState(State.COMPLETION) } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallArguments.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallArguments.kt index d9bb79c94da..d2de50397e5 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallArguments.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallArguments.kt @@ -119,6 +119,7 @@ interface CallableReferenceKotlinCallArgument : KotlinCallArgument { val rhsName: Name } +interface CollectionLiteralKotlinCallArgument : KotlinCallArgument interface TypeArgument diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallElements.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallElements.kt index c78ad4d3075..b012d0b07a3 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallElements.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallElements.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,4 +59,13 @@ class ResolvedCallableReferenceArgument( ) : ArgumentWithPostponeResolution() { override val inputTypes: Collection get() = emptyList() override val outputType: UnwrappedType? = null +} + +class ResolvedCollectionLiteralArgument( + override val outerCall: KotlinCall, + override val argument: CollectionLiteralKotlinCallArgument, + val expectedType: UnwrappedType +) : ArgumentWithPostponeResolution() { + override val inputTypes: Collection get() = emptyList() + override val outputType: UnwrappedType? = null } \ No newline at end of file