Abstract FlatSignature from kotlin types
This commit is contained in:
+7
-7
@@ -11,10 +11,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstituto
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
|
||||
import org.jetbrains.kotlin.types.StubType
|
||||
import org.jetbrains.kotlin.types.TypeConstructor
|
||||
import org.jetbrains.kotlin.types.TypeProjectionImpl
|
||||
import org.jetbrains.kotlin.types.UnwrappedType
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
|
||||
import org.jetbrains.kotlin.types.checker.NewCapturedType
|
||||
import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor
|
||||
@@ -61,9 +58,12 @@ class ClassicTypeSystemContextForCS(override val builtIns: KotlinBuiltIns) : Typ
|
||||
}
|
||||
|
||||
override fun TypeSubstitutorMarker.safeSubstitute(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
require(type is UnwrappedType, this::errorMessage)
|
||||
require(this is NewTypeSubstitutor, this::errorMessage)
|
||||
return this.safeSubstitute(type)
|
||||
require(type is UnwrappedType, type::errorMessage)
|
||||
return when (this) {
|
||||
is NewTypeSubstitutor -> safeSubstitute(type)
|
||||
is TypeSubstitutor -> safeSubstitute(type, Variance.INVARIANT)
|
||||
else -> error(this.errorMessage())
|
||||
}
|
||||
}
|
||||
|
||||
override fun createStubType(typeVariable: TypeVariableMarker): StubTypeMarker {
|
||||
|
||||
+2
-5
@@ -26,10 +26,9 @@ import org.jetbrains.kotlin.resolve.calls.inference.substitute
|
||||
import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem
|
||||
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
|
||||
import org.jetbrains.kotlin.types.checker.requireOrDescribe
|
||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
import org.jetbrains.kotlin.types.UnwrappedType
|
||||
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeParameterMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
|
||||
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
|
||||
|
||||
@@ -38,7 +37,7 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, builtIn
|
||||
val csBuilder: ConstraintSystemBuilder =
|
||||
system.getBuilder()
|
||||
|
||||
override fun registerTypeVariables(typeParameters: Collection<TypeParameterMarker>): TypeSubstitutor {
|
||||
override fun registerTypeVariables(typeParameters: Collection<TypeParameterMarker>): TypeSubstitutorMarker {
|
||||
|
||||
val substitutionMap = typeParameters.associate {
|
||||
requireOrDescribe(it is TypeParameterDescriptor, it)
|
||||
@@ -58,8 +57,6 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, builtIn
|
||||
}
|
||||
|
||||
override fun addSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker) {
|
||||
require(subType is UnwrappedType)
|
||||
require(superType is UnwrappedType)
|
||||
csBuilder.addSubtypeConstraint(
|
||||
subType,
|
||||
superType,
|
||||
|
||||
+8
-13
@@ -22,10 +22,7 @@ import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor
|
||||
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.checker.captureFromExpression
|
||||
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeParameterMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
|
||||
import org.jetbrains.kotlin.types.model.*
|
||||
|
||||
interface SpecificityComparisonCallbacks {
|
||||
fun isNonSubtypeNotLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean
|
||||
@@ -114,7 +111,7 @@ class FlatSignature<out T> constructor(
|
||||
|
||||
|
||||
interface SimpleConstraintSystem {
|
||||
fun registerTypeVariables(typeParameters: Collection<TypeParameterMarker>): TypeSubstitutor
|
||||
fun registerTypeVariables(typeParameters: Collection<TypeParameterMarker>): TypeSubstitutorMarker
|
||||
fun addSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker)
|
||||
fun hasContradiction(): Boolean
|
||||
|
||||
@@ -134,6 +131,7 @@ fun <T> SimpleConstraintSystem.isSignatureNotLessSpecific(
|
||||
if (specific.valueParameterTypes.size != general.valueParameterTypes.size) return false
|
||||
|
||||
val typeParameters = general.typeParameters
|
||||
val typeSubstitutor = registerTypeVariables(typeParameters)
|
||||
|
||||
for ((specificType, generalType) in specific.valueParameterTypes.zip(general.valueParameterTypes)) {
|
||||
if (specificType == null || generalType == null) continue
|
||||
@@ -142,18 +140,14 @@ fun <T> SimpleConstraintSystem.isSignatureNotLessSpecific(
|
||||
return false
|
||||
}
|
||||
|
||||
if (typeParameters.isEmpty() /*|| !TypeUtils.dependsOnTypeParameters(generalType, typeParameters)*/) {
|
||||
if (typeParameters.isEmpty() || !generalType.dependsOnTypeParameters(context, typeParameters)) {
|
||||
if (!AbstractTypeChecker.isSubtypeOf(context, specificType, generalType)) {
|
||||
if (!callbacks.isNonSubtypeNotLessSpecific(specificType, generalType)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val typeSubstitutor = registerTypeVariables(typeParameters)
|
||||
require(generalType is KotlinType) { TODO("not supported") }
|
||||
require(specificType is KotlinType) { TODO("not supported") }
|
||||
|
||||
val substitutedGeneralType = typeSubstitutor.safeSubstitute(generalType, Variance.INVARIANT)
|
||||
val substitutedGeneralType = typeSubstitutor.safeSubstitute(context, generalType)
|
||||
|
||||
/**
|
||||
* Example:
|
||||
@@ -162,8 +156,9 @@ fun <T> SimpleConstraintSystem.isSignatureNotLessSpecific(
|
||||
* Here, when we try solve this CS(Y is variables) then Array<out X> <: Array<out Y> and this system impossible to solve,
|
||||
* so we capture types from receiver and value parameters.
|
||||
*/
|
||||
val specificCapturedType = specificType.unwrap().let { if (captureFromArgument) captureFromExpression(it) ?: it else it }
|
||||
addSubtypeConstraint(specificCapturedType, substitutedGeneralType.unwrap())
|
||||
val specificCapturedType =
|
||||
context.prepareType(specificType).let { if (captureFromArgument) context.captureFromExpression(it) ?: it else it }
|
||||
addSubtypeConstraint(specificCapturedType, substitutedGeneralType)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user