From 254151d82cbccccaabe147e92fcaab16dacfd1b6 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Thu, 12 May 2022 21:00:21 +0400 Subject: [PATCH] [FIR IDE] Use proper `FirSession`s in `FirSupertypesResolution` Symbols might be from other modules, so we need to use corresponding `FirSession`s; otherwise it would be impossible to query symbols from `firProvider` ^KTIJ-21714 Fixed --- ...nosisCompilerFirTestdataTestGenerated.java | 18 +++++++ ...TouchedTilContractsPhaseTestGenerated.java | 15 ++++++ ...erentlyImportedTypeFromOtherModule.fir.txt | 34 +++++++++++++ .../inherentlyImportedTypeFromOtherModule.kt | 24 +++++++++ ...rentlyImportedTypeFromOtherModule2.fir.txt | 46 +++++++++++++++++ .../inherentlyImportedTypeFromOtherModule2.kt | 27 ++++++++++ ...rentlyImportedTypeFromOtherModule3.fir.txt | 50 +++++++++++++++++++ .../inherentlyImportedTypeFromOtherModule3.kt | 32 ++++++++++++ .../runners/FirDiagnosticTestGenerated.java | 18 +++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 18 +++++++ .../transformers/FirSupertypesResolution.kt | 14 +++--- 11 files changed, 290 insertions(+), 6 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt create mode 100644 compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt create mode 100644 compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index 609bcea3c55..9714f46c41c 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -284,6 +284,24 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolve/incorrectSuperCall.kt"); } + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule.kt") + public void testInherentlyImportedTypeFromOtherModule() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt"); + } + + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule2.kt") + public void testInherentlyImportedTypeFromOtherModule2() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt"); + } + + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule3.kt") + public void testInherentlyImportedTypeFromOtherModule3() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt"); + } + @Test @TestMetadata("intersectionScope.kt") public void testIntersectionScope() throws Exception { diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index 74b2fbf6855..64f21a91da5 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -244,6 +244,21 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/incorrectSuperCall.kt"); } + @TestMetadata("inherentlyImportedTypeFromOtherModule.kt") + public void testInherentlyImportedTypeFromOtherModule() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt"); + } + + @TestMetadata("inherentlyImportedTypeFromOtherModule2.kt") + public void testInherentlyImportedTypeFromOtherModule2() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt"); + } + + @TestMetadata("inherentlyImportedTypeFromOtherModule3.kt") + public void testInherentlyImportedTypeFromOtherModule3() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt"); + } + @TestMetadata("intersectionScope.kt") public void testIntersectionScope() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/intersectionScope.kt"); diff --git a/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.fir.txt b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.fir.txt new file mode 100644 index 00000000000..8cda78fe718 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.fir.txt @@ -0,0 +1,34 @@ +Module: lib +FILE: module_lib_inherentlyImportedTypeFromOtherModule.kt + package dependency + + public abstract class Test : R|kotlin/Any| { + public constructor(): R|dependency/Test| { + super() + } + + public abstract interface Result : R|kotlin/Any| { + } + + public final class Success : R|dependency/Test.Result| { + public constructor(): R|dependency/Test.Success| { + super() + } + + } + + } +Module: main +FILE: module_main_inherentlyImportedTypeFromOtherModule.kt + package main + + public final class Main : R|dependency/Test| { + public constructor(): R|main/Main| { + super() + } + + public final fun usage(): R|dependency/Test.Result| { + ^usage R|dependency/Test.Success.Success|() + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt new file mode 100644 index 00000000000..1efd3836dad --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt @@ -0,0 +1,24 @@ +// SKIP_JAVAC +// This directive is needed to skip this test in LazyBodyIsNotTouchedTilContractsPhaseTestGenerated, +// because it fails to parse module structure of multimodule test + +// MODULE: lib +package dependency + +abstract class Test { + interface Result + + class Success : Result +} + + +// MODULE: main(lib) +package main + +import dependency.Test + +class Main : Test() { + fun usage(): Result { + return Success() + } +} \ No newline at end of file diff --git a/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.fir.txt b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.fir.txt new file mode 100644 index 00000000000..31eafef0794 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.fir.txt @@ -0,0 +1,46 @@ +Module: lib +FILE: module_lib_inherentlyImportedTypeFromOtherModule2.kt + package lib + + public abstract class FirstBase : R|kotlin/Any| { + public constructor(): R|lib/FirstBase| { + super() + } + + public abstract interface Result : R|kotlin/Any| { + } + + } + public abstract class SecondBase : R|lib/FirstBase| { + public constructor(): R|lib/SecondBase| { + super() + } + + } + public abstract class Test : R|lib/SecondBase| { + public constructor(): R|lib/Test| { + super() + } + + public final class Success : R|lib/FirstBase.Result| { + public constructor(): R|lib/Test.Success| { + super() + } + + } + + } +Module: main +FILE: module_main_inherentlyImportedTypeFromOtherModule2.kt + package main + + public final class Main : R|lib/Test| { + public constructor(): R|main/Main| { + super() + } + + public final fun usage(): R|lib/FirstBase.Result| { + ^usage R|lib/Test.Success.Success|() + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt new file mode 100644 index 00000000000..b2d12503195 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt @@ -0,0 +1,27 @@ +// SKIP_JAVAC +// This directive is needed to skip this test in LazyBodyIsNotTouchedTilContractsPhaseTestGenerated, +// because it fails to parse module structure of multimodule test + +// MODULE: lib +package lib + +abstract class FirstBase { + interface Result +} + +abstract class SecondBase : FirstBase() + +abstract class Test : SecondBase() { + class Success : Result +} + +// MODULE: main(lib) +package main + +import lib.Test + +class Main : Test() { + fun usage(): Result { + return Success() + } +} \ No newline at end of file diff --git a/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.fir.txt b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.fir.txt new file mode 100644 index 00000000000..0266a18dee3 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.fir.txt @@ -0,0 +1,50 @@ +Module: lib1 +FILE: module_lib1_inherentlyImportedTypeFromOtherModule3.kt + package lib1 + + public abstract class FirstBase : R|kotlin/Any| { + public constructor(): R|lib1/FirstBase| { + super() + } + + public abstract interface Result : R|kotlin/Any| { + } + + } + public abstract class SecondBase : R|lib1/FirstBase| { + public constructor(): R|lib1/SecondBase| { + super() + } + + } +Module: lib2 +FILE: module_lib2_inherentlyImportedTypeFromOtherModule3.kt + package lib2 + + public abstract class Test : R|lib1/SecondBase| { + public constructor(): R|lib2/Test| { + super() + } + + public final class Success : R|lib1/FirstBase.Result| { + public constructor(): R|lib2/Test.Success| { + super() + } + + } + + } +Module: main +FILE: module_main_inherentlyImportedTypeFromOtherModule3.kt + package main + + public final class Main : R|lib2/Test| { + public constructor(): R|main/Main| { + super() + } + + public final fun usage(): R|lib1/FirstBase.Result| { + ^usage R|lib2/Test.Success.Success|() + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt new file mode 100644 index 00000000000..492a95f8159 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt @@ -0,0 +1,32 @@ +// SKIP_JAVAC +// This directive is needed to skip this test in LazyBodyIsNotTouchedTilContractsPhaseTestGenerated, +// because it fails to parse module structure of multimodule test + +// MODULE: lib1 +package lib1 + +abstract class FirstBase { + interface Result +} + +abstract class SecondBase : FirstBase() + +// MODULE: lib2(lib1) +package lib2 + +import lib1.SecondBase + +abstract class Test : SecondBase() { + class Success : Result +} + +// MODULE: main(lib1, lib2) +package main + +import lib2.Test + +class Main : Test() { + fun usage(): Result { + return Success() + } +} \ No newline at end of file diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index b495dffac4c..01688934b66 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -284,6 +284,24 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/incorrectSuperCall.kt"); } + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule.kt") + public void testInherentlyImportedTypeFromOtherModule() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt"); + } + + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule2.kt") + public void testInherentlyImportedTypeFromOtherModule2() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt"); + } + + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule3.kt") + public void testInherentlyImportedTypeFromOtherModule3() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt"); + } + @Test @TestMetadata("intersectionScope.kt") public void testIntersectionScope() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 99c50cf8831..dee05bb85fc 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -284,6 +284,24 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/incorrectSuperCall.kt"); } + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule.kt") + public void testInherentlyImportedTypeFromOtherModule() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule.kt"); + } + + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule2.kt") + public void testInherentlyImportedTypeFromOtherModule2() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule2.kt"); + } + + @Test + @TestMetadata("inherentlyImportedTypeFromOtherModule3.kt") + public void testInherentlyImportedTypeFromOtherModule3() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inherentlyImportedTypeFromOtherModule3.kt"); + } + @Test @TestMetadata("intersectionScope.kt") public void testIntersectionScope() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt index e30d01c3432..d459b41c4c5 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt @@ -243,9 +243,9 @@ open class FirSupertypeResolverVisitor( if (firProviderInterceptor != null) firProviderInterceptor.getFirClassifierContainerFileIfAny(symbol) else symbol.moduleData.session.firProvider.getFirClassifierContainerFileIfAny(symbol.classId) - private fun getFirClassifierByFqName(classId: ClassId): FirClassLikeDeclaration? = + private fun getFirClassifierByFqName(moduleSession: FirSession, classId: ClassId): FirClassLikeDeclaration? = if (firProviderInterceptor != null) firProviderInterceptor.getFirClassifierByFqName(classId) - else session.firProvider.getFirClassifierByFqName(classId) + else moduleSession.firProvider.getFirClassifierByFqName(classId) override fun visitElement(element: FirElement, data: Any?) {} @@ -283,12 +283,13 @@ open class FirSupertypeResolverVisitor( visited: MutableSet = mutableSetOf() ) { if (!visited.add(classLikeDeclaration)) return - val supertypes = + val supertypes: List = resolveSpecificClassLikeSupertypes(classLikeDeclaration, supertypeRefs).map { it.coneType } for (supertype in supertypes) { if (supertype !is ConeClassLikeType) continue - val fir = getFirClassifierByFqName(supertype.lookupTag.classId) ?: continue + val supertypeModuleSession = supertype.toSymbol(session)?.moduleData?.session ?: continue + val fir = getFirClassifierByFqName(supertypeModuleSession, supertype.lookupTag.classId) ?: continue resolveAllSupertypes(fir, fir.supertypeRefs(), visited) } } @@ -301,6 +302,7 @@ open class FirSupertypeResolverVisitor( private fun prepareScopes(classLikeDeclaration: FirClassLikeDeclaration): PersistentList { val classId = classLikeDeclaration.symbol.classId + val classModuleSession = classLikeDeclaration.moduleData.session val result = when { classId.isLocal -> { @@ -316,11 +318,11 @@ open class FirSupertypeResolverVisitor( } } classLikeDeclaration.safeAs()?.isCompanion == true -> { - val outerClassFir = classId.outerClassId?.let(::getFirClassifierByFqName) as? FirRegularClass + val outerClassFir = classId.outerClassId?.let { getFirClassifierByFqName(classModuleSession, it) } as? FirRegularClass prepareScopeForCompanion(outerClassFir ?: return persistentListOf()) } classId.isNestedClass -> { - val outerClassFir = classId.outerClassId?.let(::getFirClassifierByFqName) as? FirRegularClass + val outerClassFir = classId.outerClassId?.let { getFirClassifierByFqName(classModuleSession, it) } as? FirRegularClass prepareScopeForNestedClasses(outerClassFir ?: return persistentListOf()) } else -> getFirClassifierContainerFileIfAny(classLikeDeclaration.symbol)?.let(::prepareFileScopes) ?: persistentListOf()