You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an @MergeSubcomponent is declared with an inner parent component interface, Anvil fails to generate a proper subclass of the parent component interface with the correct return type during component merging. What a mouthful, so let's look at an example 😅
Context
Consider the following, within the :sample:app module of this repository:
When Anvil generates the subcomponent, it will create the following subclass of ParentBindings where createLoggedInComponent is now overridded to the merged type MergedLoggedInComponent:
MergedAppComponent#createLoggedInComponent cannot be overridden with a return type of LoggedInComponent because MergedLoggedInComponent.ParentComponent already overrides it to explicitly be MergedLoggedInComponent:
MergedAppComponent.kt:44:43Return type of 'createLoggedInComponent'isnot a subtype of the return type of the overridden member 'public abstract fun createLoggedInComponent(): MergedLoggedInComponent defined in com.squareup.anvil.sample.MergedLoggedInComponent.ParentComponent'
A patch is available to reproduce this example: issue.patch
Workaround
If you declare a @MergeComponent.Factory and return it from ParentBindings instead, the issue is no longer present:
What's the purpose of the ParentComponent interface, if there's already a generated @Binds for the concrete subcomponent? It seems like if that didn't exist, everything would compile?
It's hard to say off-hand without digging into the code (this all gets really murky with all the various vague names floating around) but it might be that we already generate it and it's a bit of the left hand not knowing about the right hand in this area of code gen
Summary
When an
@MergeSubcomponent
is declared with an inner parent component interface, Anvil fails to generate a proper subclass of the parent component interface with the correct return type during component merging. What a mouthful, so let's look at an example 😅Context
Consider the following, within the
:sample:app
module of this repository:When Anvil generates the subcomponent, it will create the following subclass of
ParentBindings
wherecreateLoggedInComponent
is now overridded to the merged typeMergedLoggedInComponent
:When Anvil later performs component merging for
AppScope
, it will attempt to generate the following:MergedAppComponent#createLoggedInComponent
cannot be overridden with a return type ofLoggedInComponent
becauseMergedLoggedInComponent.ParentComponent
already overrides it to explicitly beMergedLoggedInComponent
:A patch is available to reproduce this example: issue.patch
Workaround
If you declare a
@MergeComponent.Factory
and return it fromParentBindings
instead, the issue is no longer present:The text was updated successfully, but these errors were encountered: