refactoring: register type variable together

not one by one
This commit is contained in:
Svetlana Isakova
2013-09-25 20:50:52 +04:00
parent de85d3df72
commit c150c22473
5 changed files with 17 additions and 11 deletions
@@ -562,10 +562,11 @@ public class CandidateResolver {
// Thus, we replace the parameters of our descriptor with fresh objects (perform alpha-conversion) // Thus, we replace the parameters of our descriptor with fresh objects (perform alpha-conversion)
CallableDescriptor candidateWithFreshVariables = FunctionDescriptorUtil.alphaConvertTypeParameters(candidate); CallableDescriptor candidateWithFreshVariables = FunctionDescriptorUtil.alphaConvertTypeParameters(candidate);
Map<TypeParameterDescriptor, Variance> typeVariables = Maps.newLinkedHashMap();
for (TypeParameterDescriptor typeParameterDescriptor : candidateWithFreshVariables.getTypeParameters()) { for (TypeParameterDescriptor typeParameterDescriptor : candidateWithFreshVariables.getTypeParameters()) {
constraintSystem.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT); // TODO: variance of the occurrences typeVariables.put(typeParameterDescriptor, Variance.INVARIANT); // TODO: variance of the occurrences
} }
constraintSystem.registerTypeVariables(typeVariables);
TypeSubstitutor substituteDontCare = ConstraintsUtil TypeSubstitutor substituteDontCare = ConstraintsUtil
.makeConstantSubstitutor(candidateWithFreshVariables.getTypeParameters(), DONT_CARE); .makeConstantSubstitutor(candidateWithFreshVariables.getTypeParameters(), DONT_CARE);
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.types.expressions; package org.jetbrains.jet.lang.types.expressions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
@@ -301,11 +302,13 @@ public class ExpressionTypingUtils {
Set<Name> typeNamesInReceiver = collectUsedTypeNames(receiverParameter.getType()); Set<Name> typeNamesInReceiver = collectUsedTypeNames(receiverParameter.getType());
ConstraintSystem constraintSystem = new ConstraintSystemImpl(); ConstraintSystem constraintSystem = new ConstraintSystemImpl();
Map<TypeParameterDescriptor, Variance> typeVariables = Maps.newLinkedHashMap();
for (TypeParameterDescriptor typeParameterDescriptor : callableDescriptor.getTypeParameters()) { for (TypeParameterDescriptor typeParameterDescriptor : callableDescriptor.getTypeParameters()) {
if (typeNamesInReceiver.contains(typeParameterDescriptor.getName())) { if (typeNamesInReceiver.contains(typeParameterDescriptor.getName())) {
constraintSystem.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT); typeVariables.put(typeParameterDescriptor, Variance.INVARIANT);
} }
} }
constraintSystem.registerTypeVariables(typeVariables);
constraintSystem.addSubtypeConstraint(receiverType, receiverParameter.getType(), ConstraintPosition.RECEIVER_POSITION); constraintSystem.addSubtypeConstraint(receiverType, receiverParameter.getType(), ConstraintPosition.RECEIVER_POSITION);
return constraintSystem.getStatus().isSuccessful() && ConstraintsUtil.checkBoundsAreSatisfied(constraintSystem, true); return constraintSystem.getStatus().isSuccessful() && ConstraintsUtil.checkBoundsAreSatisfied(constraintSystem, true);
@@ -23,15 +23,15 @@ import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor; import org.jetbrains.jet.lang.types.TypeSubstitutor;
import org.jetbrains.jet.lang.types.Variance; import org.jetbrains.jet.lang.types.Variance;
import java.util.Collection; import java.util.Map;
import java.util.Set; import java.util.Set;
public interface ConstraintSystem { public interface ConstraintSystem {
/** /**
* Registers a variable in a constraint system. * Registers variables in a constraint system.
*/ */
void registerTypeVariable(@NotNull TypeParameterDescriptor typeVariable, @NotNull Variance positionVariance); void registerTypeVariables(@NotNull Map<TypeParameterDescriptor, Variance> typeVariables);
/** /**
* Returns a set of all registered type variables. * Returns a set of all registered type variables.
@@ -163,8 +163,12 @@ public class ConstraintSystemImpl implements ConstraintSystem {
} }
@Override @Override
public void registerTypeVariable(@NotNull TypeParameterDescriptor typeVariable, @NotNull Variance positionVariance) { public void registerTypeVariables(@NotNull Map<TypeParameterDescriptor, Variance> typeVariables) {
typeParameterConstraints.put(typeVariable, new TypeConstraintsImpl(positionVariance)); for (Map.Entry<TypeParameterDescriptor, Variance> entry : typeVariables.entrySet()) {
TypeParameterDescriptor typeVariable = entry.getKey();
Variance positionVariance = entry.getValue();
typeParameterConstraints.put(typeVariable, new TypeConstraintsImpl(positionVariance));
}
} }
@Override @Override
@@ -261,9 +261,7 @@ public class TypeUtils {
processAllTypeParameters(withParameters, Variance.INVARIANT, processor); processAllTypeParameters(withParameters, Variance.INVARIANT, processor);
processAllTypeParameters(expected, Variance.INVARIANT, processor); processAllTypeParameters(expected, Variance.INVARIANT, processor);
ConstraintSystemImpl constraintSystem = new ConstraintSystemImpl(); ConstraintSystemImpl constraintSystem = new ConstraintSystemImpl();
for (Map.Entry<TypeParameterDescriptor, Variance> entry : parameters.entrySet()) { constraintSystem.registerTypeVariables(parameters);
constraintSystem.registerTypeVariable(entry.getKey(), entry.getValue());
}
constraintSystem.addSubtypeConstraint(withParameters, expected, ConstraintPosition.SPECIAL); constraintSystem.addSubtypeConstraint(withParameters, expected, ConstraintPosition.SPECIAL);
return constraintSystem.getStatus().isSuccessful(); return constraintSystem.getStatus().isSuccessful();