Skip to content

Commit 598664f

Browse files
committed
Use TypeKey to track scopes
This ensures qualifiers are included in the comparision Fixes #424
1 parent 946a583 commit 598664f

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

integration-tests/common-test/src/test/kotlin/me/tatarka/inject/test/QualifierTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,12 @@ class QualifierTest {
7575
assertThat(component.two).isEqualTo("two")
7676
assertThat(component.three).isEqualTo("three")
7777
}
78+
79+
@Test
80+
fun generates_a_component_that_constructs_different_scoped_values_based_on_a_qualifier_annotation2() {
81+
val component = ScopedNamedComponent2::class.create()
82+
83+
assertThat(component.bar.one).isSameInstanceAs(component.bar.one)
84+
assertThat(component.bar.two).isSameInstanceAs(component.bar.two)
85+
}
7886
}

integration-tests/common/src/main/kotlin/me/tatarka/inject/test/QualifierComponents.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.tatarka.inject.test
22

33
import me.tatarka.inject.annotations.Component
4+
import me.tatarka.inject.annotations.Inject
45
import me.tatarka.inject.annotations.Provides
56
import me.tatarka.inject.annotations.Qualifier
67

@@ -71,3 +72,34 @@ abstract class ScopedNamedComponent {
7172
@Provides get() = "three"
7273
}
7374

75+
@Inject
76+
class ScopedNamedBar(
77+
@Named("two")
78+
val two: IFoo,
79+
80+
@Named("one")
81+
val one: IFoo,
82+
)
83+
84+
@Component
85+
@CustomScope
86+
abstract class ScopedNamedComponent2 {
87+
abstract val bar: ScopedNamedBar
88+
89+
@CustomScope
90+
@Provides
91+
@Named("one")
92+
fun provideFoo1(): IFoo {
93+
return Foo()
94+
}
95+
96+
@CustomScope
97+
@Provides
98+
@Named("two")
99+
fun provideFoo2(
100+
@Named("one")
101+
foo: IFoo,
102+
): IFoo {
103+
return Foo()
104+
}
105+
}

kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/Context.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ data class Context(
1616
val scopeInterface: AstClass?,
1717
val nameAllocator: NameAllocator,
1818
val args: List<Pair<AstType, String>> = emptyList(),
19-
val skipScoped: AstType? = null,
19+
val skipScoped: TypeKey? = null,
2020
val skipProvider: AstType? = null,
2121
) {
22-
fun withoutScoped(scoped: AstType, scopeComponent: AstClass) =
22+
fun withoutScoped(scoped: TypeKey, scopeComponent: AstClass) =
2323
copy(skipScoped = scoped, scopeComponent = scopeComponent)
2424

2525
fun withoutProvider(provider: AstType) = copy(skipProvider = provider)

kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
257257
}
258258

259259
private fun Context.method(key: TypeKey, creator: Member): TypeResult {
260-
return if (creator.scopedComponent != null && skipScoped != creator.method.returnType) {
260+
return if (creator.scopedComponent != null && skipScoped != key) {
261261
Scoped(
262262
context = this,
263263
accessor = creator.accessor,
@@ -379,8 +379,7 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
379379
astClass,
380380
)
381381
}
382-
// constructor type is resolved from a typealias, need to resolve skipScoped too to ensure match
383-
return if (scopedResult != null && skipScoped?.resolvedType() != injectCtor.type) {
382+
return if (scopedResult != null && skipScoped != key) {
384383
val (scopedComponent, types) = scopedResult
385384
Scoped(
386385
context = withTypes(types),
@@ -460,7 +459,7 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
460459
) = TypeResult.Scoped(
461460
key = key,
462461
accessor = accessor,
463-
result = resolve(context.withoutScoped(key.type, scopedComponent), element, key)
462+
result = resolve(context.withoutScoped(key, scopedComponent), element, key)
464463
)
465464

466465
private fun Constructor(

0 commit comments

Comments
 (0)