[NI] Don't loose diagnostic after type variable fixation

#KT-24488 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-08-19 01:51:04 +03:00
parent ca8da22569
commit e0fb586aaf
15 changed files with 77 additions and 22 deletions
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
import org.jetbrains.kotlin.resolve.calls.inference.addSubtypeConstraintIfCompatible
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.model.Constraint
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage.Empty.hasContradiction
import org.jetbrains.kotlin.resolve.calls.inference.model.ExpectedTypeConstraintPosition
import org.jetbrains.kotlin.resolve.calls.model.*
@@ -19,6 +20,7 @@ import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
import org.jetbrains.kotlin.types.model.defaultType
import org.jetbrains.kotlin.types.model.isIntegerLiteralTypeConstructor
import org.jetbrains.kotlin.types.model.typeConstructor
import org.jetbrains.kotlin.types.typeUtil.contains
@@ -41,7 +41,7 @@ class KotlinConstraintSystemCompleter(
// mutable operations
fun addError(error: KotlinCallDiagnostic)
fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker)
fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?)
}
fun runCompletion(
@@ -87,7 +87,7 @@ class KotlinConstraintSystemCompleter(
val variableWithConstraints = c.notFixedTypeVariables.getValue(variableForFixation.variable)
if (variableForFixation.hasProperConstraint)
fixVariable(c, topLevelType, variableWithConstraints, postponedKtPrimitives)
fixVariable(c, topLevelType, variableWithConstraints, postponedKtPrimitives, topLevelAtoms)
else
processVariableWhenNotEnoughInformation(c, variableWithConstraints, topLevelAtoms)
@@ -206,19 +206,22 @@ class KotlinConstraintSystemCompleter(
c: Context,
topLevelType: UnwrappedType,
variableWithConstraints: VariableWithConstraints,
postponedResolveKtPrimitives: List<PostponedResolvedAtom>
postponedResolveKtPrimitives: List<PostponedResolvedAtom>,
topLevelAtoms: List<ResolvedAtom>
) {
val direction = TypeVariableDirectionCalculator(c, postponedResolveKtPrimitives, topLevelType).getDirection(variableWithConstraints)
fixVariable(c, variableWithConstraints, direction)
fixVariable(c, variableWithConstraints, direction, topLevelAtoms)
}
fun fixVariable(
c: Context,
variableWithConstraints: VariableWithConstraints,
direction: TypeVariableDirectionCalculator.ResolveDirection
direction: TypeVariableDirectionCalculator.ResolveDirection,
topLevelAtoms: List<ResolvedAtom>
) {
val resultType = resultTypeResolver.findResultType(c, variableWithConstraints, direction)
c.fixVariable(variableWithConstraints.typeVariable, resultType)
val resolvedAtom = findResolvedAtomBy(variableWithConstraints.typeVariable, topLevelAtoms) ?: topLevelAtoms.firstOrNull()
c.fixVariable(variableWithConstraints.typeVariable, resultType, resolvedAtom)
}
private fun processVariableWhenNotEnoughInformation(
@@ -238,7 +241,7 @@ class KotlinConstraintSystemCompleter(
else
ErrorUtils.createErrorType("Cannot infer type variable $typeVariable")
c.fixVariable(typeVariable, resultErrorType)
c.fixVariable(typeVariable, resultErrorType, resolvedAtom)
}
private fun findResolvedAtomBy(typeVariable: TypeVariableMarker, topLevelAtoms: List<ResolvedAtom>): ResolvedAtom? {
@@ -53,7 +53,7 @@ class ReceiverConstraintPosition(val argument: KotlinCallArgument) : ConstraintP
override fun toString() = "Receiver $argument"
}
class FixVariableConstraintPosition(val variable: TypeVariableMarker) : ConstraintPosition() {
class FixVariableConstraintPosition(val variable: TypeVariableMarker, val resolvedAtom: ResolvedAtom?) : ConstraintPosition() {
override fun toString() = "Fix variable $variable"
}
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintS
import org.jetbrains.kotlin.resolve.calls.inference.components.ResultTypeResolver
import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic
import org.jetbrains.kotlin.resolve.calls.model.OnlyInputTypesDiagnostic
import org.jetbrains.kotlin.resolve.calls.model.ResolvedAtom
import org.jetbrains.kotlin.types.IntersectionTypeConstructor
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
@@ -247,10 +248,13 @@ class NewConstraintSystemImpl(
}
// KotlinConstraintSystemCompleter.Context
override fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker) {
override fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?) {
checkState(State.BUILDING, State.COMPLETION)
constraintInjector.addInitialEqualityConstraint(this, variable.defaultType(), resultType, FixVariableConstraintPosition(variable))
constraintInjector.addInitialEqualityConstraint(
this, variable.defaultType(), resultType, FixVariableConstraintPosition(variable, atom)
)
val variableWithConstraints = notFixedTypeVariables.remove(variable.freshTypeConstructor())
checkOnlyInputTypesAnnotation(variableWithConstraints, resultType)