[AA] Provide separate non-static and static declared member scopes

- Member scopes already don't contain static callables, only their
  static member scope counterparts. However, declared member scopes
  contained both non-static and static callables, which was confusing to
  users. See for example KT-61255.
- Now declared member scopes only contain non-static callables and
  static declared member scopes only contain static callables.
- In `KtFirScopeProvider`, the new implementation is different for
  Kotlin and Java classes, because the standard declared member scope
  doesn't work for Java. Instead, we have to get the Java *enhancement*
  scopes from `JavaScopeProvider`. Unfortunately, `JavaScopeProvider`
  doesn't have a direct enhancement declared member scope. This results
  in a somewhat complex scope structure with the declared members filter
  scope around the use-site/static Java enhancement scope, but since the
  declared members filtering scope properly reduces the set of callable
  names and scopes in general are cached, this shouldn't be an issue.
- `getCombinedDeclaredMemberScope` is introduced as a separate public
  function because for Kotlin scopes, we don't actually have to create a
  combined scope, as the non-static and static scopes are just filters
  around a combined declared member scope provided by the compiler. It's
  also important to have a convenient function to get the combined
  declared member scope, because some usages explicitly want access to
  all declared members (such as symbol light classes).
- This commit also fixes KT-61901, because
  `getFirJavaDeclaredMemberScope` now provides a proper static scope for
  Java classes, which will be accessible via the combined declared
  member scope as well.

^KT-61800 fixed
^KT-61901 fixed
^KT-61255 fixed
This commit is contained in:
Marco Pennekamp
2023-09-13 20:58:40 +02:00
committed by Space Team
parent 8b5f87f15f
commit 71017298d9
6 changed files with 181 additions and 55 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.analysis.api.descriptors.scopes.KtFe10FileScope
import org.jetbrains.kotlin.analysis.api.descriptors.scopes.KtFe10PackageScope
import org.jetbrains.kotlin.analysis.api.descriptors.scopes.KtFe10ScopeLexical
import org.jetbrains.kotlin.analysis.api.descriptors.scopes.KtFe10ScopeMember
import org.jetbrains.kotlin.analysis.api.descriptors.scopes.KtFe10ScopeNonStaticMember
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.KtFe10FileSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.KtFe10PackageSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.base.KtFe10Symbol
@@ -63,10 +64,33 @@ internal class KtFe10ScopeProvider(
return KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, descriptor.constructors, analysisContext)
}
override fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope {
val descriptor = getDescriptor<ClassDescriptor>(symbol) ?: return getEmptyScope()
return KtFe10ScopeMember(descriptor.staticScope, emptyList(), analysisContext)
}
override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope {
val descriptor = getDescriptor<ClassDescriptor>(classSymbol)
?: return getEmptyScope()
return KtFe10ScopeNonStaticMember(DeclaredMemberScope(descriptor), descriptor.constructors, analysisContext)
}
override fun getStaticDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope {
val descriptor = getDescriptor<ClassDescriptor>(classSymbol)
?: return getEmptyScope()
return KtFe10ScopeMember(
DeclaredMemberScope(descriptor.staticScope, descriptor, forDelegatedMembersOnly = false),
emptyList(),
analysisContext,
)
}
override fun getCombinedDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope {
val descriptor = getDescriptor<ClassDescriptor>(classSymbol)
?: return getEmptyScope()
return KtFe10ScopeMember(DeclaredMemberScope(descriptor), descriptor.constructors, analysisContext)
}
@@ -157,12 +181,6 @@ internal class KtFe10ScopeProvider(
}
}
override fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope {
val descriptor = getDescriptor<ClassDescriptor>(symbol) ?: return getEmptyScope()
return KtFe10ScopeMember(descriptor.staticScope, emptyList(), analysisContext)
}
override fun getEmptyScope(): KtScope {
return KtEmptyScope(token)
}
@@ -16,7 +16,9 @@ import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassifierSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtPropertySymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtPossiblyNamedSymbol
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.name.Name
@@ -102,6 +104,22 @@ internal open class KtFe10ScopeMember(
}
}
internal open class KtFe10ScopeNonStaticMember(
scope: MemberScope,
constructors: Collection<ConstructorDescriptor>,
analysisContext: Fe10AnalysisContext
) : KtFe10ScopeMember(scope, constructors, analysisContext) {
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> = withValidityAssertion {
super.getCallableSymbols(nameFilter).filter { symbol ->
when (symbol) {
is KtFunctionSymbol -> !symbol.isStatic
is KtPropertySymbol -> !symbol.isStatic
else -> true
}
}
}
}
internal class KtFe10ScopeImporting(
override val scope: ImportingScope,
override val analysisContext: Fe10AnalysisContext