From b53c00cf69849e767f1057fb734f96f7a34f3827 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 16 Dec 2019 15:05:49 +0300 Subject: [PATCH] FIR: Optimize simple things in inference --- .../fir/resolve/calls/ConeInferenceContext.kt | 3 +-- .../kotlin/fir/types/ConeTypeContext.kt | 19 ++++++++-------- ...ctTypeCheckerContextForConstraintSystem.kt | 4 ++-- .../model/NewConstraintSystemImpl.kt | 22 +++++++++++++++++++ 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeInferenceContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeInferenceContext.kt index 46895675ad8..5fe64534154 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeInferenceContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ConeInferenceContext.kt @@ -116,8 +116,7 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo visited: HashSet = hashSetOf() ): Boolean { if (this == null) return false - if (this in visited) return false - visited += this + if (!visited.add(this)) return false /* TODO:? diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt index adf28885d39..a8cdf1ebf5d 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt @@ -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 -> diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt index 287e2509ea5..afa3b16e197 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt @@ -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) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index 783f5581f30..43f20f975bd 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -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()}" }