Skip to content

Commit 420c2fa

Browse files
committed
fix code generation when using receiver bindings and @AssistedFactory
We'd get foo = [email protected]@InjectAssistedFactoryComponent.provideFoo().bind instead of foo = [email protected]().bind See evant#487 (comment)
1 parent ab29895 commit 420c2fa

File tree

2 files changed

+11
-4
lines changed
  • integration-tests/common/src/main/kotlin/me/tatarka/inject/test
  • kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler

2 files changed

+11
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SomethingProvided()
1919
@Inject
2020
class FactoryAssistedBar(
2121
@Assisted val num: Int,
22-
val foo: Foo,
22+
val foo: IFoo,
2323
@Assisted val name: String,
2424
val provided: SomethingProvided,
2525
)
@@ -37,6 +37,12 @@ abstract class AssistedFactoryComponent {
3737
abstract val barFactory: AssistedBarFactory
3838
abstract val somethingDependant: SomethingDependantOnAssistedFactory
3939

40+
@Provides
41+
fun provideFoo(): Foo = Foo()
42+
43+
val Foo.bind: IFoo
44+
@Provides get() = this
45+
4046
@get:Provides
4147
val name: SomethingProvided = SomethingProvided() // makes sure names don't clash with assisted params
4248
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ data class TypeResultGenerator(val options: Options, val implicitAccessor: Acces
107107
}
108108
add("%L.", accessorInScope)
109109
}
110-
} else if (implicitAccessor.isNotEmpty()) {
111-
if (accessor == accessorInScope) {
110+
} else if (implicitAccessor.isNotEmpty() && accessor == accessorInScope) {
111+
// `receiver` will generate its own `this` expression below
112+
if (receiver == null) {
112113
add("this@%L.", className)
113114
}
114115
}
@@ -422,4 +423,4 @@ private fun TypeName.rawClass(): ClassName {
422423
is ParameterizedTypeName -> rawType
423424
else -> throw IllegalArgumentException("cannot convert $this to ClassName")
424425
}
425-
}
426+
}

0 commit comments

Comments
 (0)