[NI] Don't perform useless computation for smartcasts

This commit is contained in:
Mikhail Zarechenskiy
2018-03-23 05:28:22 +03:00
parent e06c13732f
commit 15b46cdda9
@@ -120,24 +120,33 @@ class KotlinCallCompleter(
expectedType: UnwrappedType?, expectedType: UnwrappedType?,
resolutionCallbacks: KotlinResolutionCallbacks resolutionCallbacks: KotlinResolutionCallbacks
): ConstraintSystemCompletionMode { ): ConstraintSystemCompletionMode {
val unsubstitutedReturnType = resolvedCall.candidateDescriptor.returnType?.unwrap() ?: return ConstraintSystemCompletionMode.PARTIAL if (expectedType != null && TypeUtils.noExpectedType(expectedType)) return ConstraintSystemCompletionMode.FULL
val withSmartCastInfo = resolutionCallbacks.createReceiverWithSmartCastInfo(resolvedCall)
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 (!resolutionCallbacks.isCompileTimeConstant(resolvedCall, expectedType)) {
if (expectedType != null && !TypeUtils.noExpectedType(expectedType) && !resolutionCallbacks.isCompileTimeConstant( csBuilder.addSubtypeConstraint(substitutedType, expectedType, ExpectedTypeConstraintPosition(resolvedCall.atom))
resolvedCall, }
expectedType
)) {
csBuilder.addSubtypeConstraint(returnType, expectedType, ExpectedTypeConstraintPosition(resolvedCall.atom))
}
return if (expectedType != null || csBuilder.isProperType(returnType)) {
ConstraintSystemCompletionMode.FULL
} else { } 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( private fun KotlinResolutionCandidate?.asCallResolutionResult(