ConstraintSystem renamed to ConstraintsBuilder

This commit is contained in:
Svetlana Isakova
2012-07-11 14:48:04 +04:00
parent f9389d2fff
commit dbd3d62c54
4 changed files with 34 additions and 50 deletions
@@ -28,8 +28,8 @@ import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetClassOrObject; import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintPosition; import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintPosition;
import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintsUtil;
import org.jetbrains.jet.lang.resolve.calls.inference.InferenceErrorData; import org.jetbrains.jet.lang.resolve.calls.inference.InferenceErrorData;
import org.jetbrains.jet.lang.resolve.calls.inference.TypeBounds;
import org.jetbrains.jet.lang.types.JetType; 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;
@@ -41,7 +41,6 @@ import static org.jetbrains.jet.lang.diagnostics.rendering.TabledDescriptorRende
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author svtk * @author svtk
@@ -169,16 +168,17 @@ public class Renderers {
public static TabledDescriptorRenderer renderConflictingSubstitutionsInferenceError(InferenceErrorData inferenceErrorData, public static TabledDescriptorRenderer renderConflictingSubstitutionsInferenceError(InferenceErrorData inferenceErrorData,
TabledDescriptorRenderer result) { TabledDescriptorRenderer result) {
assert inferenceErrorData.constraintSystem.hasConflictingParameters(); assert inferenceErrorData.constraintsBuilder.hasConflictingParameters();
Collection<CallableDescriptor> substitutedDescriptors = Lists.newArrayList(); Collection<CallableDescriptor> substitutedDescriptors = Lists.newArrayList();
Collection<TypeSubstitutor> substitutors = inferenceErrorData.constraintSystem.getSubstitutors(); Collection<TypeSubstitutor> substitutors = ConstraintsUtil.getSubstitutorsForConflictingParameters(
inferenceErrorData.constraintsBuilder);
for (TypeSubstitutor substitutor : substitutors) { for (TypeSubstitutor substitutor : substitutors) {
CallableDescriptor substitutedDescriptor = inferenceErrorData.descriptor.substitute(substitutor); CallableDescriptor substitutedDescriptor = inferenceErrorData.descriptor.substitute(substitutor);
substitutedDescriptors.add(substitutedDescriptor); substitutedDescriptors.add(substitutedDescriptor);
} }
TypeParameterDescriptor firstConflictingParameter = inferenceErrorData.constraintSystem.getFirstConflictingParameter(); TypeParameterDescriptor firstConflictingParameter = ConstraintsUtil.getFirstConflictingParameter(inferenceErrorData.constraintsBuilder);
assert firstConflictingParameter != null; assert firstConflictingParameter != null;
result.text(newText() result.text(newText()
@@ -200,7 +200,7 @@ public class Renderers {
valueArgumentTypes.add(valueParameterDescriptor.getType()); valueArgumentTypes.add(valueParameterDescriptor.getType());
JetType actualType = inferenceErrorData.valueArgumentsTypes.get(valueParameterDescriptor.getIndex()); JetType actualType = inferenceErrorData.valueArgumentsTypes.get(valueParameterDescriptor.getIndex());
if (!JetTypeChecker.INSTANCE.isSubtypeOf(actualType, valueParameterDescriptor.getType())) { if (!JetTypeChecker.INSTANCE.isSubtypeOf(actualType, valueParameterDescriptor.getType())) {
errorPositions.add(ConstraintPosition.valueParameterPosition(valueParameterDescriptor.getIndex())); errorPositions.add(ConstraintPosition.getValueParameterPosition(valueParameterDescriptor.getIndex()));
} }
} }
@@ -226,16 +226,16 @@ public class Renderers {
.functionArgumentTypeList( .functionArgumentTypeList(
inferenceErrorData.receiverArgumentType, inferenceErrorData.receiverArgumentType,
inferenceErrorData.valueArgumentsTypes, inferenceErrorData.valueArgumentsTypes,
inferenceErrorData.constraintSystem inferenceErrorData.constraintsBuilder
.getErrorConstraintPositions())); .getTypeConstructorMismatchConstraintPositions()));
} }
public static TabledDescriptorRenderer renderNoInformationForParameterError(InferenceErrorData inferenceErrorData, public static TabledDescriptorRenderer renderNoInformationForParameterError(InferenceErrorData inferenceErrorData,
TabledDescriptorRenderer renderer) { TabledDescriptorRenderer renderer) {
TypeParameterDescriptor firstUnknownParameter = null; TypeParameterDescriptor firstUnknownParameter = null;
for (Map.Entry<TypeParameterDescriptor, TypeBounds> entry : inferenceErrorData.constraintSystem.getTypeBoundsMap().entrySet()) { for (TypeParameterDescriptor typeParameter : inferenceErrorData.constraintsBuilder.getTypeParameters()) {
if (entry.getValue().isEmpty()) { if (inferenceErrorData.constraintsBuilder.getTypeConstraints(typeParameter).isEmpty()) {
firstUnknownParameter = entry.getKey(); firstUnknownParameter = typeParameter;
break; break;
} }
} }
@@ -253,7 +253,7 @@ public class Renderers {
public static TabledDescriptorRenderer renderUpperBoundViolatedInferenceError(InferenceErrorData inferenceErrorData, TabledDescriptorRenderer result) { public static TabledDescriptorRenderer renderUpperBoundViolatedInferenceError(InferenceErrorData inferenceErrorData, TabledDescriptorRenderer result) {
TypeParameterDescriptor typeParameterDescriptor = null; TypeParameterDescriptor typeParameterDescriptor = null;
for (TypeParameterDescriptor typeParameter : inferenceErrorData.descriptor.getTypeParameters()) { for (TypeParameterDescriptor typeParameter : inferenceErrorData.descriptor.getTypeParameters()) {
if (!inferenceErrorData.constraintSystem.checkUpperBound(typeParameter)) { if (!ConstraintsUtil.checkUpperBoundIsSatisfied(inferenceErrorData.constraintsBuilder, typeParameter)) {
typeParameterDescriptor = typeParameter; typeParameterDescriptor = typeParameter;
break; break;
} }
@@ -264,9 +264,9 @@ public class Renderers {
.table(newTable(). .table(newTable().
descriptor(inferenceErrorData.descriptor)); descriptor(inferenceErrorData.descriptor));
JetType type = inferenceErrorData.constraintSystem.getValue(typeParameterDescriptor); JetType type = inferenceErrorData.constraintsBuilder.getValue(typeParameterDescriptor);
JetType upperBound = typeParameterDescriptor.getUpperBoundsAsType(); JetType upperBound = typeParameterDescriptor.getUpperBoundsAsType();
JetType substitute = inferenceErrorData.constraintSystem.getSubstitutor().substitute(upperBound, Variance.INVARIANT); JetType substitute = inferenceErrorData.constraintsBuilder.getSubstitutor().substitute(upperBound, Variance.INVARIANT);
result.text(newText() result.text(newText()
.normal(" is not satisfied: inferred type ") .normal(" is not satisfied: inferred type ")
@@ -25,26 +25,21 @@ 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.Collection;
import java.util.Map; import java.util.Set;
/** /**
* @author svtk * @author svtk
*/ */
public interface ConstraintSystem { public interface ConstraintsBuilder {
enum ConstraintType {
SUB_TYPE, SUPER_TYPE, EQUAL
}
JetType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE");
void registerTypeVariable(@NotNull TypeParameterDescriptor typeParameterDescriptor, @NotNull Variance positionVariance); void registerTypeVariable(@NotNull TypeParameterDescriptor typeParameterDescriptor, @NotNull Variance positionVariance);
void registerTypeVariable(@NotNull TypeParameterDescriptor typeParameterDescriptor, @NotNull TypeBounds typeBounds); @NotNull
Set<TypeParameterDescriptor> getTypeParameters();
void addSubtypingConstraint(@NotNull JetType exactType, @NotNull JetType expectedType, @NotNull ConstraintPosition constraintPosition); void addSubtypingConstraint(@NotNull JetType subjectType, @NotNull JetType constrainingType, @NotNull ConstraintPosition constraintPosition);
void addConstraint(@NotNull ConstraintType constraintType, @NotNull JetType exactType, @NotNull JetType expectedType, @NotNull ConstraintPosition constraintPosition); void addSupertypeConstraint(@NotNull JetType subjectType, @NotNull JetType constrainingType, @NotNull ConstraintPosition constraintPosition);
boolean isSuccessful(); boolean isSuccessful();
@@ -56,24 +51,15 @@ public interface ConstraintSystem {
boolean hasTypeConstructorMismatch(); boolean hasTypeConstructorMismatch();
TypeBounds getTypeBounds(TypeParameterDescriptor typeParameterDescriptor);
Map<TypeParameterDescriptor, TypeBounds> getTypeBoundsMap();
@Nullable @Nullable
TypeParameterDescriptor getFirstConflictingParameter(); TypeConstraints getTypeConstraints(@NotNull TypeParameterDescriptor typeParameterDescriptor);
@NotNull @NotNull
TypeSubstitutor getSubstitutor(); TypeSubstitutor getSubstitutor();
@NotNull
Collection<TypeSubstitutor> getSubstitutors();
@Nullable @Nullable
JetType getValue(TypeParameterDescriptor typeParameterDescriptor); JetType getValue(@NotNull TypeParameterDescriptor typeParameterDescriptor);
@NotNull @NotNull
Collection<ConstraintPosition> getErrorConstraintPositions(); Collection<ConstraintPosition> getTypeConstructorMismatchConstraintPositions();
boolean checkUpperBound(@NotNull TypeParameterDescriptor typeParameterDescriptor);
} }
@@ -19,35 +19,33 @@ package org.jetbrains.jet.lang.resolve.calls.inference;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author svtk * @author svtk
*/ */
public class InferenceErrorData { public class InferenceErrorData {
public final CallableDescriptor descriptor; public final CallableDescriptor descriptor;
public final ConstraintSystem constraintSystem; public final ConstraintsBuilder constraintsBuilder;
public final JetType receiverArgumentType; public final JetType receiverArgumentType;
public final List<JetType> valueArgumentsTypes; public final List<JetType> valueArgumentsTypes;
private InferenceErrorData(@NotNull CallableDescriptor descriptor, @NotNull ConstraintSystem constraintSystem, private InferenceErrorData(@NotNull CallableDescriptor descriptor, @NotNull ConstraintsBuilder constraintsBuilder,
@Nullable List<JetType> valueArgumentsTypes, @Nullable JetType receiverArgumentType) { @Nullable List<JetType> valueArgumentsTypes, @Nullable JetType receiverArgumentType) {
this.descriptor = descriptor; this.descriptor = descriptor;
this.constraintSystem = constraintSystem; this.constraintsBuilder = constraintsBuilder;
this.receiverArgumentType = receiverArgumentType; this.receiverArgumentType = receiverArgumentType;
this.valueArgumentsTypes = valueArgumentsTypes; this.valueArgumentsTypes = valueArgumentsTypes;
} }
public static InferenceErrorData create(@NotNull CallableDescriptor descriptor, @NotNull ConstraintSystem constraintSystem, public static InferenceErrorData create(@NotNull CallableDescriptor descriptor, @NotNull ConstraintsBuilder constraintsBuilder,
@NotNull List<JetType> valueArgumentsTypes, @Nullable JetType receiverArgumentType) { @NotNull List<JetType> valueArgumentsTypes, @Nullable JetType receiverArgumentType) {
return new InferenceErrorData(descriptor, constraintSystem, valueArgumentsTypes, receiverArgumentType); return new InferenceErrorData(descriptor, constraintsBuilder, valueArgumentsTypes, receiverArgumentType);
} }
public static InferenceErrorData create(@NotNull CallableDescriptor descriptor, @NotNull ConstraintSystem constraintSystem) { public static InferenceErrorData create(@NotNull CallableDescriptor descriptor, @NotNull ConstraintsBuilder constraintsBuilder) {
return new InferenceErrorData(descriptor, constraintSystem, null, null); return new InferenceErrorData(descriptor, constraintsBuilder, null, null);
} }
} }
@@ -232,20 +232,20 @@ public class ExpressionTypingUtils {
@NotNull JetType receiverType, @NotNull JetType receiverType,
@NotNull CallableDescriptor receiverArgument @NotNull CallableDescriptor receiverArgument
) { ) {
ConstraintSystemImpl constraintSystem = new ConstraintSystemImpl(); ConstraintsBuilderImpl constraintsBuilder = new ConstraintsBuilderImpl();
for (TypeParameterDescriptor typeParameterDescriptor : receiverArgument.getTypeParameters()) { for (TypeParameterDescriptor typeParameterDescriptor : receiverArgument.getTypeParameters()) {
constraintSystem.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT); constraintsBuilder.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT);
} }
ReceiverDescriptor receiverParameter = receiverArgument.getReceiverParameter(); ReceiverDescriptor receiverParameter = receiverArgument.getReceiverParameter();
if (expectedReceiver.exists() && receiverParameter.exists()) { if (expectedReceiver.exists() && receiverParameter.exists()) {
constraintSystem.addSubtypingConstraint(receiverType, receiverParameter.getType(), ConstraintPosition.RECEIVER_POSITION); constraintsBuilder.addSubtypingConstraint(receiverType, receiverParameter.getType(), ConstraintPosition.RECEIVER_POSITION);
} }
else if (expectedReceiver.exists() || receiverParameter.exists()) { else if (expectedReceiver.exists() || receiverParameter.exists()) {
// Only one of receivers exist // Only one of receivers exist
return false; return false;
} }
return constraintSystem.isSuccessful() && constraintSystem.upperBoundsAreSatisfied(); return constraintsBuilder.isSuccessful() && constraintsBuilder.upperBoundsAreSatisfied();
} }
} }