scalacenter / scala-debug-adapter

Implementation of the Debug Adapter Protocol for Scala
Apache License 2.0
57 stars 26 forks source link

Evaluation of local lazy val in Scala 3 #236

Open adpi2 opened 2 years ago

adpi2 commented 2 years ago

Problem

It is not possible to evaluate a local lazy val in Scala 3 (probably also in Scala 2) because the local variable is not visible in the class file or stack frame.

Example:

package example

object Main {
  def main(args: Array[String]): Unit = {
    lazy val msg = "Hello, World!"
=>    println(msg)
  }
}
$ msg
Cannot evaluate because of failed compilation:
-- Error: <expression>:1:0 -----------------------------------------------------
1 |msg
  |^^^
  |Evaluation of local lazy val not supported: val msg.

Details

The compiler tree of the main method afther phase genBCode is:

def main(args: String[]): Unit = 
      {
        lazy var msg$lzy1: scala.runtime.LazyRef = new scala.runtime.LazyRef()
        println(this.msg$1(msg$lzy1))
      }

But msg$lzy1 is not visible in the stack frame nor in the class file:

$ javap -l example.Main$
Compiled from "Main.scala"
public final class example.Main$ implements java.io.Serializable {
  public static final example.Main$ MODULE$;

  public static {};
    LineNumberTable:
      line 4: 0

  public void main(java.lang.String[]);
    LineNumberTable:
      line 4: 0
      line 5: 0
      line 6: 8
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      20     0  this   Lexample/Main$;
          0      20     1  args   [Ljava/lang/String;

To fix this we probably need to take action in the compiler itself.

unkarjedy commented 8 months ago

Related issues in IntelliJ Scala Plugin YouTrack:

cc @vasilmkd