Alias imports to hide symbols by original name from the current package too

This commit is contained in:
Valentin Kipyatkov
2016-01-18 16:09:37 +03:00
parent e83bd759e6
commit bfe728526a
4 changed files with 43 additions and 9 deletions
@@ -16,8 +16,10 @@
package org.jetbrains.kotlin.resolve.lazy
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtImportsFactory
@@ -25,13 +27,11 @@ import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver
import org.jetbrains.kotlin.resolve.TemporaryBindingTrace
import org.jetbrains.kotlin.resolve.bindingContextUtil.recordScope
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.SubpackagesImportingScope
import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.utils.sure
import org.jetbrains.kotlin.utils.Printer
open class FileScopeProviderImpl(
private val topLevelDescriptorProvider: TopLevelDescriptorProvider,
@@ -100,7 +100,7 @@ open class FileScopeProviderImpl(
scope = SubpackagesImportingScope(scope, moduleDescriptor, FqName.ROOT)
scope = packageView.memberScope.memberScopeAsImportingScope(scope) //TODO: problems with visibility too
scope = packageMemberScopeWithAliasedNamesExcluded(packageView, aliasImportNames).memberScopeAsImportingScope(scope) //TODO: problems with visibility too
scope = LazyImportScope(scope, explicitImportResolver, LazyImportScope.FilteringKind.ALL, "Explicit imports in $debugName")
@@ -126,4 +126,38 @@ open class FileScopeProviderImpl(
return FileData(lexicalScope, importResolver)
}
private fun packageMemberScopeWithAliasedNamesExcluded(packageView: PackageViewDescriptor, aliasImportNames: Collection<FqName>): MemberScope {
val scope = packageView.memberScope
if (aliasImportNames.isEmpty()) return scope
val packageName = packageView.fqName
val excludedNames = aliasImportNames.mapNotNull { if (it.parent() == packageName) it.shortName() else null }
if (excludedNames.isEmpty()) return scope
return object: MemberScope {
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? {
if (name in excludedNames) return null
return scope.getContributedClassifier(name, location)
}
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
if (name in excludedNames) return emptyList()
return scope.getContributedVariables(name, location)
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
if (name in excludedNames) return emptyList()
return scope.getContributedFunctions(name, location)
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return scope.getContributedDescriptors(kindFilter, { name -> name !in excludedNames && nameFilter(name) })
}
override fun printScopeStructure(p: Printer) {
return scope.printScopeStructure(p)
}
}
}
}
@@ -3,6 +3,6 @@ package a
import a.A as ER
interface A {
val a: A
val a: <!UNRESOLVED_REFERENCE!>A<!>
val b: ER
}
@@ -3,7 +3,7 @@ package
package a {
public interface A {
public abstract val a: a.A
public abstract val a: [ERROR : A]
public abstract val b: a.A
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
@@ -19003,7 +19003,7 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
@TestMetadata("compiler/testData/codegen/box/diagnostics/vararg")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Vararg extends AbstractDiagnosticsTest {
public static class IIImpoiVararg extends AbstractDiagnosticsTest {
public void testAllFilesPresentInVararg() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/diagnostics/vararg"), Pattern.compile("^(.+)\\.kt$"), true);
}