diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/Renderers.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/Renderers.java index 5b562284d21..38ecf8bba10 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/Renderers.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/Renderers.java @@ -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 substitutedDescriptors = Lists.newArrayList(); - Collection substitutors = inferenceErrorData.constraintSystem.getSubstitutors(); + Collection 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 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 ") diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystem.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsBuilder.java similarity index 56% rename from compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystem.java rename to compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsBuilder.java index 825f34d2155..001c1a69345 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystem.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsBuilder.java @@ -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 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 getTypeBoundsMap(); - @Nullable - TypeParameterDescriptor getFirstConflictingParameter(); + TypeConstraints getTypeConstraints(@NotNull TypeParameterDescriptor typeParameterDescriptor); @NotNull TypeSubstitutor getSubstitutor(); - @NotNull - Collection getSubstitutors(); - @Nullable - JetType getValue(TypeParameterDescriptor typeParameterDescriptor); + JetType getValue(@NotNull TypeParameterDescriptor typeParameterDescriptor); @NotNull - Collection getErrorConstraintPositions(); - - boolean checkUpperBound(@NotNull TypeParameterDescriptor typeParameterDescriptor); + Collection getTypeConstructorMismatchConstraintPositions(); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/InferenceErrorData.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/InferenceErrorData.java index 259eeae9a37..e76c1e95698 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/InferenceErrorData.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/InferenceErrorData.java @@ -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 valueArgumentsTypes; - private InferenceErrorData(@NotNull CallableDescriptor descriptor, @NotNull ConstraintSystem constraintSystem, + private InferenceErrorData(@NotNull CallableDescriptor descriptor, @NotNull ConstraintsBuilder constraintsBuilder, @Nullable List 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 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); } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java index 5052c89b9ab..1f377abf7c4 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java @@ -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(); } }