Unexpected containing declaration while working with typealias of typealias
- Dominant language
- Kotlin
- Stars
- 3.5k
- Forks
- 415
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 53
Description
Hey, I'm working on an Android app and tried running Dagger with alpha support for KSP and received exception during the build. I'm not exactly whether it is KSP specific, or KSP + Dagger mix, however, the stacktrace directed me here.
### Stacktrace of issue
```
e: [ksp] java.lang.IllegalStateException: Unexpected containing declaration for package.class.T, please file a bug at https://github.com/google/ksp/issues/new
at com.google.devtools.ksp.symbol.impl.binary.KSTypeParameterDescriptorImpl$parentDeclaration$2.invoke(KSTypeParameterDescriptorImpl.kt:53)
at com.google.devtools.ksp.symbol.impl.binary.KSTypeParameterDescriptorImpl$parentDeclaration$2.invoke(KSTypeParameterDescriptorImpl.kt:48)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.google.devtools.ksp.symbol.impl.binary.KSTypeParameterDescriptorImpl.getParentDeclaration(KSTypeParameterDescriptorImpl.kt:48)
at com.google.devtools.ksp.symbol.impl.binary.KSDeclarationDescriptorImpl$parent$2.invoke(KSDeclarationDescriptorImpl.kt:57)
at com.google.devtools.ksp.symbol.impl.binary.KSDeclarationDescriptorImpl$parent$2.invoke(KSDeclarationDescriptorImpl.kt:56)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.google.devtools.ksp.symbol.impl.binary.KSDeclarationDescriptorImpl.getParent(KSDeclarationDescriptorImpl.kt:56)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KSTypeExtKt.hasSuppressWildcardsAnnotationInHierarchy(KSTypeExt.kt:203)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KSTypeVarianceResolver.applyJvmWildcardAnnotations(KSTypeVarianceResolver.kt:353)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KSTypeVarianceResolver.applyJvmWildcardAnnotations(KSTypeVarianceResolver.kt:336)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KSTypeVarianceResolver.applyTypeVariance(KSTypeVarianceResolver.kt:72)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspProcessingEnv.resolveWildcards$room_compiler_processing(KspProcessingEnv.kt:320)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspType$xTypeName$2.invoke(KspType.kt:74)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspType$xTypeName$2.invoke(KspType.kt:73)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspType.getXTypeName(KspType.kt:73)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspType.access$getXTypeName(KspType.kt:49)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspType$typeName$2.invoke(KspType.kt:64)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspType$typeName$2.invoke(KspType.kt:63)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspType.getTypeName(KspType.kt:63)
at dagger.internal.codegen.xprocessing.XTypes.toStableString(XTypes.java:458)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.base/java.util.stream.ReferencePipeline.collect(Unknown Source)
at dagger.internal.codegen.xprocessing.XElements.toStableString(XElements.java:340)
at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.getMessageForElement(DaggerSuperficialValidation.java:637)
at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.append(DaggerSuperficialValidation.java:547)
at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.access$200(DaggerSuperficialValidation.java:491)
at dagger.internal.codegen.base.DaggerSuperficialValidation.validateTypeOf(DaggerSuperficialValidation.java:142)
at dagger.internal.codegen.validation.InjectValidator.validateConstructor(InjectValidator.java:171)
at dagger.internal.codegen.validation.InjectValidator.validateUncached(InjectValidator.java:154)
at dagger.internal.codegen.base.Util.reentrantComputeIfAbsent(Util.java:33)
at dagger.internal.codegen.validation.InjectValidator.validate(InjectValidator.java:137)
at dagger.internal.codegen.validation.InjectBindingRegistryImpl.tryRegisterConstructor(InjectBindingRegistryImpl.java:252)
at dagger.internal.codegen.validation.InjectBindingRegistryImpl.tryRegisterInjectConstructor(InjectBindingRegistryImpl.java:241)
at dagger.internal.codegen.processingstep.InjectProcessingStep.process(InjectProcessingStep.java:73)
at dagger.internal.codegen.processingstep.TypeCheckingProcessingStep.lambda$process$0(TypeCheckingProcessingStep.java:82)
at com.google.common.collect.RegularImmutableMap.forEach(RegularImmutableMap.java:196)
at dagger.internal.codegen.processingstep.TypeCheckingProcessingStep.process(TypeCheckingProcessingStep.java:70)
at dagger.internal.codegen.processingstep.TypeCheckingProcessingStep.process(TypeCheckingProcessingStep.java:48)
at dagger.spi.shaded.androidx.room.compiler.processing.XProcessingStep.process(XProcessingStep.kt:59)
at dagger.spi.shaded.androidx.room.compiler.processing.CommonProcessorDelegate.processRound(XBasicAnnotationProcessor.kt:130)
at dagger.spi.shaded.androidx.room.compiler.processing.ksp.KspBasicAnnotationProcessor.process(KspBasicAnnotationProcessor.kt:62)
at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$8$1.invoke(KotlinSymbolProcessingExtension.kt:305)
at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$8$1.invoke(KotlinSymbolProcessingExtension.kt:303)
at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.handleException(KotlinSymbolProcessingExtension.kt:409)
at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.doAnalysis(KotlinSymbolProcessingExtension.kt:303)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:112)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:88)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:256)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:42)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:115)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:247)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:87)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:47)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:168)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:53)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1497)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at java.rmi/sun.rmi.transport.Transport$1.run(Unknown Source)
at java.rmi/sun.rmi.transport.Transport$1.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Unknown Source)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
```
I'm not exactly sure how to reproduce the exact issue (or even to narrow down where it's happening now), but my guess is that it is related to Kotlin type aliases with generic types. I've tried to reproduce this stacktrace on an empty project, however, didn't manage to get the exact error (but still got some errors that might help with the investigation).
### Trying to reproduce the issue
I have an interface with multiple generic types, typealias called `TypeAlias` that's reducing generic types to one and second typealias called `AnotherTypeAlias` specifying that generic type. The following code only fails when injecting `AnotherTypeAlias` (injecting either `TypeAlias` or `MyInterface` works fine). Also reducing generic types to one seems to fix the issue as well.
```
class Test {
@Inject
lateinit var member: AnotherTypeAlias
}
@Component(modules = [com.example.myapplication.Module::class])
interface Component {
fun inject(test: Test)
}
@Module
class Module {
@Provides
fun provideMyInterface(): AnotherTypeAlias {
return object : MyInterface {
override fun foo() {
}
}
}
}
typealias AnotherTypeAlias = TypeAlias
typealias TypeAlias = MyInterface
interface MyInterface {
fun foo()
}
```
I got the following error:
```
> Task :app:kspDebugKotlin FAILED
e: [ksp] InjectProcessingStep was unable to process 'member' because 'error.NonExistentClass' could not be resolved.
Dependency trace:
=> element (CLASS): com.example.myapplication.Test
=> element (FIELD): member
=> type (ERROR field type): error.NonExistentClass
If type 'error.NonExistentClass' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'error.NonExistentClass' is on your classpath.
e: [ksp] ModuleProcessingStep was unable to process 'com.example.myapplication.Module' because 'error.NonExistentClass' could not be resolved.
Dependency trace:
=> element (CLASS): com.example.myapplication.Module
=> element (METHOD): provideMyInterface()
=> type (ERROR return type): error.NonExistentClass
If type 'error.NonExistentClass' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'error.NonExistentClass' is on your classpath.
e: [ksp] ComponentProcessingStep was unable to process 'com.example.myapplication.Component' because 'error.NonExistentClass' could not be resolved.
Dependency trace:
=> element (CLASS): com.example.myapplication.Module
=> element (METHOD): provideMyInterface()
=> type (EXECUTABLE method): ()error.NonExistentClass
=> type (ERROR return type): error.NonExistentClass
If type 'error.NonExistentClass' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'error.NonExistentClass' is on your classpath.
e: [ksp] BindingMethodProcessingStep was unable to process 'provideMyInterface()' because 'error.NonExistentClass' could not be resolved.
Dependency trace:
=> element (CLASS): com.example.myapplication.Module
=> element (METHOD): provideMyInterface()
=> type (ERROR return type): error.NonExistentClass
If type 'error.NonExistentClass' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'error.NonExistentClass' is on your classpath.
e: Error occurred in KSP, check log for detail
```
I'm not sure how can I be more helpful with the issue, but if there is anything else I could provide please let me know.
Dagger: 2.48
ksp: 1.9.0-1.0.12
kotlin-android: 1.9.0
Contributor guide
Research direction
Start with the minimal reproducer using AnotherTypeAlias, TypeAlias, MyInterface, and Dagger injection, then trace the reported KSP entry point at KSTypeParameterDescriptorImpl.kt:53 and the callers in KSTypeExtKt.kt, KSTypeVarianceResolver.kt, and KspType.kt. Done means the nested generic typealias reproducer processes without the unexpected containing declaration or unresolved error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 42/100