Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vincenzobaz opened this issue Feb 2, 2022 · 4 comments · May be fixed by #22534
Closed

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

vincenzobaz opened this issue Feb 2, 2022 · 4 comments · May be fixed by #22534
Labels
area:refchecks area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement

Comments

@vincenzobaz
Copy link
Contributor

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.

@vincenzobaz vincenzobaz added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 2, 2022
@bishabosha bishabosha added area:refchecks and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 2, 2022
@bishabosha
Copy link
Member

bishabosha commented Feb 2, 2022

this also fails in the same way in Scala 2, so it might be expected,

i.e. it doesn't matter what the function parameter is called because line 3 defines a:

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

  b
}

@som-snytt
Copy link
Contributor

Since last year, Scala 2 tells you where the reference refers to.

scala/scala#9654

The similar example (from 2012) at

scala/bug#12382 (comment)

object Test {
  def main(args: Array[String]) {
    class NotUsed {val xs = args}
    val dummy = false
    // oops, shadows the parameter
    def args = Seq("a","b","c")
  }
}

In solidarity with OP, I too expected a lint for the shadowing. Either I'm remembering a previous speculative conversation about such a lint, or (note to self) just the last tweak to name binding, where an inherited member must not shadow a definition in outer scope.

A linting addendum would be extra: args shadows parameter of main.

This issue also recalls that people expect to have a syntax to refer to a shadowed parameter, similar to selection from this.

@bishabosha bishabosha reopened this Feb 2, 2022
@bishabosha bishabosha added area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement and removed itype:bug labels Feb 2, 2022
@bishabosha bishabosha changed the title Shadowing function parameter produces forward reference compilation error maybe warn when "forward reference to a local variable" is shadowing parameter. Feb 2, 2022
@bishabosha
Copy link
Member

changed this issue to be about producing warning specifically when there is this forward reference error and the forward reference matches the name of a parameter

@odersky
Copy link
Contributor

odersky commented Feb 21, 2022

Shadowing warnings are very hard to get right in all cases. In this case it seems the forward reference error is indication enough of what goes on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:refchecks area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants