Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider providing KSDeclaration.isPlatformCode() #2233

Open
AngeloSalvade opened this issue Nov 27, 2024 · 0 comments
Open

Consider providing KSDeclaration.isPlatformCode() #2233

AngeloSalvade opened this issue Nov 27, 2024 · 0 comments

Comments

@AngeloSalvade
Copy link

AngeloSalvade commented Nov 27, 2024

I'm generating code with KSP. The generated code will be used in common/intermediate code.

I understand that this is not allowed.
See https://slack-chats.kotlinlang.org/t/16366233/i-m-trying-out-kotlin-2-0-beta-3-and-it-looks-like-generated:
Common/intermediate (= none-platform) code cannot reference generated code in the compilation of platform code.
Generated codes are treated as platform code (you'll have to use expect/actual).

I therefore implemented the proposed expect/actual solution.
Everything works fine (see https://github.com/softappeal/yass2/blob/main/yass2-generate/src/jvmMain/kotlin/ksp/Generate.kt).
For finding out if a KSDeclaration is in platform specific code I use the following code:

private val Platforms = setOf(
    "jvm", // JVM
    "js", // JavaScript
    "wasmJs", "wasmWasi",// WebAssembly
    "macosX64", "macosArm64", // macOS
    "iosArm64", "iosX64", "iosSimulatorArm64", // iOS
    "linuxX64", "linuxArm64", // Linux
    "watchosArm64", "watchosX64", "watchosSimulatorArm64", "watchosDeviceArm64", // watchOS
    "tvosArm64", "tvosX64", "tvosSimulatorArm64", // tvOS
    "mingwX64", // Windows
)

public fun KSDeclaration.isPlatformCode(): Boolean {
    val filePath = containingFile!!.filePath
    return Platforms.any { platform -> filePath.contains("/${platform}Main/") || filePath.contains("/${platform}Test/") }
}

isPlatformCode() is then used to find out if a generated function needs the actual keyword.
I believe/hope there must be a more elegant solution for isPlatformCode().
Any ideas are welcome.

Or even better: Could isPlatformCode() be part of KSP API?
That would be very convenient for implementing expect/actual solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant