Skip to content

Missing error messages: Parsers.scala: 2064 #1601

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

Merged
merged 1 commit into from
Oct 16, 2016
Merged
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
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,7 @@ object Parsers {
def templateBody(): (ValDef, List[Tree]) = {
val r = inDefScopeBraces { templateStatSeq() }
if (in.token == WITH) {
syntaxError("early definitions are not supported; use trait parameters instead")
syntaxError(EarlyDefinitionsNotSupported())
in.nextToken()
template(emptyConstructor)
}
Expand Down
44 changes: 44 additions & 0 deletions src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,48 @@ object messages {

val explanation = ""
}

case class EarlyDefinitionsNotSupported()(implicit ctx:Context) extends Message(9) {
val kind = "Syntax"

val msg = "early definitions are not supported; use trait parameters instead"

val code1 =
"""|trait Logging {
| val f: File
| f.open()
| onExit(f.close())
| def log(msg: String) = f.write(msg)
|}
|
|class B extends Logging {
| val f = new File("log.data") // triggers a null pointer exception
|}
|
|class C extends {
| val f = new File("log.data") // early definition gets around the null pointer exception
|} with Logging""".stripMargin

val code2 =
"""|trait Logging(f: File) {
| f.open()
| onExit(f.close())
| def log(msg: String) = f.write(msg)
|}
|
|class C extends Logging(new File("log.data"))""".stripMargin

val explanation =
hl"""Earlier versions of Scala did not support trait parameters and "early definitions" (also known as "early initializers")
|were used as an alternative.
|
|Example of old syntax:
|
|$code1
|
|The above code can now be written as:
|
|$code2
|""".stripMargin
}
}