From 5042158df5c7352db321bb0e072f50b1dabfd4b6 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 22 Sep 2016 21:51:49 +0300 Subject: [PATCH] Determine automatically which types from java.lang is aliased in default imported kotlin packages and exclude them from imported java.lang.* scope. --- .../resolve/jvm/platform/JvmPlatform.kt | 14 +--------- .../kotlin/resolve/lazy/FileScopeFactory.kt | 2 +- .../kotlin/descriptors/ModuleDescriptor.kt | 2 ++ .../descriptors/impl/ModuleDescriptorImpl.kt | 26 ++++++++++++++++--- .../kotlin/resolve/scopes/MemberScope.kt | 20 +++++++++----- .../jetbrains/kotlin/types/ErrorUtils.java | 6 +++++ 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatform.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatform.kt index 1e90f5861d9..d52d8759874 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatform.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatform.kt @@ -62,16 +62,4 @@ private val DEFAULT_IMPORTS_FOR_JVM: List = ArrayList(). } } -private val EXCLUDED_IMPORTS_FOR_JVM: List = listOf( - "Error", - "Exception", - "RuntimeException", - "IllegalArgumentException", - "IllegalStateException", - "IndexOutOfBoundsException", - "UnsupportedOperationException", - "NumberFormatException", - "NullPointerException", - "ClassCastException", - "AssertionError" -).map { FqName("java.lang.$it") } \ No newline at end of file +private val EXCLUDED_IMPORTS_FOR_JVM: List = emptyList() \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeFactory.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeFactory.kt index 40f8ad3ccbe..983f21fa19a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeFactory.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeFactory.kt @@ -95,7 +95,7 @@ class FileScopeFactory( val allUnderImportResolver = createImportResolver(AllUnderImportsIndexed(imports), bindingTrace) // TODO: should we count excludedImports here also? val defaultExplicitImportResolver = createImportResolver(ExplicitImportsIndexed(defaultImportsFiltered), tempTrace) - val defaultAllUnderImportResolver = createImportResolver(AllUnderImportsIndexed(defaultImportsFiltered), tempTrace, moduleDescriptor.excludedImports) + val defaultAllUnderImportResolver = createImportResolver(AllUnderImportsIndexed(defaultImportsFiltered), tempTrace, moduleDescriptor.effectivelyExcludedImports) val dummyContainerDescriptor = DummyContainerDescriptor(file, packageFragment) diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/ModuleDescriptor.kt b/core/descriptors/src/org/jetbrains/kotlin/descriptors/ModuleDescriptor.kt index c8f5295498a..b45d99fdc0d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/ModuleDescriptor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/ModuleDescriptor.kt @@ -44,6 +44,8 @@ interface ModuleDescriptor : DeclarationDescriptor, ModuleParameters { fun getCapability(capability: Capability): T? class Capability(val name: String) + + val effectivelyExcludedImports: List } interface ModuleParameters { diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ModuleDescriptorImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ModuleDescriptorImpl.kt index f93ce96e0b1..cb9accb276a 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ModuleDescriptorImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ModuleDescriptorImpl.kt @@ -17,14 +17,16 @@ package org.jetbrains.kotlin.descriptors.impl import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.ModuleDescriptor -import org.jetbrains.kotlin.descriptors.ModuleParameters -import org.jetbrains.kotlin.descriptors.PackageFragmentProvider -import org.jetbrains.kotlin.descriptors.PackageViewDescriptor +import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.name.isSubpackageOf +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe +import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.storage.StorageManager +import org.jetbrains.kotlin.storage.getValue +import org.jetbrains.kotlin.utils.addToStdlib.check import org.jetbrains.kotlin.utils.sure import java.lang.IllegalArgumentException @@ -48,6 +50,22 @@ class ModuleDescriptorImpl @JvmOverloads constructor( fqName: FqName -> LazyPackageViewDescriptorImpl(this, fqName, storageManager) } + override val effectivelyExcludedImports: List by storageManager.createLazyValue { + val packagesWithAliases = listOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME, KotlinBuiltIns.COLLECTIONS_PACKAGE_FQ_NAME) + val dependencies = this.dependencies.sure { "Dependencies of module $id were not set" } + val builtinTypeAliases = dependencies.allDependencies.filter { it != this }.flatMap { + System.out.flush() + packagesWithAliases.map(it::getPackage).flatMap { + it.memberScope.getContributedDescriptors(DescriptorKindFilter.TYPE_ALIASES).filterIsInstance() + } + } + + val nonKotlinDefaultImportedPackages = defaultImports.filter { it.isAllUnder }.mapNotNull { it.fqnPart().check { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) } } + val nonKotlinAliasedTypeFqNames = builtinTypeAliases.mapNotNull { it.expandedType.constructor.declarationDescriptor?.fqNameSafe }.filter { nonKotlinDefaultImportedPackages.any(it::isSubpackageOf) } + + excludedImports + nonKotlinAliasedTypeFqNames + } + override fun getPackage(fqName: FqName): PackageViewDescriptor = packages(fqName) override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection { diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/MemberScope.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/MemberScope.kt index f48968b8d7a..196145c01ed 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/MemberScope.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/MemberScope.kt @@ -114,6 +114,7 @@ class DescriptorKindFilter( private fun DeclarationDescriptor.kind(): Int { return when (this) { is ClassDescriptor -> if (this.kind.isSingleton) SINGLETON_CLASSIFIERS_MASK else NON_SINGLETON_CLASSIFIERS_MASK + is TypeAliasDescriptor -> TYPE_ALIASES_MASK is ClassifierDescriptor -> NON_SINGLETON_CLASSIFIERS_MASK is PackageFragmentDescriptor, is PackageViewDescriptor -> PACKAGES_MASK is FunctionDescriptor -> FUNCTIONS_MASK @@ -123,14 +124,18 @@ class DescriptorKindFilter( } companion object { - val NON_SINGLETON_CLASSIFIERS_MASK: Int = 0x01 - val SINGLETON_CLASSIFIERS_MASK: Int = 0x02 - val PACKAGES_MASK: Int = 0x04 - val FUNCTIONS_MASK: Int = 0x08 - val VARIABLES_MASK: Int = 0x10 + private var nextMaskValue: Int = 0x01 + private fun nextMask() = nextMaskValue.apply { nextMaskValue = nextMaskValue shl 1 } - val ALL_KINDS_MASK: Int = 0x1F - val CLASSIFIERS_MASK: Int = NON_SINGLETON_CLASSIFIERS_MASK or SINGLETON_CLASSIFIERS_MASK + val NON_SINGLETON_CLASSIFIERS_MASK: Int = nextMask() + val SINGLETON_CLASSIFIERS_MASK: Int = nextMask() + val TYPE_ALIASES_MASK: Int = nextMask() + val PACKAGES_MASK: Int = nextMask() + val FUNCTIONS_MASK: Int = nextMask() + val VARIABLES_MASK: Int = nextMask() + + val ALL_KINDS_MASK: Int = nextMask() - 1 + val CLASSIFIERS_MASK: Int = NON_SINGLETON_CLASSIFIERS_MASK or SINGLETON_CLASSIFIERS_MASK or TYPE_ALIASES_MASK val VALUES_MASK: Int = SINGLETON_CLASSIFIERS_MASK or FUNCTIONS_MASK or VARIABLES_MASK val CALLABLES_MASK: Int = FUNCTIONS_MASK or VARIABLES_MASK @@ -138,6 +143,7 @@ class DescriptorKindFilter( @JvmField val CALLABLES: DescriptorKindFilter = DescriptorKindFilter(CALLABLES_MASK) @JvmField val NON_SINGLETON_CLASSIFIERS: DescriptorKindFilter = DescriptorKindFilter(NON_SINGLETON_CLASSIFIERS_MASK) @JvmField val SINGLETON_CLASSIFIERS: DescriptorKindFilter = DescriptorKindFilter(SINGLETON_CLASSIFIERS_MASK) + @JvmField val TYPE_ALIASES: DescriptorKindFilter = DescriptorKindFilter(TYPE_ALIASES_MASK) @JvmField val CLASSIFIERS: DescriptorKindFilter = DescriptorKindFilter(CLASSIFIERS_MASK) @JvmField val PACKAGES: DescriptorKindFilter = DescriptorKindFilter(PACKAGES_MASK) @JvmField val FUNCTIONS: DescriptorKindFilter = DescriptorKindFilter(FUNCTIONS_MASK) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java index ef5887304a3..67ed32f09b5 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java @@ -68,6 +68,12 @@ public class ErrorUtils { return emptyList(); } + @NotNull + @Override + public List getEffectivelyExcludedImports() { + return emptyList(); + } + @NotNull @Override public Annotations getAnnotations() {