Skip to content

maybe warn when "forward reference to a local variable" is shadowing parameter.  #14401

@vincenzobaz

Description

@vincenzobaz

Compiler version

3.1.1

Minimized code

def f(a: Int) =
  val b = a + 1
  val a = 3

  b

@main def run =
  println(f(4))

Output

  dottyforwardbug sbt run
[info] Updated file /home/vincenzo/Desktop/dottyforwardbug/project/build.properties: set sbt.version to 1.5.5
[info] welcome to sbt 1.5.5 (AdoptOpenJDK Java 1.8.0_292)
[info] loading project definition from /home/vincenzo/Desktop/dottyforwardbug/project
[info] loading settings for project dottyforwardbug from build.sbt ...
[info] set current project to dottyforwardbug (in build file:/home/vincenzo/Desktop/dottyforwardbug/)
[info] compiling 1 Scala source to /home/vincenzo/Desktop/dottyforwardbug/target/scala-3.1.1/classes ...
[error] -- [E039] Reference Error: /home/vincenzo/Desktop/dottyforwardbug/src/main/scala/main.scala:2:10 
[error] 2 |  val b = a + 1
[error]   |          ^
[error]   |          a is a forward reference extending over the definition of b
[error] one error found
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 2 s, completed Feb 2, 2022 9:33:55 AM

Expectation

I would expect to receive a 5. Maybe a warning about shadowing.
I do not expect a compilation error.

Notes

I initially thought that this was a problem of top level definitions, so I tried

object Main:
  def f(a: Int) =
    val b = a + 1
    val a = 3

    b

@main def run =
  println(Main.f(4))

obtaining the same error.

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