FIR: Optimize simple things in inference

This commit is contained in:
Denis Zharkov
2019-12-16 15:05:49 +03:00
parent 03c2350e79
commit b53c00cf69
4 changed files with 35 additions and 13 deletions
@@ -116,8 +116,7 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
visited: HashSet<KotlinTypeMarker> = hashSetOf()
): Boolean {
if (this == null) return false
if (this in visited) return false
visited += this
if (!visited.add(this)) return false
/*
TODO:?
@@ -301,19 +301,20 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
override fun captureFromArguments(type: SimpleTypeMarker, status: CaptureStatus): SimpleTypeMarker? {
require(type is ConeKotlinType)
val argumentsCount = type.argumentsCount()
val argumentsCount = type.typeArguments.size
if (argumentsCount == 0) return null
val typeConstructor = type.typeConstructor()
if (argumentsCount != typeConstructor.parametersCount()) return null
if (type.asArgumentList().all(this) { !it.isStarProjection() && it.getVariance() == TypeVariance.INV }) return null
val newArguments = Array(argumentsCount) { index ->
val argument = type.getArgument(index)
if (!argument.isStarProjection() && argument.getVariance() == TypeVariance.INV) return@Array argument as ConeKotlinTypeProjection
if (type.typeArguments.all { it !is ConeStarProjection && it.kind == ProjectionKind.INVARIANT }) return null
val lowerType = if (!argument.isStarProjection() && argument.getVariance() == TypeVariance.IN) {
argument.getType() as ConeKotlinType
val newArguments = Array(argumentsCount) { index ->
val argument = type.typeArguments[index]
if (argument !is ConeStarProjection && argument.kind == ProjectionKind.INVARIANT) return@Array argument
val lowerType = if (argument !is ConeStarProjection && argument.getVariance() == TypeVariance.IN) {
(argument as ConeTypedProjection).type
} else {
null
}
@@ -322,10 +323,10 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
}
for (index in 0 until argumentsCount) {
val oldArgument = type.getArgument(index)
val oldArgument = type.typeArguments[index]
val newArgument = newArguments[index]
if (!oldArgument.isStarProjection() && oldArgument.getVariance() == TypeVariance.INV) continue
if (oldArgument !is ConeStarProjection && oldArgument.kind == ProjectionKind.INVARIANT) continue
val parameter = typeConstructor.getParameter(index)
val upperBounds = (0 until parameter.upperBoundCount()).mapTo(mutableListOf()) { paramIndex ->
@@ -60,10 +60,10 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
}
private fun KotlinTypeMarker.isTypeVariableWithExact() =
anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable) && hasExactAnnotation()
hasExactAnnotation() && anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable)
private fun KotlinTypeMarker.isTypeVariableWithNoInfer() =
anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable) && hasNoInferAnnotation()
hasNoInferAnnotation() && anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable)
private fun internalAddSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean? {
assertInputTypes(subType, superType)
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.ResultTypeResolve
import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic
import org.jetbrains.kotlin.resolve.calls.model.OnlyInputTypesDiagnostic
import org.jetbrains.kotlin.resolve.calls.model.ResolvedAtom
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.IntersectionTypeConstructor
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
@@ -43,7 +44,28 @@ class NewConstraintSystemImpl(
COMPLETION
}
private fun checkState(a: State) {
if (!AbstractTypeChecker.RUN_SLOW_ASSERTIONS) return
checkState(*arrayOf(a))
}
private fun checkState(a: State, b: State) {
if (!AbstractTypeChecker.RUN_SLOW_ASSERTIONS) return
checkState(*arrayOf(a, b))
}
private fun checkState(a: State, b: State, c: State) {
if (!AbstractTypeChecker.RUN_SLOW_ASSERTIONS) return
checkState(*arrayOf(a, b, c))
}
private fun checkState(a: State, b: State, c: State, d: State) {
if (!AbstractTypeChecker.RUN_SLOW_ASSERTIONS) return
checkState(*arrayOf(a, b, c, d))
}
private fun checkState(vararg allowedState: State) {
if (!AbstractTypeChecker.RUN_SLOW_ASSERTIONS) return
assert(state in allowedState) {
"State $state is not allowed. AllowedStates: ${allowedState.joinToString()}"
}