From 116de97f5467c8264be85fa75ab9274c45be4593 Mon Sep 17 00:00:00 2001 From: aleksandrina-streltsova Date: Sat, 24 Jun 2023 19:53:22 +0300 Subject: [PATCH] [AA] Add synthetic properties scopes to scope context for position --- .../api/fir/components/KtFirScopeProvider.kt | 80 ++++-- ...eScopeContextForPositionTestGenerated.java | 6 + ...sisSourceModuleTypeScopeTestGenerated.java | 6 + ...eScopeContextForPositionTestGenerated.java | 6 + ...sisSourceModuleTypeScopeTestGenerated.java | 6 + ...eScopeContextForPositionTestGenerated.java | 6 + ...sisSourceModuleTypeScopeTestGenerated.java | 6 + .../api/components/KtScopeProvider.kt | 15 +- .../contextReceiver.pretty.txt | 2 +- .../contextReceiver.txt | 2 +- .../enumEntry.pretty.txt | 4 +- .../scopeContextForPosition/enumEntry.txt | 4 +- .../errorType.pretty.txt | 4 +- .../scopeContextForPosition/errorType.txt | 4 +- .../localTypeScope.pretty.txt | 2 +- .../localTypeScope.txt | 2 +- .../simpleScopeContextForPosition.pretty.txt | 6 +- .../simpleScopeContextForPosition.txt | 6 +- .../syntheticPropertiesScope.kt | 11 + .../syntheticPropertiesScope.pretty.txt | 37 +++ .../syntheticPropertiesScope.txt | 243 ++++++++++++++++++ .../typeScope/typeWithSyntheticProperties.kt | 13 + .../typeWithSyntheticProperties.pretty.txt | 15 ++ .../typeScope/typeWithSyntheticProperties.txt | 220 ++++++++++++++++ .../fir/declarations/ImplicitReceiverUtils.kt | 14 +- 25 files changed, 672 insertions(+), 48 deletions(-) create mode 100644 analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.kt create mode 100644 analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.pretty.txt create mode 100644 analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.txt create mode 100644 analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.kt create mode 100644 analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.pretty.txt create mode 100644 analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.txt diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirScopeProvider.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirScopeProvider.kt index 49cb88e8e5a..049efa5a0d1 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirScopeProvider.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirScopeProvider.kt @@ -45,6 +45,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.* import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhaseWithCallableMembers +import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtElement @@ -160,16 +161,17 @@ internal class KtFirScopeProvider( return KtCompositeScope.create(subScopes, token) } - override fun getTypeScope(type: KtType): KtTypeScope? = getFirTypeScope(type)?.let { convertToKtTypeScope(it) } + override fun getTypeScope(type: KtType): KtTypeScope? { + check(type is KtFirType) { "KtFirScopeProvider can only work with KtFirType, but ${type::class} was provided" } + return getFirTypeScope(type) + ?.withSyntheticPropertiesScopeOrSelf(type.coneType) + ?.let { convertToKtTypeScope(it) } + } override fun getSyntheticJavaPropertiesScope(type: KtType): KtTypeScope? { check(type is KtFirType) { "KtFirScopeProvider can only work with KtFirType, but ${type::class} was provided" } - val firTypeScope = getFirTypeScope(type) ?: return null - return FirSyntheticPropertiesScope.createIfSyntheticNamesProviderIsDefined( - firResolveSession.useSiteFirSession, - type.coneType, - firTypeScope - )?.let { convertToKtTypeScope(it) } + val typeScope = getFirTypeScope(type) ?: return null + return getFirSyntheticPropertiesScope(type.coneType, typeScope)?.let { convertToKtTypeScope(it) } } override fun getImportingScopeContext(file: KtFile): KtScopeContext { @@ -209,7 +211,9 @@ internal class KtFirScopeProvider( } val firScopes = towerDataElementsIndexed.flatMap { (index, towerDataElement) -> - val availableScopes = towerDataElement.getAvailableScopes().flatMap { flattenFirScope(it) } + val availableScopes = towerDataElement + .getAvailableScopes { coneType -> withSyntheticPropertiesScopeOrSelf(coneType) } + .flatMap { flattenFirScope(it) } availableScopes.map { IndexedValue(index, it) } } val ktScopesWithKinds = createScopesWithKind(firScopes) @@ -245,7 +249,7 @@ internal class KtFirScopeProvider( is FirNameAwareOnlyClassifiersScope -> getScopeKind(firScope.delegate, indexInTower) is FirLocalScope -> KtScopeKind.LocalScope(indexInTower) - is FirTypeScope -> KtScopeKind.SimpleTypeScope(indexInTower) + is FirTypeScope -> KtScopeKind.TypeScope(indexInTower) is FirTypeParameterScope -> KtScopeKind.TypeParameterScope(indexInTower) is FirPackageMemberScope -> KtScopeKind.PackageMemberScope(indexInTower) @@ -275,14 +279,23 @@ internal class KtFirScopeProvider( } } - private fun getFirTypeScope(type: KtType): FirTypeScope? { - check(type is KtFirType) { "KtFirScopeProvider can only work with KtFirType, but ${type::class} was provided" } - return type.coneType.scope( + private fun getFirTypeScope(type: KtFirType): FirTypeScope? = type.coneType.scope( + firResolveSession.useSiteFirSession, + getScopeSession(), + FakeOverrideTypeCalculator.Forced, + requiredMembersPhase = FirResolvePhase.STATUS, + ) + + private fun getFirSyntheticPropertiesScope(coneType: ConeKotlinType, typeScope: FirTypeScope): FirSyntheticPropertiesScope? = + FirSyntheticPropertiesScope.createIfSyntheticNamesProviderIsDefined( firResolveSession.useSiteFirSession, - getScopeSession(), - FakeOverrideTypeCalculator.Forced, - requiredMembersPhase = FirResolvePhase.STATUS, + coneType, + typeScope ) + + private fun FirTypeScope.withSyntheticPropertiesScopeOrSelf(coneType: ConeKotlinType): FirTypeScope { + val syntheticPropertiesScope = getFirSyntheticPropertiesScope(coneType, this) ?: return this + return FirTypeScopeWithSyntheticProperties(typeScope = this, syntheticPropertiesScope) } private fun buildJavaEnhancementDeclaredMemberScope( @@ -310,6 +323,43 @@ internal class KtFirScopeProvider( } } +private class FirTypeScopeWithSyntheticProperties( + val typeScope: FirTypeScope, + val syntheticPropertiesScope: FirSyntheticPropertiesScope, +) : FirTypeScope() { + override fun getCallableNames(): Set = typeScope.getCallableNames() + syntheticPropertiesScope.getCallableNames() + override fun getClassifierNames(): Set = typeScope.getClassifierNames() + override fun mayContainName(name: Name): Boolean = typeScope.mayContainName(name) || syntheticPropertiesScope.mayContainName(name) + override val scopeOwnerLookupNames: List get() = typeScope.scopeOwnerLookupNames + + override fun processDirectOverriddenFunctionsWithBaseScope( + functionSymbol: FirNamedFunctionSymbol, + processor: (FirNamedFunctionSymbol, FirTypeScope) -> ProcessorAction, + ): ProcessorAction = typeScope.processDirectOverriddenFunctionsWithBaseScope(functionSymbol, processor) + + override fun processDirectOverriddenPropertiesWithBaseScope( + propertySymbol: FirPropertySymbol, + processor: (FirPropertySymbol, FirTypeScope) -> ProcessorAction, + ): ProcessorAction = typeScope.processDirectOverriddenPropertiesWithBaseScope(propertySymbol, processor) + + override fun processClassifiersByNameWithSubstitution(name: Name, processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit) { + typeScope.processClassifiersByNameWithSubstitution(name, processor) + } + + override fun processFunctionsByName(name: Name, processor: (FirNamedFunctionSymbol) -> Unit) { + typeScope.processFunctionsByName(name, processor) + } + + override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) { + typeScope.processPropertiesByName(name, processor) + syntheticPropertiesScope.processPropertiesByName(name, processor) + } + + override fun processDeclaredConstructors(processor: (FirConstructorSymbol) -> Unit) { + typeScope.processDeclaredConstructors(processor) + } +} + private class EnumEntryContainingNamesAwareScope(private val originalScope: FirContainingNamesAwareScope) : FirContainingNamesAwareScope() { override fun getCallableNames(): Set = originalScope.getCallableNames() override fun getClassifierNames(): Set = originalScope.getClassifierNames() diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java index 0544e0c6a63..d1291fc2d1e 100644 --- a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java @@ -75,4 +75,10 @@ public class FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGener public void testSimpleScopeContextForPosition() throws Exception { runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.kt"); } + + @Test + @TestMetadata("syntheticPropertiesScope.kt") + public void testSyntheticPropertiesScope() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.kt"); + } } diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleTypeScopeTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleTypeScopeTestGenerated.java index 967fbdd63bd..e4f8de48efe 100644 --- a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleTypeScopeTestGenerated.java +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleTypeScopeTestGenerated.java @@ -81,4 +81,10 @@ public class FirIdeDependentAnalysisSourceModuleTypeScopeTestGenerated extends A public void testTypeParamList() throws Exception { runTest("analysis/analysis-api/testData/components/scopeProvider/typeScope/typeParamList.kt"); } + + @Test + @TestMetadata("typeWithSyntheticProperties.kt") + public void testTypeWithSyntheticProperties() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.kt"); + } } diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java index 268cc0b0eb2..0e87ed33215 100644 --- a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java @@ -75,4 +75,10 @@ public class FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerate public void testSimpleScopeContextForPosition() throws Exception { runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.kt"); } + + @Test + @TestMetadata("syntheticPropertiesScope.kt") + public void testSyntheticPropertiesScope() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.kt"); + } } diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleTypeScopeTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleTypeScopeTestGenerated.java index 8b52179c28b..3197d2673f8 100644 --- a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleTypeScopeTestGenerated.java +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleTypeScopeTestGenerated.java @@ -81,4 +81,10 @@ public class FirIdeNormalAnalysisSourceModuleTypeScopeTestGenerated extends Abst public void testTypeParamList() throws Exception { runTest("analysis/analysis-api/testData/components/scopeProvider/typeScope/typeParamList.kt"); } + + @Test + @TestMetadata("typeWithSyntheticProperties.kt") + public void testTypeWithSyntheticProperties() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.kt"); + } } diff --git a/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java index af48da44030..d1348e5739d 100644 --- a/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java +++ b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java @@ -75,4 +75,10 @@ public class FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestG public void testSimpleScopeContextForPosition() throws Exception { runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.kt"); } + + @Test + @TestMetadata("syntheticPropertiesScope.kt") + public void testSyntheticPropertiesScope() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.kt"); + } } diff --git a/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleTypeScopeTestGenerated.java b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleTypeScopeTestGenerated.java index fa239dd6476..c0d10540209 100644 --- a/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleTypeScopeTestGenerated.java +++ b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleTypeScopeTestGenerated.java @@ -81,4 +81,10 @@ public class FirStandaloneNormalAnalysisSourceModuleTypeScopeTestGenerated exten public void testTypeParamList() throws Exception { runTest("analysis/analysis-api/testData/components/scopeProvider/typeScope/typeParamList.kt"); } + + @Test + @TestMetadata("typeWithSyntheticProperties.kt") + public void testTypeWithSyntheticProperties() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.kt"); + } } diff --git a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtScopeProvider.kt b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtScopeProvider.kt index 23405189ac2..9e1aa19a6fb 100644 --- a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtScopeProvider.kt +++ b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtScopeProvider.kt @@ -90,7 +90,7 @@ public interface KtScopeProviderMixIn : KtAnalysisSessionMixIn { * Inside the `LIST_KT_ELEMENT.getKtType().getTypeScope()` would contain the `get(i: Int): String` method with substituted type `T = String` * * @return type scope for the given type if given `KtType` is not error type, `null` otherwise. - * Returned [KtTypeScope] doesn't include synthetic Java properties. To get such properties use [getSyntheticJavaPropertiesScope]. + * Returned [KtTypeScope] includes synthetic Java properties. * * @see KtTypeScope * @see KtTypeProviderMixIn.getKtType @@ -105,11 +105,10 @@ public interface KtScopeProviderMixIn : KtAnalysisSessionMixIn { withValidityAssertion { analysisSession.scopeProvider.getSyntheticJavaPropertiesScope(this) } /** - * Scopes in returned [KtScopeContext] don't include synthetic Java properties. - * To get such properties use [getSyntheticJavaPropertiesScope]. - * * For each scope in [KtScopeContext] an index is calculated. The indexes are relative to position, and they are only known for * scopes obtained with [getScopeContextForPosition]. + * + * Scopes with [KtScopeKind.TypeScope] include synthetic Java properties. */ public fun KtFile.getScopeContextForPosition(positionInFakeFile: KtElement): KtScopeContext = withValidityAssertion { analysisSession.scopeProvider.getScopeContextForPosition(this, positionInFakeFile) } @@ -174,14 +173,10 @@ public sealed class KtScopeKind { public class LocalScope(override val indexInTower: Int) : KtScopeKind() - public sealed class TypeScope : KtScopeKind() - /** - * Represents [KtTypeScope], which doesn't include synthetic Java properties of corresponding type. + * Represents [KtScope] for type, which include synthetic Java properties of corresponding type. */ - public class SimpleTypeScope(override val indexInTower: Int) : TypeScope() - - public class SyntheticJavaPropertiesScope(override val indexInTower: Int) : TypeScope() + public class TypeScope(override val indexInTower: Int) : KtScopeKind() public sealed class NonLocalScope : KtScopeKind() diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.pretty.txt index ef725dd94fd..fdecdcff98f 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.pretty.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.pretty.txt @@ -6,7 +6,7 @@ implicit receivers: scopes: LocalScope, index = 0, empty - SimpleTypeScope, index = 1 + TypeScope, index = 1 classifiers: 0 callables: 4 fun memberInContext() diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.txt index 59ecddb3fa0..2bf0103a575 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/contextReceiver.txt @@ -9,7 +9,7 @@ implicit receivers: scopes: LocalScope, index = 0, empty - SimpleTypeScope, index = 1 + TypeScope, index = 1 classifiers: 0 callables: 4 KtFunctionSymbol: diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.pretty.txt index 4ede6f1c1f4..5fd76d7a0f3 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.pretty.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.pretty.txt @@ -11,7 +11,7 @@ scopes: LocalScope, index = 1, empty - SimpleTypeScope, index = 2 + TypeScope, index = 2 classifiers: 1 companion object callables: 11 @@ -40,7 +40,7 @@ scopes: companion object callables: 0 - SimpleTypeScope, index = 5 + TypeScope, index = 5 classifiers: 0 callables: 3 fun equals(other: kotlin.Any?): kotlin.Boolean diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.txt index f84104966e9..df5f791baa3 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/enumEntry.txt @@ -17,7 +17,7 @@ scopes: LocalScope, index = 1, empty - SimpleTypeScope, index = 2 + TypeScope, index = 2 classifiers: 1 KtNamedClassOrObjectSymbol: annotationsList: [] @@ -642,7 +642,7 @@ scopes: visibility: Public callables: 0 - SimpleTypeScope, index = 5 + TypeScope, index = 5 classifiers: 0 callables: 3 KtFunctionSymbol: diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.pretty.txt index 16c33becaa1..2c79c73a1ec 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.pretty.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.pretty.txt @@ -7,13 +7,13 @@ implicit receivers: owner symbol: KtFirAnonymousFunctionSymbol scopes: - SimpleTypeScope, index = 0, empty + TypeScope, index = 0, empty LocalScope, index = 1, empty LocalScope, index = 2, empty - SimpleTypeScope, index = 3 + TypeScope, index = 3 classifiers: 0 callables: 5 fun add(e: T) diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.txt index 9a33cbbed2c..e8316c8c62e 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/errorType.txt @@ -16,13 +16,13 @@ implicit receivers: owner symbol: KtFirAnonymousFunctionSymbol scopes: - SimpleTypeScope, index = 0, empty + TypeScope, index = 0, empty LocalScope, index = 1, empty LocalScope, index = 2, empty - SimpleTypeScope, index = 3 + TypeScope, index = 3 classifiers: 0 callables: 5 KtFunctionSymbol: diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.pretty.txt index 3d1711e4f57..8b0d03e6310 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.pretty.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.pretty.txt @@ -11,7 +11,7 @@ scopes: LocalScope, index = 1, empty - SimpleTypeScope, index = 2 + TypeScope, index = 2 classifiers: 0 callables: 5 val propertyInY: kotlin.Int diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.txt index ce2441d573b..39bafb48595 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.txt @@ -28,7 +28,7 @@ scopes: LocalScope, index = 1, empty - SimpleTypeScope, index = 2 + TypeScope, index = 2 classifiers: 0 callables: 5 KtKotlinPropertySymbol: diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.pretty.txt index 8efb32c91bf..feb856631da 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.pretty.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.pretty.txt @@ -10,7 +10,7 @@ implicit receivers: owner symbol: KtFirNamedClassOrObjectSymbol scopes: - SimpleTypeScope, index = 0 + TypeScope, index = 0 classifiers: 0 callables: 4 fun memberInA() @@ -22,7 +22,7 @@ scopes: LocalScope, index = 2, empty - SimpleTypeScope, index = 3 + TypeScope, index = 3 classifiers: 0 callables: 4 fun memberInB() @@ -58,7 +58,7 @@ scopes: T callables: 0 - SimpleTypeScope, index = 10 + TypeScope, index = 10 classifiers: 2 class NestedInC class NestedInJavaClass diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.txt index 31bbcad8e61..2a8f93e4a9f 100644 --- a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.txt +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/simpleScopeContextForPosition.txt @@ -19,7 +19,7 @@ implicit receivers: owner symbol: KtFirNamedClassOrObjectSymbol scopes: - SimpleTypeScope, index = 0 + TypeScope, index = 0 classifiers: 0 callables: 4 KtFunctionSymbol: @@ -156,7 +156,7 @@ scopes: LocalScope, index = 2, empty - SimpleTypeScope, index = 3 + TypeScope, index = 3 classifiers: 0 callables: 4 KtFunctionSymbol: @@ -416,7 +416,7 @@ scopes: variance: INVARIANT callables: 0 - SimpleTypeScope, index = 10 + TypeScope, index = 10 classifiers: 2 KtNamedClassOrObjectSymbol: annotationsList: [] diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.kt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.kt new file mode 100644 index 00000000000..915273161da --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.kt @@ -0,0 +1,11 @@ +// FILE: JavaClass.java +public class JavaClass { + public Integer getValue() { + return 1; + } +} + +// FILE: main.kt +fun JavaClass.test() { + e +} \ No newline at end of file diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.pretty.txt new file mode 100644 index 00000000000..383ef0d4bf2 --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.pretty.txt @@ -0,0 +1,37 @@ +element: e +implicit receivers: + type: JavaClass + owner symbol: KtFirFunctionSymbol + +scopes: + LocalScope, index = 0, empty + + TypeScope, index = 1 + classifiers: 0 + callables: 5 + fun getValue(): kotlin.Int? + fun equals(other: kotlin.Any?): kotlin.Boolean + fun hashCode(): kotlin.Int + fun toString(): kotlin.String + val value: kotlin.Int? + get() + + LocalScope, index = 2, empty + + ExplicitSimpleImportingScope, index = 3, empty + + PackageMemberScope, index = 4 + classifiers: 0 + callables: 1 + fun JavaClass.test() + + DefaultSimpleImportingScope, index = 5 + + DefaultSimpleImportingScope, index = 6 + + ExplicitStarImportingScope, index = 7, empty + + DefaultSimpleImportingScope, index = 8 + + DefaultStarImportingScope, index = 9 + diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.txt new file mode 100644 index 00000000000..2a3e628525d --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/syntheticPropertiesScope.txt @@ -0,0 +1,243 @@ +element: e +implicit receivers: + type: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: JavaClass + owner symbol: KtFirFunctionSymbol + +scopes: + LocalScope, index = 0, empty + + TypeScope, index = 1 + classifiers: 0 + callables: 5 + KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: /JavaClass.getValue + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: false + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: false + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: getValue + origin: JAVA + receiverParameter: null + returnType: KtFlexibleType: + annotationsList: [] + type: kotlin/Int! + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [] + visibility: Public + KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: kotlin/Any.equals + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: true + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: true + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: equals + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/Boolean + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [ + KtValueParameterSymbol: + annotationsList: [] + callableIdIfNonLocal: null + contextReceivers: [] + generatedPrimaryConstructorProperty: null + hasDefaultValue: false + isCrossinline: false + isExtension: false + isImplicitLambdaParameter: false + isNoinline: false + isVararg: false + name: other + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/Any? + symbolKind: LOCAL + typeParameters: [] + ] + visibility: Public + KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: kotlin/Any.hashCode + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: true + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: false + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: hashCode + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/Int + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [] + visibility: Public + KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: kotlin/Any.toString + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: true + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: false + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: toString + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/String + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [] + visibility: Public + KtSyntheticJavaPropertySymbol: + annotationsList: [] + backingFieldSymbol: null + callableIdIfNonLocal: /JavaClass.value + contextReceivers: [] + getter: KtPropertyGetterSymbol: + annotationsList: [] + callableIdIfNonLocal: /JavaClass.getValue + contextReceivers: [] + hasBody: false + hasStableParameterNames: true + isDefault: false + isExtension: false + isInline: false + isOverride: false + modality: OPEN + origin: JAVA_SYNTHETIC_PROPERTY + receiverParameter: null + returnType: KtFlexibleType: + annotationsList: [] + type: kotlin/Int! + symbolKind: ACCESSOR + typeParameters: [] + valueParameters: [] + visibility: Public + hasBackingField: true + hasGetter: true + hasSetter: false + initializer: null + isDelegatedProperty: false + isExtension: false + isFromPrimaryConstructor: false + isOverride: false + isStatic: false + isVal: true + javaGetterSymbol: KtFunctionSymbol(/JavaClass.getValue) + javaSetterSymbol: null + modality: OPEN + name: value + origin: JAVA_SYNTHETIC_PROPERTY + receiverParameter: null + returnType: KtFlexibleType: + annotationsList: [] + type: kotlin/Int! + setter: null + symbolKind: CLASS_MEMBER + typeParameters: [] + visibility: Public + + LocalScope, index = 2, empty + + ExplicitSimpleImportingScope, index = 3, empty + + PackageMemberScope, index = 4 + classifiers: 0 + callables: 1 + KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: /test + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: true + isBuiltinFunctionInvoke: false + isExtension: true + isExternal: false + isInfix: false + isInline: false + isOperator: false + isOverride: false + isStatic: false + isSuspend: false + modality: FINAL + name: test + origin: SOURCE + receiverParameter: KtReceiverParameterSymbol: + annotationsList: [] + origin: SOURCE + owningCallableSymbol: KtFunctionSymbol(/test) + type: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: JavaClass + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/Unit + symbolKind: TOP_LEVEL + typeParameters: [] + valueParameters: [] + visibility: Public + + DefaultSimpleImportingScope, index = 5 + + DefaultSimpleImportingScope, index = 6 + + ExplicitStarImportingScope, index = 7, empty + + DefaultSimpleImportingScope, index = 8 + + DefaultStarImportingScope, index = 9 + diff --git a/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.kt b/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.kt new file mode 100644 index 00000000000..204920c0b40 --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.kt @@ -0,0 +1,13 @@ +// FILE: JavaClass.java +public class JavaClass { + public Integer getValue() { + return 1; + } +} + +// FILE: main.kt +class A : JavaClass() + +fun test(a: A) { + a +} \ No newline at end of file diff --git a/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.pretty.txt new file mode 100644 index 00000000000..2c5db9ec417 --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.pretty.txt @@ -0,0 +1,15 @@ +KtTypeScope: +fun getValue(): kotlin.Int! +fun equals(other: kotlin.Any?): kotlin.Boolean +fun hashCode(): kotlin.Int +fun toString(): kotlin.String +val value: kotlin.Int! + + +Declaration Scope: +fun getValue(): kotlin.Int? +fun equals(other: kotlin.Any?): kotlin.Boolean +fun hashCode(): kotlin.Int +fun toString(): kotlin.String +val value: kotlin.Int? + get() diff --git a/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.txt b/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.txt new file mode 100644 index 00000000000..8f71309ca5e --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/typeScope/typeWithSyntheticProperties.txt @@ -0,0 +1,220 @@ +expression: a +KtType: A + +KtTypeScope: +KtFunctionLikeSignature: + receiverType = null + returnType = kotlin.Int! + symbol = /JavaClass.getValue(: JavaClass): kotlin.Int! + valueParameters = [] + callableIdIfNonLocal = /JavaClass.getValue +KtFunctionLikeSignature: + receiverType = null + returnType = kotlin.Boolean + symbol = kotlin/Any.equals(: kotlin.Any, other: kotlin.Any?): kotlin.Boolean + valueParameters = [ + KtVariableLikeSignature: + name = other + receiverType = null + returnType = kotlin.Any? + symbol = other: kotlin.Any? + callableIdIfNonLocal = null + ] + callableIdIfNonLocal = kotlin/Any.equals +KtFunctionLikeSignature: + receiverType = null + returnType = kotlin.Int + symbol = kotlin/Any.hashCode(: kotlin.Any): kotlin.Int + valueParameters = [] + callableIdIfNonLocal = kotlin/Any.hashCode +KtFunctionLikeSignature: + receiverType = null + returnType = kotlin.String + symbol = kotlin/Any.toString(: kotlin.Any): kotlin.String + valueParameters = [] + callableIdIfNonLocal = kotlin/Any.toString +KtVariableLikeSignature: + name = value + receiverType = null + returnType = kotlin.Int! + symbol = val value: kotlin.Int! + callableIdIfNonLocal = /JavaClass.value + + +Declaration Scope: +KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: /JavaClass.getValue + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: false + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: false + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: getValue + origin: JAVA + receiverParameter: null + returnType: KtFlexibleType: + annotationsList: [] + type: kotlin/Int! + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [] + visibility: Public +KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: kotlin/Any.equals + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: true + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: true + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: equals + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/Boolean + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [ + KtValueParameterSymbol: + annotationsList: [] + callableIdIfNonLocal: null + contextReceivers: [] + generatedPrimaryConstructorProperty: null + hasDefaultValue: false + isCrossinline: false + isExtension: false + isImplicitLambdaParameter: false + isNoinline: false + isVararg: false + name: other + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/Any? + symbolKind: LOCAL + typeParameters: [] + ] + visibility: Public +KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: kotlin/Any.hashCode + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: true + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: false + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: hashCode + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/Int + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [] + visibility: Public +KtFunctionSymbol: + annotationsList: [] + callableIdIfNonLocal: kotlin/Any.toString + contextReceivers: [] + contractEffects: [] + hasStableParameterNames: true + isBuiltinFunctionInvoke: false + isExtension: false + isExternal: false + isInfix: false + isInline: false + isOperator: false + isOverride: false + isStatic: false + isSuspend: false + modality: OPEN + name: toString + origin: LIBRARY + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/String + symbolKind: CLASS_MEMBER + typeParameters: [] + valueParameters: [] + visibility: Public +KtSyntheticJavaPropertySymbol: + annotationsList: [] + backingFieldSymbol: null + callableIdIfNonLocal: /JavaClass.value + contextReceivers: [] + getter: KtPropertyGetterSymbol: + annotationsList: [] + callableIdIfNonLocal: /JavaClass.getValue + contextReceivers: [] + hasBody: false + hasStableParameterNames: true + isDefault: false + isExtension: false + isInline: false + isOverride: false + modality: OPEN + origin: JAVA_SYNTHETIC_PROPERTY + receiverParameter: null + returnType: KtFlexibleType: + annotationsList: [] + type: kotlin/Int! + symbolKind: ACCESSOR + typeParameters: [] + valueParameters: [] + visibility: Public + hasBackingField: true + hasGetter: true + hasSetter: false + initializer: null + isDelegatedProperty: false + isExtension: false + isFromPrimaryConstructor: false + isOverride: false + isStatic: false + isVal: true + javaGetterSymbol: KtFunctionSymbol(/JavaClass.getValue) + javaSetterSymbol: null + modality: OPEN + name: value + origin: JAVA_SYNTHETIC_PROPERTY + receiverParameter: null + returnType: KtFlexibleType: + annotationsList: [] + type: kotlin/Int! + setter: null + symbolKind: CLASS_MEMBER + typeParameters: [] + visibility: Public diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt index 6ae3bf5b96b..a7081ba5319 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt @@ -269,18 +269,22 @@ class FirTowerDataElement( * * Note that a scope for a companion object is an implicit scope. */ - fun getAvailableScopes(): List = when { + fun getAvailableScopes( + processTypeScope: FirTypeScope.(ConeKotlinType) -> FirTypeScope = { this }, + ): List = when { scope != null -> listOf(scope) - implicitReceiver != null -> listOf(implicitReceiver.getImplicitScope()) - contextReceiverGroup != null -> contextReceiverGroup.map { it.getImplicitScope() } + implicitReceiver != null -> listOf(implicitReceiver.getImplicitScope(processTypeScope)) + contextReceiverGroup != null -> contextReceiverGroup.map { it.getImplicitScope(processTypeScope) } else -> error("Tower data element is expected to have either scope or implicit receivers.") } - private fun ImplicitReceiverValue<*>.getImplicitScope(): FirScope { + private fun ImplicitReceiverValue<*>.getImplicitScope( + processTypeScope: FirTypeScope.(ConeKotlinType) -> FirTypeScope, + ): FirScope { return when (type.fullyExpandedType(useSiteSession)) { is ConeErrorType, is ConeStubType -> FirTypeScope.Empty - else -> implicitScope ?: error("Scope for type ${type::class.simpleName} is null.") + else -> implicitScope?.processTypeScope(type) ?: error("Scope for type ${type::class.simpleName} is null.") } } }