scalac missing closing brace error reports with weird line number
Asked Answered
L

2

6

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?

Libel answered 21/8, 2012 at 7:31 Comment(2)
By the way: the Scala team considers good compiler error messages extremely important. So much so, in fact, that they don't add useful features to the language if they can't figure out how to implement them while keeping good error messages. So, you should definitely raise this issue on the scala-dev mailinglist and maybe file a bug in the Scala issue tracker.Lynnelynnea
@Jörg W Mittag - Thanks for the input. After I posted here I realized the mailing list would probably be a better place for this, but since I'd already posted I figured I'd wait and see if I got a good answer here before double-posting to the mailing list. I love Scala, but I think the transition from Java is very non-trivial. I'm glad to hear the community puts such an emphasis on clear compiler error messages. Thanks again for the suggestion!Libel
L
1

Following Jörg W Mittag's suggestion in his comment on my question, I've re-opened an old ticket in the Scala issue tracker: Confusing Unmatched closing brace errors. I've added the code from this question as a new, short, reproducible example of this class of confusing error messages.

Libel answered 21/10, 2012 at 18:29 Comment(0)
H
5

Consider the following example (I removed indentation on purpose):

case class Foo( i: Int ) {
case class Bar( d: Double ) {
def get = d 
}

It doesn't compile but there are several possible correct codes:

case class Foo( i: Int ) {
}
case class Bar( d: Double ) {
  def get = d 
}

//OR

case class Foo( i: Int ) {
  case class Bar( d: Double ) {
    def get = d 
  }
}

//OR even (still won't compile but the structure is correct so the compiler will proceed with
//another error)

case class Foo( i: Int ) {
  case class Bar( d: Double ) { }
  def get = d 
}

So how should the compiler guess which version is correct ? In this case it picks the first location that could make sense:

hello.scala:3: error: Missing closing brace `}' assumed here
def get = d

Which corresponds to the third option.

Hereditable answered 21/8, 2012 at 9:10 Comment(2)
That makes sense in the example you gave, but not with the code I posted. I've updated my question to make this more clear.Libel
-1 because although your answer is informative, it didn't actually address the weird line number from the code I posted.Libel
L
1

Following Jörg W Mittag's suggestion in his comment on my question, I've re-opened an old ticket in the Scala issue tracker: Confusing Unmatched closing brace errors. I've added the code from this question as a new, short, reproducible example of this class of confusing error messages.

Libel answered 21/10, 2012 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.