All tests pass with new error reporting

This commit is contained in:
Andrey Breslav
2011-09-14 20:57:55 +04:00
parent 8f5255dc68
commit 84af6a512a
20 changed files with 310 additions and 159 deletions
@@ -10,8 +10,9 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.diagnostics.JetDiagnostic;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.ErrorUtils;
@@ -54,10 +55,9 @@ public class DebugInfoAnnotator implements Annotator {
final BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(file);
final Set<JetReferenceExpression> unresolvedReferences = Sets.newHashSet();
for (JetDiagnostic diagnostic : bindingContext.getOld_Diagnostics()) {
if (diagnostic instanceof JetDiagnostic.UnresolvedReferenceError) {
JetDiagnostic.UnresolvedReferenceError error = (JetDiagnostic.UnresolvedReferenceError) diagnostic;
unresolvedReferences.add(error.getReferenceExpression());
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
if (diagnostic instanceof Errors.UnresolvedReferenceDiagnostic) {
unresolvedReferences.add(((Errors.UnresolvedReferenceDiagnostic) diagnostic).getReference());
}
}
@@ -1,7 +1,6 @@
package org.jetbrains.jet.plugin.annotations;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.lang.ASTNode;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.progress.ProcessCanceledException;
@@ -10,16 +9,19 @@ import com.intellij.psi.MultiRangeReference;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.diagnostics.ErrorHandler;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.diagnostics.Severity;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.AnalyzerFacade;
import org.jetbrains.jet.plugin.JetHighlighter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@@ -45,61 +47,92 @@ public class JetPsiChecker implements Annotator {
try {
final BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(file);
ErrorHandler errorHandler = new ErrorHandler() {
private final Set<DeclarationDescriptor> redeclarations = new HashSet<DeclarationDescriptor>();
@Override
public void unresolvedReference(@NotNull JetReferenceExpression referenceExpression) {
PsiReference reference = referenceExpression.getReference();
if (reference instanceof MultiRangeReference) {
MultiRangeReference mrr = (MultiRangeReference) reference;
for (TextRange range : mrr.getRanges()) {
holder.createErrorAnnotation(range.shiftRight(referenceExpression.getTextOffset()), "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
else {
holder.createErrorAnnotation(referenceExpression, "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
@Override
public void typeMismatch(@NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull JetType actualType) {
holder.createErrorAnnotation(expression, "Type mismatch: inferred type is " + actualType + " but " + expectedType + " was expected");
}
@Override
public void redeclaration(@NotNull DeclarationDescriptor existingDescriptor, @NotNull DeclarationDescriptor redeclaredDescriptor) {
markRedeclaration(existingDescriptor);
markRedeclaration(redeclaredDescriptor);
}
private void markRedeclaration(DeclarationDescriptor redeclaration) {
if (!redeclarations.add(redeclaration)) return;
PsiElement declarationPsiElement = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, redeclaration);
if (declarationPsiElement instanceof JetNamedDeclaration) {
PsiElement nameIdentifier = ((JetNamedDeclaration) declarationPsiElement).getNameIdentifier();
if (nameIdentifier != null) {
holder.createErrorAnnotation(nameIdentifier, "Redeclaration");
}
}
else if (declarationPsiElement != null) {
holder.createErrorAnnotation(declarationPsiElement, "Redeclaration");
}
}
@Override
public void genericError(@NotNull ASTNode node, @NotNull String errorMessage) {
holder.createErrorAnnotation(node, errorMessage);
}
@Override
public void genericWarning(@NotNull ASTNode node, @NotNull String message) {
holder.createWarningAnnotation(node, message);
}
};
// ErrorHandler errorHandler = new ErrorHandler() {
// private final Set<DeclarationDescriptor> redeclarations = new HashSet<DeclarationDescriptor>();
//
// @Override
// public void unresolvedReference(@NotNull JetReferenceExpression referenceExpression) {
// PsiReference reference = referenceExpression.getReference();
// if (reference instanceof MultiRangeReference) {
// MultiRangeReference mrr = (MultiRangeReference) reference;
// for (TextRange range : mrr.getRanges()) {
// holder.createErrorAnnotation(range.shiftRight(referenceExpression.getTextOffset()), "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
// }
// }
// else {
// holder.createErrorAnnotation(referenceExpression, "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
// }
// }
//
// @Override
// public void typeMismatch(@NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull JetType actualType) {
// holder.createErrorAnnotation(expression, "Type mismatch: inferred type is " + actualType + " but " + expectedType + " was expected");
// }
//
// @Override
// public void redeclaration(@NotNull DeclarationDescriptor existingDescriptor, @NotNull DeclarationDescriptor redeclaredDescriptor) {
// markRedeclaration(existingDescriptor);
// markRedeclaration(redeclaredDescriptor);
// }
//
// private void markRedeclaration(DeclarationDescriptor redeclaration) {
// if (!redeclarations.add(redeclaration)) return;
// PsiElement declarationPsiElement = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, redeclaration);
// if (declarationPsiElement instanceof JetNamedDeclaration) {
// PsiElement nameIdentifier = ((JetNamedDeclaration) declarationPsiElement).getNameIdentifier();
// if (nameIdentifier != null) {
// holder.createErrorAnnotation(nameIdentifier, "Redeclaration");
// }
// }
// else if (declarationPsiElement != null) {
// holder.createErrorAnnotation(declarationPsiElement, "Redeclaration");
// }
// }
//
// @Override
// public void genericError(@NotNull ASTNode node, @NotNull String errorMessage) {
// holder.createErrorAnnotation(node, errorMessage);
// }
//
// @Override
// public void genericWarning(@NotNull ASTNode node, @NotNull String message) {
// holder.createWarningAnnotation(node, message);
// }
// };
if (errorReportingEnabled) {
ErrorHandler.applyHandler(errorHandler, bindingContext);
// ErrorHandler.applyHandler(errorHandler, bindingContext);
Collection<Diagnostic> diagnostics = bindingContext.getDiagnostics();
Set<DeclarationDescriptor> redeclarations = new HashSet<DeclarationDescriptor>();
for (Diagnostic diagnostic : diagnostics) {
if (diagnostic.getSeverity() == Severity.ERROR) {
if (diagnostic instanceof Errors.UnresolvedReferenceDiagnostic) {
Errors.UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic = (Errors.UnresolvedReferenceDiagnostic) diagnostic;
JetReferenceExpression referenceExpression = unresolvedReferenceDiagnostic.getReference();
PsiReference reference = referenceExpression.getReference();
if (reference instanceof MultiRangeReference) {
MultiRangeReference mrr = (MultiRangeReference) reference;
for (TextRange range : mrr.getRanges()) {
holder.createErrorAnnotation(range.shiftRight(referenceExpression.getTextOffset()), "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
else {
holder.createErrorAnnotation(referenceExpression, "Unresolved").setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
else if (diagnostic instanceof Errors.RedeclarationDiagnostic) {
Errors.RedeclarationDiagnostic redeclarationDiagnostic = (Errors.RedeclarationDiagnostic) diagnostic;
markRedeclaration(redeclarations, redeclarationDiagnostic.getA(), bindingContext, holder);
markRedeclaration(redeclarations, redeclarationDiagnostic.getB(), bindingContext, holder);
}
else {
holder.createErrorAnnotation(diagnostic.getFactory().getMarkerPosition(diagnostic), diagnostic.getMessage());
}
}
else if (diagnostic.getSeverity() == Severity.WARNING) {
holder.createWarningAnnotation(diagnostic.getFactory().getMarkerPosition(diagnostic), diagnostic.getMessage());
}
}
}
highlightBackingFields(holder, file, bindingContext);
@@ -130,6 +163,20 @@ public class JetPsiChecker implements Annotator {
}
}
}
private void markRedeclaration(Set<DeclarationDescriptor> redeclarations, DeclarationDescriptor redeclaration, BindingContext bindingContext, AnnotationHolder holder) {
if (!redeclarations.add(redeclaration)) return;
PsiElement declarationPsiElement = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, redeclaration);
if (declarationPsiElement instanceof JetNamedDeclaration) {
PsiElement nameIdentifier = ((JetNamedDeclaration) declarationPsiElement).getNameIdentifier();
if (nameIdentifier != null) {
holder.createErrorAnnotation(nameIdentifier, "Redeclaration");
}
}
else if (declarationPsiElement != null) {
holder.createErrorAnnotation(declarationPsiElement, "Redeclaration");
}
}
private void highlightBackingFields(final AnnotationHolder holder, JetFile file, final BindingContext bindingContext) {
@@ -3,6 +3,7 @@ package org.jetbrains.jet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.ErrorHandler;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.diagnostics.JetDiagnostic;
import org.jetbrains.jet.lang.psi.JetReferenceExpression;
import org.jetbrains.jet.lang.resolve.BindingContext;
@@ -67,7 +68,10 @@ public class JetTestUtils {
@Override
public void report(@NotNull Diagnostic diagnostic) {
throw new UnsupportedOperationException(); // TODO
if (diagnostic instanceof Errors.UnresolvedReferenceDiagnostic) {
Errors.UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic = (Errors.UnresolvedReferenceDiagnostic) diagnostic;
throw new IllegalStateException("Unresolved: " + unresolvedReferenceDiagnostic.getReference().getText());
}
}
};
}
@@ -9,11 +9,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetTestCaseBase;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.diagnostics.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.cfg.JetFlowInformationProvider;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.diagnostics.DiagnosticHolder;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.java.JavaPackageScope;
@@ -527,7 +527,7 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
}
private WritableScopeImpl addImports(JetScope scope) {
WritableScopeImpl writableScope = new WritableScopeImpl(scope, scope.getContainingDeclaration(), ErrorHandler.DO_NOTHING);
WritableScopeImpl writableScope = new WritableScopeImpl(scope, scope.getContainingDeclaration(), DiagnosticHolder.DO_NOTHING);
writableScope.importScope(library.getLibraryScope());
JavaSemanticServices javaSemanticServices = new JavaSemanticServices(getProject(), semanticServices, JetTestUtils.DUMMY_TRACE);
writableScope.importScope(new JavaPackageScope("", null, javaSemanticServices));
@@ -637,7 +637,7 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
trace.record(BindingContext.CLASS, classElement, classDescriptor);
final WritableScope parameterScope = new WritableScopeImpl(scope, classDescriptor, trace.getErrorHandler());
final WritableScope parameterScope = new WritableScopeImpl(scope, classDescriptor, trace);
// This call has side-effects on the parameterScope (fills it in)
List<TypeParameterDescriptor> typeParameters
@@ -656,7 +656,7 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
// }
boolean open = classElement.hasModifier(JetTokens.OPEN_KEYWORD);
final WritableScope memberDeclarations = new WritableScopeImpl(JetScope.EMPTY, classDescriptor, trace.getErrorHandler());
final WritableScope memberDeclarations = new WritableScopeImpl(JetScope.EMPTY, classDescriptor, trace);
List<JetDeclaration> declarations = classElement.getDeclarations();
for (JetDeclaration declaration : declarations) {