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); DiagnosticFactory1<JetSimpleNameExpression, DeclarationDescriptor> CANNOT_BE_IMPORTED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<JetExpression> USELESS_HIDDEN_IMPORT = DiagnosticFactory0.create(WARNING); DiagnosticFactory0<JetExpression> USELESS_HIDDEN_IMPORT = DiagnosticFactory0.create(WARNING);
DiagnosticFactory1<JetExpression, String> CONFLICTING_IMPORT = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<JetExpression> USELESS_SIMPLE_IMPORT = DiagnosticFactory0.create(WARNING); DiagnosticFactory0<JetExpression> USELESS_SIMPLE_IMPORT = DiagnosticFactory0.create(WARNING);
// Modifiers // Modifiers
@@ -169,6 +169,7 @@ public class DefaultErrorMessages {
MAP.put(CANNOT_IMPORT_FROM_ELEMENT, "Cannot import from ''{0}''", NAME); 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(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(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(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); 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(); JetExpression importedReference = importDirective.getImportedReference();
if (importedReference == null || resolvedTo == null) { if (importedReference == null || resolvedTo == null) return;
return;
}
Name aliasName = JetPsiUtil.getAliasName(importDirective); Name aliasName = JetPsiUtil.getAliasName(importDirective);
if (aliasName == null) { if (aliasName == null) return;
return;
}
boolean uselessHiddenImport = true; boolean uselessHiddenImport = true;
for (DeclarationDescriptor target : resolvedTo) { for (DeclarationDescriptor target : resolvedTo) {
@@ -202,6 +199,23 @@ public class ImportsResolver {
if (uselessHiddenImport) { if (uselessHiddenImport) {
trace.report(USELESS_HIDDEN_IMPORT.on(importedReference)); 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() && if (!importDirective.isAllUnder() &&
importedReference instanceof JetSimpleNameExpression && importedReference instanceof JetSimpleNameExpression &&
@@ -11,7 +11,7 @@ class X
// FILE: c.kt // FILE: c.kt
package c package c
import a.X import <!CONFLICTING_IMPORT!>a.X<!>
import b.X import <!CONFLICTING_IMPORT!>b.X<!>
class Y : <!UNRESOLVED_REFERENCE!>X<!> class Y : <!UNRESOLVED_REFERENCE!>X<!>
@@ -0,0 +1,17 @@
// FILE: a.kt
package a.x
class X
// FILE: b.kt
package b.x
class X
// FILE: c.kt
package c
import <!CONFLICTING_IMPORT!>a.x<!>
import <!CONFLICTING_IMPORT!>b.x<!>
class Y : <!UNRESOLVED_REFERENCE!>x<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>X<!>
@@ -0,0 +1,37 @@
package
package a {
package a.x {
internal final class X {
public constructor X()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
package b {
package b.x {
internal final class X {
public constructor X()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
package c {
internal final class Y {
public constructor Y()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -4739,6 +4739,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("ExplicitPackageImportsAmbiguity.kt")
public void testExplicitPackageImportsAmbiguity() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/ExplicitPackageImportsAmbiguity.kt");
doTest(fileName);
}
@TestMetadata("importFunctionWithAllUnderImport.kt") @TestMetadata("importFunctionWithAllUnderImport.kt")
public void testImportFunctionWithAllUnderImport() throws Exception { public void testImportFunctionWithAllUnderImport() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/importFunctionWithAllUnderImport.kt"); String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/importFunctionWithAllUnderImport.kt");
@@ -129,6 +129,7 @@ public class QuickFixRegistrar {
JetSingleIntentionActionFactory removeImportFixFactory = RemovePsiElementSimpleFix.createRemoveImportFactory(); JetSingleIntentionActionFactory removeImportFixFactory = RemovePsiElementSimpleFix.createRemoveImportFactory();
QuickFixes.factories.put(USELESS_SIMPLE_IMPORT, removeImportFixFactory); QuickFixes.factories.put(USELESS_SIMPLE_IMPORT, removeImportFixFactory);
QuickFixes.factories.put(USELESS_HIDDEN_IMPORT, removeImportFixFactory); QuickFixes.factories.put(USELESS_HIDDEN_IMPORT, removeImportFixFactory);
QuickFixes.factories.put(CONFLICTING_IMPORT, removeImportFixFactory);
QuickFixes.factories.put(SUPERTYPE_NOT_INITIALIZED, ChangeToConstructorInvocationFix.createFactory()); QuickFixes.factories.put(SUPERTYPE_NOT_INITIALIZED, ChangeToConstructorInvocationFix.createFactory());
QuickFixes.factories.put(FUNCTION_CALL_EXPECTED, ChangeToFunctionInvocationFix.createFactory()); QuickFixes.factories.put(FUNCTION_CALL_EXPECTED, ChangeToFunctionInvocationFix.createFactory());