[NI] Get rid of FE 1.0 types in ConstraintInjector

This commit is contained in:
Dmitriy Novozhilov
2020-08-26 13:33:39 +03:00
parent ad039a28bd
commit af0df35623
6 changed files with 25 additions and 15 deletions
@@ -31,4 +31,8 @@ object ConeConstraintSystemUtilContext : ConstraintSystemUtilContext {
// TODO
return false
}
override fun KotlinTypeMarker.refineType(): KotlinTypeMarker {
return this
}
}
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.ResultTypeResolve
import org.jetbrains.kotlin.resolve.calls.inference.components.TrivialConstraintTypeInferenceOracle
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
import org.jetbrains.kotlin.types.AbstractTypeApproximator
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
class InferenceComponents(
val ctx: ConeInferenceContext,
@@ -27,7 +26,7 @@ class InferenceComponents(
val approximator: AbstractTypeApproximator = object : AbstractTypeApproximator(ctx) {}
val trivialConstraintTypeInferenceOracle = TrivialConstraintTypeInferenceOracle.create(ctx)
private val incorporator = ConstraintIncorporator(approximator, trivialConstraintTypeInferenceOracle, ConeConstraintSystemUtilContext)
private val injector = ConstraintInjector(incorporator, approximator, KotlinTypeRefiner.Default)
private val injector = ConstraintInjector(incorporator, approximator)
val resultTypeResolver = ResultTypeResolver(approximator, trivialConstraintTypeInferenceOracle)
@set:PrivateForInline
@@ -101,7 +101,7 @@ private fun StorageComponentContainer.configurePlatformIndependentComponents() {
useImpl<CompilerDeserializationConfiguration>()
useImpl<ClassicTypeSystemContextForCS>()
useInstance(ClassicConstraintSystemUtilContext)
useImpl<ClassicConstraintSystemUtilContext>()
useInstance(ProgressManagerBasedCancellationChecker)
}
@@ -9,11 +9,13 @@ import org.jetbrains.kotlin.resolve.calls.components.CreateFreshVariablesSubstit
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeVariableMarker
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.types.typeUtil.unCapture as unCaptureKotlinType
object ClassicConstraintSystemUtilContext : ConstraintSystemUtilContext {
class ClassicConstraintSystemUtilContext(val kotlinTypeRefiner: KotlinTypeRefiner) : ConstraintSystemUtilContext {
override fun TypeVariableMarker.shouldBeFlexible(): Boolean {
return this is TypeVariableFromCallableDescriptor && this.originalTypeParameter.shouldBeFlexible()
}
@@ -32,4 +34,10 @@ object ClassicConstraintSystemUtilContext : ConstraintSystemUtilContext {
if (this !is TypeVariableFromCallableDescriptor) return false
return originalTypeParameter.isReified
}
@OptIn(TypeRefinement::class)
override fun KotlinTypeMarker.refineType(): KotlinTypeMarker {
require(this is KotlinType)
return kotlinTypeRefiner.refineType(this)
}
}
@@ -20,17 +20,17 @@ package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemOperation
import org.jetbrains.kotlin.resolve.calls.inference.model.*
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind.*
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.types.AbstractTypeApproximator
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.utils.SmartList
import kotlin.math.max
class ConstraintInjector(
val constraintIncorporator: ConstraintIncorporator,
val typeApproximator: AbstractTypeApproximator,
val kotlinTypeRefiner: KotlinTypeRefiner
val typeApproximator: AbstractTypeApproximator
) {
private val ALLOWED_DEPTH_DELTA_FOR_INCORPORATION = 1
@@ -175,12 +175,9 @@ class ConstraintInjector(
return baseContext.prepareType(type)
}
@OptIn(TypeRefinement::class)
override fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
return if (type is KotlinType) {
kotlinTypeRefiner.refineType(type)
} else {
type
return with(constraintIncorporator.utilContext) {
type.refineType()
}
}
@@ -200,12 +197,13 @@ class ConstraintInjector(
if (!isSubtypeOf(upperType)) {
// todo improve error reporting -- add information about base types
if (shouldTryUseDifferentFlexibilityForUpperType && upperType is SimpleType) {
if (shouldTryUseDifferentFlexibilityForUpperType && upperType.isSimpleType()) {
/*
* Please don't reuse this logic.
* It's necessary to solve constraint systems when flexibility isn't propagated through a type variable.
* It's OK in the old inference because it uses already substituted types, that are with the correct flexibility.
*/
require(upperType is SimpleTypeMarker)
val flexibleUpperType = createFlexibleType(upperType, upperType.withNullability(true))
if (!isSubtypeOf(flexibleUpperType)) {
c.addError(NewConstraintError(lowerType, flexibleUpperType, position))
@@ -18,4 +18,5 @@ interface ConstraintSystemUtilContext {
fun TypeVariableMarker.hasOnlyInputTypesAttribute(): Boolean
fun KotlinTypeMarker.unCapture(): KotlinTypeMarker
fun TypeVariableMarker.isReified(): Boolean
fun KotlinTypeMarker.refineType(): KotlinTypeMarker
}