KT-960 import namespace should give unresolved reference
This commit is contained in:
@@ -69,22 +69,20 @@ public class ImportsResolver {
|
||||
if (importedReference == null) return;
|
||||
Collection<DeclarationDescriptor> descriptors;
|
||||
if (importedReference instanceof JetQualifiedExpression) {
|
||||
descriptors = lookupDescriptorsForQualifiedExpression((JetQualifiedExpression) importedReference, namespaceScope);
|
||||
descriptors = lookupDescriptorsForQualifiedExpression((JetQualifiedExpression) importedReference,
|
||||
namespaceScope, !importDirective.isAllUnder());
|
||||
}
|
||||
else {
|
||||
assert importedReference instanceof JetSimpleNameExpression;
|
||||
|
||||
if (importDirective.isAllUnder()) {
|
||||
// Example: import java.*
|
||||
descriptors = lookupDescriptorsForSimpleNameReference((JetSimpleNameExpression) importedReference, namespaceScope);
|
||||
}
|
||||
else {
|
||||
// Example: import IDENTIFIER
|
||||
// Should be unresolved error
|
||||
descriptors = lookupDescriptorsForSimpleNameReference((JetSimpleNameExpression) importedReference, JetScope.EMPTY);
|
||||
trace.record(BindingContext.RESOLUTION_SCOPE, importedReference, namespaceScope);
|
||||
}
|
||||
// for "import java.*" there's should be a correct resolving
|
||||
// but for "import SomeClass" we want to get unresolved error, even if SomeClass descriptor exist in outer scope
|
||||
JetScope resolveScope = importDirective.isAllUnder() ? namespaceScope : JetScope.EMPTY;
|
||||
|
||||
descriptors = lookupDescriptorsForSimpleNameReference((JetSimpleNameExpression) importedReference,
|
||||
resolveScope, namespaceScope, !importDirective.isAllUnder());
|
||||
}
|
||||
|
||||
JetSimpleNameExpression referenceExpression = getLastReference(importedReference);
|
||||
if (importDirective.isAllUnder()) {
|
||||
if (referenceExpression == null || !canImportMembersFrom(descriptors, referenceExpression)) return;
|
||||
@@ -147,16 +145,19 @@ public class ImportsResolver {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<DeclarationDescriptor> lookupDescriptorsForQualifiedExpression(@NotNull JetQualifiedExpression importedReference, @NotNull JetScope outerScope) {
|
||||
private Collection<DeclarationDescriptor> lookupDescriptorsForQualifiedExpression(
|
||||
@NotNull JetQualifiedExpression importedReference, @NotNull JetScope outerScope, boolean isForLastPart) {
|
||||
|
||||
JetExpression receiverExpression = importedReference.getReceiverExpression();
|
||||
Collection<DeclarationDescriptor> declarationDescriptors;
|
||||
if (receiverExpression instanceof JetQualifiedExpression) {
|
||||
declarationDescriptors = lookupDescriptorsForQualifiedExpression((JetQualifiedExpression) receiverExpression, outerScope);
|
||||
declarationDescriptors = lookupDescriptorsForQualifiedExpression((JetQualifiedExpression) receiverExpression, outerScope, false);
|
||||
}
|
||||
else {
|
||||
assert receiverExpression instanceof JetSimpleNameExpression;
|
||||
declarationDescriptors = lookupDescriptorsForSimpleNameReference((JetSimpleNameExpression) receiverExpression, outerScope);
|
||||
declarationDescriptors = lookupDescriptorsForSimpleNameReference((JetSimpleNameExpression) receiverExpression, outerScope, false);
|
||||
}
|
||||
|
||||
JetExpression selectorExpression = importedReference.getSelectorExpression();
|
||||
assert selectorExpression instanceof JetSimpleNameExpression;
|
||||
JetSimpleNameExpression selector = (JetSimpleNameExpression) selectorExpression;
|
||||
@@ -166,7 +167,7 @@ public class ImportsResolver {
|
||||
}
|
||||
for (DeclarationDescriptor declarationDescriptor : declarationDescriptors) {
|
||||
if (declarationDescriptor instanceof NamespaceDescriptor) {
|
||||
return lookupDescriptorsForSimpleNameReference(selector, ((NamespaceDescriptor) declarationDescriptor).getMemberScope());
|
||||
return lookupDescriptorsForSimpleNameReference(selector, ((NamespaceDescriptor) declarationDescriptor).getMemberScope(), isForLastPart);
|
||||
}
|
||||
if (declarationDescriptor instanceof ClassDescriptor) {
|
||||
return lookupObjectMembers(selector, (ClassDescriptor) declarationDescriptor);
|
||||
@@ -195,7 +196,9 @@ public class ImportsResolver {
|
||||
return canImport;
|
||||
}
|
||||
|
||||
private boolean canImportMembersFrom(@NotNull DeclarationDescriptor descriptor, @NotNull JetSimpleNameExpression reference, @NotNull BindingTrace trace) {
|
||||
private boolean canImportMembersFrom(@NotNull DeclarationDescriptor descriptor,
|
||||
@NotNull JetSimpleNameExpression reference,
|
||||
@NotNull BindingTrace trace) {
|
||||
assert !firstPhase;
|
||||
if (descriptor instanceof NamespaceDescriptor) {
|
||||
return true;
|
||||
@@ -222,7 +225,7 @@ public class ImportsResolver {
|
||||
if (firstPhase) return Collections.emptyList();
|
||||
JetType classObjectType = classDescriptor.getClassObjectType();
|
||||
assert classObjectType != null;
|
||||
Collection<DeclarationDescriptor> members = lookupDescriptorsForSimpleNameReference(memberReference, classObjectType.getMemberScope());
|
||||
Collection<DeclarationDescriptor> members = lookupDescriptorsForSimpleNameReference(memberReference, classObjectType.getMemberScope(), true);
|
||||
return addBoundToReceiver(members, classDescriptor);
|
||||
}
|
||||
|
||||
@@ -231,17 +234,42 @@ public class ImportsResolver {
|
||||
|
||||
JetType variableType = variableDescriptor.getReturnType();
|
||||
if (variableType == null) return Collections.emptyList();
|
||||
Collection<DeclarationDescriptor> members = lookupDescriptorsForSimpleNameReference(memberReference, variableType.getMemberScope());
|
||||
Collection<DeclarationDescriptor> members = lookupDescriptorsForSimpleNameReference(memberReference, variableType.getMemberScope(), true);
|
||||
return addBoundToReceiver(members, variableDescriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Collection<DeclarationDescriptor> addBoundToReceiver(@NotNull Collection<DeclarationDescriptor> descriptors, @NotNull final DeclarationDescriptor receiver) {
|
||||
public static Collection<DeclarationDescriptor> addBoundToReceiver(@NotNull Collection<DeclarationDescriptor> descriptors,
|
||||
@NotNull final DeclarationDescriptor receiver) {
|
||||
return Collections2.transform(descriptors, DescriptorUtils.getAddBoundToReceiverFunction(receiver));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<DeclarationDescriptor> lookupDescriptorsForSimpleNameReference(@NotNull JetSimpleNameExpression referenceExpression, @NotNull JetScope outerScope) {
|
||||
private Collection<DeclarationDescriptor> lookupDescriptorsForSimpleNameReference(
|
||||
@NotNull JetSimpleNameExpression referenceExpression,
|
||||
@NotNull JetScope outerScope,
|
||||
boolean isLastPart) {
|
||||
|
||||
return lookupDescriptorsForSimpleNameReference(referenceExpression, outerScope, outerScope, isLastPart);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param referenceExpression
|
||||
* @param outerScope
|
||||
* @param bindingScope scope for searching completion descriptors. In most cases it's equal to outer scope
|
||||
* but for case "import SomeIdentifier" we want an unresolved reference error, so give empty
|
||||
* outer scope.
|
||||
* @param isLastPart is the part given by referenceExpression last in import directive. Parameter should be false
|
||||
* if there's .* after it or some other parts in full qualified name.
|
||||
* @return
|
||||
*/
|
||||
@NotNull
|
||||
private Collection<DeclarationDescriptor> lookupDescriptorsForSimpleNameReference(
|
||||
@NotNull JetSimpleNameExpression referenceExpression,
|
||||
@NotNull JetScope outerScope,
|
||||
@NotNull JetScope bindingScope,
|
||||
boolean isLastPart) {
|
||||
|
||||
List<DeclarationDescriptor> descriptors = Lists.newArrayList();
|
||||
String referencedName = referenceExpression.getReferencedName();
|
||||
@@ -256,9 +284,21 @@ public class ImportsResolver {
|
||||
|
||||
descriptors.addAll(outerScope.getProperties(referencedName));
|
||||
}
|
||||
|
||||
storeResolutionResult(descriptors, referenceExpression, bindingScope, isLastPart);
|
||||
|
||||
return descriptors;
|
||||
}
|
||||
|
||||
private void storeResolutionResult(
|
||||
@NotNull Collection<DeclarationDescriptor> descriptors,
|
||||
@NotNull JetSimpleNameExpression referenceExpression,
|
||||
@NotNull JetScope resolutionScope,
|
||||
boolean isLastPart) {
|
||||
|
||||
if (!firstPhase) {
|
||||
if (descriptors.size() == 1) {
|
||||
trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, descriptors.get(0));
|
||||
if (descriptors.size() == 1 && !(isLastPart && descriptors.iterator().next() instanceof NamespaceDescriptor)) {
|
||||
trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, descriptors.iterator().next());
|
||||
}
|
||||
else if (descriptors.size() > 1) {
|
||||
trace.record(BindingContext.AMBIGUOUS_REFERENCE_TARGET, referenceExpression, descriptors);
|
||||
@@ -268,11 +308,10 @@ public class ImportsResolver {
|
||||
}
|
||||
|
||||
// If it's not an ambiguous case - store resolution scope
|
||||
if (descriptors.size() <= 1 && outerScope != JetScope.EMPTY) {
|
||||
trace.record(BindingContext.RESOLUTION_SCOPE, referenceExpression, outerScope);
|
||||
if (descriptors.size() <= 1) {
|
||||
trace.record(BindingContext.RESOLUTION_SCOPE, referenceExpression, resolutionScope);
|
||||
}
|
||||
}
|
||||
return descriptors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// KT-960 import namespace should give unresolved reference
|
||||
package some
|
||||
|
||||
import java.<error>util</error>
|
||||
|
||||
class Test {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user