use data flow info in constraint system

intersect data flow value possible types
add intersection type (or exact type for the most cases) as a lower bound to constraint system

always get common super type for intersection type as a result
(avoid returning it, even in error messages)
This commit is contained in:
Svetlana Isakova
2013-07-29 16:32:37 +04:00
parent b91846eb5d
commit 27cff93ed8
7 changed files with 195 additions and 2 deletions
@@ -578,7 +578,7 @@ public class CandidateResolver {
CallResolutionContext<?> newContext = context.replaceBindingTrace(traceToResolveArgument).replaceExpectedType(expectedType);
JetTypeInfo typeInfoForCall = argumentTypeResolver.getArgumentTypeInfo(argumentExpression, newContext,
resolveFunctionArgumentBodies, traceToResolveArgument);
JetType type = typeInfoForCall.getType();
JetType type = updateResultTypeForSmartCasts(typeInfoForCall.getType(), argumentExpression, context);
constraintSystem.addSubtypeConstraint(type, effectiveExpectedType, ConstraintPosition.getValueParameterPosition(
valueParameterDescriptor.getIndex()));
if (isErrorType != null) {
@@ -586,6 +586,22 @@ public class CandidateResolver {
}
}
@Nullable
private static JetType updateResultTypeForSmartCasts(
@Nullable JetType type,
@Nullable JetExpression argumentExpression,
@NotNull CallCandidateResolutionContext<?> context
) {
if (argumentExpression == null || type == null) return type;
DataFlowValue dataFlowValue =
DataFlowValueFactory.INSTANCE.createDataFlowValue(argumentExpression, type, context.trace.getBindingContext());
Set<JetType> possibleTypes = context.dataFlowInfo.getPossibleTypes(dataFlowValue);
if (possibleTypes.isEmpty()) return type;
return TypeUtils.intersect(JetTypeChecker.INSTANCE, possibleTypes);
}
private <D extends CallableDescriptor> ValueArgumentsCheckingResult checkAllValueArguments(
@NotNull CallCandidateResolutionContext<D> context,
@NotNull CallResolverUtil.ResolveArgumentsMode resolveFunctionArgumentBodies) {
@@ -110,6 +110,12 @@ public class ConstraintsUtil {
@Nullable
private static JetType commonSupertype(@NotNull Collection<JetType> lowerBounds) {
if (lowerBounds.isEmpty()) return null;
if (lowerBounds.size() == 1) {
JetType type = lowerBounds.iterator().next();
if (type.getConstructor() instanceof IntersectionTypeConstructor) {
return commonSupertype(type.getConstructor().getSupertypes());
}
}
return CommonSupertypes.commonSupertype(lowerBounds);
}
@@ -124,6 +130,7 @@ public class ConstraintsUtil {
@NotNull TypeConstraints typeConstraints
) {
if (suggestion == null) return false;
if (!suggestion.getConstructor().isDenotable()) return false;
if (typeConstraints.getExactBounds().size() > 1) return false;
for (JetType exactBound : typeConstraints.getExactBounds()) {