Changed priority for default imports + default imports include all built-ins explicitly for java module
#KT-4374 Fixed
This commit is contained in:
+20
-7
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.kotlin.resolve.jvm;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
@@ -26,16 +25,19 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.analyzer.AnalysisResult;
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||
import org.jetbrains.kotlin.context.GlobalContext;
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider;
|
||||
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
|
||||
import org.jetbrains.kotlin.di.InjectorForTopDownAnalyzerForJvm;
|
||||
import org.jetbrains.kotlin.load.kotlin.incremental.IncrementalPackageFragmentProvider;
|
||||
import org.jetbrains.kotlin.load.kotlin.incremental.cache.IncrementalCache;
|
||||
import org.jetbrains.kotlin.load.kotlin.incremental.cache.IncrementalCacheProvider;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap;
|
||||
import org.jetbrains.kotlin.psi.JetFile;
|
||||
import org.jetbrains.kotlin.resolve.BindingTrace;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
import org.jetbrains.kotlin.resolve.ImportPath;
|
||||
import org.jetbrains.kotlin.resolve.TopDownAnalysisParameters;
|
||||
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory;
|
||||
@@ -48,12 +50,23 @@ public enum TopDownAnalyzerFacadeForJVM {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
public static final List<ImportPath> DEFAULT_IMPORTS = ImmutableList.of(
|
||||
new ImportPath("java.lang.*"),
|
||||
new ImportPath("kotlin.*"),
|
||||
new ImportPath("kotlin.jvm.*"),
|
||||
new ImportPath("kotlin.io.*")
|
||||
);
|
||||
public static final List<ImportPath> DEFAULT_IMPORTS = buildDefaultImports();
|
||||
|
||||
private static List<ImportPath> buildDefaultImports() {
|
||||
List<ImportPath> list = new ArrayList<ImportPath>();
|
||||
list.add(new ImportPath("java.lang.*"));
|
||||
list.add(new ImportPath("kotlin.*"));
|
||||
list.add(new ImportPath("kotlin.jvm.*"));
|
||||
list.add(new ImportPath("kotlin.io.*"));
|
||||
// all classes from package "kotlin" mapped to java classes are imported explicitly so that they take priority over classes from java.lang
|
||||
for (ClassDescriptor descriptor : JavaToKotlinClassMap.INSTANCE.allKotlinClasses()) {
|
||||
FqName fqName = DescriptorUtils.getFqNameSafe(descriptor);
|
||||
if (fqName.parent().equals(new FqName("kotlin"))) {
|
||||
list.add(new ImportPath(fqName, false));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private TopDownAnalyzerFacadeForJVM() {
|
||||
}
|
||||
|
||||
@@ -26,14 +26,13 @@ import java.util.ArrayList
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
|
||||
public class Importer(private val platformToKotlinClassMap: PlatformToKotlinClassMap) {
|
||||
public class Importer {
|
||||
private val allUnderImportScopes = ArrayList<JetScope>()
|
||||
private val explicitImports = ArrayList<Pair<DeclarationDescriptor, Name>>()
|
||||
|
||||
public fun addAllUnderImport(descriptor: DeclarationDescriptor) {
|
||||
if (descriptor is PackageViewDescriptor) {
|
||||
val scope = NoSubpackagesInPackageScope(descriptor)
|
||||
allUnderImportScopes.add(createFilteringScope(scope, descriptor, platformToKotlinClassMap))
|
||||
allUnderImportScopes.add(NoSubpackagesInPackageScope(descriptor))
|
||||
}
|
||||
else if (descriptor is ClassDescriptor && descriptor.getKind() != ClassKind.OBJECT) {
|
||||
allUnderImportScopes.add(descriptor.getStaticScope())
|
||||
@@ -45,12 +44,6 @@ public class Importer(private val platformToKotlinClassMap: PlatformToKotlinClas
|
||||
}
|
||||
}
|
||||
|
||||
private fun createFilteringScope(scope: JetScope, descriptor: PackageViewDescriptor, platformToKotlinClassMap: PlatformToKotlinClassMap): JetScope {
|
||||
val kotlinAnalogsForClassesInside = platformToKotlinClassMap.mapPlatformClassesInside(descriptor)
|
||||
if (kotlinAnalogsForClassesInside.isEmpty()) return scope
|
||||
return FilteringScope(scope) { descriptor -> kotlinAnalogsForClassesInside.all { it.getName() != descriptor.getName() } }
|
||||
}
|
||||
|
||||
public fun addAliasImport(descriptor: DeclarationDescriptor, aliasName: Name) {
|
||||
explicitImports.add(descriptor to aliasName)
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ public class ImportsResolver {
|
||||
) {
|
||||
@NotNull JetScope rootScope = JetModuleUtil.getSubpackagesOfRootScope(module);
|
||||
|
||||
Importer importer = new Importer(module.getPlatformToKotlinClassMap());
|
||||
Importer importer = new Importer();
|
||||
if (lookupMode == LookupMode.EVERYTHING) {
|
||||
fileScope.clearImports();
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class LazyFileScope private(
|
||||
debugName: String
|
||||
) : ChainedScope(containingDeclaration,
|
||||
debugName,
|
||||
*(listOf(aliasImportsScope, defaultAliasImportsScope, currentPackageMembersScope, rootPackagesScope, allUnderImportsScope, defaultAllUnderImportsScope) + additionalScopes).copyToArray()) {
|
||||
*(listOf(aliasImportsScope, currentPackageMembersScope, rootPackagesScope, defaultAliasImportsScope, defaultAllUnderImportsScope, allUnderImportsScope) + additionalScopes).copyToArray()) {
|
||||
|
||||
public fun forceResolveAllImports() {
|
||||
aliasImportsScope.forceResolveAllContents()
|
||||
|
||||
@@ -98,7 +98,7 @@ class LazyImportScope(
|
||||
val directiveImportScope = WritableScopeImpl(JetScope.Empty, containingDeclaration, RedeclarationHandler.DO_NOTHING, "Scope for import '" + directive.getDebugText() + "' resolve in " + toString())
|
||||
directiveImportScope.changeLockLevel(WritableScope.LockLevel.BOTH)
|
||||
|
||||
val importer = Importer(resolveSession.getModuleDescriptor().platformToKotlinClassMap)
|
||||
val importer = Importer()
|
||||
directiveUnderResolve = directive
|
||||
|
||||
val descriptors: Collection<DeclarationDescriptor>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import java.lang.reflect.*
|
||||
import <!PLATFORM_CLASS_MAPPED_TO_KOTLIN!>java.util.List<!>
|
||||
|
||||
fun foo(
|
||||
<!UNUSED_PARAMETER!>p1<!>: Array<String> /* should be resolved to kotlin.Array */,
|
||||
<!UNUSED_PARAMETER!>p2<!>: <!PLATFORM_CLASS_MAPPED_TO_KOTLIN!>List<String><!> /* should be resolved to java.util.List */) { }
|
||||
@@ -0,0 +1,3 @@
|
||||
package
|
||||
|
||||
internal fun foo(/*0*/ p1: kotlin.Array<kotlin.String>, /*1*/ p2: java.util.List<kotlin.String>): kotlin.Unit
|
||||
@@ -4,8 +4,8 @@ package test
|
||||
|
||||
import testing.custom.*
|
||||
|
||||
// Non default import has priority over default one. No conflicts are expected.
|
||||
val a1: `custom`List<Int>? = null
|
||||
// Default import has priority over on-demand ones. No conflicts are expected.
|
||||
val a1: `kotlin::List`List<Int>? = null
|
||||
|
||||
//FILE:javaUtilImport.kt
|
||||
//----------------------------------------------------------------------------------
|
||||
@@ -13,7 +13,7 @@ package test
|
||||
|
||||
import java.util.*
|
||||
|
||||
// Mapped declarations are dropped from on-demand imports.
|
||||
// Default imports take over import-on-demand
|
||||
// TODO: Fix for lazy resolve test
|
||||
// val a2: 'kotlin::List'List<Int>? = null
|
||||
|
||||
@@ -24,8 +24,8 @@ package test
|
||||
import testing.custom.*
|
||||
import java.util.*
|
||||
|
||||
// Mapped declarations are dropped from on-demand "java.util" import. So no conflicts are expected.
|
||||
val a3: `custom`List<Int>? = null
|
||||
// Default import has priority over on-demand ones. No conflicts are expected.
|
||||
val a3: `kotlin::List`List<Int>? = null
|
||||
|
||||
//FILE:singleClassImportFromJavaUtil.kt
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
@@ -29,9 +29,9 @@ class WithPredicate() {
|
||||
val p : Boolean
|
||||
}
|
||||
|
||||
open class List<E>()
|
||||
open class AbstractList<E> : List<E?>
|
||||
open class ArrayList<E>() : Any, AbstractList<E?>, List<E?>
|
||||
open class InvList<E>()
|
||||
open class AbstractList<E> : InvList<E?>
|
||||
open class ArrayList<E>() : Any, AbstractList<E?>, InvList<E?>
|
||||
|
||||
fun f() : Unit {}
|
||||
fun f(a : Int) : Int {a}
|
||||
|
||||
@@ -4745,6 +4745,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("DefaultImportsPriority.kt")
|
||||
public void testDefaultImportsPriority() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/DefaultImportsPriority.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("DontImportRootScope.kt")
|
||||
public void testDontImportRootScope() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/DontImportRootScope.kt");
|
||||
|
||||
@@ -304,7 +304,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
|
||||
|
||||
assertSubtype("Derived_T<Int>", "Base_T<in Int>");
|
||||
assertSubtype("MDerived_T<Int>", "Base_T<in Int>");
|
||||
assertSubtype("ArrayList<Int>", "List<in Int>");
|
||||
assertSubtype("ArrayList<Int>", "InvList<in Int>");
|
||||
|
||||
// assertSubtype("java.lang.Integer", "java.lang.Comparable<java.lang.Integer>?");
|
||||
}
|
||||
@@ -596,7 +596,14 @@ public class JetTypeCheckerTest extends JetLiteFixture {
|
||||
InjectorForJavaDescriptorResolver injector = InjectorForJavaDescriptorResolverUtil.create(getProject(), trace, true);
|
||||
ModuleDescriptor module = injector.getModule();
|
||||
for (ImportPath defaultImport : module.getDefaultImports()) {
|
||||
writableScope.importScope(module.getPackage(defaultImport.fqnPart()).getMemberScope());
|
||||
FqName fqName = defaultImport.fqnPart();
|
||||
if (defaultImport.isAllUnder()) {
|
||||
writableScope.importScope(module.getPackage(fqName).getMemberScope());
|
||||
}
|
||||
else {
|
||||
writableScope.addClassifierAlias(defaultImport.getImportedName(),
|
||||
module.getPackage(fqName.parent()).getMemberScope().getClassifier(fqName.shortName()));
|
||||
}
|
||||
}
|
||||
writableScope.importScope(module.getPackage(FqName.ROOT).getMemberScope());
|
||||
writableScope.changeLockLevel(WritableScope.LockLevel.BOTH);
|
||||
|
||||
Reference in New Issue
Block a user