Skip to content

Commit 22fef84

Browse files
committed
Forward reference error includes line numbers
1 parent a374c1d commit 22fef84

File tree

5 files changed

+80
-35
lines changed

5 files changed

+80
-35
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,10 @@ extends DeclarationMsg(OverrideErrorID), NoDisambiguation:
12141214

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

12191222
def explain(using Context) =
12201223
i"""|${definition.name} is used before you define it, and the definition of ${value.name}

tests/neg/i14401.check

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- [E039] Reference Error: tests/neg/i14401.scala:6:17 -----------------------------------------------------------------
2+
6 | def f: Int = x; // error
3+
| ^
4+
| x is a forward reference extending over the definition of x (on L7)
5+
|
6+
| longer explanation available when compiling with `-explain`
7+
-- [E039] Reference Error: tests/neg/i14401.scala:10:17 ----------------------------------------------------------------
8+
10 | def f: Int = g; // error
9+
| ^
10+
| g (defined on L12) is a forward reference extending over the definition of x (on L11)
11+
|
12+
| longer explanation available when compiling with `-explain`
13+
-- [E039] Reference Error: tests/neg/i14401.scala:15:17 ----------------------------------------------------------------
14+
15 | def f: Int = g; // error
15+
| ^
16+
| g (defined on L17) is a forward reference extending over the definition of x (on L16)
17+
|
18+
| longer explanation available when compiling with `-explain`
19+
-- [E039] Reference Error: tests/neg/i14401.scala:31:15 ----------------------------------------------------------------
20+
31 | } yield a // error
21+
| ^
22+
| ec (defined on L33) is a forward reference extending over the definition of z (on L29)
23+
|
24+
| longer explanation available when compiling with `-explain`
25+
-- [E039] Reference Error: tests/neg/i14401.scala:41:28 ----------------------------------------------------------------
26+
41 | class NotUsed {val xs = args} // error
27+
| ^^^^
28+
| args (defined on L44) is a forward reference extending over the definition of dummy (on L42)
29+
|
30+
| longer explanation available when compiling with `-explain`

tests/neg/i14401.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
object Test {
2+
def f: Int = x;
3+
val x: Int = f;
4+
5+
{
6+
def f: Int = x; // error
7+
val x: Int = f;
8+
}
9+
{
10+
def f: Int = g; // error
11+
val x: Int = f;
12+
def g: Int = x;
13+
}
14+
{
15+
def f: Int = g; // error
16+
var x: Int = f;
17+
def g: Int = x;
18+
}
19+
{
20+
def f: Int = g;
21+
Console.println("foo");
22+
def g: Int = f;
23+
}
24+
{
25+
import scala.concurrent.{ExecutionContext, Future}, ExecutionContext.Implicits
26+
27+
def foo: Future[Int] = {
28+
val fInt = Future.successful(1)
29+
val z = for {
30+
a <- fInt
31+
} yield a // error
32+
33+
implicit val ec: ExecutionContext = Implicits.global
34+
z
35+
}
36+
foo
37+
}
38+
}
39+
object MyApp {
40+
def main(args: Array[String]) = {
41+
class NotUsed {val xs = args} // error
42+
val dummy = false
43+
// oops, shadows the parameter
44+
def args = Seq("a","b","c")
45+
}
46+
}

tests/untried/neg/forward.check

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/untried/neg/forward.scala

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)