Repository: evant/kotlin-inject (kotlin)

Issue:
Assisted injected args are incorrectly used in other dependencies
Given this:

```kotlin
@Component
abstract class MyComponent {
  abstract val test: (String) -> Test
  @Provides fun provideSomeString() = "string"
}

@Inject
class Test(
  @Assisted assistedString: String,
  private val unrelatedDependency: UnrelatedDependency,
)

@Inject
class UnrelatedDependency(someString: String)
```

The generated code looks like this:

```kotlin
public class InjectMyComponent() : MyComponent() {
  public override val test: Function1<String, Test>
    get() = { arg0 ->
      Test(
        assistedString = arg0,
        unrelatedDependency = UnrelatedDependency(
          someString = arg0 // This shouldn't be the assisted argument!
        )
      )
    }
}
```

This seems to have been introduced on `0.6.0`, but it also happens on `0.6.1-SNAPSHOT`.

Pull request:
Fixes #247

Patch:
diff --git a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/Context.kt b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/Context.kt
index 5e170f7e..182fe68e 100644
--- a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/Context.kt
+++ b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/Context.kt
@@ -26,6 +26,8 @@ data class Context(
 
     fun withArgs(args: List<Pair<AstType, String>>) = copy(args = args)
 
+    fun withoutArgs() = copy(args = emptyList())
+
     fun withTypes(types: TypeCollector.Result) = if (this.types === types) {
         this
     } else {
diff --git a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt
index cbc4b833..9cd5b0d0 100644
--- a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt
+++ b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt
@@ -95,7 +95,7 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
                     assistedFailed = true
                 }
             } else {
-                val result = resolveOrNull(context, element, key)
+                val result = resolveOrNull(context.withoutArgs(), element, key)
                 if (result != null) {
                     paramsWithName[param.name] = result
                 } else if (!param.hasDefault) {
@@ -147,7 +147,7 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
                     continue
                 }
             }
-            val result = resolveOrNull(context, element, key)
+            val result = resolveOrNull(context.withoutArgs(), element, key)
             if (result != null) {
                 paramsWithName[param.name] = result
             } else if (!param.hasDefault) {


Test patch:
diff --git a/integration-tests/common/src/test/kotlin/me/tatarka/inject/test/AssistedTest.kt b/integration-tests/common/src/test/kotlin/me/tatarka/inject/test/AssistedTest.kt
index 9623208f..4bbcb3b1 100644
--- a/integration-tests/common/src/test/kotlin/me/tatarka/inject/test/AssistedTest.kt
+++ b/integration-tests/common/src/test/kotlin/me/tatarka/inject/test/AssistedTest.kt
@@ -8,6 +8,7 @@ import assertk.assertions.prop
 import me.tatarka.inject.annotations.Assisted
 import me.tatarka.inject.annotations.Component
 import me.tatarka.inject.annotations.Inject
+import me.tatarka.inject.annotations.Provides
 import kotlin.test.Test
 
 @Inject
@@ -84,6 +85,23 @@ abstract class DefaultAssistedComponent {
     abstract val withDefault: (String) -> DefaultAssistedBar
 }
 
+@Inject
+class UnrelatedDependency(val someString: String)
+
+@Inject
+class AssistedAndUnrelatedDep(
+    @Assisted val assistedString: String,
+    val unrelatedDependency: UnrelatedDependency,
+)
+
+@Component
+abstract class AssistedWithOtherDependency {
+    abstract val test: (String) -> AssistedAndUnrelatedDep
+
+    @Provides
+    fun string() = "provided"
+}
+
 class AssistedTest {
 
     @Test
@@ -133,4 +151,15 @@ class AssistedTest {
         assertThat(component.withoutDefault("one", 2)).isEqualTo(DefaultAssistedBar("one", 2))
         assertThat(component.withDefault("one")).isEqualTo(DefaultAssistedBar("one", 2))
     }
+
+    @Test
+    fun generates_a_component_that_separates_assisted_and_provided_values() {
+        val component = AssistedWithOtherDependency::class.create()
+
+        assertThat(component.test("assisted")).all {
+            prop(AssistedAndUnrelatedDep::assistedString).isEqualTo("assisted")
+            prop(AssistedAndUnrelatedDep::unrelatedDependency).prop(UnrelatedDependency::someString)
+                .isEqualTo("provided")
+        }
+    }
 }
\ No newline at end of file