From ab7133a1a6c4b63e8a34fc589766303c8c6cf707 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 10 Oct 2013 16:05:12 +0400 Subject: [PATCH] improved error message added debug info about constraint system status --- .../calls/inference/ConstraintsUtil.java | 30 +++++++++++++++++++ .../ControlStructureTypingUtils.java | 19 ++++++++---- .../calls/inference/ConstraintSystemImpl.java | 2 ++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java index b0af2476eb5..7a7e124de7b 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java @@ -24,6 +24,8 @@ import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.*; public class ConstraintsUtil { @@ -110,4 +112,32 @@ public class ConstraintsUtil { } return true; } + + public static String getDebugMessageForStatus(@NotNull ConstraintSystemStatus status) { + StringBuilder sb = new StringBuilder(); + List interestingMethods = Lists.newArrayList(); + for (Method method : status.getClass().getMethods()) { + String name = method.getName(); + boolean isInteresting = name.startsWith("is") || name.startsWith("has") && !name.equals("hashCode"); + if (method.getParameterTypes().length == 0 && isInteresting) { + interestingMethods.add(method); + } + } + for (Iterator iterator = interestingMethods.iterator(); iterator.hasNext(); ) { + Method method = iterator.next(); + try { + sb.append("-").append(method.getName()).append(": ").append(method.invoke(status)); + if (iterator.hasNext()) { + sb.append("\n"); + } + } + catch (IllegalAccessException e) { + sb.append(e.getMessage()); + } + catch (InvocationTargetException e) { + sb.append(e.getMessage()); + } + } + return sb.toString(); + } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingUtils.java index bacdea5963b..f763f60442e 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingUtils.java @@ -35,6 +35,7 @@ import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo; import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintPosition; import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystem; import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystemStatus; +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.model.MutableDataFlowInfoForArguments; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; @@ -345,13 +346,13 @@ public class ControlStructureTypingUtils { if (status.hasErrorInConstrainingTypes()) { return; } + JetExpression expression = call.getCalleeExpression(); + if (expression == null) return; if (status.hasOnlyErrorsFromPosition(ConstraintPosition.EXPECTED_TYPE_POSITION) || status.hasConflictingConstraints()) { - JetExpression expression = call.getCalleeExpression(); - if (expression != null) { - expression.accept(checkTypeVisitor, new CheckTypeContext(trace, data.expectedType)); - } + expression.accept(checkTypeVisitor, new CheckTypeContext(trace, data.expectedType)); return; } + throwError("Expression: " + expression.getText() + ".\nConstraint system status: \n" + ConstraintsUtil.getDebugMessageForStatus(status)); super.typeInferenceFailed(trace, data); } }; @@ -365,7 +366,15 @@ public class ControlStructureTypingUtils { } private void throwError() { - throw new IllegalStateException("Resolution error of this type shouldn't occur for " + debugName); + throwError(null); + } + + protected void throwError(@Nullable String additionalInformation) { + String errorMessage = "Resolution error of this type shouldn't occur for " + debugName; + if (additionalInformation != null) { + errorMessage += ".\n" + additionalInformation; + } + throw new IllegalStateException(errorMessage); } @Override 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 e89c3e73de3..ffd8f65e792 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 @@ -59,6 +59,8 @@ public class ConstraintSystemImpl implements ConstraintSystem { private boolean hasErrorInConstrainingTypes; private final ConstraintSystemStatus constraintSystemStatus = new ConstraintSystemStatus() { + // for debug ConstraintsUtil.getDebugMessageForStatus might be used + @Override public boolean isSuccessful() { return !hasContradiction() && !hasUnknownParameters();