Wire proper KotlinTypeRefiner into ClassicTypeSystemCotextForCS

^KT-48589 Fixed
This commit is contained in:
Dmitry Savvinov
2021-09-03 18:19:46 +03:00
parent 9e50f40586
commit d6cb96aa24
8 changed files with 42 additions and 19 deletions
@@ -63,7 +63,7 @@ class BuilderInferenceSession(
private var nestedBuilderInferenceSessions: MutableSet<BuilderInferenceSession> = mutableSetOf() private var nestedBuilderInferenceSessions: MutableSet<BuilderInferenceSession> = mutableSetOf()
private lateinit var lambda: ResolvedLambdaAtom private lateinit var lambda: ResolvedLambdaAtom
private val commonSystem = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns) private val commonSystem = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns, callComponents.kotlinTypeRefiner)
init { init {
if (topLevelCallContext.inferenceSession is BuilderInferenceSession) { if (topLevelCallContext.inferenceSession is BuilderInferenceSession) {
@@ -40,12 +40,15 @@ import org.jetbrains.kotlin.resolve.calls.model.SimpleKotlinCallArgument
import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem
import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeRefinerImpl
import org.jetbrains.kotlin.types.TypeIntersector import org.jetbrains.kotlin.types.TypeIntersector
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class KotlinResolutionStatelessCallbacksImpl( class KotlinResolutionStatelessCallbacksImpl(
private val deprecationResolver: DeprecationResolver, private val deprecationResolver: DeprecationResolver,
private val languageVersionSettings: LanguageVersionSettings, private val languageVersionSettings: LanguageVersionSettings,
private val kotlinTypeRefiner: KotlinTypeRefiner
) : KotlinResolutionStatelessCallbacks { ) : KotlinResolutionStatelessCallbacks {
override fun isDescriptorFromSource(descriptor: CallableDescriptor) = override fun isDescriptorFromSource(descriptor: CallableDescriptor) =
DescriptorToSourceUtils.descriptorToDeclaration(descriptor) != null DescriptorToSourceUtils.descriptorToDeclaration(descriptor) != null
@@ -107,10 +110,10 @@ class KotlinResolutionStatelessCallbacksImpl(
} }
override fun createConstraintSystemForOverloadResolution( override fun createConstraintSystemForOverloadResolution(
constraintInjector: ConstraintInjector, builtIns: KotlinBuiltIns, constraintInjector: ConstraintInjector, builtIns: KotlinBuiltIns
): SimpleConstraintSystem { ): SimpleConstraintSystem {
return if (languageVersionSettings.getFlag(AnalysisFlags.constraintSystemForOverloadResolution).forNewInference()) return if (languageVersionSettings.getFlag(AnalysisFlags.constraintSystemForOverloadResolution).forNewInference())
SimpleConstraintSystemImpl(constraintInjector, builtIns) SimpleConstraintSystemImpl(constraintInjector, builtIns, kotlinTypeRefiner)
else else
ConstraintSystemBuilderImpl.forSpecificity() ConstraintSystemBuilderImpl.forSpecificity()
} }
@@ -109,7 +109,7 @@ abstract class ManyCandidatesResolver<D : CallableDescriptor>(
for (callInfo in listOf(goodCandidate, badCandidate)) { for (callInfo in listOf(goodCandidate, badCandidate)) {
val atomsToAnalyze = mutableListOf<ResolvedAtom>(callInfo.callResolutionResult) val atomsToAnalyze = mutableListOf<ResolvedAtom>(callInfo.callResolutionResult)
val system = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns).apply { val system = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns, callComponents.kotlinTypeRefiner).apply {
addOtherSystem(callInfo.callResolutionResult.constraintSystem) addOtherSystem(callInfo.callResolutionResult.constraintSystem)
/* /*
* This is needed for very stupid case, when we have some delegate with good `getValue` and bad `setValue` that * This is needed for very stupid case, when we have some delegate with good `getValue` and bad `setValue` that
@@ -144,7 +144,7 @@ abstract class ManyCandidatesResolver<D : CallableDescriptor>(
) )
} }
} else { } else {
val commonSystem = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns).apply { val commonSystem = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns, callComponents.kotlinTypeRefiner).apply {
addOtherSystem(currentConstraintSystem()) addOtherSystem(currentConstraintSystem())
} }
@@ -16,13 +16,14 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImp
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.TypeVariableTypeConstructor import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstructor
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext import org.jetbrains.kotlin.types.checker.*
import org.jetbrains.kotlin.types.checker.NewCapturedType
import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor
import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.cast
class ClassicTypeSystemContextForCS(override val builtIns: KotlinBuiltIns) : TypeSystemInferenceExtensionContextDelegate, class ClassicTypeSystemContextForCS(
override val builtIns: KotlinBuiltIns,
val kotlinTypeRefiner: KotlinTypeRefiner
) : TypeSystemInferenceExtensionContextDelegate,
ClassicTypeSystemContext, ClassicTypeSystemContext,
BuiltInsProvider { BuiltInsProvider {
@@ -97,6 +98,15 @@ class ClassicTypeSystemContextForCS(override val builtIns: KotlinBuiltIns) : Typ
require(this is TypeVariableTypeConstructor) require(this is TypeVariableTypeConstructor)
return isContainedInInvariantOrContravariantPositions return isContainedInInvariantOrContravariantPositions
} }
override fun newTypeCheckerState(errorTypesEqualToAnything: Boolean, stubTypesEqualToAnything: Boolean): TypeCheckerState {
return createClassicTypeCheckerState(
errorTypesEqualToAnything,
stubTypesEqualToAnything,
typeSystemContext = this,
kotlinTypeRefiner = kotlinTypeRefiner
)
}
} }
@@ -109,7 +119,8 @@ private inline fun Any?.errorMessage(): String {
@Suppress("FunctionName") @Suppress("FunctionName")
fun NewConstraintSystemImpl( fun NewConstraintSystemImpl(
constraintInjector: ConstraintInjector, constraintInjector: ConstraintInjector,
builtIns: KotlinBuiltIns builtIns: KotlinBuiltIns,
kotlinTypeRefiner: KotlinTypeRefiner
): NewConstraintSystemImpl { ): NewConstraintSystemImpl {
return NewConstraintSystemImpl(constraintInjector, ClassicTypeSystemContextForCS(builtIns)) return NewConstraintSystemImpl(constraintInjector, ClassicTypeSystemContextForCS(builtIns, kotlinTypeRefiner))
} }
@@ -26,11 +26,16 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallab
import org.jetbrains.kotlin.resolve.calls.inference.substitute import org.jetbrains.kotlin.resolve.calls.inference.substitute
import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem
import org.jetbrains.kotlin.types.TypeConstructorSubstitution import org.jetbrains.kotlin.types.TypeConstructorSubstitution
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, builtIns: KotlinBuiltIns) : SimpleConstraintSystem { class SimpleConstraintSystemImpl(
val system = NewConstraintSystemImpl(constraintInjector, ClassicTypeSystemContextForCS(builtIns)) constraintInjector: ConstraintInjector,
builtIns: KotlinBuiltIns,
kotlinTypeRefiner: KotlinTypeRefiner
) : SimpleConstraintSystem {
val system = NewConstraintSystemImpl(constraintInjector, ClassicTypeSystemContextForCS(builtIns, kotlinTypeRefiner))
val csBuilder: ConstraintSystemBuilder = val csBuilder: ConstraintSystemBuilder =
system.getBuilder() system.getBuilder()
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.sam.SamConversionResolver
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo
import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
import org.jetbrains.kotlin.types.isDynamic import org.jetbrains.kotlin.types.isDynamic
@@ -49,7 +50,8 @@ class KotlinCallComponents(
val samConversionOracle: SamConversionOracle, val samConversionOracle: SamConversionOracle,
val samConversionResolver: SamConversionResolver, val samConversionResolver: SamConversionResolver,
val kotlinTypeChecker: NewKotlinTypeChecker, val kotlinTypeChecker: NewKotlinTypeChecker,
val lookupTracker: LookupTracker val lookupTracker: LookupTracker,
val kotlinTypeRefiner: KotlinTypeRefiner,
) )
class SimpleCandidateFactory( class SimpleCandidateFactory(
@@ -64,7 +66,7 @@ class SimpleCandidateFactory(
val baseSystem: ConstraintStorage val baseSystem: ConstraintStorage
init { init {
val baseSystem = NewConstraintSystemImpl(callComponents.constraintInjector, callComponents.builtIns) val baseSystem = NewConstraintSystemImpl(callComponents.constraintInjector, callComponents.builtIns, callComponents.kotlinTypeRefiner)
if (!inferenceSession.resolveReceiverIndependently()) { if (!inferenceSession.resolveReceiverIndependently()) {
baseSystem.addSubsystemFromArgument(kotlinCall.explicitReceiver) baseSystem.addSubsystemFromArgument(kotlinCall.explicitReceiver)
baseSystem.addSubsystemFromArgument(kotlinCall.dispatchReceiverForInvokeExtension) baseSystem.addSubsystemFromArgument(kotlinCall.dispatchReceiverForInvokeExtension)
@@ -243,4 +245,3 @@ class GivenCandidate(
val dispatchReceiver: ReceiverValueWithSmartCastInfo?, val dispatchReceiver: ReceiverValueWithSmartCastInfo?,
val knownTypeParametersResultingSubstitutor: TypeSubstitutor? val knownTypeParametersResultingSubstitutor: TypeSubstitutor?
) )
@@ -92,7 +92,7 @@ class KotlinResolutionCandidate(
fun getSystem(): NewConstraintSystem { fun getSystem(): NewConstraintSystem {
if (newSystem == null) { if (newSystem == null) {
newSystem = NewConstraintSystemImpl(callComponents.constraintInjector, callComponents.builtIns) newSystem = NewConstraintSystemImpl(callComponents.constraintInjector, callComponents.builtIns, callComponents.kotlinTypeRefiner)
newSystem!!.addOtherSystem(baseSystem) newSystem!!.addOtherSystem(baseSystem)
} }
return newSystem!! return newSystem!!
@@ -20,11 +20,15 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.resolve.calls.components.ClassicTypeSystemContextForCS import org.jetbrains.kotlin.resolve.calls.components.ClassicTypeSystemContextForCS
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
class TypeApproximator( class TypeApproximator(
builtIns: KotlinBuiltIns, builtIns: KotlinBuiltIns,
languageVersionSettings: LanguageVersionSettings, languageVersionSettings: LanguageVersionSettings,
) : AbstractTypeApproximator(ClassicTypeSystemContextForCS(builtIns), languageVersionSettings) { ) : AbstractTypeApproximator(
ClassicTypeSystemContextForCS(builtIns, KotlinTypeRefiner.Default),
languageVersionSettings
) {
fun approximateDeclarationType(baseType: KotlinType, local: Boolean): UnwrappedType { fun approximateDeclarationType(baseType: KotlinType, local: Boolean): UnwrappedType {
if (!languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) return baseType.unwrap() if (!languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) return baseType.unwrap()
@@ -42,4 +46,3 @@ class TypeApproximator(
fun approximateToSubType(type: UnwrappedType, conf: TypeApproximatorConfiguration): UnwrappedType? = fun approximateToSubType(type: UnwrappedType, conf: TypeApproximatorConfiguration): UnwrappedType? =
super.approximateToSubType(type, conf) as UnwrappedType? super.approximateToSubType(type, conf) as UnwrappedType?
} }