From 3e27fdb964b7a78784e4e65443736a4be56442a8 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 12 Nov 2015 22:16:47 +0300 Subject: [PATCH] Inline and move out utilities from ConstraintSystem --- .../calls/inference/ConstraintSystem.kt | 7 ------- .../calls/inference/ConstraintSystemImpl.kt | 8 -------- .../calls/inference/ConstraintsUtil.java | 19 +++++++++++++++++-- .../calls/inference/constraintSystemUtils.kt | 6 ++++++ .../jetbrains/kotlin/idea/util/FuzzyType.kt | 2 +- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystem.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystem.kt index 0e82d7b21cf..3abca039f59 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystem.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystem.kt @@ -24,18 +24,11 @@ import org.jetbrains.kotlin.types.TypeSubstitutor interface ConstraintSystem { val status: ConstraintSystemStatus - /** - * Returns a set of all non-external type parameter descriptors. - */ - val typeParameterDescriptors: Set - /** * Returns a set of all registered type variables. */ val typeVariables: Set - fun descriptorToVariable(call: CallHandle, descriptor: TypeParameterDescriptor): TypeVariable - /** * Returns the resulting type constraints of solving the constraint system for specific type parameter descriptor. * Throws IllegalArgumentException if the type parameter descriptor is not known to the system. diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt index a1dcba7d88a..714dd87b01d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt @@ -105,17 +105,9 @@ internal class ConstraintSystemImpl( return SubstitutionFilteringInternalResolveAnnotations(substitution).buildSubstitutor() } - override val typeParameterDescriptors: Set - get() = typeVariables.map { it.originalTypeParameter }.toSet() - override val typeVariables: Set get() = allTypeParameterBounds.keys - override fun descriptorToVariable(call: CallHandle, descriptor: TypeParameterDescriptor): TypeVariable = - typeVariables.firstOrNull { - it.call == call && it.originalTypeParameter == descriptor - } ?: throw IllegalArgumentException("Unknown descriptor: $descriptor, call: $call") - override fun getTypeBounds(typeVariable: TypeVariable): TypeBoundsImpl { return allTypeParameterBounds[typeVariable] ?: throw IllegalArgumentException("TypeParameterDescriptor is not a type variable for constraint system: $typeVariable") diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintsUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintsUtil.java index 039203765c4..7f0e8e51d9d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintsUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintsUtil.java @@ -18,6 +18,8 @@ package org.jetbrains.kotlin.resolve.calls.inference; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import kotlin.CollectionsKt; +import kotlin.jvm.functions.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor; @@ -88,12 +90,25 @@ public class ConstraintsUtil { @NotNull Call call, boolean substituteOtherTypeParametersInBound ) { - TypeVariable typeVariable = constraintSystem.descriptorToVariable(TypeVariableKt.toHandle(call), typeParameter); + TypeVariable typeVariable = ConstraintSystemUtilsKt.descriptorToVariable( + constraintSystem, TypeVariableKt.toHandle(call), typeParameter + ); KotlinType type = constraintSystem.getTypeBounds(typeVariable).getValue(); if (type == null) return true; + + List typeParametersUsedInSystem = CollectionsKt.map( + constraintSystem.getTypeVariables(), + new Function1() { + @Override + public TypeParameterDescriptor invoke(TypeVariable variable) { + return variable.getOriginalTypeParameter(); + } + } + ); + for (KotlinType upperBound : typeParameter.getUpperBounds()) { if (!substituteOtherTypeParametersInBound && - TypeUtils.dependsOnTypeParameters(upperBound, constraintSystem.getTypeParameterDescriptors())) { + TypeUtils.dependsOnTypeParameters(upperBound, typeParametersUsedInSystem)) { continue; } KotlinType substitutedUpperBound = constraintSystem.getResultingSubstitutor().substitute(upperBound, Variance.INVARIANT); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/constraintSystemUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/constraintSystemUtils.kt index f700ed2c925..e969f10de3f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/constraintSystemUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/constraintSystemUtils.kt @@ -33,6 +33,12 @@ fun ConstraintSystem.filterConstraintsOut(excludePositionKind: ConstraintPositio return toBuilder { !it.derivedFrom(excludePositionKind) }.build() } +fun ConstraintSystem.descriptorToVariable(call: CallHandle, descriptor: TypeParameterDescriptor): TypeVariable { + return typeVariables.firstOrNull { + it.call == call && it.originalTypeParameter == descriptor + } ?: throw IllegalArgumentException("Unknown descriptor: $descriptor, call: $call") +} + internal fun KotlinType.getNestedArguments(): List { val result = ArrayList() diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt index 1d3964e75c7..136666be103 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt @@ -143,7 +143,7 @@ class FuzzyType( if (otherSubstitutedType.isError) return null if (!substitutedType.checkInheritance(otherSubstitutedType)) return null - val substitution = constraintSystem.typeParameterDescriptors.toMap({ it.typeConstructor }) { + val substitution = constraintSystem.typeVariables.map { it.originalTypeParameter }.toMap({ it.typeConstructor }) { val type = it.defaultType val solution = substitutor.substitute(type, Variance.INVARIANT) TypeProjectionImpl(if (solution != null && !ErrorUtils.containsUninferredParameter(solution)) solution else type)