Repository: square/anvil
Issue: [Code Generator] Compilation Error in generated Factory because of `Type mismatch` in constructor

Hey, we recently started integrating Anvil into one of our android applications.  But when we enabled code generator for a module in the app we started getting the following compilation error

```
e: 'public' function exposes its 'private-in-file' parameter type State
e: Cannot access 'State': it is private in file
e: Type mismatch: inferred type is kotlin.collections.State but com.failing.anvil.SomeContract.State was expected
```

Here is the class for which I am generating factory -
```
package com.sample.anvil.failing

import javax.inject.Inject
import com.sample.anvil.failing.SomeContract.*

class SomeClass @Inject constructor(
    state: State,
)

package com.sample.anvil.failing

interface SomeContract {

    data class State(
        val loading: Boolean,
    )
}
```
The generated factory looks something like this:
```
package com.sample.anvil.failing

import dagger.`internal`.Factory
import javax.inject.Provider
import kotlin.Suppress
import kotlin.collections.State
import kotlin.jvm.JvmStatic

public class SomeClass_Factory(
  private val param0: Provider<State>
) : Factory<SomeClass> {
  public override fun `get`(): SomeClass = newInstance(param0.get())

  public companion object {
    @JvmStatic
    public fun create(param0: Provider<State>): SomeClass_Factory = SomeClass_Factory(param0)

    @JvmStatic
    public fun newInstance(param0: State): SomeClass = SomeClass(param0)
  }
}
```

Please note that this is only happening when we keep the name of the data class in the contract interface as `State`. I know that this could be a very specific use case but we have been following the same nomenclature throughout the project.

Changed files:
compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleGeneratorTest.kt
compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/PsiUtils.kt
compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilMergeAnnotationDetectorCheckTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesMultibindingGeneratorTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilAnnotationDetectorCheckTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/dagger/ComponentDetectorCheckTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesToGeneratorTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/MergeModulesTest.kt
compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/TestUtils.kt
compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesBindingGeneratorTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/dagger/InjectConstructorFactoryGeneratorTest.kt
compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleMultibindingSetTest.kt

diff --git a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/PsiUtils.kt b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/PsiUtils.kt
index 79d5048..5cd10bc 100644
--- a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/PsiUtils.kt
+++ b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/PsiUtils.kt
@@ -319,6 +319,19 @@ public fun PsiElement.requireFqName(
       }
     }
 
+  containingKtFile.importDirectives
+    .asSequence()
+    .filter { it.isAllUnder }
+    .mapNotNull {
+      // This fqName is everything in front of the star, e.g. for "import java.io.*" it
+      // returns "java.io".
+      it.importPath?.fqName
+    }
+    .forEach { importFqName ->
+      module.resolveFqNameOrNull(importFqName, classReference)
+        ?.let { return it }
+    }
+
   // If there is no import, then try to resolve the class with the same package as this file.
   module.resolveFqNameOrNull(containingKtFile.packageFqName, classReference)
     ?.let { return it }
@@ -342,19 +355,6 @@ public fun PsiElement.requireFqName(
   findFqNameInSuperTypes(module, classReference)
     ?.let { return it }
 
-  containingKtFile.importDirectives
-    .asSequence()
-    .filter { it.isAllUnder }
-    .mapNotNull {
-      // This fqName is the everything in front of the star, e.g. for "import java.io.*" it
-      // returns "java.io".
-      it.importPath?.fqName
-    }
-    .forEach { importFqName ->
-      module.resolveFqNameOrNull(importFqName, classReference)
-        ?.let { return it }
-    }
-
   // Check if it's a named import.
   containingKtFile.importDirectives
     .firstOrNull { classReference == it.importPath?.importedName?.asString() }
diff --git a/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/TestUtils.kt b/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/TestUtils.kt
index e78e5bd..0e8d87e 100644
--- a/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/TestUtils.kt
+++ b/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/TestUtils.kt
@@ -81,7 +81,7 @@ public fun compileAnvil(
         )
       )
 
-      this.sources = sources.map { content ->
+      this.sources = sources.mapIndexed { index, content ->
         val packageDir = content.lines()
           .firstOrNull { it.trim().startsWith("package ") }
           ?.substringAfter("package ")
@@ -89,7 +89,8 @@ public fun compileAnvil(
           ?.let { "$it/" }
           ?: ""
 
-        val name = "${this.workingDir.absolutePath}/sources/src/main/java/$packageDir/Source.kt"
+        val name =
+          "${this.workingDir.absolutePath}/sources/src/main/java/$packageDir/Source$index.kt"
         with(File(name).parentFile) {
           check(exists() || mkdirs())
         }
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt
index fb49a72..7fce5b1 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt
@@ -143,7 +143,7 @@ class InterfaceMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (6, 16)")
+      assertThat(messages).contains("Source0.kt: (6, 16)")
     }
   }
 
@@ -197,7 +197,7 @@ class InterfaceMergerTest(
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class. Unfortunately, a different error is reported that the class is
       // missing an @Module annotation.
-      assertThat(messages).contains("Source.kt: (7, 7)")
+      assertThat(messages).contains("Source0.kt: (7, 7)")
     }
   }
 
