Optimize ConstraintInjector::isMyTypeVariable

Do not look into the map for constructors that are not
type variables
This commit is contained in:
Denis Zharkov
2019-06-05 14:51:13 +03:00
parent b43f717f6d
commit 3c8ed21e59
5 changed files with 24 additions and 8 deletions
@@ -246,6 +246,11 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext,
return this.typeConstructor
}
override fun KotlinTypeMarker.mayBeTypeVariable(): Boolean {
require(this is ConeKotlinType)
return this is ConeTypeVariableType
}
override fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker {
require(this is ConeCapturedType)
return this.constructor.projection
@@ -310,4 +315,4 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext,
override fun TypeConstructorMarker.isCapturedTypeConstructor(): Boolean {
return this is ConeCapturedTypeConstructor
}
}
}
@@ -11,6 +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.resolve.calls.inference.model.TypeVariableTypeConstructor
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
import org.jetbrains.kotlin.types.checker.NewCapturedType
@@ -32,6 +33,11 @@ class ClassicTypeSystemContextForCS(override val builtIns: KotlinBuiltIns) : Typ
return this.freshTypeConstructor
}
override fun KotlinTypeMarker.mayBeTypeVariable(): Boolean {
require(this is KotlinType, this::errorMessage)
return this.constructor is TypeVariableTypeConstructor
}
override fun createCapturedType(
constructorProjection: TypeArgumentMarker,
constructorSupertypes: List<KotlinTypeMarker>,
@@ -84,4 +90,4 @@ fun NewConstraintSystemImpl(
builtIns: KotlinBuiltIns
): NewConstraintSystemImpl {
return NewConstraintSystemImpl(constraintInjector, ClassicTypeSystemContextForCS(builtIns))
}
}
@@ -143,7 +143,8 @@ class ConstraintInjector(val constraintIncorporator: ConstraintIncorporator, val
}
// from AbstractTypeCheckerContextForConstraintSystem
override fun isMyTypeVariable(type: SimpleTypeMarker): Boolean = c.allTypeVariables.containsKey(type.typeConstructor())
override fun isMyTypeVariable(type: SimpleTypeMarker): Boolean =
type.mayBeTypeVariable() && c.allTypeVariables.containsKey(type.typeConstructor())
override fun addUpperConstraint(typeVariable: TypeConstructorMarker, superType: KotlinTypeMarker) =
addConstraint(typeVariable, superType, UPPER)
@@ -244,4 +245,4 @@ class ConstraintInjector(val constraintIncorporator: ConstraintIncorporator, val
}
}
data class ConstraintContext(val kind: ConstraintKind, val derivedFrom: Set<TypeVariableMarker>)
data class ConstraintContext(val kind: ConstraintKind, val derivedFrom: Set<TypeVariableMarker>)
@@ -11,18 +11,17 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.calls.inference.CapturedType
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
import org.jetbrains.kotlin.resolve.descriptorUtil.hasExactAnnotation
import org.jetbrains.kotlin.resolve.descriptorUtil.hasNoInferAnnotation
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
import org.jetbrains.kotlin.resolve.descriptorUtil.isExactAnnotation
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.model.CaptureStatus
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
import org.jetbrains.kotlin.types.typeUtil.contains
import kotlin.math.max
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.math.max
interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext {
override fun TypeConstructorMarker.isDenotable(): Boolean {
@@ -376,6 +375,10 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext {
errorSupportedOnlyInTypeInference()
}
override fun KotlinTypeMarker.mayBeTypeVariable(): Boolean {
errorSupportedOnlyInTypeInference()
}
override fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker {
require(this is NewCapturedType, this::errorMessage)
return this.constructor.projection
@@ -563,4 +566,4 @@ fun requireOrDescribe(condition: Boolean, value: Any?) {
} else ""
"Unexpected: value = '$value'$typeInfo"
}
}
}
@@ -122,6 +122,7 @@ interface TypeSystemInferenceExtensionContext : TypeSystemContext, TypeSystemBui
fun TypeVariableMarker.freshTypeConstructor(): TypeConstructorMarker
fun KotlinTypeMarker.mayBeTypeVariable(): Boolean
fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker
fun CapturedTypeMarker.captureStatus(): CaptureStatus