Conflicting imports error for imports of classes and packages

This commit is contained in:
Valentin Kipyatkov
2015-01-14 21:36:34 +03:00
parent 0c74f1679b
commit 2e420fc312
8 changed files with 85 additions and 9 deletions
@@ -103,7 +103,7 @@ public interface Errors {
DiagnosticFactory1<JetSimpleNameExpression, DeclarationDescriptor> CANNOT_BE_IMPORTED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<JetExpression> USELESS_HIDDEN_IMPORT = DiagnosticFactory0.create(WARNING);
DiagnosticFactory1<JetExpression, String> CONFLICTING_IMPORT = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<JetExpression> USELESS_SIMPLE_IMPORT = DiagnosticFactory0.create(WARNING);
// Modifiers
@@ -169,6 +169,7 @@ public class DefaultErrorMessages {
MAP.put(CANNOT_IMPORT_FROM_ELEMENT, "Cannot import from ''{0}''", NAME);
MAP.put(CANNOT_BE_IMPORTED, "Cannot import ''{0}'', functions and properties can be imported only from packages", NAME);
MAP.put(USELESS_HIDDEN_IMPORT, "Useless import, it is hidden further");
MAP.put(CONFLICTING_IMPORT, "Conflicting import, imported name ''{0}'' is ambiguous", STRING);
MAP.put(USELESS_SIMPLE_IMPORT, "Useless import, does nothing");
MAP.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, "This class shouldn''t be used in Kotlin. Use {0} instead.", CLASSES_OR_SEPARATED);
@@ -175,13 +175,10 @@ public class ImportsResolver {
) {
JetExpression importedReference = importDirective.getImportedReference();
if (importedReference == null || resolvedTo == null) {
return;
}
if (importedReference == null || resolvedTo == null) return;
Name aliasName = JetPsiUtil.getAliasName(importDirective);
if (aliasName == null) {
return;
}
if (aliasName == null) return;
boolean uselessHiddenImport = true;
for (DeclarationDescriptor target : resolvedTo) {
@@ -202,6 +199,23 @@ public class ImportsResolver {
if (uselessHiddenImport) {
trace.report(USELESS_HIDDEN_IMPORT.on(importedReference));
}
else {
if (resolvedTo.size() == 1) {
DeclarationDescriptor target = resolvedTo.iterator().next();
if (target instanceof ClassDescriptor) {
if (fileScope.getClassifier(aliasName) == null) {
trace.report(CONFLICTING_IMPORT.on(importedReference, aliasName.asString()));
return;
}
}
else if (target instanceof PackageViewDescriptor) {
if (fileScope.getPackage(aliasName) == null) {
trace.report(CONFLICTING_IMPORT.on(importedReference, aliasName.asString()));
return;
}
}
}
}
if (!importDirective.isAllUnder() &&
importedReference instanceof JetSimpleNameExpression &&