@@ -225,7 +225,7 @@ class InterfaceMergerTest(
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class. Unfortunately, a different error is reported that the class is
       // missing an @Module annotation.
-      assertThat(messages).contains("Source.kt: (13, 11)")
+      assertThat(messages).contains("Source0.kt: (13, 11)")
       assertThat(messages).contains(
         "com.squareup.test.SecondContributingInterface with scope kotlin.Any wants to replace " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
@@ -313,7 +313,7 @@ class InterfaceMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (18, 11)")
+      assertThat(messages).contains("Source0.kt: (18, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
@@ -444,7 +444,7 @@ class InterfaceMergerTest(
       ) {
         assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
         // Position to the class.
-        assertThat(messages).contains("Source.kt: (7, ")
+        assertThat(messages).contains("Source0.kt: (7, ")
       }
     }
   }
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/MergeModulesTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/MergeModulesTest.kt
index 27da138..3152e37 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/MergeModulesTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/MergeModulesTest.kt
@@ -87,7 +87,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (7, 7)")
+      assertThat(messages).contains("Source0.kt: (7, 7)")
     }
   }
 
@@ -156,7 +156,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (7, 16)")
+      assertThat(messages).contains("Source0.kt: (7, 16)")
     }
   }
 
@@ -279,7 +279,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 16)")
+      assertThat(messages).contains("Source0.kt: (17, 16)")
       assertThat(messages).contains(
         "com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
@@ -315,7 +315,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 16)")
+      assertThat(messages).contains("Source0.kt: (17, 16)")
       assertThat(messages).contains(
         "com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
@@ -411,7 +411,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
           "com.squareup.test.DaggerModule2 with scope kotlin.Unit. The replacement must use " +
@@ -447,7 +447,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
           "com.squareup.test.DaggerModule2 with scope kotlin.Unit. The replacement must use " +
@@ -479,7 +479,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (13, 16)")
+      assertThat(messages).contains("Source0.kt: (13, 16)")
     }
   }
 
@@ -508,7 +508,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (15, 16)")
+      assertThat(messages).contains("Source0.kt: (15, 16)")
       assertThat(messages).contains(
         "com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
           "com.squareup.test.DaggerModule3 with scope kotlin.Unit. The replacement must use " +
@@ -603,7 +603,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (16, 7)")
+      assertThat(messages).contains("Source0.kt: (16, 7)")
       assertThat(messages).contains(
         "com.squareup.test.DaggerModule1 with scope kotlin.Any wants to exclude " +
           "com.squareup.test.DaggerModule2 with scope kotlin.Unit. The exclusion must " +
@@ -688,7 +688,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
@@ -721,7 +721,7 @@ class MergeModulesTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
@@ -785,7 +785,7 @@ class MergeModulesTest {
       ) {
         assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
         // Position to the class.
-        assertThat(messages).contains("Source.kt: (8, ")
+        assertThat(messages).contains("Source0.kt: (8, ")
       }
     }
   }
