https://kotlinlang.org logo
Join Slack
Powered by
# compiler
  • j

    Javier

    11/18/2025, 3:20 PM
    I have cloned the Kotlin Compiler Plugin template and after adding a new test I am getting the error next error:
    Copy code
    Module Module <CommonOne> doesn't contain package <http://com.foo.dev.app|com.foo.dev.app>
    It is throwed inside this function:
    Copy code
    override fun getPackage(fqName: FqName): PackageViewDescriptor {
        val symbolProvider = session.symbolProvider
        if (symbolProvider.hasPackage(fqName)) {
            return FirPackageViewDescriptor(fqName, this)
        }
        error("Module $moduleData doesn't contain package $fqName")
    }
    The test file is
    Copy code
    // MODULE: CommonOne
    // file: CommonOneFooDao.kt
    
    package com.foo.common.one
    
    import androidx.room.Dao
    
    @Dao
    interface CommonOneFooDao {
    
        fun foo()
    }
    
    // MODULE: FeatureOne(CommonOne)
    // file: FeatureOneFooDao.kt
    
    package com.foo.feature.one
    
    import androidx.room.Dao
    
    @Dao
    interface FeatureOneFooDao {
    
        fun foo()
    }
    
    
    // MODULE: DevApp(FeatureOne, CommonOne)
    // file: main.kt
    package com.foo.dev.app.feature.one
    
    fun main() {
    
    }
    b
    • 2
    • 10
  • c

    Caleb B

    11/18/2025, 8:16 PM
    Can compiler plugins stop a vanilla Kotlin compiler error/diagnostic from appearing? I'm working on a composite-annotations plugin (like Spring), and to do it I need to be able to apply any annotation to an annotation class, not just ones with
    ANNOTATION_CLASS
    as an allowed target.
    j
    b
    • 3
    • 5
  • m

    martmists

    11/18/2025, 11:36 PM
    How can I see annotations through type aliases? e.g. I have
    typealias CMyType = @CustomAnnotation(...) MyType
    but inspecting the IrType shows no annotation and printing the fq name just shows MyType. using the expanded form instead of the type alias works fine. I checked in parent declarations and dumping them always shows the original type without annotations.
    🤔 1
  • f

    Fergus Hewson

    11/19/2025, 7:36 AM
    I have a
    FirDeclarationGenerationExtension
    that I am using to generate a nested class with the same params as a data class plus custom setters, flows for each property and and all flow. Is the correct process to generate callables for each method/property in
    getCallableNamesForClass
    and the generate properties and functions in
    generateProperties
    and
    generateFunctions?
    To do this I have to parse the constructor for the supertype twice or cache callable information in the extensions and to generate the method signatures I need? Am I missing something about how this extension should work? If anyone has similar work. I could read I would really appreciate it too!
    b
    • 2
    • 2
  • j

    Javier

    11/19/2025, 12:23 PM
    I am using
    buildArrayliteral {}
    to populate an array of an annotation I am trying to build this:
    Copy code
    @Database(
        entities = [
            OneEntity::class,
            TwoEntity:class,
        ]
    )
    abstract class FooDatabase : RoomDatabase()
    Everything is created, but building the arrow with the function above fails due `Expected expression
    FirArrayLiteralImpl
    to be resolved`. But I do not find a way to resolve this expression.
    d
    • 2
    • 9
  • c

    Caleb B

    11/19/2025, 5:17 PM
    Given a reference to a property (either as an
    FirPropertySymbol
    or
    FirNamedReference
    ), how do you get the class that "owns" said property? (The receiver if it's an extension, or the actual owning class if it's a standard property.) I have
    R|<local>/inst|.R|/MyClass.bar|
    , I just can't figure out how to go one level up to
    R|<local>/inst|.R|/MyClass|
    .
    ✅ 1
    d
    y
    • 3
    • 3
  • c

    Caleb B

    11/19/2025, 7:41 PM
    When constructing new FIR, how would you emit
    this@R|/MyClass|.R|/MyClass.bar| = R|<local>/value|
    ? I'm assuming it's through
    buildVariableAssignment
    , but I can't figure out what builders to wire into the LValue and RValue.
    j
    • 2
    • 10
  • o

    Oliver.O

    11/20/2025, 11:34 AM
    It looks like IC is working differently, depending on the KGP variant: KT-82560 shows a case where it works with
    kotlin("multiplatform")
    , but not
    kotlin("jvm")
    . Is this a known situation and will it change?
    t
    d
    s
    • 4
    • 65
  • t

    tapchicoma

    11/20/2025, 3:23 PM
    Hey compiler plugin developers. I am curious to gauge the following idea with the following rough outline - We are thinking about introducing compiler plugin devkit in a form of separate Gradle plugin. The core idea is that it will create a set of the following source sets: • API - common api for the compiler plugin (for example compiler plugin options) • implementations for concrete Kotlin compiler version. The supported versions are defined in the DSL and will be provided as
    compileOnly
    dependencies by devkit plugin • optional bootstrap for Gradle and Maven plugins The project will publish a set of publications for API + implementation for every supported Kotlin compiler version with additional (Gradle or some way) attribute. Based on this attribute Kotlin build plugins could select proper publication for used in the project Kotlin compiler version. What do you think about it?
    🔝 3
    o
    y
    +6
    • 9
    • 42
  • j

    Javier

    11/20/2025, 4:18 PM
    I am applying the plugin via Gradle and referencing the generated class in the code is working correctly via terminal, but in the IDE it is not found, so red. I have marked as false the registry property
    kotlin.k2.only.bundled.compiler.plugins.enabled
    . Am I missing something?
    h
    • 2
    • 6
  • m

    mikehearn

    11/20/2025, 4:56 PM
    Quick optimization question - is kotlinc capable of moving the initialization of Regex objects into static fields automatically? GPT-5 loves to inline Regex("..") into the place where it's used, which could be quite inefficient and I'm not sure HotSpot will fix that
  • c

    Caleb B

    11/20/2025, 5:36 PM
    In FIR, how do you get all callables for a given class, including extension methods and inherited members?
    FirClassSymbol<*>#declaredX
    only gives class members. This is specifically for diagnostics, so it should be resolved at that point, right?
    y
    d
    • 3
    • 3
  • p

    Pablichjenkov

    11/20/2025, 10:41 PM
    https://kotlinlang.slack.com/archives/C2R77UD35/p1763678424304139
    solved 1
    ✅ 1
    • 1
    • 1
  • m

    martmists

    11/23/2025, 1:00 PM
    Copy code
    @Target(AnnotationTarget.TYPE)
    @Retention(AnnotationRetention.SOURCE)
    annotation class CodecLocation(val klass: KClass<*>, val field: String)  // Ideally I could do CodecLocation(val prop: KProperty<*>) to specify as @CodecLocation(Codecs::B_CODEC) but that doesn't seem to work
    
    @Codec
    data class A(
        val b: @CodecLocation(Codecs::class, "B_CODEC") B
    )
    
    typealias CodecB = @CodecLocation(Codecs::class, "B_CODEC") B
    
    @Codec
    data class A2(
        val b: CodecB
    )
    In A I can grab
    param.type.annotations[0]
    to get the CodecLocation annotation, but in A2 it seems to just resolve as
    B
    with no annotations present. How can I still get the annotation in A2?
  • f

    Fergus Hewson

    11/24/2025, 3:38 AM
    During IR phase, when is best time to create backing fields for properties? I have StateFlows which are properties and I want to generate backing MutableStateFlows in the constructor using args from that constructor for the initial values. Then I need to wire the properties up to their associated backing fields, I am getting concurrent modification exceptions for adding fields during the visit class call. Is there a correct way of wiring this up?
  • d

    Devanshu Pathsariya

    11/24/2025, 7:48 AM
    Hi, I am creating compile-time di framework for kmp, Now i want to produce a class after the compilation of a shared module(is concretely used in a native module) which can access the classes,function defined even in the transitive module just like hilt does for jvm?
    e
    • 2
    • 1
  • f

    Fergus Hewson

    11/24/2025, 5:53 PM
    How do you make generated FIR visible in the IDE? I can see the seralizer() method from classes annotated with @Serializable, but not my generated nested classes. Is kotlinx.serialization public? Or is there an example repo I can look at to figure out how this works?
    m
    • 2
    • 3
  • e

    Empa

    11/25/2025, 2:14 PM
    Hi, is it possible for a compiler plugin to modify the generated sources? We want to automatically add
    @Deprecated
    annotations to declarations annotated with our custom annotation, but currently only our annotation appears in the sources, and the
    @Deprecated
    one does not. Am I supposed to use something else instead of a compiler plugin for this?
    c
    • 2
    • 1
  • i

    Isaac Udy

    11/29/2025, 3:18 AM
    I'm hitting a dead-end trying to build out (what I thought would be simple) code generation in a compiler plugin. Essentially, I want to call a function with a
    @Composable () -> Unit
    lambda parameter: like this:
    Copy code
    fun myGeneratedFunction() {
        someOtherExternalFunction( parameter = { MyExternalComposableFunction() } )
    }
    But I can't figure out how to do this correctly. Attempting the generation in the IR phase leaves me with compiler errors related to unhandled intrinsics, and attempting the generation in the FIR phase is giving me grief about constructing the lambda/anonymous function call - it compiles fine, but then crashes at runtime because the anonymous function apparently isn't making it into the byte code. Would love some help/tips if anyone has experience generating code that calls Composables in either IR or FIR in a compiler plugin. Thanks!
    f
    c
    • 3
    • 8
  • c

    Caleb B

    11/29/2025, 8:14 PM
    In FIR, how do you search all items accessible in your current scope? I'm specifically looking for extension properties that have been imported.
    d
    • 2
    • 2
  • c

    Caleb B

    11/29/2025, 9:23 PM
    In custom FIR diagnostics, can you also make suggestions for fixing the issue? Either a fully-custom suggestion, or just linking to a stock suggestion type?
  • z

    Zac Sweers

    11/30/2025, 7:01 AM
    11th hour but I started testing the new
    containingFileName
    APIs introduced to FIR in 2.3.0, and finding that it doesn't actually seem to be compatible with KGP's IC implementation 😞. Something seems to get lost in translation between how kotlinc understands this file path (which is relative/based on the package) and how KGP does (it expects an absolute path?) KT-82809 [FIR][IC] New containingFileName parameter API is not actually compatible with IC
    o
    i
    d
    • 4
    • 7
  • i

    Isaac Udy

    12/01/2025, 7:54 AM
    I'm tearing my hair out trying to create a simple property with a class reference in FIR generation. I want to create FIR that's the same as this source:
    Copy code
    val myProperty = MyClass::class
    but I can't figure out how I'm supposed to do this between
    buildGetClassCall
    and
    buildClassReferenceExpression
    . Does anyone have any tips?
    d
    • 2
    • 2
  • o

    Olaf Gottschalk

    12/01/2025, 9:48 AM
    I have a huge problem with K2 and overload resolution of a function taking a lambda type. I have two possibilities and need two distinct implementations for one function (the name must be identical, it's a DSL function):
    Copy code
    fun <O : Any> test(doIt: () -> O) {
        TODO()
    }
    
    fun <O> test(doIt: () -> O?) {
        TODO()
    }
    
    fun demo() {
        test { "" }
        test { null }
    }
    test must exist in two flavors: • one where the lambda block can return anything that is nullable • another that must be chosen if the return value cannot be null. With my declaration as above, I think the compiler can find a matching method for the String and also for the null value: the first must resolve to the nun-nullable version, the second should (it's the only match!) resolve to the second implementation. But the reality is different, sadly - the one returning null is getting a compiler error:
    Cannot infer type for type parameter 'O'. Specify it explicitly.
    I tried every variation I could think of, nothing works. Also tried
    @OverloadResolutionByLambdaReturnType
    but that also did not change anything. Is there any trick I need to pull here to teach the compiler to take the second function for nullable lambda return types?
    b
    • 2
    • 12
  • j

    Jakob Heher

    12/01/2025, 1:35 PM
    is there any way to tell the compiler not to allow "implicit Unit" to be returned from a lambda block?
  • j

    Jakob Heher

    12/01/2025, 1:36 PM
    for example:
    Copy code
    fun fnTakingUnitLambda(fn: ()->Unit) = ...
    
    // this is legal, and i want it not to be:
    fnTakingUnitLambda { 42 }
    
    // this should still be legal:
    fnTakingUnitLambda { Unit }
    
    // this should also work:
    fnTakingUnitLambda { someOtherFnThatReturnsUnit() }
    y
    • 2
    • 17
  • c

    Caleb B

    12/02/2025, 2:00 AM
    For some reason, I'm getting `IllegalStateException`s for expected FIR errors in my diagnostics tests. Full test kt: https://paste.ofcode.org/yFaZvQjQFy7JMnVKRHULnp Test class:
    Copy code
    class MyClass : MySuperClass() {
    	private fun <!SETTER_DECL_TARGET_PROPERTY_NOT_VISIBLE!>`set-someProperty`<!>(value: Int) {
    		someProperty = value.toString()
    	}
    }
    Log:
    Copy code
    java.lang.IllegalStateException: SETTER_DECL_TARGET_PROPERTY_NOT_VISIBLE: Setter target ''var someProperty: String' defined in 'foo.MySuperClass'' is not visible in this scope. at MyClass.kt:(212,230)
    at org.jetbrains.kotlin.test.backend.handlers.NoFirCompilationErrorsHandler.processModule(NoFirCompilationErrorsHandler.kt:54)
    Does anyone know what's going on? I'm so close to getting this to work.
    d
    • 2
    • 2
  • c

    Caleb B

    12/02/2025, 8:35 PM
    Thank you all so much for all your help over the past few weeks! With your assistance, I've just finished work on my first compiler plugin: Overloadable Setters in Kotlin! It wouldn't have been possible without you guys. The general idea is to let you define ways to set a property with multiple types of input, making builders a lot cleaner:
    Copy code
    buildThing {
      // ThingBuilder#defaultValue: () -> String
      defaultValue = "literal string"
      defaultValue = 0
      defaultValue = { someExpensiveComputation() }
    }
    You can check it out here: https://github.com/cbrandt77/Kotlin-OverloadableSetters I'm still working on publishing it and getting it on the plugin portal, but it's fully kitted out with diagnostics and should work as-is. I'll be implementing more language features in the future, so keep an eye out! I've got a list that's like two pages long, lol
    👀 2
  • r

    Rey (Kingg22)

    12/04/2025, 12:19 PM
    Hiii, I'm a complete noob about "compiler plugins". I'd like to know if my annotation for searching within the compiler is used in a suspend function or an inline function. Will my plugin find original data from the source code or will it all already be inline and transformed? Are there any utilities for working with suspend or inline within the compiler plugin?
  • r

    Rey (Kingg22)

    12/04/2025, 12:33 PM
    can I debug a compiler plugin?