From 15b46cdda9bf1317a29107f33fccd2227061a033 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Fri, 23 Mar 2018 05:28:22 +0300 Subject: [PATCH] [NI] Don't perform useless computation for smartcasts --- .../calls/components/KotlinCallCompleter.kt | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) 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 a983e7c9820..027d2522827 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 @@ -120,24 +120,33 @@ class KotlinCallCompleter( expectedType: UnwrappedType?, resolutionCallbacks: KotlinResolutionCallbacks ): ConstraintSystemCompletionMode { - val unsubstitutedReturnType = resolvedCall.candidateDescriptor.returnType?.unwrap() ?: return ConstraintSystemCompletionMode.PARTIAL - val withSmartCastInfo = resolutionCallbacks.createReceiverWithSmartCastInfo(resolvedCall) + if (expectedType != null && TypeUtils.noExpectedType(expectedType)) return ConstraintSystemCompletionMode.FULL - val actualType = withSmartCastInfo?.stableType ?: unsubstitutedReturnType + val returnType = resolvedCall.candidateDescriptor.returnType?.unwrap() ?: return ConstraintSystemCompletionMode.PARTIAL + val substitutedType: UnwrappedType + if (expectedType != null) { + val returnTypeWithSmartCastInfo = computeReturnTypeWithSmartCastInfo(returnType, resolutionCallbacks) + substitutedType = resolvedCall.substitutor.substituteKeepAnnotations(returnTypeWithSmartCastInfo) - val returnType = resolvedCall.substitutor.substituteKeepAnnotations(actualType) - if (expectedType != null && !TypeUtils.noExpectedType(expectedType) && !resolutionCallbacks.isCompileTimeConstant( - resolvedCall, - expectedType - )) { - csBuilder.addSubtypeConstraint(returnType, expectedType, ExpectedTypeConstraintPosition(resolvedCall.atom)) - } - - return if (expectedType != null || csBuilder.isProperType(returnType)) { - ConstraintSystemCompletionMode.FULL + if (!resolutionCallbacks.isCompileTimeConstant(resolvedCall, expectedType)) { + csBuilder.addSubtypeConstraint(substitutedType, expectedType, ExpectedTypeConstraintPosition(resolvedCall.atom)) + } } else { - ConstraintSystemCompletionMode.PARTIAL + substitutedType = resolvedCall.substitutor.substituteKeepAnnotations(returnType) } + + return if (expectedType != null || csBuilder.isProperType(substitutedType)) + ConstraintSystemCompletionMode.FULL + else + ConstraintSystemCompletionMode.PARTIAL + } + + private fun KotlinResolutionCandidate.computeReturnTypeWithSmartCastInfo( + returnType: UnwrappedType, + resolutionCallbacks: KotlinResolutionCallbacks + ): UnwrappedType { + if (resolvedCall.atom.callKind != KotlinCallKind.VARIABLE) return returnType + return resolutionCallbacks.createReceiverWithSmartCastInfo(resolvedCall)?.stableType ?: returnType } private fun KotlinResolutionCandidate?.asCallResolutionResult(