From 8e0919e3a1d333ef67572791e315a556489b11d7 Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Mon, 6 May 2024 14:23:46 +0200 Subject: [PATCH] [K2/JS] Use declaration session for looking up containing declaration There was a couple of helper functions, in particular hasAnnotationOrInsideAnnotatedClass, with looked up containing class or file of declaration, using call-site session. This is incorrect because it can find a file of actual declaration, instead of expect one, with unpredictable result. ^KT-67978 Fixed --- .../fir/analysis/js/checkers/FirJsHelpers.kt | 8 ++++---- .../js/checkers/FirJsModuleCheckUtils.kt | 2 +- .../kotlin/fir/analysis/checkers/FirHelpers.kt | 2 +- .../multiplatform/expectsWithJsExport.fir.kt | 16 ++++++++-------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsHelpers.kt b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsHelpers.kt index ce8595438568b..a2d78789c2e82 100644 --- a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsHelpers.kt +++ b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsHelpers.kt @@ -122,14 +122,14 @@ fun FirBasedSymbol<*>.isExportedObject(session: FirSession): Boolean { return when { hasAnnotationOrInsideAnnotatedClass(JsStandardClassIds.Annotations.JsExportIgnore, session) -> false hasAnnotationOrInsideAnnotatedClass(JsStandardClassIds.Annotations.JsExport, session) -> true - else -> getContainingFile(session)?.hasAnnotation(JsStandardClassIds.Annotations.JsExport, session) == true + else -> getContainingFile()?.hasAnnotation(JsStandardClassIds.Annotations.JsExport, session) == true } } -internal fun FirBasedSymbol<*>.getContainingFile(session: FirSession): FirFile? { +internal fun FirBasedSymbol<*>.getContainingFile(): FirFile? { return when (this) { - is FirCallableSymbol<*> -> session.firProvider.getFirCallableContainerFile(this) - is FirClassLikeSymbol<*> -> session.firProvider.getFirClassifierContainerFileIfAny(this) + is FirCallableSymbol<*> -> moduleData.session.firProvider.getFirCallableContainerFile(this) + is FirClassLikeSymbol<*> -> moduleData.session.firProvider.getFirClassifierContainerFileIfAny(this) else -> return null } } diff --git a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsModuleCheckUtils.kt b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsModuleCheckUtils.kt index 1877aaf2d36c4..77d4e5d2207d5 100644 --- a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsModuleCheckUtils.kt +++ b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/FirJsModuleCheckUtils.kt @@ -37,7 +37,7 @@ internal fun checkJsModuleUsage( val calleeSession = callee.moduleData.session val calleeRoot = getRootClassLikeSymbolOrSelf(callee, calleeSession) - val calleeContainingFile = calleeRoot.getContainingFile(calleeSession) + val calleeContainingFile = calleeRoot.getContainingFile() val callToModule = calleeRoot.getAnnotationStringParameter(JsStandardClassIds.Annotations.JsModule, calleeSession) != null || calleeContainingFile?.symbol?.getAnnotationStringParameter(JsStandardClassIds.Annotations.JsModule, calleeSession) != null diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirHelpers.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirHelpers.kt index f7588541ecdef..9b482f57c9a78 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirHelpers.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirHelpers.kt @@ -791,7 +791,7 @@ val CheckerContext.isTopLevel get() = containingDeclarations.lastOrNull().let { fun FirBasedSymbol<*>.hasAnnotationOrInsideAnnotatedClass(classId: ClassId, session: FirSession): Boolean { if (hasAnnotation(classId, session)) return true - val container = getContainingClassSymbol(session) ?: return false + val container = getContainingClassSymbol(moduleData.session) ?: return false return container.hasAnnotationOrInsideAnnotatedClass(classId, session) } diff --git a/compiler/testData/diagnostics/tests/multiplatform/expectsWithJsExport.fir.kt b/compiler/testData/diagnostics/tests/multiplatform/expectsWithJsExport.fir.kt index 36b846f08413f..2e6da2d964b67 100644 --- a/compiler/testData/diagnostics/tests/multiplatform/expectsWithJsExport.fir.kt +++ b/compiler/testData/diagnostics/tests/multiplatform/expectsWithJsExport.fir.kt @@ -13,16 +13,16 @@ expect class WithExportOnExpect { } expect class WithExportOnActual { - fun foo() - val bar: Int + fun foo() + val bar: Int } expect class WithExportTypealiasOnActual { - fun foo() - val bar: Int + fun foo() + val bar: Int } -expect class WithFileExportOnActual { +expect class WithFileExportOnActual { fun foo() val bar: Int } @@ -30,9 +30,9 @@ expect class WithFileExportOnActual { // FILE: common2.kt @file:Export -expect class WithExportOnExpectFile { - fun foo() - val bar: Int +expect class WithExportOnExpectFile { + fun foo() + val bar: Int } // MODULE: m1-js()()(m1-common)