[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:
Marco Pennekamp
2023-10-19 18:38:09 +02:00
committed by Space Team
parent e79bc5bb59
commit 8034d522b9
3 changed files with 36 additions and 12 deletions
@@ -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
* collection is empty.
*
* [mapToSet] should be preferred over `collection.mapTo(mutableSetOf()) { ... }` when `collection` may be empty and the resulting set may
* be cached, because [mapToSet] saves memory by avoiding the creation of an empty mutable set.
* [mapToSetOrEmpty] should be preferred over `collection.mapTo(mutableSetOf()) { ... }` when `collection` may be empty and the resulting
* 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()
public inline fun <T> Collection<T>.filterToSetOrEmpty(predicate: (T) -> Boolean): Set<T> =
filterTo(mutableSetOf(), predicate).ifEmpty { emptySet() }
@@ -102,7 +102,7 @@ internal class LLFirProviderHelper(
val symbolNameCache = FirCompositeCachedSymbolNamesProvider.create(
firSession,
listOfNotNull(
object : LLFirKotlinSymbolNamesProvider(declarationProvider) {
object : LLFirKotlinSymbolNamesProvider(declarationProvider, allowKotlinPackage) {
// This is a temporary workaround for KTIJ-25536.
override fun getPackageNamesWithTopLevelCallables(): Set<String>? = null
},
@@ -6,7 +6,9 @@
package org.jetbrains.kotlin.analysis.low.level.api.fir.util
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.resolve.providers.FirSymbolNamesProvider
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].
*
* @param allowKotlinPackage Whether the associated symbol provider is allowed to provide symbols from the `kotlin` package.
*/
internal open class LLFirKotlinSymbolNamesProvider(
private val declarationProvider: KotlinDeclarationProvider,
private val allowKotlinPackage: Boolean? = null,
) : FirSymbolNamesProvider() {
override fun getTopLevelClassifierNamesInPackage(packageFqName: FqName): Set<String> =
declarationProvider
override fun getTopLevelClassifierNamesInPackage(packageFqName: FqName): Set<String> {
if (allowKotlinPackage == false && packageFqName.isKotlinPackage()) return emptySet()
return declarationProvider
.getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName)
.mapToSet { it.asString() }
.mapToSetOrEmpty { it.asString() }
}
override fun getPackageNamesWithTopLevelCallables(): Set<String>? =
declarationProvider.computePackageSetWithTopLevelCallableDeclarations()
override fun getPackageNamesWithTopLevelCallables(): Set<String>? {
val packageNames = declarationProvider.computePackageSetWithTopLevelCallableDeclarations()
override fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name> =
declarationProvider.getTopLevelCallableNamesInPackage(packageFqName).ifEmpty { emptySet() }
if (allowKotlinPackage == false && packageNames.any { it.isKotlinPackage() }) {
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 {
fun cached(session: FirSession, declarationProvider: KotlinDeclarationProvider): FirCachedSymbolNamesProvider =
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())