Hi folks :wave: a bit noobish question: I’m trying...
# compiler
j
Hi folks 👋 a bit noobish question: I’m trying to play around with compiler plugins. I built a simple plugin and ensured that it’s picked up in the other module that I use for testing it. Now I want to do some IR manipulations to learn more. For starters I decided to create
Test
class which contains
hi
method and I annotated it with
@MyTest
. In my plugin I tried to rename this method to
new_hi
. I used kotlin-compile-testing to test it (in compiler plugin module) and I can see that the method name has changed. But when I add
Test().new_hi()
inside my test the test fails with
Unresolved reference: new_hi
. I also did the same thing in other module that is using the compiler plugin and came across the same problem. Moreover when I inspected
build/classes…/Test
I can see that there is still old name for
hi
method. Two questions regarding to this: • How can I make my
new_hi
available in source files that use the plugin (it doesn’t have to have autocompletion and can be red in IDE, but just so it compiles and works)? • What am I missing and what is happening behind the scenes that causes
build/classes…/Test
to remain unchanged and tests to fail? I’m attaching my test code in 🧵
Copy code
@Test
    fun test() {
        val file = SourceFile.kotlin(
            "main.kt", """
            import com.example.MyTest           

            @MyTest    
            class Test {
                fun hi () {
            
                }
            }

            fun main() {
                Test().new_hi() // this fails unless changed to Test().hi()
            }
        """.trimIndent()
        )

        val result = KotlinCompilation().apply {
            sources = listOf(file)
            useIR = true
            compilerPluginRegistrars = listOf(MyTestCompilerPluginRegistrar())
            inheritClassPath = true
        }.compile()

        val javaCode = result.javaCode("Test") // this has new_hi() as I expected
        assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)
    }
javaCode
extension is based on TestUtils from debuglog repository
s
You'll need to change it with frontend extensions (
SyntheticResolveExtension
or
FirDeclarationGenerationExtension
) to make it available for other modules. For IDE support, you'll have to create IDE plugin, I believe, that adds your compiler plugin to Kotlin resolution in IDE.
gratitude thank you 1
j
Thanks, one more thing as I’m still struggling a little bit, do I have to take any more steps? I introduced
SyntheticResolveExtension
in which I overrode
generateSyntheticMethods
to generate new method called
helloWorld
for classes with
@MyTest
. I also created
IrGenerationExtension
which uses
visitFunction
to set body of my newly created function. Again it works fine and in test I can see that this class is available in
javaCode
, but when I try to call it using
Test().helloWorld()
I get
Unresolved reference: helloWorld
again
My current code ⬇️ (sorry for not the greatest quality, but just trying to make it work 😅)