Skip to content

Commit

Permalink
Forward reference error includes line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Feb 6, 2025
1 parent 0ea05aa commit b666de9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 35 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,10 @@ extends DeclarationMsg(OverrideErrorID), NoDisambiguation:

class ForwardReferenceExtendsOverDefinition(value: Symbol, definition: Symbol)(using Context)
extends ReferenceMsg(ForwardReferenceExtendsOverDefinitionID) {
def msg(using Context) = i"${definition.name} is a forward reference extending over the definition of ${value.name}"
extension (sym: Symbol) def srcLine = sym.line + 1
def msg(using Context) = i"${definition.name}${
if value != definition then s" (defined on L${definition.srcLine})" else ""
} is a forward reference extending over the definition of ${value.name} (on L${value.srcLine})"

def explain(using Context) =
i"""|${definition.name} is used before you define it, and the definition of ${value.name}
Expand Down
24 changes: 24 additions & 0 deletions tests/neg/i14401.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- [E039] Reference Error: tests/neg/i14401.scala:6:17 -----------------------------------------------------------------
6 | def f: Int = x; // error
| ^
| x is a forward reference extending over the definition of x (on L7)
|
| longer explanation available when compiling with `-explain`
-- [E039] Reference Error: tests/neg/i14401.scala:10:17 ----------------------------------------------------------------
10 | def f: Int = g; // error
| ^
| g (defined on L12) is a forward reference extending over the definition of x (on L11)
|
| longer explanation available when compiling with `-explain`
-- [E039] Reference Error: tests/neg/i14401.scala:15:17 ----------------------------------------------------------------
15 | def f: Int = g; // error
| ^
| g (defined on L17) is a forward reference extending over the definition of x (on L16)
|
| longer explanation available when compiling with `-explain`
-- [E039] Reference Error: tests/neg/i14401.scala:28:28 ----------------------------------------------------------------
28 | class NotUsed {val xs = args} // error
| ^^^^
| args (defined on L31) is a forward reference extending over the definition of dummy (on L29)
|
| longer explanation available when compiling with `-explain`
33 changes: 33 additions & 0 deletions tests/neg/i14401.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
object Test {
def f: Int = x;
val x: Int = f;

{
def f: Int = x; // error
val x: Int = f;
}
{
def f: Int = g; // error
val x: Int = f;
def g: Int = x;
}
{
def f: Int = g; // error
var x: Int = f;
def g: Int = x;
}
{
def f: Int = g;
Console.println("foo");
def g: Int = f;
}
}

object MyApp {
def main(args: Array[String]) = {
class NotUsed {val xs = args} // error
val dummy = false
// oops, shadows the parameter
def args = Seq("a","b","c")
}
}
10 changes: 0 additions & 10 deletions tests/untried/neg/forward.check

This file was deleted.

24 changes: 0 additions & 24 deletions tests/untried/neg/forward.scala

This file was deleted.

0 comments on commit b666de9

Please sign in to comment.