[LL FIR] Respect allowKotlinPackage in LLFirKotlinSymbolNamesProvider
- `LLFirProviderHelper` is guarded by `allowKotlinPackage` and the symbol names provider should reflect that in the name sets. - Note that this does not apply to stub-based deserialized symbol providers, because they do not pass any value to `allowKotlinPackage`. ^KT-57314
This commit is contained in:
committed by
Space Team
parent
e79bc5bb59
commit
8034d522b9
+6
-3
@@ -9,8 +9,11 @@ package org.jetbrains.kotlin.analysis.utils.collections
|
|||||||
* Maps all elements of this non-empty collection with the given [transform] function to a new mutable set, or returns [emptySet] if this
|
* Maps all elements of this non-empty collection with the given [transform] function to a new mutable set, or returns [emptySet] if this
|
||||||
* collection is empty.
|
* collection is empty.
|
||||||
*
|
*
|
||||||
* [mapToSet] should be preferred over `collection.mapTo(mutableSetOf()) { ... }` when `collection` may be empty and the resulting set may
|
* [mapToSetOrEmpty] should be preferred over `collection.mapTo(mutableSetOf()) { ... }` when `collection` may be empty and the resulting
|
||||||
* be cached, because [mapToSet] saves memory by avoiding the creation of an empty mutable set.
|
* set may be cached, because [mapToSetOrEmpty] saves memory by avoiding the creation of an empty mutable set.
|
||||||
*/
|
*/
|
||||||
public inline fun <T, R> Collection<T>.mapToSet(transform: (T) -> R): Set<R> =
|
public inline fun <T, R> Collection<T>.mapToSetOrEmpty(transform: (T) -> R): Set<R> =
|
||||||
if (isNotEmpty()) mapTo(mutableSetOf(), transform) else emptySet()
|
if (isNotEmpty()) mapTo(mutableSetOf(), transform) else emptySet()
|
||||||
|
|
||||||
|
public inline fun <T> Collection<T>.filterToSetOrEmpty(predicate: (T) -> Boolean): Set<T> =
|
||||||
|
filterTo(mutableSetOf(), predicate).ifEmpty { emptySet() }
|
||||||
|
|||||||
+1
-1
@@ -102,7 +102,7 @@ internal class LLFirProviderHelper(
|
|||||||
val symbolNameCache = FirCompositeCachedSymbolNamesProvider.create(
|
val symbolNameCache = FirCompositeCachedSymbolNamesProvider.create(
|
||||||
firSession,
|
firSession,
|
||||||
listOfNotNull(
|
listOfNotNull(
|
||||||
object : LLFirKotlinSymbolNamesProvider(declarationProvider) {
|
object : LLFirKotlinSymbolNamesProvider(declarationProvider, allowKotlinPackage) {
|
||||||
// This is a temporary workaround for KTIJ-25536.
|
// This is a temporary workaround for KTIJ-25536.
|
||||||
override fun getPackageNamesWithTopLevelCallables(): Set<String>? = null
|
override fun getPackageNamesWithTopLevelCallables(): Set<String>? = null
|
||||||
},
|
},
|
||||||
|
|||||||
+29
-8
@@ -6,7 +6,9 @@
|
|||||||
package org.jetbrains.kotlin.analysis.low.level.api.fir.util
|
package org.jetbrains.kotlin.analysis.low.level.api.fir.util
|
||||||
|
|
||||||
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
||||||
import org.jetbrains.kotlin.analysis.utils.collections.mapToSet
|
import org.jetbrains.kotlin.analysis.utils.collections.filterToSetOrEmpty
|
||||||
|
import org.jetbrains.kotlin.analysis.utils.collections.mapToSetOrEmpty
|
||||||
|
import org.jetbrains.kotlin.builtins.StandardNames
|
||||||
import org.jetbrains.kotlin.fir.FirSession
|
import org.jetbrains.kotlin.fir.FirSession
|
||||||
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolNamesProvider
|
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolNamesProvider
|
||||||
import org.jetbrains.kotlin.fir.resolve.providers.FirDelegatingCachedSymbolNamesProvider
|
import org.jetbrains.kotlin.fir.resolve.providers.FirDelegatingCachedSymbolNamesProvider
|
||||||
@@ -16,23 +18,42 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A [FirSymbolNamesProvider] that fetches top-level names from a Kotlin [declarationProvider].
|
* A [FirSymbolNamesProvider] that fetches top-level names from a Kotlin [declarationProvider].
|
||||||
|
*
|
||||||
|
* @param allowKotlinPackage Whether the associated symbol provider is allowed to provide symbols from the `kotlin` package.
|
||||||
*/
|
*/
|
||||||
internal open class LLFirKotlinSymbolNamesProvider(
|
internal open class LLFirKotlinSymbolNamesProvider(
|
||||||
private val declarationProvider: KotlinDeclarationProvider,
|
private val declarationProvider: KotlinDeclarationProvider,
|
||||||
|
private val allowKotlinPackage: Boolean? = null,
|
||||||
) : FirSymbolNamesProvider() {
|
) : FirSymbolNamesProvider() {
|
||||||
override fun getTopLevelClassifierNamesInPackage(packageFqName: FqName): Set<String> =
|
override fun getTopLevelClassifierNamesInPackage(packageFqName: FqName): Set<String> {
|
||||||
declarationProvider
|
if (allowKotlinPackage == false && packageFqName.isKotlinPackage()) return emptySet()
|
||||||
|
|
||||||
|
return declarationProvider
|
||||||
.getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName)
|
.getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName)
|
||||||
.mapToSet { it.asString() }
|
.mapToSetOrEmpty { it.asString() }
|
||||||
|
}
|
||||||
|
|
||||||
override fun getPackageNamesWithTopLevelCallables(): Set<String>? =
|
override fun getPackageNamesWithTopLevelCallables(): Set<String>? {
|
||||||
declarationProvider.computePackageSetWithTopLevelCallableDeclarations()
|
val packageNames = declarationProvider.computePackageSetWithTopLevelCallableDeclarations()
|
||||||
|
|
||||||
override fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name> =
|
if (allowKotlinPackage == false && packageNames.any { it.isKotlinPackage() }) {
|
||||||
declarationProvider.getTopLevelCallableNamesInPackage(packageFqName).ifEmpty { emptySet() }
|
return packageNames.filterToSetOrEmpty { !it.isKotlinPackage() }
|
||||||
|
}
|
||||||
|
|
||||||
|
return packageNames
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name> {
|
||||||
|
if (allowKotlinPackage == false && packageFqName.isKotlinPackage()) return emptySet()
|
||||||
|
|
||||||
|
return declarationProvider.getTopLevelCallableNamesInPackage(packageFqName).ifEmpty { emptySet() }
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun cached(session: FirSession, declarationProvider: KotlinDeclarationProvider): FirCachedSymbolNamesProvider =
|
fun cached(session: FirSession, declarationProvider: KotlinDeclarationProvider): FirCachedSymbolNamesProvider =
|
||||||
FirDelegatingCachedSymbolNamesProvider(session, LLFirKotlinSymbolNamesProvider(declarationProvider))
|
FirDelegatingCachedSymbolNamesProvider(session, LLFirKotlinSymbolNamesProvider(declarationProvider))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun FqName.isKotlinPackage(): Boolean = startsWith(StandardNames.BUILT_INS_PACKAGE_NAME)
|
||||||
|
private fun String.isKotlinPackage(): Boolean = startsWith(StandardNames.BUILT_INS_PACKAGE_NAME.asString())
|
||||||
|
|||||||
Reference in New Issue
Block a user