Skip to content

ScriptSourceFile doesn't work for toplevel statements #17530

Open
@tgodzik

Description

@tgodzik

Compiler version

3.3.0-RC5

Minimized code

Running:

//> using scala "3.2.2"
//> using dep "org.scala-lang:scala3-compiler_3:3.2.2"
//> using dep "io.get-coursier:interface:1.0.1"
//> using dep "com.lihaoyi::pprint:0.8.1"
//> using option "-Wunused:all"

import coursierapi.Dependency
import coursierapi.Fetch
import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.ScriptSourceFile
import dotty.tools.dotc.util.SourceFile

import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
import scala.jdk.CollectionConverters._

@main
def run =
  val fetch = Fetch.create()

  fetch.addDependencies(
    Dependency.of("org.scala-lang", "scala3-library_3", "3.1.3")
  )
  val extraLibraries: Seq[Path] = fetch
    .fetch()
    .asScala
    .map(_.toPath())
    .toSeq

  val driver = new InteractiveDriver(
    List(
      "-Ykind-projector",
      "-color:never",
      "-classpath",
      extraLibraries.mkString(File.pathSeparator)
    )
  )

  val text = """
      |val hello = "Hello, world!"
      |println(hello)
      |""".stripMargin
  val uri = Paths.get("file://fake.sc").toUri()
  val filePath = Paths.get(uri).toString()
  val virtualFile = SourceFile.virtual(filePath, text)
  val sourceFile =
    if filePath.endsWith(".sc") then
      ScriptSourceFile(virtualFile.file, virtualFile.content)
    else virtualFile
  driver.run(
    uri,
    sourceFile
  )
  pprint.log(driver.openedTrees.head._2.head)

Output

The tree is missing the println statement.

Expectation

Tree contains all toplevel statements

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions