KT-45553. Hide declaration from star import if imported with alias
This commit is contained in:
+5
@@ -41,6 +41,8 @@ abstract class FirAbstractImportingScope(
|
||||
fun getStaticsScope(classId: ClassId): FirContainingNamesAwareScope? =
|
||||
provider.getClassLikeSymbolByClassId(classId)?.fullyExpandedSymbol?.getStaticsScope()
|
||||
|
||||
protected abstract fun isExcluded(import: FirResolvedImport, name: Name): Boolean
|
||||
|
||||
protected fun processImportsByName(
|
||||
name: Name?,
|
||||
imports: List<FirResolvedImport>,
|
||||
@@ -48,6 +50,7 @@ abstract class FirAbstractImportingScope(
|
||||
) {
|
||||
for (import in imports) {
|
||||
val importedName = name ?: import.importedName ?: continue
|
||||
if (isExcluded(import, importedName)) continue
|
||||
val classId = import.resolvedParentClassId?.createNestedClassId(importedName)
|
||||
?: ClassId.topLevel(import.packageFqName.child(importedName))
|
||||
val symbol = provider.getClassLikeSymbolByClassId(classId) ?: continue
|
||||
@@ -58,6 +61,7 @@ abstract class FirAbstractImportingScope(
|
||||
protected fun processFunctionsByName(name: Name?, imports: List<FirResolvedImport>, processor: (FirNamedFunctionSymbol) -> Unit) {
|
||||
for (import in imports) {
|
||||
val importedName = name ?: import.importedName ?: continue
|
||||
if (isExcluded(import, importedName)) continue
|
||||
val staticsScope = import.resolvedParentClassId?.let(::getStaticsScope)
|
||||
if (staticsScope != null) {
|
||||
staticsScope.processFunctionsByName(importedName, processor)
|
||||
@@ -73,6 +77,7 @@ abstract class FirAbstractImportingScope(
|
||||
protected fun processPropertiesByName(name: Name?, imports: List<FirResolvedImport>, processor: (FirVariableSymbol<*>) -> Unit) {
|
||||
for (import in imports) {
|
||||
val importedName = name ?: import.importedName ?: continue
|
||||
if (isExcluded(import, importedName)) continue
|
||||
val staticsScope = import.resolvedParentClassId?.let(::getStaticsScope)
|
||||
if (staticsScope != null) {
|
||||
staticsScope.processPropertiesByName(importedName, processor)
|
||||
|
||||
+2
@@ -22,6 +22,8 @@ abstract class FirAbstractSimpleImportingScope(
|
||||
// TODO try to hide this
|
||||
abstract val simpleImports: Map<Name, List<FirResolvedImport>>
|
||||
|
||||
override fun isExcluded(import: FirResolvedImport, name: Name): Boolean = false
|
||||
|
||||
override fun processClassifiersByNameWithSubstitution(name: Name, processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit) {
|
||||
val imports = simpleImports[name] ?: return
|
||||
processImportsByName(name = null, imports) { symbol ->
|
||||
|
||||
+10
-1
@@ -12,17 +12,26 @@ import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassifierSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
abstract class FirAbstractStarImportingScope(
|
||||
session: FirSession,
|
||||
scopeSession: ScopeSession,
|
||||
lookupInFir: Boolean
|
||||
lookupInFir: Boolean,
|
||||
val excludedImportNames: Set<FqName>
|
||||
) : FirAbstractImportingScope(session, scopeSession, lookupInFir) {
|
||||
|
||||
// TODO try to hide this
|
||||
abstract val starImports: List<FirResolvedImport>
|
||||
|
||||
override fun isExcluded(import: FirResolvedImport, name: Name): Boolean {
|
||||
if (excludedImportNames.isNotEmpty()) {
|
||||
return import.importedFqName!!.child(name) in excludedImportNames
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private val absentClassifierNames = mutableSetOf<Name>()
|
||||
|
||||
override fun processClassifiersByNameWithSubstitution(name: Name, processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit) {
|
||||
|
||||
+5
-2
@@ -15,15 +15,18 @@ import org.jetbrains.kotlin.fir.moduleData
|
||||
import org.jetbrains.kotlin.fir.resolve.ScopeSession
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
class FirDefaultStarImportingScope(
|
||||
session: FirSession,
|
||||
scopeSession: ScopeSession,
|
||||
priority: DefaultImportPriority
|
||||
priority: DefaultImportPriority,
|
||||
excludedImportNames: Set<FqName>
|
||||
) : FirAbstractStarImportingScope(
|
||||
session, scopeSession,
|
||||
lookupInFir = session.languageVersionSettings.getFlag(AnalysisFlags.allowKotlinPackage)
|
||||
lookupInFir = session.languageVersionSettings.getFlag(AnalysisFlags.allowKotlinPackage),
|
||||
excludedImportNames
|
||||
) {
|
||||
// TODO: put languageVersionSettings into FirSession?
|
||||
override val starImports = run {
|
||||
|
||||
+4
-2
@@ -9,12 +9,14 @@ import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.FirImport
|
||||
import org.jetbrains.kotlin.fir.declarations.FirResolvedImport
|
||||
import org.jetbrains.kotlin.fir.resolve.ScopeSession
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
open class FirExplicitStarImportingScope(
|
||||
imports: List<FirImport>,
|
||||
session: FirSession,
|
||||
scopeSession: ScopeSession
|
||||
) : FirAbstractStarImportingScope(session, scopeSession, lookupInFir = true) {
|
||||
scopeSession: ScopeSession,
|
||||
excludedImportNames: Set<FqName>
|
||||
) : FirAbstractStarImportingScope(session, scopeSession, lookupInFir = true, excludedImportNames) {
|
||||
override val starImports = imports.filterIsInstance<FirResolvedImport>().filter { it.isAllUnder }
|
||||
|
||||
override val scopeOwnerLookupNames: List<String> by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||
|
||||
+2
-1
@@ -23,7 +23,8 @@ class FirWhenSubjectImportingScope(
|
||||
classId: ClassId, session: FirSession, scopeSession: ScopeSession
|
||||
) : FirExplicitStarImportingScope(
|
||||
listOf(buildResolvedImportByClassId(classId)),
|
||||
session, scopeSession
|
||||
session, scopeSession,
|
||||
emptySet()
|
||||
) {
|
||||
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
|
||||
super.processPropertiesByName(name) {
|
||||
|
||||
@@ -13,11 +13,14 @@ import org.jetbrains.kotlin.fir.resolve.ScopeSession
|
||||
import org.jetbrains.kotlin.fir.resolve.scopeSessionKey
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.*
|
||||
import org.jetbrains.kotlin.fir.symbols.ensureResolved
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
private val ALL_IMPORTS = scopeSessionKey<FirFile, ListStorageFirScope>()
|
||||
private val DEFAULT_STAR_IMPORT = scopeSessionKey<DefaultImportPriority, FirDefaultStarImportingScope>()
|
||||
private val DEFAULT_STAR_IMPORT = scopeSessionKey<DefaultStarImportKey, FirDefaultStarImportingScope>()
|
||||
private val DEFAULT_SIMPLE_IMPORT = scopeSessionKey<DefaultImportPriority, FirDefaultSimpleImportingScope>()
|
||||
|
||||
private data class DefaultStarImportKey(val priority: DefaultImportPriority, val excludedImportNames: Set<FqName>)
|
||||
|
||||
fun createImportingScopes(
|
||||
file: FirFile,
|
||||
session: FirSession,
|
||||
@@ -37,14 +40,16 @@ private fun doCreateImportingScopes(
|
||||
scopeSession: ScopeSession
|
||||
): List<FirScope> {
|
||||
file.ensureResolved(FirResolvePhase.IMPORTS)
|
||||
val excludedImportNames =
|
||||
file.imports.filter { it.aliasName != null }.mapNotNullTo(hashSetOf()) { it.importedFqName }.ifEmpty { emptySet() }
|
||||
return listOf(
|
||||
scopeSession.getOrBuild(DefaultImportPriority.LOW, DEFAULT_STAR_IMPORT) {
|
||||
FirDefaultStarImportingScope(session, scopeSession, DefaultImportPriority.LOW)
|
||||
scopeSession.getOrBuild(DefaultStarImportKey(DefaultImportPriority.LOW, excludedImportNames), DEFAULT_STAR_IMPORT) {
|
||||
FirDefaultStarImportingScope(session, scopeSession, DefaultImportPriority.LOW, excludedImportNames)
|
||||
},
|
||||
scopeSession.getOrBuild(DefaultImportPriority.HIGH, DEFAULT_STAR_IMPORT) {
|
||||
FirDefaultStarImportingScope(session, scopeSession, DefaultImportPriority.HIGH)
|
||||
scopeSession.getOrBuild(DefaultStarImportKey(DefaultImportPriority.HIGH, excludedImportNames), DEFAULT_STAR_IMPORT) {
|
||||
FirDefaultStarImportingScope(session, scopeSession, DefaultImportPriority.HIGH, excludedImportNames)
|
||||
},
|
||||
FirExplicitStarImportingScope(file.imports, session, scopeSession),
|
||||
FirExplicitStarImportingScope(file.imports, session, scopeSession, excludedImportNames),
|
||||
|
||||
scopeSession.getOrBuild(DefaultImportPriority.LOW, DEFAULT_SIMPLE_IMPORT) {
|
||||
FirDefaultSimpleImportingScope(session, scopeSession, priority = DefaultImportPriority.LOW)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
// FILE: a.kt
|
||||
package a
|
||||
|
||||
val x = 1
|
||||
val y = 1
|
||||
|
||||
// FILE: b.kt
|
||||
package b
|
||||
|
||||
val x = ""
|
||||
|
||||
// FILE: c.kt
|
||||
package c
|
||||
|
||||
import a.x as AX
|
||||
import a.*
|
||||
import b.*
|
||||
import a.y as AY
|
||||
|
||||
val v1: Int = AX
|
||||
val v2: String = <!OVERLOAD_RESOLUTION_AMBIGUITY!>x<!>
|
||||
val v3 = y
|
||||
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// FILE: a.kt
|
||||
package a
|
||||
|
||||
|
||||
Reference in New Issue
Block a user