diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java index 9c81ddadd08..e9e470c84ae 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java @@ -579,14 +579,15 @@ public class CandidateResolver { constraintSystem.addSubtypeConstraint(receiverType, receiverParameter.getType(), ConstraintPosition.RECEIVER_POSITION); } - ConstraintSystem constraintSystemWithRightTypeParameters = constraintSystem.replaceTypeVariables( + // Restore type variables before alpha-conversion + ConstraintSystem constraintSystemWithRightTypeParameters = constraintSystem.substituteTypeVariables( new Function() { - @Override - public TypeParameterDescriptor apply(@Nullable TypeParameterDescriptor typeParameterDescriptor) { - assert typeParameterDescriptor != null; - return candidate.getTypeParameters().get(typeParameterDescriptor.getIndex()); - } - }); + @Override + public TypeParameterDescriptor apply(@Nullable TypeParameterDescriptor typeParameterDescriptor) { + assert typeParameterDescriptor != null; + return candidate.getTypeParameters().get(typeParameterDescriptor.getIndex()); + } + }); candidateCall.setConstraintSystem(constraintSystemWithRightTypeParameters); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java index 74c32d9e32d..e89c3e73de3 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java @@ -39,7 +39,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static org.jetbrains.jet.lang.resolve.calls.inference.ConstraintPosition.EXPECTED_TYPE_POSITION; import static org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystemImpl.ConstraintKind.EQUAL; import static org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystemImpl.ConstraintKind.SUB_TYPE; import static org.jetbrains.jet.lang.resolve.calls.inference.TypeBoundsImpl.BoundKind.*; @@ -73,8 +72,7 @@ public class ConstraintSystemImpl implements ConstraintSystem { @Override public boolean hasViolatedUpperBound() { if (isSuccessful()) return false; - ConstraintSystem systemWithoutWeakConstraints = getSystemWithoutWeakConstraints(); - return systemWithoutWeakConstraints.getStatus().isSuccessful(); + return getSystemWithoutWeakConstraints().getStatus().isSuccessful(); } @Override @@ -193,21 +191,22 @@ public class ConstraintSystemImpl implements ConstraintSystem { @Override @NotNull public ConstraintSystem copy() { - return replaceTypeVariables(Functions.identity(), - new Function() { - @Override - public TypeBoundsImpl apply(TypeBoundsImpl typeBounds) { - return typeBounds.copy(); - } - }, - Conditions.alwaysTrue()); + return createNewConstraintSystemFromThis(Functions.identity(), + new Function() { + @Override + public TypeBoundsImpl apply(TypeBoundsImpl typeBounds) { + return typeBounds.copy(); + } + }, + Conditions.alwaysTrue()); } @NotNull - public ConstraintSystem replaceTypeVariables(@NotNull Function typeVariablesMap) { - return replaceTypeVariables(typeVariablesMap, - Functions.identity(), - Conditions.alwaysTrue()); + public ConstraintSystem substituteTypeVariables(@NotNull Function typeVariablesMap) { + return createNewConstraintSystemFromThis(typeVariablesMap, + // type bounds are proper types and don't contain other variables + Functions.identity(), + Conditions.alwaysTrue()); } @NotNull @@ -223,14 +222,14 @@ public class ConstraintSystemImpl implements ConstraintSystem { @NotNull public ConstraintSystem filterConstraints(@NotNull final Condition condition) { - return replaceTypeVariables(Functions.identity(), - new Function() { - @Override - public TypeBoundsImpl apply(TypeBoundsImpl typeBounds) { - return typeBounds.filter(condition); - } - }, - condition); + return createNewConstraintSystemFromThis(Functions.identity(), + new Function() { + @Override + public TypeBoundsImpl apply(TypeBoundsImpl typeBounds) { + return typeBounds.filter(condition); + } + }, + condition); } @NotNull @@ -251,25 +250,24 @@ public class ConstraintSystemImpl implements ConstraintSystem { } @NotNull - private ConstraintSystem replaceTypeVariables( - @NotNull Function typeVariablesMap, - @NotNull Function typeBoundsMap, - @NotNull Condition constraintPositionCondition - + private ConstraintSystem createNewConstraintSystemFromThis( + @NotNull Function substituteTypeVariable, + @NotNull Function replaceTypeBounds, + @NotNull Condition filterConstraintPosition ) { - ConstraintSystemImpl newConstraintSystem = new ConstraintSystemImpl(); + ConstraintSystemImpl newSystem = new ConstraintSystemImpl(); for (Map.Entry entry : typeParameterBounds.entrySet()) { TypeParameterDescriptor typeParameter = entry.getKey(); TypeBoundsImpl typeBounds = entry.getValue(); - TypeParameterDescriptor newTypeParameter = typeVariablesMap.apply(typeParameter); + TypeParameterDescriptor newTypeParameter = substituteTypeVariable.apply(typeParameter); assert newTypeParameter != null; - newConstraintSystem.typeParameterBounds.put(newTypeParameter, typeBoundsMap.apply(typeBounds)); + newSystem.typeParameterBounds.put(newTypeParameter, replaceTypeBounds.apply(typeBounds)); } - newConstraintSystem.errorConstraintPositions.addAll(ContainerUtil.filter(errorConstraintPositions, constraintPositionCondition)); - //todo if 'constraintPositionCondition' is not trivial, it's incorrect to just copy 'hasErrorInConstrainingTypes' - newConstraintSystem.hasErrorInConstrainingTypes = hasErrorInConstrainingTypes; - return newConstraintSystem; + newSystem.errorConstraintPositions.addAll(ContainerUtil.filter(errorConstraintPositions, filterConstraintPosition)); + //todo if 'filterConstraintPosition' is not trivial, it's incorrect to just copy 'hasErrorInConstrainingTypes' + newSystem.hasErrorInConstrainingTypes = hasErrorInConstrainingTypes; + return newSystem; } @Override