type inference errors report changed

This commit is contained in:
Svetlana Isakova
2012-07-08 15:21:47 +04:00
parent 73c1db571c
commit d7de3973d9
@@ -247,7 +247,8 @@ public class CallResolver {
} }
} }
return doResolveCallOrGetCachedResults(RESOLUTION_RESULTS_FOR_FUNCTION, context, prioritizedTasks, CallTransformer.FUNCTION_CALL_TRANSFORMER, functionReference); return doResolveCallOrGetCachedResults(RESOLUTION_RESULTS_FOR_FUNCTION, context, prioritizedTasks,
CallTransformer.FUNCTION_CALL_TRANSFORMER, functionReference);
} }
private <D extends CallableDescriptor, F extends D> OverloadResolutionResults<F> doResolveCallOrGetCachedResults( private <D extends CallableDescriptor, F extends D> OverloadResolutionResults<F> doResolveCallOrGetCachedResults(
@@ -298,22 +299,22 @@ public class CallResolver {
if (results.getResultCode() != OverloadResolutionResults.Code.DIRTY) return results; if (results.getResultCode() != OverloadResolutionResults.Code.DIRTY) return results;
Set<ResolvedCallWithTrace<D>> successful = Sets.newLinkedHashSet(); Set<ResolvedCallWithTrace<D>> successful = Sets.newLinkedHashSet();
Set<ResolvedCallWithTrace<D>> failed = Sets.newLinkedHashSet(); Set<ResolvedCallWithTrace<D>> failed = Sets.newLinkedHashSet();
for (ResolvedCall<? extends D> resolvedCall : results.getResultingCalls()) { for (ResolvedCall<? extends D> call : results.getResultingCalls()) {
if (!(resolvedCall instanceof ResolvedCallImpl)) continue; if (!(call instanceof ResolvedCallImpl)) continue;
ResolvedCallImpl<D> call = (ResolvedCallImpl<D>) resolvedCall; ResolvedCallImpl<D> resolvedCall = (ResolvedCallImpl<D>) call;
if (!call.hasUnknownTypeParameters()) { if (!resolvedCall.hasUnknownTypeParameters()) {
if (call.getStatus().isSuccess()) { if (resolvedCall.getStatus().isSuccess()) {
successful.add(call); successful.add(resolvedCall);
} }
else { else {
failed.add(call); failed.add(resolvedCall);
} }
continue; continue;
} }
ConstraintSystem constraintSystem = new ConstraintSystemImpl(); ConstraintSystem constraintSystem = new ConstraintSystemImpl();
D descriptor = call.getCandidateDescriptor(); D descriptor = resolvedCall.getCandidateDescriptor();
Map<TypeParameterDescriptor, TypeBounds> typeArgumentBounds = call.getTypeArgumentBounds(); Map<TypeParameterDescriptor, TypeBounds> typeArgumentBounds = resolvedCall.getTypeArgumentBounds();
for (TypeParameterDescriptor typeParameterDescriptor : descriptor.getTypeParameters()) { for (TypeParameterDescriptor typeParameterDescriptor : descriptor.getTypeParameters()) {
if (typeArgumentBounds.containsKey(typeParameterDescriptor)) { if (typeArgumentBounds.containsKey(typeParameterDescriptor)) {
constraintSystem.registerTypeVariable(typeParameterDescriptor, typeArgumentBounds.get(typeParameterDescriptor)); constraintSystem.registerTypeVariable(typeParameterDescriptor, typeArgumentBounds.get(typeParameterDescriptor));
@@ -322,12 +323,13 @@ public class CallResolver {
constraintSystem.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT); constraintSystem.registerTypeVariable(typeParameterDescriptor, Variance.INVARIANT);
} }
} }
constraintSystem.addConstraint(ConstraintSystemImpl.ConstraintType.SUPER_TYPE, context.expectedType, descriptor.getReturnType()); constraintSystem.addConstraint(ConstraintSystem.ConstraintType.SUPER_TYPE, context.expectedType, descriptor.getReturnType(),
ConstraintPosition.EXPECTED_TYPE_POSITION);
// constraints for function literals // constraints for function literals
// Value parameters // Value parameters
for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : call.getValueArguments().entrySet()) { for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : resolvedCall.getValueArguments().entrySet()) {
ResolvedValueArgument resolvedValueArgument = entry.getValue(); ResolvedValueArgument resolvedValueArgument = entry.getValue();
ValueParameterDescriptor valueParameterDescriptor = entry.getKey(); ValueParameterDescriptor valueParameterDescriptor = entry.getKey();
@@ -347,7 +349,8 @@ public class CallResolver {
constraintSystem.getSubstitutor().substitute(valueParameterDescriptor.getType(), Variance.INVARIANT), constraintSystem.getSubstitutor().substitute(valueParameterDescriptor.getType(), Variance.INVARIANT),
context.dataFlowInfo, traceForUnknown) : null; context.dataFlowInfo, traceForUnknown) : null;
if (type != null && !ErrorUtils.isErrorType(type)) { if (type != null && !ErrorUtils.isErrorType(type)) {
constraintSystem.addSubtypingConstraint(type, effectiveExpectedType); constraintSystem.addSubtypingConstraint(type, effectiveExpectedType, ConstraintPosition.valueParameterPosition(
valueParameterDescriptor.getIndex()));
} }
} }
} }
@@ -356,38 +359,31 @@ public class CallResolver {
if (constraintSystem.isSuccessful()) { if (constraintSystem.isSuccessful()) {
D substitute = (D) descriptor.substitute(constraintSystem.getSubstitutor()); D substitute = (D) descriptor.substitute(constraintSystem.getSubstitutor());
assert substitute != null; assert substitute != null;
replaceValueParametersWithSubstitutedOnes(call, substitute); replaceValueParametersWithSubstitutedOnes(resolvedCall, substitute);
call.setResultingDescriptor(substitute); //replacement resolvedCall.setResultingDescriptor(substitute); //replacement
// Here we type check the arguments with inferred types expected // Here we type check the arguments with inferred types expected
checkValueArgumentTypes(context, call, context.trace); checkValueArgumentTypes(context, resolvedCall, context.trace, null);
checkBounds(call, constraintSystem, context); checkBounds(resolvedCall, constraintSystem, context);
call.setHasUnknownTypeParameters(false); resolvedCall.setHasUnknownTypeParameters(false);
if (call.getStatus().isSuccess() || call.getStatus() == ResolutionStatus.UNKNOWN_STATUS) { if (resolvedCall.getStatus().isSuccess() || resolvedCall.getStatus() == ResolutionStatus.UNKNOWN_STATUS) {
call.addStatus(ResolutionStatus.SUCCESS); resolvedCall.addStatus(ResolutionStatus.SUCCESS);
successful.add(call); successful.add(resolvedCall);
} }
else { else {
failed.add(call); failed.add(resolvedCall);
} }
} }
else { else {
context.trace.report(TYPE_INFERENCE_FAILED.on(context.call.getCallElement(), new SolutionStatus() { List<JetType> argumentTypes = Lists.newArrayList();
@Override checkValueArgumentTypes(context, resolvedCall, context.trace, argumentTypes);
public boolean isSuccessful() { JetType receiverType = resolvedCall.getReceiverArgument().exists() ? resolvedCall.getReceiverArgument().getType() : null;
return false; reportTypeInferenceFailed(context.trace, context.call, InferenceErrorData.create(descriptor, constraintSystem,
} argumentTypes, receiverType));
@Override resolvedCall.addStatus(ResolutionStatus.TYPE_INFERENCE_ERROR);
public String toString() { failed.add(resolvedCall);
return "Type inference failed";
}
}));
call.addStatus(ResolutionStatus.TYPE_INFERENCE_ERROR);
checkValueArgumentTypes(context, call, context.trace);
failed.add(call);
} }
} }
if (results.getResultingCalls().size() > 1) { if (results.getResultingCalls().size() > 1) {
@@ -402,16 +398,24 @@ public class CallResolver {
return computeResultAndReportErrors(context.trace, tracing, successful, failed); return computeResultAndReportErrors(context.trace, tracing, successful, failed);
} }
private void reportTypeInferenceFailed(@NotNull BindingTrace trace, @NotNull Call call, @NotNull InferenceErrorData inferenceErrorData) {
JetExpression calleeExpression = call.getCalleeExpression();
PsiElement element = calleeExpression != null ? calleeExpression : call.getCallElement();
if (inferenceErrorData.constraintSystem.hasError()) {
trace.report(TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH.on(element, inferenceErrorData));
}
else if (inferenceErrorData.constraintSystem.hasContradiction()) {
trace.report(TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS.on(element, inferenceErrorData));
}
else {
trace.report(TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER.on(element, inferenceErrorData));
}
}
private <D extends CallableDescriptor> void checkBounds(ResolvedCallImpl<D> call, ConstraintSystem constraintSystem, BasicResolutionContext context) { private <D extends CallableDescriptor> void checkBounds(ResolvedCallImpl<D> call, ConstraintSystem constraintSystem, BasicResolutionContext context) {
for (TypeParameterDescriptor typeParameter : call.getCandidateDescriptor().getTypeParameters()) { for (TypeParameterDescriptor typeParameter : call.getCandidateDescriptor().getTypeParameters()) {
JetType type = constraintSystem.getValue(typeParameter); if (!constraintSystem.checkUpperBound(typeParameter)) {
JetType upperBound = typeParameter.getUpperBoundsAsType(); context.trace.report(Errors.TYPE_INFERENCE_UPPER_BOUND_VIOLATED.on(context.call.getCallElement(), InferenceErrorData.create(call.getCandidateDescriptor(), constraintSystem)));
JetType substitute = constraintSystem.getSubstitutor().substitute(upperBound, Variance.INVARIANT);
if (type != null) {
if (substitute == null || !JetTypeChecker.INSTANCE.isSubtypeOf(type, substitute)) {
context.trace.report(Errors.TYPE_INFERENCE_UPPER_BOUND_VIOLATED.on(context.call.getCallElement(), type, substitute != null ? substitute : upperBound));
}
} }
} }
} }
@@ -642,7 +646,7 @@ public class CallResolver {
candidateCall.addStatus(status); candidateCall.addStatus(status);
} }
else { else {
candidateCall.addStatus(checkAllValueArguments(context)); candidateCall.addStatus(checkAllValueArguments(context, null));
} }
} }
else { else {
@@ -678,7 +682,7 @@ public class CallResolver {
TypeParameterDescriptor typeParameterDescriptor = typeParameters.get(i); TypeParameterDescriptor typeParameterDescriptor = typeParameters.get(i);
candidateCall.recordTypeArgument(typeParameterDescriptor, typeArguments.get(i)); candidateCall.recordTypeArgument(typeParameterDescriptor, typeArguments.get(i));
} }
candidateCall.addStatus(checkAllValueArguments(context)); candidateCall.addStatus(checkAllValueArguments(context, null));
} }
else { else {
candidateCall.addStatus(OTHER_ERROR); candidateCall.addStatus(OTHER_ERROR);
@@ -745,7 +749,7 @@ public class CallResolver {
context.scope, argumentExpression, substituteDontCare.substitute(valueParameterDescriptor.getType(), Variance.INVARIANT), context.scope, argumentExpression, substituteDontCare.substitute(valueParameterDescriptor.getType(), Variance.INVARIANT),
context.dataFlowInfo, traceForUnknown) : null; context.dataFlowInfo, traceForUnknown) : null;
if (type != null && !ErrorUtils.isErrorType(type)) { if (type != null && !ErrorUtils.isErrorType(type)) {
constraintSystem.addSubtypingConstraint(type, effectiveExpectedType); constraintSystem.addSubtypingConstraint(type, effectiveExpectedType, ConstraintPosition.valueParameterPosition(valueParameterDescriptor.getIndex()));
} }
else { else {
candidateCall.argumentHasNoType(); candidateCall.argumentHasNoType();
@@ -758,32 +762,29 @@ public class CallResolver {
ReceiverDescriptor receiverArgument = candidateCall.getReceiverArgument(); ReceiverDescriptor receiverArgument = candidateCall.getReceiverArgument();
ReceiverDescriptor receiverParameter = candidateWithFreshVariables.getReceiverParameter(); ReceiverDescriptor receiverParameter = candidateWithFreshVariables.getReceiverParameter();
if (receiverArgument.exists() && receiverParameter.exists()) { if (receiverArgument.exists() && receiverParameter.exists()) {
constraintSystem.addSubtypingConstraint(receiverArgument.getType(), receiverParameter.getType()); constraintSystem.addSubtypingConstraint(receiverArgument.getType(), receiverParameter.getType(), ConstraintPosition.RECEIVER_POSITION);
} }
ConstraintSystemImpl constraintSystemWithRightTypeParameters = new ConstraintSystemImpl(constraintSystem.hasError(), constraintSystem.getErrorConstraintPositions());
for (TypeParameterDescriptor typeParameterDescriptor : candidate.getTypeParameters()) { for (TypeParameterDescriptor typeParameterDescriptor : candidate.getTypeParameters()) {
candidateCall.recordTypeBounds(typeParameterDescriptor, constraintSystem.getTypeBounds( TypeBounds typeBounds = constraintSystem.getTypeBounds(
candidateWithFreshVariables.getTypeParameters().get(typeParameterDescriptor.getIndex()))); candidateWithFreshVariables.getTypeParameters().get(typeParameterDescriptor.getIndex()));
candidateCall.recordTypeBounds(typeParameterDescriptor, typeBounds);
constraintSystemWithRightTypeParameters.registerTypeVariable(typeParameterDescriptor, typeBounds);
} }
// Solution // Solution
if (!constraintSystem.hasContradiction()) { if (!constraintSystem.hasContradiction()) {
candidateCall.setHasUnknownTypeParameters(true); candidateCall.setHasUnknownTypeParameters(true);
return SUCCESS; return SUCCESS;
} }
else { else {
context.tracing.typeInferenceFailed(context.trace, new SolutionStatus() { List<JetType> argumentTypes = Lists.newArrayList();
@Override ResolutionStatus argumentsStatus = checkAllValueArguments(context, argumentTypes);
public boolean isSuccessful() { JetType receiverType = candidateCall.getReceiverArgument().exists() ? candidateCall.getReceiverArgument().getType() : null;
return false; reportTypeInferenceFailed(context.trace, context.call, InferenceErrorData.create(candidate, constraintSystemWithRightTypeParameters, argumentTypes, receiverType));
} return TYPE_INFERENCE_ERROR.combine(argumentsStatus);
@Override
public String toString() {
return "Type inference failed";
}
});
return TYPE_INFERENCE_ERROR.combine(checkAllValueArguments(context));
} }
} }
@@ -856,8 +857,8 @@ public class CallResolver {
} }
} }
private <D extends CallableDescriptor, F extends D> ResolutionStatus checkAllValueArguments(CallResolutionContext<D, F> context) { private <D extends CallableDescriptor, F extends D> ResolutionStatus checkAllValueArguments(CallResolutionContext<D, F> context, @Nullable List<JetType> argumentTypes) {
ResolutionStatus result = checkValueArgumentTypes(context, context.candidateCall); ResolutionStatus result = checkValueArgumentTypes(context, context.candidateCall, argumentTypes);
ResolvedCall<D> candidateCall = context.candidateCall; ResolvedCall<D> candidateCall = context.candidateCall;
// Comment about a very special case. // Comment about a very special case.
@@ -893,7 +894,9 @@ public class CallResolver {
JetType effectiveReceiverArgumentType = safeAccess JetType effectiveReceiverArgumentType = safeAccess
? TypeUtils.makeNotNullable(receiverArgumentType) ? TypeUtils.makeNotNullable(receiverArgumentType)
: receiverArgumentType; : receiverArgumentType;
if (!typeChecker.isSubtypeOf(effectiveReceiverArgumentType, receiverParameter.getType())) { if (!TypeUtils.dependsOnTypeParameters(receiverParameter.getType(),
candidateCall.getCandidateDescriptor().getTypeParameters()) &&
!typeChecker.isSubtypeOf(effectiveReceiverArgumentType, receiverParameter.getType())) {
context.tracing.wrongReceiverType(context.candidateCall.getTrace(), receiverParameter, receiverArgument); context.tracing.wrongReceiverType(context.candidateCall.getTrace(), receiverParameter, receiverArgument);
result = OTHER_ERROR; result = OTHER_ERROR;
} }
@@ -905,11 +908,12 @@ public class CallResolver {
return result; return result;
} }
private <D extends CallableDescriptor> ResolutionStatus checkValueArgumentTypes(ResolutionContext context, ResolvedCallImpl<D> candidateCall) { private <D extends CallableDescriptor> ResolutionStatus checkValueArgumentTypes(ResolutionContext context, ResolvedCallImpl<D> candidateCall, List<JetType> argumentTypes) {
return checkValueArgumentTypes(context, candidateCall, candidateCall.getTrace()); return checkValueArgumentTypes(context, candidateCall, candidateCall.getTrace(), argumentTypes);
} }
private <D extends CallableDescriptor> ResolutionStatus checkValueArgumentTypes(ResolutionContext context, ResolvedCallImpl<D> candidateCall, BindingTrace trace) { private <D extends CallableDescriptor> ResolutionStatus checkValueArgumentTypes(ResolutionContext context, ResolvedCallImpl<D> candidateCall, BindingTrace trace,
@Nullable List<JetType> argumentTypes) {
ResolutionStatus result = SUCCESS; ResolutionStatus result = SUCCESS;
DataFlowInfo dataFlowInfo = context.dataFlowInfo; DataFlowInfo dataFlowInfo = context.dataFlowInfo;
for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : candidateCall.getValueArguments().entrySet()) { for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : candidateCall.getValueArguments().entrySet()) {
@@ -922,14 +926,19 @@ public class CallResolver {
if (expression == null) continue; if (expression == null) continue;
JetType expectedType = getEffectiveExpectedType(parameterDescriptor, argument); JetType expectedType = getEffectiveExpectedType(parameterDescriptor, argument);
JetTypeInfo typeInfo = expressionTypingServices.getTypeInfo(context.scope, expression, expectedType, dataFlowInfo, if (TypeUtils.dependsOnTypeParameters(expectedType, candidateCall.getCandidateDescriptor().getTypeParameters())) {
trace); expectedType = NO_EXPECTED_TYPE;
}
JetTypeInfo typeInfo = expressionTypingServices.getTypeInfo(context.scope, expression, expectedType, dataFlowInfo, trace);
JetType type = typeInfo.getType(); JetType type = typeInfo.getType();
if (argumentTypes != null) {
argumentTypes.add(type);
}
dataFlowInfo = dataFlowInfo.and(typeInfo.getDataFlowInfo()); dataFlowInfo = dataFlowInfo.and(typeInfo.getDataFlowInfo());
if (type == null || ErrorUtils.isErrorType(type)) { if (type == null || ErrorUtils.isErrorType(type)) {
candidateCall.argumentHasNoType(); candidateCall.argumentHasNoType();
} }
else if (!typeChecker.isSubtypeOf(type, expectedType)) { else if (expectedType != NO_EXPECTED_TYPE && !typeChecker.isSubtypeOf(type, expectedType)) {
// VariableDescriptor variableDescriptor = AutoCastUtils.getVariableDescriptorFromSimpleName(temporaryTrace.getBindingContext(), argument); // VariableDescriptor variableDescriptor = AutoCastUtils.getVariableDescriptorFromSimpleName(temporaryTrace.getBindingContext(), argument);
// if (variableDescriptor != null) { // if (variableDescriptor != null) {
// JetType autoCastType = null; // JetType autoCastType = null;