I'm using an IR rewrite pass to replace a property...
# compiler
r
I'm using an IR rewrite pass to replace a property with a new one with the same name, but a new backing field, and accessors that get/set a property of that backing field. This works fine for the most part, but in
init
blocks and class methods, references to the property's accessors seem to be replaced with the backing field. Ex:
Copy code
class Test(@Watch var x: Int){
    fun print(){
        println("Prop: $x")
    }
}
fun main() {
    val t = Test(3)
    
    println(t.x)
    t.print()
}
The
println(t.x)
works fine and prints 3, but the
println
in
print
prints the backing field. I'm using 1.4-M1. Ideas? I've dumped the IR and there doesn't seem to be any differences
IR: https://gist.github.com/rnett/6c90b8d0441341048165f93304bf0212 Note the
print
method:
Copy code
FUN name:print visibility:public modality:FINAL <> ($this:com.rnett.klairvoyant.Test) returnType:kotlin.Unit
      $this: VALUE_PARAMETER name:<this> type:com.rnett.klairvoyant.Test
      BLOCK_BODY
        CALL 'public final fun println (message: kotlin.Any?): kotlin.Unit [inline] declared in <http://kotlin.io|kotlin.io>' type=kotlin.Unit origin=null
          message: STRING_CONCATENATION type=kotlin.String
            CONST String type=kotlin.String value="Prop: "
            CALL 'public final fun <get-x> (): @[Watched] <http://kotlin.Int|kotlin.Int> declared in com.rnett.klairvoyant.Test' type=@[Watched] <http://kotlin.Int|kotlin.Int> origin=null
              $this: GET_VAR '<this>: com.rnett.klairvoyant.Test declared in com.rnett.klairvoyant.Test.print' type=com.rnett.klairvoyant.Test origin=null
And the non-method
println
call:
Copy code
CALL 'public final fun println (message: <http://kotlin.Int|kotlin.Int>): kotlin.Unit [inline] declared in <http://kotlin.io|kotlin.io>' type=kotlin.Unit origin=null
        message: CALL 'public final fun <get-x> (): @[Watched] <http://kotlin.Int|kotlin.Int> declared in com.rnett.klairvoyant.Test' type=@[Watched] <http://kotlin.Int|kotlin.Int> origin=null
          $this: GET_VAR 'val t: com.rnett.klairvoyant.Test [val] declared in com.rnett.klairvoyant.main' type=com.rnett.klairvoyant.Test origin=null
p
May be you should also replace all GetField expressions to get your transformed field?
r
There aren't any, except for in my accessors
I figured it out: the issue was that I was setting the property's backing field to a field of a different type, and apparently future passes assume they are the same. I can just add the field separately, and reference it in the property's accessors.