Did I understand correctly that there's no way to ...
# compiler
e
Did I understand correctly that there's no way to get the
resolvedReturnType
of a
FirVariableSymbol
in
FirDeclarationGenerationExtension
? I successfully used
FirAdditionalCheckersExtension
to run my parser and validator, but today I started adding
FirDeclarationGenerationExtension
and thought it would be a great idea to make
FirDeclarationGenerationExtension
the entry point in logic (KSP-style pipeline)... It seems that wasn't the best idea. Now I've started reworking the parsing for two-pass. Btw, later I even found this: github.com/google/kotlin/blob/…/FirLombokBuilderChecker.kt#… I couldn't find an explanation in the compiler code (2.4.0), and I also couldn't find a way to log the current phase (I tried to find
FirResolvedPhase.currentPhase()
, similar to
Thread
)
In my case, a developer can write this:
Copy code
@KMixin(Creeper::class, Side.Common)
abstract class CreeperMixin(@Origin val creeper: Creeper) {
    @Extension fun boom(): String = ":p"
}
And I will generate this (virtual):
Copy code
fun Creeper.boom(): String =
    (this as CreeperMixin_Duck).`example_mod$boom`() // calls KSP-generated Java boilerplate
Also detailed schema:
Copy code
[         @KMixin         ]
              │
              ├─────── (FIR+IR Plugin) ──────► Replaces extension calls with zero-cost duck casts
              │
              └────────── (KSP) ─────────────► Generates Java Duck Interface
                                                     │
                                                     ▼
                                           [ Generated Java Mixin ] ◄── (Processed at runtime by SpongePowered Mixin engine)
                                                     │
                                                     ▼
                                           [ Kotlin Impl Bridge ]
(this is Minecraft modding tooling)
So I'm planning to do it this way:
FirDeclarationGenerationExtension
will only parse
@Extension
as much as its phase allows.
FirAdditionalCheckersExtension
will parse the entire thing (because the
BODY_RESOLVE
phase is enough for me).
I'm building a multi-frontend - KSP with K2 companion - and using a single abstract core with all the logic, which is why I tried to invent a single-pass... but it looks like I'll have to accept that phases aren't that simple 🙂
y
Some explanation of phases is available here but in short yes, AFAIK you're not guaranteed to have
resolvedReturnType
in
FirDeclarationGenerationExtension
. I'm not sure what you were doing before in
FirAdditionalCheckersExtension
to run your parser, but you're not reallyyyy supposed to do things other than checking in there; you're definitely not supposed to modify FIR in there either (i.e. there's no guarantee it'll continue working); it's really fun to break these rules, though! Anyway, I think you could use
.fir.returnTypeRef
instead, or
resolvedReturnType
and catch the error maybe? Don't quote me on this. Actually wait, what exactly were you doing with
FirAdditionalCheckersExtension
? Are you just trying to access the fully resolved code to then produce a file or similar from it? If so, just use an IR plugin
e
@Youssef Shoaib [MOD] No, no, I definitely wasn't trying to mutate FIR inside the checker! The idea behind calling the parser in
FirAdditionalCheckersExtension
comes from my multi-frontend architecture: My core validation logic is fully abstracted to work identically across both KSP and K2. To reuse that shared validator, I need to parse declarations into my domain model at the same level of fidelity that KSP provides (fully resolved types, annotation arguments, etc.). So in the checker, I simply: 1. Run my FIR parser on the class symbol if it has KMixin annotation. 2. Feed the parsed model into
coreValidator
. 3. Receive domain models + diagnostic reports. 4. Iterate over those reports and emit them via
reporter.reportOn(...)
. Overall, my plugin has 3 main goals: 1. Highlight errors as the user types (handled nicely by
FirAdditionalCheckersExtension
). 2. Provide generated
@Extension
signatures in FIR so IDE autocompletion and calls work smoothly, then lower them to zero-cost duck casts in IR. 3. Attach parsed domain data to symbol attributes (
FirDeclarationDataKey
) so an upcoming IDEA plugin can read and make things. Goal #1 was working smoothly, but yesterday I started working on Goal #2 (
FirDeclarationGenerationExtension
) and thought: "Hey, why not move the whole parsing logic there and make it the single entry point?" That's when I hit the phase wall. So now I've reverted the checker back to how it was. I'll handle declaration generation as a separate, lightweight pass, and the main parser/validator will simply report a diagnostic error if someone tries to use an implicit return type on
@Extension
!
❤️ 1
It's not entirely up-to-date, but it captures the overall idea if you're curious
y
Thanks for the thorough explanation! That sounds pretty cool! Yeah, I think the best approach is to split generation and checking so it naturally follows the phases Good luck with your project! Don't hesitate to ask any further questions here!
❤️ 1