From ef0d2913a628e3ce06bc81942dc236170aa744c1 Mon Sep 17 00:00:00 2001 From: Sebastian Harko Date: Mon, 24 Oct 2016 22:52:14 -0700 Subject: add error message related to missing yield/do in for-comprehensions --- src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- .../tools/dotc/reporting/diagnostic/messages.scala | 35 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala index 17e894144..5d738d5d0 100644 --- a/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/src/dotty/tools/dotc/parsing/Parsers.scala @@ -1326,7 +1326,7 @@ object Parsers { if (in.token == YIELD) { in.nextToken(); ForYield(enums, expr()) } else if (in.token == DO) { in.nextToken(); ForDo(enums, expr()) } else { - if (!wrappedEnums) syntaxErrorOrIncomplete("`yield' or `do' expected") + if (!wrappedEnums) syntaxErrorOrIncomplete(YieldOrDoExpectedInForComprehension()) ForDo(enums, expr()) } } diff --git a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index c4e84aaee..264479467 100644 --- a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -512,4 +512,39 @@ object messages { |}""".stripMargin } + case class YieldOrDoExpectedInForComprehension()(implicit ctx: Context) extends Message(19) { + val kind = "Syntax" + val msg = hl"${"yield"} or ${"do"} expected" + + val code1 = "val numbers = for i <- 1 to 3 yield i" + val code2 = "val numbers = for (i <- 1 to 3) yield i" + val code3 = "for (i <- 1 to 3) println(i)" + val code4 = "for i <- 1 to 3 do println(i) // notice the 'do' keyword" + + val explanation = + hl"""When the enumerators in a for comprehension are not placed in parentheses or braces, a ${"do"} or ${"yield"} statement + |is required after the enumerators section of the comprehension. + | + |You can save some keystrokes by omitting the parentheses and writing + | + |$code1 + | + | instead of + | + |$code2 + | + |but the ${"yield"} keyword is still required. + | + |For comprehensions that simply perform a side effect without yielding anything can also be written without parentheses + |but a ${"do"} keyword has to be included. For example, + | + |$code3 + | + | can be written as + | + |$code4 + | + |""".stripMargin + } + } -- cgit v1.2.3 From 6660729efd3d8d74db0614947d30b3b6c08484c0 Mon Sep 17 00:00:00 2001 From: Sebastian Harko Date: Tue, 25 Oct 2016 07:51:21 -0700 Subject: break lines --- .../tools/dotc/reporting/diagnostic/messages.scala | 25 ++++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 264479467..303ab0437 100644 --- a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -515,34 +515,31 @@ object messages { case class YieldOrDoExpectedInForComprehension()(implicit ctx: Context) extends Message(19) { val kind = "Syntax" val msg = hl"${"yield"} or ${"do"} expected" - - val code1 = "val numbers = for i <- 1 to 3 yield i" - val code2 = "val numbers = for (i <- 1 to 3) yield i" - val code3 = "for (i <- 1 to 3) println(i)" - val code4 = "for i <- 1 to 3 do println(i) // notice the 'do' keyword" - + val explanation = - hl"""When the enumerators in a for comprehension are not placed in parentheses or braces, a ${"do"} or ${"yield"} statement - |is required after the enumerators section of the comprehension. + hl"""When the enumerators in a for comprehension are not placed in parentheses or + |braces, a ${"do"} or ${"yield"} statement is required after the enumerators section + |of the comprehension. | |You can save some keystrokes by omitting the parentheses and writing | - |$code1 + |${"val numbers = for i <- 1 to 3 yield i"} | | instead of | - |$code2 + |${"val numbers = for (i <- 1 to 3) yield i"} | |but the ${"yield"} keyword is still required. | - |For comprehensions that simply perform a side effect without yielding anything can also be written without parentheses - |but a ${"do"} keyword has to be included. For example, + |For comprehensions that simply perform a side effect without yielding anything + |can also be written without parentheses but a ${"do"} keyword has to be included. + |For example, | - |$code3 + |${"for (i <- 1 to 3) println(i)"} | | can be written as | - |$code4 + |${"for i <- 1 to 3 do println(i) // notice the 'do' keyword"} | |""".stripMargin } -- cgit v1.2.3