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.resolve.calls.ResolvedCall;
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.TypeBounds;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
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.Iterator;
import java.util.List;
import java.util.Map;
/**
* @author svtk
@@ -169,16 +168,17 @@ public class Renderers {
public static TabledDescriptorRenderer renderConflictingSubstitutionsInferenceError(InferenceErrorData inferenceErrorData,
TabledDescriptorRenderer result) {
assert inferenceErrorData.constraintSystem.hasConflictingParameters();
assert inferenceErrorData.constraintsBuilder.hasConflictingParameters();
Collection<CallableDescriptor> substitutedDescriptors = Lists.newArrayList();
Collection<TypeSubstitutor> substitutors = inferenceErrorData.constraintSystem.getSubstitutors();
Collection<TypeSubstitutor> substitutors = ConstraintsUtil.getSubstitutorsForConflictingParameters(
inferenceErrorData.constraintsBuilder);
for (TypeSubstitutor substitutor : substitutors) {
CallableDescriptor substitutedDescriptor = inferenceErrorData.descriptor.substitute(substitutor);
substitutedDescriptors.add(substitutedDescriptor);
}
TypeParameterDescriptor firstConflictingParameter = inferenceErrorData.constraintSystem.getFirstConflictingParameter();
TypeParameterDescriptor firstConflictingParameter = ConstraintsUtil.getFirstConflictingParameter(inferenceErrorData.constraintsBuilder);
assert firstConflictingParameter != null;
result.text(newText()
@@ -200,7 +200,7 @@ public class Renderers {
valueArgumentTypes.add(valueParameterDescriptor.getType());
JetType actualType = inferenceErrorData.valueArgumentsTypes.get(valueParameterDescriptor.getIndex());
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(
inferenceErrorData.receiverArgumentType,
inferenceErrorData.valueArgumentsTypes,
inferenceErrorData.constraintSystem
.getErrorConstraintPositions()));
inferenceErrorData.constraintsBuilder
.getTypeConstructorMismatchConstraintPositions()));
}
public static TabledDescriptorRenderer renderNoInformationForParameterError(InferenceErrorData inferenceErrorData,
TabledDescriptorRenderer renderer) {
TypeParameterDescriptor firstUnknownParameter = null;
for (Map.Entry<TypeParameterDescriptor, TypeBounds> entry : inferenceErrorData.constraintSystem.getTypeBoundsMap().entrySet()) {
if (entry.getValue().isEmpty()) {
firstUnknownParameter = entry.getKey();
for (TypeParameterDescriptor typeParameter : inferenceErrorData.constraintsBuilder.getTypeParameters()) {
if (inferenceErrorData.constraintsBuilder.getTypeConstraints(typeParameter).isEmpty()) {
firstUnknownParameter = typeParameter;
break;
}
}
@@ -253,7 +253,7 @@ public class Renderers {
public static TabledDescriptorRenderer renderUpperBoundViolatedInferenceError(InferenceErrorData inferenceErrorData, TabledDescriptorRenderer result) {
TypeParameterDescriptor typeParameterDescriptor = null;
for (TypeParameterDescriptor typeParameter : inferenceErrorData.descriptor.getTypeParameters()) {
if (!inferenceErrorData.constraintSystem.checkUpperBound(typeParameter)) {
if (!ConstraintsUtil.checkUpperBoundIsSatisfied(inferenceErrorData.constraintsBuilder, typeParameter)) {
typeParameterDescriptor = typeParameter;
break;
}
@@ -264,9 +264,9 @@ public class Renderers {
.table(newTable().
descriptor(inferenceErrorData.descriptor));
JetType type = inferenceErrorData.constraintSystem.getValue(typeParameterDescriptor);
JetType type = inferenceErrorData.constraintsBuilder.getValue(typeParameterDescriptor);
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()
.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 java.util.Collection;
import java.util.Map;
import java.util.Set;
/**
* @author svtk
*/
public interface ConstraintSystem {
enum ConstraintType {
SUB_TYPE, SUPER_TYPE, EQUAL
}
JetType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE");
public interface ConstraintsBuilder {
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();
@@ -56,24 +51,15 @@ public interface ConstraintSystem {
boolean hasTypeConstructorMismatch();
TypeBounds getTypeBounds(TypeParameterDescriptor typeParameterDescriptor);
Map<TypeParameterDescriptor, TypeBounds> getTypeBoundsMap();
@Nullable
TypeParameterDescriptor getFirstConflictingParameter();
TypeConstraints getTypeConstraints(@NotNull TypeParameterDescriptor typeParameterDescriptor);
@NotNull
TypeSubstitutor getSubstitutor();
@NotNull
Collection<TypeSubstitutor> getSubstitutors();
@Nullable
JetType getValue(TypeParameterDescriptor typeParameterDescriptor);
JetType getValue(@NotNull TypeParameterDescriptor typeParameterDescriptor);
@NotNull
Collection<ConstraintPosition> getErrorConstraintPositions();
boolean checkUpperBound(@NotNull TypeParameterDescriptor typeParameterDescriptor);
Collection<ConstraintPosition> getTypeConstructorMismatchConstraintPositions();
}
@@ -19,35 +19,33 @@ package org.jetbrains.jet.lang.resolve.calls.inference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import java.util.List;
import java.util.Map;
/**
* @author svtk
*/
public class InferenceErrorData {
public final CallableDescriptor descriptor;
public final ConstraintSystem constraintSystem;
public final ConstraintsBuilder constraintsBuilder;
public final JetType receiverArgumentType;
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) {
this.descriptor = descriptor;
this.constraintSystem = constraintSystem;
this.constraintsBuilder = constraintsBuilder;
this.receiverArgumentType = receiverArgumentType;
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) {
return new InferenceErrorData(descriptor, constraintSystem, valueArgumentsTypes, receiverArgumentType);
return new InferenceErrorData(descriptor, constraintsBuilder, valueArgumentsTypes, receiverArgumentType);
}
public static InferenceErrorData create(@NotNull CallableDescriptor descriptor, @NotNull ConstraintSystem constraintSystem) {
return new InferenceErrorData(descriptor, constraintSystem, null, null);
public static InferenceErrorData create(@NotNull CallableDescriptor descriptor, @NotNull ConstraintsBuilder constraintsBuilder) {
return new InferenceErrorData(descriptor, constraintsBuilder, null, null);
}
}
@@ -232,20 +232,20 @@ public class ExpressionTypingUtils {
@NotNull JetType receiverType,
@NotNull CallableDescriptor receiverArgument
) {
ConstraintSystemImpl constraintSystem = new ConstraintSystemImpl();
ConstraintsBuilderImpl constraintsBuilder = new ConstraintsBuilderImpl();
for (TypeParameterDescriptor typeParameterDescriptor : receiverArgument.getTypeParameters()) {
constraintSystem.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT);
constraintsBuilder.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT);
}
ReceiverDescriptor receiverParameter = receiverArgument.getReceiverParameter();
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()) {
// Only one of receivers exist
return false;
}
return constraintSystem.isSuccessful() && constraintSystem.upperBoundsAreSatisfied();
return constraintsBuilder.isSuccessful() && constraintsBuilder.upperBoundsAreSatisfied();
}
}