Skip to content

fix(compiler-plugin): resolve ABI incompatibilities with Kotlin 2.4+ runtimes - #121

Draft
aryapreetam wants to merge 1 commit into
Kotlin:mainfrom
aryapreetam:fix/compiler-plugin-k2-compat
Draft

aryapreetam wants to merge 1 commit into
Kotlin:mainfrom
aryapreetam:fix/compiler-plugin-k2-compat

Conversation

@aryapreetam

@aryapreetam aryapreetam commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Consuming projects targeting Kotlin 2.4+ (such as projects on Compose Multiplatform 1.11+ using Kotlin 2.4.10 or 2.4.20) fail during compilation:

  1. Extension Registration Signature Mismatch: Kotlin 2.4 changed the parameter type of ExtensionStorage.registerExtension from ProjectExtensionDescriptor to ExtensionPointDescriptor. When a compiler daemon running Kotlin 2.4 executes the plugin compiled against Kotlin 2.3, this causes a ClassCastException.
  2. Metadata Annotation Type Requirement: In Kotlin 2.4, declarations passed to metadataDeclarationRegistrar.addMetadataVisibleAnnotationsToElement must implement IrAnnotation (instantiated via IrAnnotationImpl). Passing IrConstructorCallImpl results in a ClassCastException.
  3. Unscoped Native Annotation Lookup: HiddenFromObjC was being resolved on all compilation targets, rather than being restricted to Kotlin/Native targets.

Solution

1. Dynamic Extension Point Registration

In StorytaleComponentRegistrar.kt, resolve and invoke ExtensionStorage.registerExtension reflectively. This allows the same compiled plugin binary to execute on both Kotlin 2.3.x and Kotlin 2.4+ compiler environments without ABI conflicts.

2. Cross-Version IrAnnotation Instantiation

In StorytaleLoweringExtension.kt, reflectively instantiate IrAnnotationImpl via BuildersKt.fromSymbolOwner on Kotlin 2.4+ runtimes, while falling back to IrConstructorCallImpl on Kotlin 2.3.x runtimes.

3. Platform-Scoped Native Annotation

Added an isNativeTarget check to ensure the HiddenFromObjC constructor lookup and annotation application are only executed for Kotlin/Native compilations.


Design Decisions

  • Why use reflection instead of raising the plugin's Kotlin dependency? Bumping the plugin's compile-time Kotlin dependency to 2.4+ would drop compatibility for existing projects on Kotlin 2.3.x. Targeted reflective lookups allow one plugin artifact to support both Kotlin 2.3.x and 2.4+ runtimes.

Verification

1. Automated Checks

  • ./gradlew spotlessCheck (passes)
  • ./gradlew -p modules/compiler-plugin test (passes)

2. Multi-Environment & Multi-Target Execution

Verified live execution across internal and external projects running different Kotlin and AGP environments:

Project Environment Target Task
:examples Kotlin 2.3.20 / AGP 8.11 Desktop JVM ./gradlew :examples:desktopStoriesRun
:examples Kotlin 2.3.20 / AGP 8.11 Wasm ./gradlew :examples:wasmJsBrowserStoriesDevelopmentRun
storytale-sample Kotlin 2.4.20 / AGP 9.1.1 Desktop JVM ./gradlew :shared:jvmStoriesRun -PcmpProfile=1.12
storytale-sample Kotlin 2.4.20 / AGP 9.1.1 Wasm ./gradlew :shared:wasmJsBrowserStoriesDevelopmentRun -PcmpProfile=1.12
storytale-sample Kotlin 2.3.21 / AGP 9.1.1 Desktop JVM ./gradlew :shared:jvmStoriesRun -PcmpProfile=1.10
storytale-sample Kotlin 2.3.21 / AGP 9.1.1 Wasm ./gradlew :shared:wasmJsBrowserStoriesDevelopmentRun -PcmpProfile=1.10
Reproduction Steps (storytale-sample)
  1. Publish updated Storytale artifacts locally (from Storytale root):
./gradlew publishToMavenLocal
  1. Clone storytale-sample repo (if not already cloned):
git clone https://github.com/aryapreetam/storytale-sample.git
cd storytale-sample
  1. In shared/build.gradle.kts, apply the local snapshot:
plugins {
  ...
- id("io.github.aryapreetam.storytale") version "0.0.6"
+ id("org.jetbrains.compose.storytale") version "0.0.4-SNAPSHOT"
}
  1. Run Desktop or Wasm stories on modern Kotlin 2.4.20 (Profile 1.12):
# Desktop JVM
./gradlew :shared:jvmStoriesRun -PcmpProfile=1.12

# Wasm Web
./gradlew :shared:wasmJsBrowserStoriesDevelopmentRun -PcmpProfile=1.12
  1. Run Desktop or Wasm stories on Kotlin 2.3.21 (Profile 1.10):
# Desktop JVM
./gradlew :shared:jvmStoriesRun -PcmpProfile=1.10

# Wasm Web
./gradlew :shared:wasmJsBrowserStoriesDevelopmentRun -PcmpProfile=1.10

…runtimes

- Kotlin 2.4 changed the parameter type of ExtensionStorage.registerExtension from ProjectExtensionDescriptor to ExtensionPointDescriptor, causing ClassCastException. Resolve and invoke registerExtension reflectively to support both 2.3.x and 2.4+ runtimes.
- Kotlin 2.4 requires metadata-visible declarations to implement IrAnnotation. Instantiate IrAnnotationImpl reflectively on 2.4+ while falling back to IrConstructorCallImpl on 2.3.x.
- Guard HiddenFromObjC lookup so it only runs on Kotlin/Native targets.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant