Introduce frontend independent constraint positions

This commit is contained in:
Dmitriy Novozhilov
2020-08-25 17:30:18 +03:00
parent e5e3d7cab1
commit fae21d4db3
23 changed files with 151 additions and 99 deletions
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.DelegatedPropertyConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.DelegatedPropertyConstraintPositionImpl
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.resolve.calls.tower.ManyCandidatesResolver
import org.jetbrains.kotlin.resolve.calls.tower.PSICallResolver
@@ -54,7 +54,7 @@ class DelegatedPropertyInferenceSession(
val valueParameterForThis = descriptor.valueParameters.getOrNull(0) ?: return
val substitutedType = freshVariablesSubstitutor.safeSubstitute(valueParameterForThis.type.unwrap())
commonSystem.addSubtypeConstraint(typeOfThis.unwrap(), substitutedType, DelegatedPropertyConstraintPosition(atom))
commonSystem.addSubtypeConstraint(typeOfThis.unwrap(), substitutedType, DelegatedPropertyConstraintPositionImpl(atom))
}
private fun ResolvedCallAtom.addConstraintsForGetValueMethod(commonSystem: ConstraintSystemBuilder) {
@@ -62,7 +62,7 @@ class DelegatedPropertyInferenceSession(
val unsubstitutedReturnType = candidateDescriptor.returnType?.unwrap() ?: return
val substitutedReturnType = freshVariablesSubstitutor.safeSubstitute(unsubstitutedReturnType)
commonSystem.addSubtypeConstraint(substitutedReturnType, expectedType, DelegatedPropertyConstraintPosition(atom))
commonSystem.addSubtypeConstraint(substitutedReturnType, expectedType, DelegatedPropertyConstraintPositionImpl(atom))
}
addConstraintForThis(candidateDescriptor, commonSystem)
@@ -73,7 +73,7 @@ class DelegatedPropertyInferenceSession(
val unsubstitutedParameterType = candidateDescriptor.valueParameters.getOrNull(2)?.type?.unwrap() ?: return
val substitutedParameterType = freshVariablesSubstitutor.safeSubstitute(unsubstitutedParameterType)
commonSystem.addSubtypeConstraint(expectedType, substitutedParameterType, DelegatedPropertyConstraintPosition(atom))
commonSystem.addSubtypeConstraint(expectedType, substitutedParameterType, DelegatedPropertyConstraintPositionImpl(atom))
}
addConstraintForThis(candidateDescriptor, commonSystem)
@@ -347,10 +347,10 @@ class DiagnosticReporterByTrackingStrategy(
val position = constraintError.position.from
val argument =
when (position) {
is ArgumentConstraintPosition -> position.argument
is ReceiverConstraintPosition -> position.argument
is LHSArgumentConstraintPosition -> position.argument
is LambdaArgumentConstraintPosition -> position.lambda.atom
is ArgumentConstraintPositionImpl -> position.argument
is ReceiverConstraintPositionImpl -> position.argument
is LHSArgumentConstraintPositionImpl -> position.argument
is LambdaArgumentConstraintPositionImpl -> position.lambda.atom
else -> null
}
argument?.let {
@@ -385,7 +385,7 @@ class DiagnosticReporterByTrackingStrategy(
)
}
(position as? ExpectedTypeConstraintPosition)?.let {
(position as? ExpectedTypeConstraintPositionImpl)?.let {
val call = it.topLevelCall.psiKotlinCall.psiCall.callElement.safeAs<KtExpression>()
val inferredType =
if (!constraintError.lowerKotlinType.isNullableNothing()) constraintError.lowerKotlinType
@@ -401,7 +401,7 @@ class DiagnosticReporterByTrackingStrategy(
}
}
(position as? ExplicitTypeParameterConstraintPosition)?.let {
(position as? ExplicitTypeParameterConstraintPositionImpl)?.let {
val typeArgumentReference = (it.typeArgument as SimpleTypeArgumentImpl).typeReference
trace.report(
UPPER_BOUND_VIOLATED.on(
@@ -412,9 +412,9 @@ class DiagnosticReporterByTrackingStrategy(
)
}
(position as? FixVariableConstraintPosition)?.let {
(position as? FixVariableConstraintPositionImpl)?.let {
val morePreciseDiagnosticExists = allDiagnostics.any { other ->
other is NewConstraintError && other.position.from !is FixVariableConstraintPosition
other is NewConstraintError && other.position.from !is FixVariableConstraintPositionImpl
}
if (morePreciseDiagnosticExists) return
@@ -435,8 +435,8 @@ class DiagnosticReporterByTrackingStrategy(
val capturedError = diagnostic as CapturedTypeFromSubtyping
val position = capturedError.position
val argumentPosition =
position.safeAs<ArgumentConstraintPosition>()
?: position.safeAs<IncorporationConstraintPosition>()?.from.safeAs<ArgumentConstraintPosition>()
position.safeAs<ArgumentConstraintPositionImpl>()
?: position.safeAs<IncorporationConstraintPosition>()?.from.safeAs<ArgumentConstraintPositionImpl>()
argumentPosition?.let {
val expression = it.argument.psiExpression ?: return
@@ -450,7 +450,7 @@ class DiagnosticReporterByTrackingStrategy(
}
NotEnoughInformationForTypeParameter::class.java -> {
val error = diagnostic as NotEnoughInformationForTypeParameter
val error = diagnostic as NotEnoughInformationForTypeParameterImpl
if (allDiagnostics.any {
(it is ConstrainingTypeIsError && it.typeVariable == error.typeVariable)
|| it is NewConstraintError || it is WrongCountOfTypeArguments
@@ -248,7 +248,7 @@ class CoroutineInferenceSession(
for ((variableConstructor, type) in storage.fixedTypeVariables) {
val typeVariable = storage.allTypeVariables.getValue(variableConstructor)
commonSystem.registerVariable(typeVariable)
commonSystem.addEqualityConstraint((typeVariable as NewTypeVariable).defaultType, type, CoroutinePosition())
commonSystem.addEqualityConstraint((typeVariable as NewTypeVariable).defaultType, type, CoroutinePosition)
introducedConstraint = true
}
}
@@ -909,7 +909,7 @@ class NewResolvedCallImpl<D : CallableDescriptor>(
is CapturedTypeFromSubtyping -> it.position.originalPosition()
is ConstrainingTypeIsError -> it.position.originalPosition()
else -> null
} as? ArgumentConstraintPosition ?: return@forEach
} as? ArgumentConstraintPositionImpl ?: return@forEach
val argument = position.argument.safeAs<PSIKotlinCallArgument>()?.valueArgument ?: return@forEach
result += argument to it