[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 // TODO
return false 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.components.TrivialConstraintTypeInferenceOracle
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
import org.jetbrains.kotlin.types.AbstractTypeApproximator import org.jetbrains.kotlin.types.AbstractTypeApproximator
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
class InferenceComponents( class InferenceComponents(
val ctx: ConeInferenceContext, val ctx: ConeInferenceContext,
@@ -27,7 +26,7 @@ class InferenceComponents(
val approximator: AbstractTypeApproximator = object : AbstractTypeApproximator(ctx) {} val approximator: AbstractTypeApproximator = object : AbstractTypeApproximator(ctx) {}
val trivialConstraintTypeInferenceOracle = TrivialConstraintTypeInferenceOracle.create(ctx) val trivialConstraintTypeInferenceOracle = TrivialConstraintTypeInferenceOracle.create(ctx)
private val incorporator = ConstraintIncorporator(approximator, trivialConstraintTypeInferenceOracle, ConeConstraintSystemUtilContext) 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) val resultTypeResolver = ResultTypeResolver(approximator, trivialConstraintTypeInferenceOracle)
@set:PrivateForInline @set:PrivateForInline
@@ -101,7 +101,7 @@ private fun StorageComponentContainer.configurePlatformIndependentComponents() {
useImpl<CompilerDeserializationConfiguration>() useImpl<CompilerDeserializationConfiguration>()
useImpl<ClassicTypeSystemContextForCS>() useImpl<ClassicTypeSystemContextForCS>()
useInstance(ClassicConstraintSystemUtilContext) useImpl<ClassicConstraintSystemUtilContext>()
useInstance(ProgressManagerBasedCancellationChecker) 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.NewTypeVariable
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor
import org.jetbrains.kotlin.types.KotlinType 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.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeVariableMarker import org.jetbrains.kotlin.types.model.TypeVariableMarker
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.types.typeUtil.unCapture as unCaptureKotlinType import org.jetbrains.kotlin.types.typeUtil.unCapture as unCaptureKotlinType
object ClassicConstraintSystemUtilContext : ConstraintSystemUtilContext { class ClassicConstraintSystemUtilContext(val kotlinTypeRefiner: KotlinTypeRefiner) : ConstraintSystemUtilContext {
override fun TypeVariableMarker.shouldBeFlexible(): Boolean { override fun TypeVariableMarker.shouldBeFlexible(): Boolean {
return this is TypeVariableFromCallableDescriptor && this.originalTypeParameter.shouldBeFlexible() return this is TypeVariableFromCallableDescriptor && this.originalTypeParameter.shouldBeFlexible()
} }
@@ -32,4 +34,10 @@ object ClassicConstraintSystemUtilContext : ConstraintSystemUtilContext {
if (this !is TypeVariableFromCallableDescriptor) return false if (this !is TypeVariableFromCallableDescriptor) return false
return originalTypeParameter.isReified 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.ConstraintSystemOperation
import org.jetbrains.kotlin.resolve.calls.inference.model.* import org.jetbrains.kotlin.resolve.calls.inference.model.*
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind.* import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind.*
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.AbstractTypeApproximator
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner 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.model.*
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.SmartList
import kotlin.math.max import kotlin.math.max
class ConstraintInjector( class ConstraintInjector(
val constraintIncorporator: ConstraintIncorporator, val constraintIncorporator: ConstraintIncorporator,
val typeApproximator: AbstractTypeApproximator, val typeApproximator: AbstractTypeApproximator
val kotlinTypeRefiner: KotlinTypeRefiner
) { ) {
private val ALLOWED_DEPTH_DELTA_FOR_INCORPORATION = 1 private val ALLOWED_DEPTH_DELTA_FOR_INCORPORATION = 1
@@ -175,12 +175,9 @@ class ConstraintInjector(
return baseContext.prepareType(type) return baseContext.prepareType(type)
} }
@OptIn(TypeRefinement::class)
override fun refineType(type: KotlinTypeMarker): KotlinTypeMarker { override fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
return if (type is KotlinType) { return with(constraintIncorporator.utilContext) {
kotlinTypeRefiner.refineType(type) type.refineType()
} else {
type
} }
} }
@@ -200,12 +197,13 @@ class ConstraintInjector(
if (!isSubtypeOf(upperType)) { if (!isSubtypeOf(upperType)) {
// todo improve error reporting -- add information about base types // todo improve error reporting -- add information about base types
if (shouldTryUseDifferentFlexibilityForUpperType && upperType is SimpleType) { if (shouldTryUseDifferentFlexibilityForUpperType && upperType.isSimpleType()) {
/* /*
* Please don't reuse this logic. * Please don't reuse this logic.
* It's necessary to solve constraint systems when flexibility isn't propagated through a type variable. * 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. * 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)) val flexibleUpperType = createFlexibleType(upperType, upperType.withNullability(true))
if (!isSubtypeOf(flexibleUpperType)) { if (!isSubtypeOf(flexibleUpperType)) {
c.addError(NewConstraintError(lowerType, flexibleUpperType, position)) c.addError(NewConstraintError(lowerType, flexibleUpperType, position))
@@ -18,4 +18,5 @@ interface ConstraintSystemUtilContext {
fun TypeVariableMarker.hasOnlyInputTypesAttribute(): Boolean fun TypeVariableMarker.hasOnlyInputTypesAttribute(): Boolean
fun KotlinTypeMarker.unCapture(): KotlinTypeMarker fun KotlinTypeMarker.unCapture(): KotlinTypeMarker
fun TypeVariableMarker.isReified(): Boolean fun TypeVariableMarker.isReified(): Boolean
fun KotlinTypeMarker.refineType(): KotlinTypeMarker
} }