@@ -881,7 +881,7 @@ class MergeModulesTest {
       """
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
-      assertThat(messages).contains("Source.kt: (19, 11)")
+      assertThat(messages).contains("Source0.kt: (19, 11)")
     }
   }
 }
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt
index 3946244..863226d 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt
@@ -123,7 +123,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (7, 11)")
+      assertThat(messages).contains("Source0.kt: (7, 11)")
     }
   }
 
@@ -214,7 +214,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (7, 16)")
+      assertThat(messages).contains("Source0.kt: (7, 16)")
     }
   }
 
@@ -337,7 +337,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 16)")
+      assertThat(messages).contains("Source0.kt: (17, 16)")
       assertThat(messages).contains(
         "com.squareup.test.DaggerModule1 with scope kotlin.Any wants to replace " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
@@ -373,7 +373,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 16)")
+      assertThat(messages).contains("Source0.kt: (17, 16)")
       assertThat(messages).contains(
         "com.squareup.test.DaggerModule1 with scope kotlin.Any wants to replace " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The replacement " +
@@ -469,7 +469,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
           "com.squareup.test.DaggerModule1 with scope kotlin.Unit. The replacement must use " +
@@ -505,7 +505,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ContributingInterface with scope kotlin.Any wants to replace " +
           "com.squareup.test.DaggerModule1 with scope kotlin.Unit. The replacement must use " +
@@ -537,7 +537,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (13, 16)")
+      assertThat(messages).contains("Source0.kt: (13, 16)")
     }
   }
 
@@ -566,7 +566,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (15, 16)")
+      assertThat(messages).contains("Source0.kt: (15, 16)")
       assertThat(messages).contains(
         "com.squareup.test.DaggerModule2 with scope kotlin.Any wants to replace " +
           "com.squareup.test.DaggerModule1 with scope kotlin.Unit. The replacement must " +
@@ -666,7 +666,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (20, 11)")
+      assertThat(messages).contains("Source0.kt: (20, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
           "com.squareup.test.DaggerModule1 with scope kotlin.Unit. The exclusion must use " +
@@ -816,7 +816,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
@@ -849,7 +849,7 @@ class ModuleMergerTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (17, 11)")
+      assertThat(messages).contains("Source0.kt: (17, 11)")
       assertThat(messages).contains(
         "com.squareup.test.ComponentInterface with scope kotlin.Any wants to exclude " +
           "com.squareup.test.ContributingInterface with scope kotlin.Unit. The exclusion " +
@@ -946,7 +946,7 @@ class ModuleMergerTest(
       ) {
         assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
         // Position to the class.
-        assertThat(messages).contains("Source.kt: (8, ")
+        assertThat(messages).contains("Source0.kt: (8, ")
       }
     }
   }
@@ -1067,7 +1067,7 @@ class ModuleMergerTest(
       """
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
-      assertThat(messages).contains("Source.kt: (19, 11)")
+      assertThat(messages).contains("Source0.kt: (19, 11)")
     }
   }
 
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleGeneratorTest.kt
index 2bbd715..17b9f7a 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleGeneratorTest.kt
@@ -385,7 +385,7 @@ class BindingModuleGeneratorTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
 
