Skip to content

Interactive.pathTo contains Literal(Constant(null)) as a head of path for incomplete program #15294

Closed
@tanishiking

Description

@tanishiking

area:ide

Compiler version

3.1.3-RC2 and later

Minimized code

See: https://github.com/tanishiking/dotty-interactive-playground/blob/50ea5babab3d6d124b98597873af371765b97bf3/src/main/scala/Main.scala#L45-L64

val sourceDefinition = "object Definition { def x    }"
val uriDefinition = new URI("file:///def")
driver.run(uriDefinition, sourceDefinition) // where driver: InteractiveDriver
given ctx: Context = driver.currentCtx

val pos = new SourcePosition(driver.openedFiles(uriDefinition), Spans.Span(sourceDefinition.indexOf("def x") + "def x".length))
val path = Interactive.pathTo(driver.openedTrees(uriDefinition), pos)
path.foreach(println)

Output

with 3.1.3-RC2 or later

Literal(Constant(null))
DefDef(x,List(),...)
Template(...)
TypeDef(...)

with 3.1.2

DefDef(x,List(),...)
Template(...)
TypeDef(...)

Expectation

Scala 3.1.3 should return the same result as Scala 3.1.2. There shouldn't be a Literal(Constant(null)).

Additional context

This is a bit of a problem for Metals, for example, when Metals try to invoke advanced completion based on the path to the cursor position something like def | (| represents the cursor position).
As a workaround Metals ignore Literas(Constant(null)).
https://github.com/scalameta/metals/blob/92d8d9ecdaead1a6db2a9ebb1a85500c22c4714d/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala#L136-L139

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