record ambiguous label references

This commit is contained in:
Svetlana Isakova
2012-11-14 20:50:39 +04:00
parent 6d3981503b
commit df057010f3
4 changed files with 31 additions and 7 deletions
@@ -233,6 +233,8 @@ public interface BindingContext {
WritableSlice<ClassDescriptor, Boolean> IS_ENUM_MOVED_TO_CLASS_OBJECT = Slices.createSimpleSlice(); WritableSlice<ClassDescriptor, Boolean> IS_ENUM_MOVED_TO_CLASS_OBJECT = Slices.createSimpleSlice();
WritableSlice<JetReferenceExpression, PsiElement> LABEL_TARGET = Slices.<JetReferenceExpression, PsiElement>sliceBuilder().build(); WritableSlice<JetReferenceExpression, PsiElement> LABEL_TARGET = Slices.<JetReferenceExpression, PsiElement>sliceBuilder().build();
WritableSlice<JetReferenceExpression, Collection<? extends PsiElement>> AMBIGUOUS_LABEL_TARGET =
Slices.<JetReferenceExpression, Collection<? extends PsiElement>>sliceBuilder().build();
WritableSlice<ValueParameterDescriptor, PropertyDescriptor> VALUE_PARAMETER_AS_PROPERTY = WritableSlice<ValueParameterDescriptor, PropertyDescriptor> VALUE_PARAMETER_AS_PROPERTY =
Slices.<ValueParameterDescriptor, PropertyDescriptor>sliceBuilder().build(); Slices.<ValueParameterDescriptor, PropertyDescriptor>sliceBuilder().build();
@@ -26,11 +26,10 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice; import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
import org.jetbrains.jet.util.slicedmap.Slices; import org.jetbrains.jet.util.slicedmap.Slices;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static org.jetbrains.jet.lang.diagnostics.Errors.AMBIGUOUS_LABEL;
import static org.jetbrains.jet.lang.resolve.BindingContext.AMBIGUOUS_LABEL_TARGET;
import static org.jetbrains.jet.lang.resolve.BindingContext.DECLARATION_TO_DESCRIPTOR; import static org.jetbrains.jet.lang.resolve.BindingContext.DECLARATION_TO_DESCRIPTOR;
/** /**
@@ -253,4 +252,22 @@ public class BindingContextUtils {
assert descriptor != null : "No descriptor for named declaration: " + declaration.getText() + "\n(of type " + declaration.getClass() + ")"; assert descriptor != null : "No descriptor for named declaration: " + declaration.getText() + "\n(of type " + declaration.getClass() + ")";
return descriptor; return descriptor;
} }
public static void reportAmbiguousLabel(
@NotNull BindingTrace trace,
@NotNull JetSimpleNameExpression targetLabel,
@NotNull Collection<DeclarationDescriptor> declarationsByLabel
) {
Collection<PsiElement> targets = Lists.newArrayList();
for (DeclarationDescriptor descriptor : declarationsByLabel) {
PsiElement element = descriptorToDeclaration(trace.getBindingContext(), descriptor);
if (element != null) {
targets.add(element);
}
}
if (!targets.isEmpty()) {
trace.record(AMBIGUOUS_LABEL_TARGET, targetLabel, targets);
}
trace.report(AMBIGUOUS_LABEL.on(targetLabel));
}
} }
@@ -90,7 +90,7 @@ public class LabelResolver {
else if (size == 0) { else if (size == 0) {
return resolveNamedLabel(labelName, labelExpression, reportUnresolved, context); return resolveNamedLabel(labelName, labelExpression, reportUnresolved, context);
} }
context.trace.report(AMBIGUOUS_LABEL.on(labelExpression)); BindingContextUtils.reportAmbiguousLabel(context.trace, labelExpression, declarationsByLabel);
return null; return null;
} }
@@ -117,7 +117,7 @@ public class LabelResolver {
} }
JetElement result = stack.peek(); JetElement result = stack.peek();
context.trace.record(BindingContext.LABEL_TARGET, labelExpression, result); context.trace.record(LABEL_TARGET, labelExpression, result);
return result; return result;
} }
@@ -172,7 +172,7 @@ public class LabelResolver {
} }
} }
else { else {
context.trace.report(AMBIGUOUS_LABEL.on(targetLabel)); BindingContextUtils.reportAmbiguousLabel(context.trace, targetLabel, declarationsByLabel);
} }
return LabeledReceiverResolutionResult.labelResolutionFailed(); return LabeledReceiverResolutionResult.labelResolutionFailed();
} }
@@ -35,6 +35,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.lang.resolve.BindingContext.AMBIGUOUS_LABEL_TARGET;
import static org.jetbrains.jet.lang.resolve.BindingContext.AMBIGUOUS_REFERENCE_TARGET; import static org.jetbrains.jet.lang.resolve.BindingContext.AMBIGUOUS_REFERENCE_TARGET;
public abstract class JetPsiReference implements PsiPolyVariantReference { public abstract class JetPsiReference implements PsiPolyVariantReference {
@@ -126,6 +127,10 @@ public abstract class JetPsiReference implements PsiPolyVariantReference {
if (psiElements.size() > 1) { if (psiElements.size() > 1) {
return PsiElementResolveResult.createResults(psiElements); return PsiElementResolveResult.createResults(psiElements);
} }
Collection<? extends PsiElement> labelTargets = bindingContext.get(AMBIGUOUS_LABEL_TARGET, myExpression);
if (labelTargets != null && !labelTargets.isEmpty()) {
return PsiElementResolveResult.createResults(labelTargets);
}
Collection<PsiElement> standardLibraryElements = resolveStandardLibrarySymbol(bindingContext); Collection<PsiElement> standardLibraryElements = resolveStandardLibrarySymbol(bindingContext);
if (standardLibraryElements.size() > 1) { if (standardLibraryElements.size() > 1) {
return PsiElementResolveResult.createResults(standardLibraryElements); return PsiElementResolveResult.createResults(standardLibraryElements);