As I've been learning Scala I'm often reminded of g++
compiler errors when reading the cryptic output from scalac
. However, today I came across something I doubt would happen even in the g++
universe.
A friend sent me a very simple code snippet with a fairly commonplace error:
case class Var(name: String) extends ArithExpr {
override def eval(env: Env) = env.lookup(name) match {
case Some(d) => d
case None => throw new IllegalArgumentException("Env " + env + " does not contain a binding for " + name)
}
override def exprString = name
// } // <-- MISSING THIS BRACE
The full source file is posted here. Since the case class Var
class declaration is missing its closing brace you would think the compiler would tell you that the opening brace for that declaration (on line 11) is missing its closing brace. However, scalac
reports that it "assumes" a missing closing brace in the middle of a previous case class
declaration (on line 7). (The full error output is included at the bottom of the posted code.)
Most scalac
error messages make sense if you understand the internals of the language, but I'm at a total loss here. How does a missing closing brace in a later class declaration end up propagating to an already successfully parsed class definition earlier in the file?
How on earth do you go about explaining this to a Scala beginner? Leaving off a closing brace is exactly the kind of mistake someone new to Scala might commonly do, but the error message here seems like it would lead the user so far astray that it would probably be more helpful to just report something like error: you seem to be missing a '}' somewhere
instead.
Note: I know the usual answer to questions like this is just "use an IDE and the incremental compilation will flag it right away" or "syntax highlighting should make this error obvious"—but my question is asking specifically about the scalac
output, so please keep in mind that I know those are valid points but I really just want to understand what is going on with the compiler here.
Update:
Let me try a different approach to explaining my confusion. The error stems from a missing closing brace, so it's obviously a problem with brace nesting. Transforming the text (code) in the snippet I posted to a series of line-number+brace pairs we get this:
1{ 4}
6{ 9}
11{ 12{ 15}
19{ 22}
24{ 26}
28{ 33}
We obviously have a missing closing brace. I could understand scalac
guessing the missing brace could go in any one of these places (each represented by an x
):
1{ 4}
6{ 9}
11{ x 12{ x 15} x <-- HERE OR HERE OR HERE
19{ x 22} x <-- OR HERE OR HERE
24{ x 26} x <-- OR HERE OR HERE
28{ x 33} x <-- OR HERE OR HERE
However, this is what the output from scalac
says:
+----- I THINK YOU ARE MISSING A
| CLOSING BRACE RIGHT HERE!
1{ V 4}
6{ x 9}
11{ 12{ 15}
19{ 22}
24{ 26}
28{ 33}
That part of the input is already well-nested! How could adding another closing brace there possibly make any sense?
Edit: I feel like I should reiterate my main question again: How would you go about explaining this error message (and how to find the root of the error in the source) to a newcomer to Scala?