Skip to content

Commit

Permalink
[K2/JS] Use declaration session for looking up containing declaration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kunyavskiy committed May 6, 2024
1 parent 644dd05 commit 8e0919e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ expect class <!WRONG_EXPORTED_DECLARATION!>WithExportOnExpect<!> {
}

expect class WithExportOnActual {
<!WRONG_EXPORTED_DECLARATION!>fun foo()<!>
<!WRONG_EXPORTED_DECLARATION, WRONG_EXPORTED_DECLARATION!>val bar: Int<!>
fun foo()
val bar: Int
}

expect class WithExportTypealiasOnActual {
<!WRONG_EXPORTED_DECLARATION!>fun foo()<!>
<!WRONG_EXPORTED_DECLARATION, WRONG_EXPORTED_DECLARATION!>val bar: Int<!>
fun foo()
val bar: Int
}

expect class <!WRONG_EXPORTED_DECLARATION!>WithFileExportOnActual<!> {
expect class WithFileExportOnActual {
fun foo()
val bar: Int
}

// FILE: common2.kt
@file:Export

expect class WithExportOnExpectFile {
fun foo()
val bar: Int
expect class <!WRONG_EXPORTED_DECLARATION!>WithExportOnExpectFile<!> {
<!WRONG_EXPORTED_DECLARATION!>fun foo()<!>
<!WRONG_EXPORTED_DECLARATION, WRONG_EXPORTED_DECLARATION!>val bar: Int<!>
}

// MODULE: m1-js()()(m1-common)
Expand Down

0 comments on commit 8e0919e

Please sign in to comment.