-      assertThat(messages).contains("Source.kt: (6, 11)")
+      assertThat(messages).contains("Source0.kt: (6, 11)")
       assertThat(messages).contains(
         "Binding com.squareup.test.ParentInterface contains type parameters(s)" +
           " <Map<String, List<Pair<String, Int>>>, SomeOtherType>"
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleMultibindingSetTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleMultibindingSetTest.kt
index b0d7aed..042ff8a 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleMultibindingSetTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/BindingModuleMultibindingSetTest.kt
@@ -267,7 +267,7 @@ class BindingModuleMultibindingSetTest(
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
 
-      assertThat(messages).contains("Source.kt: (6, 11)")
+      assertThat(messages).contains("Source0.kt: (6, 11)")
       assertThat(messages).contains(
         "Binding com.squareup.test.ParentInterface contains type parameters(s)" +
           " <Map<String, List<Pair<String, Int>>>, SomeOtherType>"
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesBindingGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesBindingGeneratorTest.kt
index 471bbf3..d695fbe 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesBindingGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesBindingGeneratorTest.kt
@@ -147,7 +147,7 @@ class ContributesBindingGeneratorTest {
       ) {
         assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
         // Position to the class.
-        assertThat(messages).contains("Source.kt: (8, ")
+        assertThat(messages).contains("Source0.kt: (8, ")
         assertThat(messages).contains(
           "com.squareup.test.ContributingInterface is binding a type, but the class is not " +
             "public. Only public types are supported."
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesMultibindingGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesMultibindingGeneratorTest.kt
index 36d4ba1..eb9b918 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesMultibindingGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesMultibindingGeneratorTest.kt
@@ -147,7 +147,7 @@ class ContributesMultibindingGeneratorTest {
       ) {
         assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
         // Position to the class.
-        assertThat(messages).contains("Source.kt: (8, ")
+        assertThat(messages).contains("Source0.kt: (8, ")
         assertThat(messages).contains(
           "com.squareup.test.ContributingInterface is binding a type, but the class is not " +
             "public. Only public types are supported."
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesToGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesToGeneratorTest.kt
index f171bac..8948bd5 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesToGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/ContributesToGeneratorTest.kt
@@ -164,7 +164,7 @@ class ContributesToGeneratorTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (6, 16)")
+      assertThat(messages).contains("Source0.kt: (6, 16)")
     }
   }
 
@@ -189,7 +189,7 @@ class ContributesToGeneratorTest {
       ) {
         assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
         // Position to the class.
-        assertThat(messages).contains("Source.kt: (7, ")
+        assertThat(messages).contains("Source0.kt: (7, ")
       }
     }
   }
@@ -214,7 +214,7 @@ class ContributesToGeneratorTest {
       ) {
         assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
         // Position to the class.
-        assertThat(messages).contains("Source.kt: (6, ")
+        assertThat(messages).contains("Source0.kt: (6, ")
       }
     }
   }
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilAnnotationDetectorCheckTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilAnnotationDetectorCheckTest.kt
index 9ba0393..71c67d8 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilAnnotationDetectorCheckTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilAnnotationDetectorCheckTest.kt
@@ -115,7 +115,7 @@ class AnvilAnnotationDetectorCheckTest {
 
   private fun Result.assertError() {
     assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
-    assertThat(messages).contains("Source.kt: (5, 1")
+    assertThat(messages).contains("Source0.kt: (5, 1")
     assertThat(messages).contains(
       "This Gradle module is configured to ONLY generate Dagger factories with the " +
         "`generateDaggerFactoriesOnly` flag. However, this module contains code that uses " +
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilMergeAnnotationDetectorCheckTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilMergeAnnotationDetectorCheckTest.kt
index cf2d05a..81a810b 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilMergeAnnotationDetectorCheckTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/AnvilMergeAnnotationDetectorCheckTest.kt
@@ -103,7 +103,7 @@ class AnvilMergeAnnotationDetectorCheckTest {
 
   private fun Result.assertError() {
     assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
-    assertThat(messages).contains("Source.kt: (5, 1")
+    assertThat(messages).contains("Source0.kt: (5, 1")
     assertThat(messages).contains(
       "This Gradle module is configured to ONLY generate code with the " +
         "`disableComponentMerging` flag. However, this module contains code that uses " +
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ComponentDetectorCheckTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ComponentDetectorCheckTest.kt
index f22e71a..efa2d89 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ComponentDetectorCheckTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ComponentDetectorCheckTest.kt
@@ -23,7 +23,7 @@ class ComponentDetectorCheckTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (5, 1")
+      assertThat(messages).contains("Source0.kt: (5, 1")
       assertThat(messages).contains(
         "Anvil cannot generate the code for Dagger components or subcomponents. In these " +
           "cases the Dagger annotation processor is required. Enabling the Dagger " +
@@ -63,7 +63,7 @@ class ComponentDetectorCheckTest {
     ) {
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
       // Position to the class.
-      assertThat(messages).contains("Source.kt: (6, 3")
+      assertThat(messages).contains("Source0.kt: (6, 3")
       assertThat(messages).contains(
         "Anvil cannot generate the code for Dagger components or subcomponents. In these " +
           "cases the Dagger annotation processor is required. Enabling the Dagger " +
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/InjectConstructorFactoryGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/InjectConstructorFactoryGeneratorTest.kt
index d930b07..9c3f4fe 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/InjectConstructorFactoryGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/InjectConstructorFactoryGeneratorTest.kt
@@ -7,6 +7,7 @@ import com.squareup.anvil.compiler.internal.testing.compileAnvil
 import com.squareup.anvil.compiler.internal.testing.factoryClass
 import com.squareup.anvil.compiler.internal.testing.isStatic
 import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.COMPILATION_ERROR
+import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.OK
 import com.tschuchort.compiletesting.KotlinCompilation.Result
 import dagger.Lazy
 import dagger.internal.Factory
@@ -1994,6 +1995,33 @@ public class InjectClass_Factory<T : List<String>>(
     }
   }
 
+  @Test fun `verify the right state class is imported`() {
+    // That was reported here https://github.com/square/anvil/issues/358
+    compile(
+      """
+      package com.squareup.test
+      
+      interface Contract {
+        data class State(
+          val loading: Boolean = false
+        )
+      }
+      """,
+      """
+      package com.squareup.test
+        
+      import com.squareup.test.Contract.*
+      import javax.inject.Inject
+      
+      class SomeClass @Inject constructor(
+        state: State
+      )  
+      """
+    ) {
+      assertThat(exitCode).isEqualTo(OK)
+    }
+  }
+
   @Suppress("CHANGING_ARGUMENTS_EXECUTION_ORDER_FOR_NAMED_VARARGS")
   private fun compile(
     vararg sources: String,
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt
index bd19a96..fbe107e 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/ProvidesMethodFactoryGeneratorTest.kt
@@ -2523,7 +2523,7 @@ public final class DaggerComponentInterface implements ComponentInterface {
       assumeFalse(useDagger)
 
       assertThat(exitCode).isEqualTo(COMPILATION_ERROR)
-      assertThat(messages).contains("Source.kt: (5, 3)")
+      assertThat(messages).contains("Source0.kt: (5, 3)")
       assertThat(messages).contains(
         "Dagger provider methods must specify the return type explicitly when using Anvil. " +
           "The return type cannot be inferred implicitly."