From d1e9f00e5f24fd1f4b591d973d5abdd89aca32e9 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 1 Jul 2015 22:47:12 +0300 Subject: [PATCH] Removed obsolete type parameter substitution logic when copy constraint system --- .../calls/inference/ConstraintError.kt | 3 -- .../calls/inference/ConstraintSystemImpl.kt | 49 +++++++++---------- .../resolve/calls/inference/TypeBounds.kt | 1 - .../resolve/calls/inference/TypeBoundsImpl.kt | 43 ---------------- 4 files changed, 23 insertions(+), 73 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintError.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintError.kt index e3b98365222..2f73688f0b1 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintError.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintError.kt @@ -30,8 +30,5 @@ class TypeInferenceError(constraintPosition: ConstraintPosition): ConstraintErro class CannotCapture(constraintPosition: ConstraintPosition, val typeVariable: TypeParameterDescriptor): ConstraintError(constraintPosition) -fun ConstraintError.substituteTypeVariable(substitution: (TypeParameterDescriptor) -> TypeParameterDescriptor?) = - if (this is CannotCapture) CannotCapture(constraintPosition, substitution(typeVariable) ?: typeVariable) else this - fun newTypeInferenceOrConstructorMismatchError(constraintPosition: ConstraintPosition) = if (constraintPosition is CompoundConstraintPosition) TypeInferenceError(constraintPosition) else TypeConstructorMismatch(constraintPosition) \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt index b4c0e6e804d..d8dbe991601 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt @@ -17,7 +17,6 @@ package org.jetbrains.kotlin.resolve.calls.inference import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemImpl.ConstraintKind.EQUAL @@ -29,7 +28,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.UPPER_B import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.CompoundConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind -import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.EXPECTED_TYPE_POSITION import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.TYPE_BOUND_POSITION import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.equalsOrContains import org.jetbrains.kotlin.resolve.scopes.JetScope @@ -175,19 +173,14 @@ public class ConstraintSystemImpl : ConstraintSystem { }.filterNotNull().filter { if (original) it in originalToVariables.keySet() else it in getAllTypeVariables() } } - public fun copy(): ConstraintSystem = createNewConstraintSystemFromThis({ it }, { true }) - - public fun substituteTypeVariables(typeVariablesMap: (TypeParameterDescriptor) -> TypeParameterDescriptor?): ConstraintSystem { - // type bounds are proper types and don't contain other variables - return createNewConstraintSystemFromThis(typeVariablesMap, { true }) - } + public fun copy(): ConstraintSystem = createNewConstraintSystemFromThis { true } public fun filterConstraintsOut(excludePosition: ConstraintPosition): ConstraintSystem { return filterConstraints { !it.equalsOrContains(excludePosition) } } public fun filterConstraints(condition: (ConstraintPosition) -> Boolean): ConstraintSystem { - return createNewConstraintSystemFromThis({ it }, condition) + return createNewConstraintSystemFromThis(condition) } public fun getSystemWithoutWeakConstraints(): ConstraintSystem { @@ -201,29 +194,20 @@ public class ConstraintSystemImpl : ConstraintSystem { } private fun createNewConstraintSystemFromThis( - substituteTypeVariable: (TypeParameterDescriptor) -> TypeParameterDescriptor?, filterConstraintPosition: (ConstraintPosition) -> Boolean ): ConstraintSystem { val newSystem = ConstraintSystemImpl() for ((typeParameter, typeBounds) in allTypeParameterBounds) { - val newTypeParameter = substituteTypeVariable(typeParameter) ?: typeParameter - newSystem.allTypeParameterBounds.put(newTypeParameter, typeBounds.copy(substituteTypeVariable).filter(filterConstraintPosition)) + newSystem.allTypeParameterBounds.put(typeParameter, typeBounds.filter(filterConstraintPosition)) } - for ((typeVariable, bounds) in usedInBounds) { - if (bounds.isNotEmpty()) { - val newTypeVariable = substituteTypeVariable(typeVariable) ?: typeVariable - newSystem.usedInBounds.put(newTypeVariable, ArrayList(bounds.substitute(substituteTypeVariable))) - } - } - newSystem.externalTypeParameters.addAll(externalTypeParameters.map { substituteTypeVariable(it) ?: it }) - newSystem.errors.addAll(errors.filter { filterConstraintPosition(it.constraintPosition) }.map { it.substituteTypeVariable(substituteTypeVariable) }) + newSystem.usedInBounds.putAll(usedInBounds.map { + val (variable, bounds) = it + variable to bounds.filterTo(arrayListOf()) { filterConstraintPosition(it.position )} + }.toMap()) + newSystem.externalTypeParameters.addAll(externalTypeParameters ) + newSystem.errors.addAll(errors.filter { filterConstraintPosition(it.constraintPosition) }) - val typeSubstitutor = createTypeSubstitutor(substituteTypeVariable) - newSystem.initialConstraints.addAll(initialConstraints.filter { filterConstraintPosition(it.position) }.map { - val newSubType = typeSubstitutor.substitute(it.subtype, Variance.INVARIANT) - val newSuperType = typeSubstitutor.substitute(it.superType, Variance.INVARIANT) - if (newSubType != null && newSuperType != null) Constraint(it.kind, newSubType, newSuperType, it.position) else null - }) + newSystem.initialConstraints.addAll(initialConstraints.filter { filterConstraintPosition(it.position) }) newSystem.originalToVariables.putAll(originalToVariables) newSystem.variablesToOriginal.putAll(variablesToOriginal) return newSystem @@ -563,4 +547,17 @@ public fun ConstraintSystemImpl.registerTypeVariables( variance: (TypeParameterDescriptor) -> Variance ) { registerTypeVariables(typeVariables, variance, { it }) +} + +public fun createTypeSubstitutor(conversion: (TypeParameterDescriptor) -> TypeParameterDescriptor?): TypeSubstitutor { + return TypeSubstitutor.create(object : TypeSubstitution() { + override fun get(key: TypeConstructor): TypeProjection? { + val descriptor = key.getDeclarationDescriptor() + if (descriptor !is TypeParameterDescriptor) return null + val typeParameterDescriptor = conversion(descriptor) ?: return null + + val type = JetTypeImpl(Annotations.EMPTY, typeParameterDescriptor.getTypeConstructor(), false, listOf(), JetScope.Empty) + return TypeProjectionImpl(type) + } + }) } \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBounds.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBounds.kt index 78d7963dd8f..947cc001d9b 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBounds.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBounds.kt @@ -24,7 +24,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.UPPER_B import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPosition import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.Variance -import kotlin.properties.Delegates public trait TypeBounds { public val varianceOfPosition: Variance diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBoundsImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBoundsImpl.kt index cc84ca284fb..2be71975086 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBoundsImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/TypeBoundsImpl.kt @@ -17,7 +17,6 @@ package org.jetbrains.kotlin.resolve.calls.inference import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.Bound import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.EXACT_BOUND @@ -25,7 +24,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.LOWER_B import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.UPPER_BOUND import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPosition import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor -import org.jetbrains.kotlin.resolve.scopes.JetScope import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.JetTypeChecker import org.jetbrains.kotlin.utils.addIfNotNull @@ -74,18 +72,6 @@ public class TypeBoundsImpl( return result } - fun copy(substituteTypeVariable: ((TypeParameterDescriptor) -> TypeParameterDescriptor?)? = null): TypeBoundsImpl { - val typeBounds = TypeBoundsImpl(substituteTypeVariable?.invoke(typeVariable) ?: typeVariable, varianceOfPosition) - if (substituteTypeVariable == null) { - typeBounds.bounds.addAll(bounds) - } - else { - typeBounds.bounds.addAll(bounds.substitute(substituteTypeVariable)) - } - typeBounds.resultValues = resultValues - return typeBounds - } - public fun filter(condition: (ConstraintPosition) -> Boolean): TypeBoundsImpl { val result = TypeBoundsImpl(typeVariable, varianceOfPosition) result.bounds.addAll(bounds.filter { condition(it.position) }) @@ -182,33 +168,4 @@ public class TypeBoundsImpl( } return true } -} - -fun Collection.substitute(substituteTypeVariable: (TypeParameterDescriptor) -> TypeParameterDescriptor?): List { - val typeSubstitutor = createTypeSubstitutor(substituteTypeVariable) - return map { - //todo captured types - val substitutedType = if (it.constrainingType.getConstructor().isDenotable()) { - typeSubstitutor.substitute(it.constrainingType, Variance.INVARIANT) - } else { - it.constrainingType - } - substitutedType?.let { type -> - Bound(substituteTypeVariable(it.typeVariable) ?: it.typeVariable, type, it.kind, it.position, it.isProper, - it.derivedFrom.map { substituteTypeVariable(it) ?: it }.toSet()) - } - }.filterNotNull() -} - -fun createTypeSubstitutor(substituteTypeVariable: (TypeParameterDescriptor) -> TypeParameterDescriptor?): TypeSubstitutor { - return TypeSubstitutor.create(object : TypeSubstitution() { - override fun get(key: TypeConstructor): TypeProjection? { - val descriptor = key.getDeclarationDescriptor() - if (descriptor !is TypeParameterDescriptor) return null - val typeParameterDescriptor = substituteTypeVariable(descriptor) ?: return null - - val type = JetTypeImpl(Annotations.EMPTY, typeParameterDescriptor.getTypeConstructor(), false, listOf(), JetScope.Empty) - return TypeProjectionImpl(type) - } - }) } \ No newline at end of file