refactoring: extracted method 'reportTypeInferenceExpectedTypeMismatch'

to reuse it later from 'tracing for if'
changed required/found types order in message to synchronize it with 'TYPE_MISMATCH' error
This commit is contained in:
Svetlana Isakova
2013-07-25 18:28:36 +04:00
parent 5d2ae9aec5
commit 9eda531eea
5 changed files with 24 additions and 17 deletions
@@ -353,7 +353,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, ExtendedInferenceErrorData> TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, ExtendedInferenceErrorData> TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, InferenceErrorData> TYPE_INFERENCE_UPPER_BOUND_VIOLATED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<PsiElement, JetType, JetType> TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<JetExpression, JetType, JetType> TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH = DiagnosticFactory2.create(ERROR);
// Callable references
@@ -423,7 +423,7 @@ public class DefaultErrorMessages {
MAP.put(TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER, "Type inference failed: {0}", TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER_RENDERER);
MAP.put(TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH, "Type inference failed: {0}", TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH_RENDERER);
MAP.put(TYPE_INFERENCE_UPPER_BOUND_VIOLATED, "{0}", TYPE_INFERENCE_UPPER_BOUND_VIOLATED_RENDERER);
MAP.put(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH, "Type inference failed. Expected type mismatch: found: {0} required: {1}", RENDER_TYPE, RENDER_TYPE);
MAP.put(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH, "Type inference failed. Expected type mismatch: found: {1} required: {0}", RENDER_TYPE, RENDER_TYPE);
MAP.put(WRONG_NUMBER_OF_TYPE_ARGUMENTS, "{0,choice,0#No type arguments|1#Type argument|1<{0,number,integer} type arguments} expected", null);
MAP.put(NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION, "{0,choice,0#No type arguments|1#Type argument|1<{0,number,integer} type arguments} expected. " +
@@ -351,8 +351,8 @@ public class CandidateResolver {
|| (expression instanceof JetFunctionLiteralExpression)) {
return;
}
JetType type = updateResultTypeIfNotDenotable(context, expression);
checkResultType(type, argument, context);
JetType type = updateResultArgumentTypeIfNotDenotable(context, expression);
checkResultArgumentType(type, argument, context);
return;
}
@@ -388,7 +388,7 @@ public class CandidateResolver {
}
@Nullable
private <D extends CallableDescriptor> JetType updateResultTypeIfNotDenotable(
private <D extends CallableDescriptor> JetType updateResultArgumentTypeIfNotDenotable(
@NotNull CallCandidateResolutionContext<D> context,
@NotNull JetExpression expression
) {
@@ -402,7 +402,7 @@ public class CandidateResolver {
return type;
}
private <D extends CallableDescriptor> void checkResultType(
private <D extends CallableDescriptor> void checkResultArgumentType(
@Nullable JetType type,
@NotNull ValueArgument argument,
@NotNull CallCandidateResolutionContext<D> context
@@ -21,6 +21,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory2;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingTrace;
@@ -33,7 +34,6 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue;
import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.Variance;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
import org.jetbrains.jet.lexer.JetToken;
@@ -223,14 +223,7 @@ public class TracingStrategyImpl implements TracingStrategy {
return;
}
if (constraintSystem.hasOnlyExpectedTypeMismatch()) {
JetType declaredReturnType = data.descriptor.getReturnType();
if (declaredReturnType == null) return;
JetType substitutedReturnType = constraintSystem.getResultingSubstitutor().substitute(declaredReturnType, Variance.INVARIANT);
assert substitutedReturnType != null; //todo
assert !noExpectedType(data.expectedType) : "Expected type doesn't exist, but there is an expected type mismatch error";
trace.report(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH.on(reference, substitutedReturnType, data.expectedType));
reportTypeInferenceExpectedTypeMismatch(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH, reference, data, trace);
}
else if (constraintSystem.hasTypeConstructorMismatch()) {
trace.report(TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH.on(reference, data));
@@ -244,6 +237,20 @@ public class TracingStrategyImpl implements TracingStrategy {
}
}
public static void reportTypeInferenceExpectedTypeMismatch(
@NotNull DiagnosticFactory2<JetExpression, JetType, JetType> diagnosticFactory,
@NotNull JetExpression reportOn, @NotNull InferenceErrorData.ExtendedInferenceErrorData data, @NotNull BindingTrace trace
) {
JetType declaredReturnType = data.descriptor.getReturnType();
if (declaredReturnType == null) return;
JetType substitutedReturnType = data.constraintSystem.getResultingSubstitutor().substitute(declaredReturnType, Variance.INVARIANT);
assert substitutedReturnType != null; //todo
assert !noExpectedType(data.expectedType) : "Expected type doesn't exist, but there is an expected type mismatch error";
trace.report(diagnosticFactory.on(reportOn, data.expectedType, substitutedReturnType));
}
@Override
public void upperBoundViolated(@NotNull BindingTrace trace, @NotNull InferenceErrorData inferenceErrorData) {
trace.report(Errors.TYPE_INFERENCE_UPPER_BOUND_VIOLATED.on(reference, inferenceErrorData));
@@ -46,8 +46,8 @@ public class IdeErrorMessages {
MAP.put(TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER, "<html>Type inference failed: {0}</html>", HTML_TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER_RENDERER);
MAP.put(TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH, "<html>Type inference failed: {0}</html>", HTML_TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH_RENDERER);
MAP.put(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH, tableForTypes("Type inference failed. Expected type mismatch: ",
"found: ", TextElementType.ERROR,
"required: ", TextElementType.STRONG), HTML_RENDER_TYPE, HTML_RENDER_TYPE);
"required: ", TextElementType.STRONG,
"found: ", TextElementType.ERROR), HTML_RENDER_TYPE, HTML_RENDER_TYPE);
MAP.put(TYPE_INFERENCE_UPPER_BOUND_VIOLATED, "<html>{0}</html>", HTML_TYPE_INFERENCE_UPPER_BOUND_VIOLATED_RENDERER);
MAP.put(WRONG_SETTER_PARAMETER_TYPE, "<html>Setter parameter type must be equal to the type of the property." +