We recently upgraded to 1.5 which uses IR by defau...
# scripting
e
We recently upgraded to 1.5 which uses IR by default, as a result our scripts code broke. I can not paste the code here, but basically next code worked fine in 1.4.x and is broken in 1.5:
Copy code
//========================== Script 1 =============================
@file:Import("script2.cp.kts")
fun doMain() {
  doSomeDummyWork()
}
//========================== script2.cp.kts =============================
fun doSomeDummyWork(): Int {
  return 42
}
The error message:
Copy code
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
	at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:239)
	at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:235)
...
Caused by: java.lang.RuntimeException: Exception while generating code for:
FUN name:doMain visibility:public modality:FINAL <> ($this:<root>.Script1_cp) returnType:kotlin.Unit
  $this: VALUE_PARAMETER name:<this> type:<root>.Script1_cp
  BLOCK_BODY
    COMPOSITE type=kotlin.Unit origin=null
      CALL 'public final fun doSomeDummyWork (): <http://kotlin.Int|kotlin.Int> declared in <root>.Script2_cp' type=<http://kotlin.Int|kotlin.Int> origin=null
        $this: GET_VAR '<this>: <root>.Utils_cp declared in <root>.Rater_cp.<init>' type=<root>.Utils_cp origin=null
      COMPOSITE type=kotlin.Unit origin=null

	at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
	at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate$default(FunctionCodegen.kt:43)
...
Caused by: java.lang.IllegalStateException: No mapping for symbol: VALUE_PARAMETER SCRIPT_IMPLICIT_RECEIVER name:<this> index:1 type:<root>.Script2_cp
	at org.jetbrains.kotlin.backend.jvm.codegen.IrFrameMap.typeOf(irCodegenUtils.kt:64)
	at org.jetbrains.kotlin.backend.jvm.codegen.ExpressionCodegen.visitGetValue(ExpressionCodegen.kt:648)
@ilya.chernikov can you help here?
@ilya.chernikov I'm aware that we may use
Copy code
useOldBackend
option, but : 1. We do want to use new IR, from what I read it is going to improve things if not already 2. I'm afraid that in next releases, there will be some changes that will be not compatible with previous versions.
@ilya.chernikov Additional point I'd want you to pay attention to is that if we change and use
val
s instead of
fun
s the code will wotk. I mean something like that:
Copy code
//========================== Script 1 =============================
@file:Import("script2.cp.kts")
val doMain: () -> Unit {
  doSomeDummyWork()
}
//========================== script2.cp.kts =============================
fun doSomeDummyWork(): Int {
  return 42
}
Please see that I changed
doMain
to be
val
here. Another point is that it would work if I wrapped
doSomeDummyWork()
in
object
. (it make sense after all, but not really option for us as we can not use this objects for we sometimes use functions and fields defined in our abstract Script template the one with
@KotlinScript
annotation)
The question I really want to ask is it a bug in 1.5? Is it a fix of a bug that was present in 1.4? If is it a bug, is it known one and if it is do you have some estimates for fixing? If it is not a bug, how do you guys break thing like that?
i
This is a bug in IR scripting support - https://youtrack.jetbrains.com/issue/KT-48025 On top of that some other bug in compiler caused the premature switching of the script compilation to the IR backend. So it went unnoticed for some time. The fix is almost finished and hopefully will be a part of the 1.6 release. Before that, please use
useOldBackend
switch.
e
Thanks @ilya.chernikov for quick response ( I really appreciate it) and all the great work you do with kotlin scripting.