Skip to content
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

Forward reference error includes line numbers #22534

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions tests/neg/i14401.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- [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:31:15 ----------------------------------------------------------------
31 | } yield a // error
| ^
| ec (defined on L33) is a forward reference extending over the definition of z (on L29)
|
| longer explanation available when compiling with `-explain`
-- [E039] Reference Error: tests/neg/i14401.scala:41:28 ----------------------------------------------------------------
41 | class NotUsed {val xs = args} // error
| ^^^^
| args (defined on L44) is a forward reference extending over the definition of dummy (on L42)
|
| longer explanation available when compiling with `-explain`
46 changes: 46 additions & 0 deletions tests/neg/i14401.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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;
}
{
import scala.concurrent.{ExecutionContext, Future}, ExecutionContext.Implicits

def foo: Future[Int] = {
val fInt = Future.successful(1)
val z = for {
a <- fInt
} yield a // error

implicit val ec: ExecutionContext = Implicits.global
z
}
foo
}
}
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.

Loading