Esya Bekasova
10/17/2026, 7:10 PMANNOTATION_ARGUMENTS) from the getTopLevelCallableIds getter in a FirDeclarationGenerationExtension?
I need to extract a class literal target from an annotation argument right during the top-level declaration indexing phase.
However, when running inside the AA, any attempt to call resolvedAnnotationsWithArguments or trigger lazy resolution from within getTopLevelCallableIds breaks the compiler contracts, throwing a FirLazyResolveContractViolationException. Even worse, it occasionally triggers a silent infinite recursion loop where the IDE endlessly retries the analysis (bruh).
Also, what is the idiomatic way to look up annotated class-like symbols at this early stage? Relying on session.predicateBasedProvider.getSymbolsByPredicate inside getTopLevelCallableIds returns an empty collection during clean CLI/Gradle builds, because the predicate index hasn't been populated yet when top-level IDs are being collected.
How should one handle dynamic top-level extension generation based on annotation arguments without falling into these phase/lazy resolve deadlocks?Esya Bekasova
10/17/2026, 7:11 PMorg.jetbrains.kotlin.utils.exceptions.KotlinIllegalArgumentExceptionWithAttachments: Error while resolving org.jetbrains.kotlin.fir.declarations.impl.FirRegularClassImpl
from RAW_FIR to ANNOTATION_ARGUMENTS
current declaration phase CONSTANT_EVALUATION
origin: Source
session: class org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirSourcesSession
module data: class org.jetbrains.kotlin.analysis.low.level.api.fir.projectStructure.LLFirModuleData
KaModule: class org.jetbrains.kotlin.idea.base.fir.projectStructure.modules.source.KaSourceModuleImpl
platform: JVM (25)
at org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve.LLFirModuleLazyDeclarationResolverKt.handleExceptionFromResolve(LLFirModuleLazyDeclarationResolver.kt:228)
and
Caused by: org.jetbrains.kotlin.fir.symbols.FirLazyResolveContractViolationException: `lazyResolveToPhase(ANNOTATION_ARGUMENTS)` cannot be called from a transformer with a phase ANNOTATION_ARGUMENTS.
`lazyResolveToPhase` can be called only from a transformer with a phase which is strictly greater than a requested phase;
i.e., `lazyResolveToPhase(A)` may be only called from a lazy transformer with a phase B, where A < B. This is a contract of lazy resolveEsya Bekasova
10/17/2026, 7:12 PMEsya Bekasova
10/17/2026, 7:14 PMhasPackage. If a package already physically exists and I'm going to add a virtual top-level function to it, it is not needed to return true for its match?Esya Bekasova
10/17/2026, 7:17 PMEsya Bekasova
10/17/2026, 7:20 PMdmitriy.novozhilov
10/18/2026, 5:04 AMdmitriy.novozhilov
10/18/2026, 5:06 AMEsya Bekasova
10/18/2026, 5:40 AMFirDeclarationGenerationExtension, and what other FIR resolving limitations there might be? My goal is:
1. Find classes annotated with @A
2. Extract the argument of the Klass class literal from the @A annotation
3. Extract the names of those properties/functions annotated with @B
4. Generate virtual top-level properties/functions in the Klass class package, using the receiver type of the same Klass, while otherwise completely repeating the signature of its origin (including context parameters, suspend, etc.)
5. (I think I already understand the transformation to IR, so here goes.)
As Dmitriy already told me, I can't resolve @A arguments, so I (as I understand it) must extract the class literal using raw FIR. Also, I already know that declarations annotated with @B cannot have an implicit return type.Esya Bekasova
10/18/2026, 5:45 AMpackage com.example
import compiled.library.SomeClass
@A(SomeClass::class)
class SomeMixin {
@B fun iwannabeext() {}
fun idont() {}
}
->
package compiled.library
fun SomeClass.iwannabeext() {}Esya Bekasova
10/18/2026, 5:52 AMEsya Bekasova
10/18/2026, 6:05 AMpredicateBasedProvider.getSymbolsByPredicate actually found all the annotated class symbols properly... Although it's possible that there was some kind of error in K2, I can't reproduce it now.dmitriy.novozhilov
10/18/2026, 8:39 AMEsya Bekasova
10/18/2026, 8:41 AMEsya Bekasova
10/18/2026, 3:32 PMEsya Bekasova
10/18/2026, 3:37 PMEsya Bekasova
10/18/2026, 6:41 PM@A<T> ). 😄
Since type arguments are calculated in the TYPES phase, I got out of this ANNOTATION_ARGUMENTS phase trap with lazy resolver via getAnnotationByClassId . (but I wouldn't have been able to understand that such a decision was possible without going through what happened today.) Besides, without TYPES, I would still have trouble parsing the signatures of functions located in closed files.
Gist: gist.github.com/diskria/… (If anyone is interested, I cleaned up the code a bit more.)dmitriy.novozhilov
10/19/2026, 4:12 AMEsya Bekasova
10/19/2026, 8:12 PMTYPE_CANT_BE_USED_FOR_CONST_VAL). So, as I see it, class literals are "hostages" of the ANNOTATION_ARGUMENTS phase. I'm just curious, theoretically, if it's possible to move the resolution of KClass arguments (and probably Enum too) to the TYPES phase..? I think so but. Although perhaps that's not the issue, but rather that partial resolution is impossible, right?Esya Bekasova
10/19/2026, 8:22 PMdmitriy.novozhilov
10/20/2026, 3:19 AMEsya Bekasova
10/20/2026, 11:08 AMgetTopLevelCallableIds can indeed be invoked as early as the SUPERTYPES stage, which prevented relying on fully resolved types at that point.
Because of this, I had to fall back to a dummy import-based resolution to determine the target class package, but I made it much more robust this time.
I also got the virtual top-level extension functions generation fully working via copyFirFunctionWithResolvePhase (clearing bodies/contracts, setting up receiver parameters, and attaching fake sources for proper IDE navigation).
Next up: handling the generic type parameters projection/lowering, and then I can finally move on to the IR phase.
Gist: gist.github.com/diskria/…dmitriy.novozhilov
10/20/2026, 11:25 AMEsya Bekasova
10/20/2026, 11:43 AMEsya Bekasova
10/20/2026, 11:43 AMdmitriy.novozhilov
10/20/2026, 11:46 AMEsya Bekasova
10/20/2026, 11:47 AMEsya Bekasova
10/20/2026, 11:48 AMEsya Bekasova
10/20/2026, 11:50 AM