Support definition annotations on objects via INSTANCE - #78
Open
exoego wants to merge 1 commit into
Open
Conversation
…NSTANCE A definition annotation on a Kotlin object (@single, @factory, @Scoped, @KoinViewModel, @KoinWorker) made the plugin emit a constructor call for the object. An object has no public constructor, so this produced invalid IR. With compiler assertions on it failed lowering with an AssertionError. With assertions off it compiled and then crashed at runtime with NoSuchMethodError, while Gradle still reported BUILD SUCCESSFUL. The definition body now references the singleton INSTANCE with irGetObject when the target is an object. This mirrors the existing object handling in ModuleFunctionResolver.buildModuleGetCall. Both the annotation path (DefinitionCallBuilder.createDefinitionLambda, used by the root and scoped builders) and the DSL path (KoinDSLTransformer.handleTypeParameterCall for single<T>() and friends) are covered. Interface binding on an object still emits the .bind() chain around the INSTANCE reference. Tests - box/annotations/object_binding.kt mirrors the real use case from the issue. An object is bound to a framework interface with @single([Loader::class]), extends a shared abstract base, and is collected through the interface with getAll. It fails on the unfixed plugin with an IR validation error and passes after the fix. - box/annotations/object_singleton.kt is the minimal @single object case. - box/dsl/single_object.kt covers the same case for single<T>(). - playground-apps/app-kmp-klib adds an interface-bound @single object so the change is compiled on the KLIB targets iosArm64 and wasmJs. Docs updated to note that objects are supported and resolve to INSTANCE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #77
A definition annotation (e.g.
@Single) on a Kotlin object made the plugin emit a constructor call for the object. An object has no public constructor, so this produced invalid IR. With compiler assertions on, it failed lowering with an AssertionError. With assertions off, it compiled and then crashed at runtime with NoSuchMethodError, while Gradle still reported BUILD SUCCESSFUL.The definition body now references the singleton INSTANCE with irGetObject when the target is an object. This mirrors the existing object handling in
ModuleFunctionResolver.buildModuleGetCall. Both the annotation path (DefinitionCallBuilder.createDefinitionLambda, used by the root and scoped builders) and the DSL path (KoinDSLTransformer.handleTypeParameterCall for single() and friends) are covered. Interface binding on an object still emits the .bind() chain around the INSTANCE reference.Tests
@Single([Loader::class]), extends a shared abstract base, and is collected through the interface with getAll. It fails on the unfixed plugin with an IR validation error and passes after the fix.@Singleobject case.@Singleobject so the change is compiled on the KLIB targets iosArm64 and wasmJs.Docs updated to note that objects are supported and resolve to INSTANCE.