[NI] Add support ExpectedTypeFromCast to new inference. #KT-30405 Fixed

This commit is contained in:
Dmitriy Novozhilov
2019-03-14 14:14:28 +03:00
parent 9062811231
commit 1c92c22dee
12 changed files with 123 additions and 48 deletions
@@ -49,6 +49,8 @@ interface KotlinResolutionCallbacks {
fun isCompileTimeConstant(resolvedAtom: ResolvedCallAtom, expectedType: UnwrappedType): Boolean
val inferenceSession: InferenceSession
fun getExpectedTypeFromAsExpressionAndRecordItInTrace(resolvedAtom: ResolvedCallAtom): UnwrappedType?
}
interface SamConversionTransformer {
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.resolve.calls.components
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
@@ -42,6 +43,7 @@ class KotlinCallCompleter(
val returnType = candidate.returnTypeWithSmartCastInfo(resolutionCallbacks)
candidate.addExpectedTypeConstraint(returnType, expectedType, resolutionCallbacks)
candidate.addExpectedTypeFromCastConstraint(returnType, resolutionCallbacks)
return if (resolutionCallbacks.inferenceSession.shouldRunCompletion(candidate))
candidate.runCompletion(
@@ -155,6 +157,16 @@ class KotlinCallCompleter(
}
}
private fun KotlinResolutionCandidate.addExpectedTypeFromCastConstraint(
returnType: UnwrappedType?,
resolutionCallbacks: KotlinResolutionCallbacks
) {
if (!callComponents.languageVersionSettings.supportsFeature(LanguageFeature.ExpectedTypeFromCast)) return
if (returnType == null) return
val expectedType = resolutionCallbacks.getExpectedTypeFromAsExpressionAndRecordItInTrace(resolvedCall) ?: return
csBuilder.addSubtypeConstraint(returnType, expectedType, ExpectedTypeConstraintPosition(resolvedCall.atom))
}
private fun KotlinResolutionCandidate.computeCompletionMode(
expectedType: UnwrappedType?,
currentReturnType: UnwrappedType?