Skip to content

Commit 171f4aa

Browse files
authored
Merge pull request #1601 from sebastianharko/master
Missing error messages: Parsers.scala: 2064
2 parents 5ebd552 + c44a63e commit 171f4aa

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ object Parsers {
20612061
def templateBody(): (ValDef, List[Tree]) = {
20622062
val r = inDefScopeBraces { templateStatSeq() }
20632063
if (in.token == WITH) {
2064-
syntaxError("early definitions are not supported; use trait parameters instead")
2064+
syntaxError(EarlyDefinitionsNotSupported())
20652065
in.nextToken()
20662066
template(emptyConstructor)
20672067
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,48 @@ object messages {
274274

275275
val explanation = ""
276276
}
277+
278+
case class EarlyDefinitionsNotSupported()(implicit ctx:Context) extends Message(9) {
279+
val kind = "Syntax"
280+
281+
val msg = "early definitions are not supported; use trait parameters instead"
282+
283+
val code1 =
284+
"""|trait Logging {
285+
| val f: File
286+
| f.open()
287+
| onExit(f.close())
288+
| def log(msg: String) = f.write(msg)
289+
|}
290+
|
291+
|class B extends Logging {
292+
| val f = new File("log.data") // triggers a null pointer exception
293+
|}
294+
|
295+
|class C extends {
296+
| val f = new File("log.data") // early definition gets around the null pointer exception
297+
|} with Logging""".stripMargin
298+
299+
val code2 =
300+
"""|trait Logging(f: File) {
301+
| f.open()
302+
| onExit(f.close())
303+
| def log(msg: String) = f.write(msg)
304+
|}
305+
|
306+
|class C extends Logging(new File("log.data"))""".stripMargin
307+
308+
val explanation =
309+
hl"""Earlier versions of Scala did not support trait parameters and "early definitions" (also known as "early initializers")
310+
|were used as an alternative.
311+
|
312+
|Example of old syntax:
313+
|
314+
|$code1
315+
|
316+
|The above code can now be written as:
317+
|
318+
|$code2
319+
|""".stripMargin
320+
}
277321
}

0 commit comments

Comments
 (0)