diff options
author | Sebastian Harko <sebastian.harko@lightbend.com> | 2016-10-24 22:52:14 -0700 |
---|---|---|
committer | Sebastian Harko <sebastian.harko@lightbend.com> | 2016-10-24 22:52:14 -0700 |
commit | ef0d2913a628e3ce06bc81942dc236170aa744c1 (patch) | |
tree | 10b98822f12aeb2f7da1d62c76b7f7b5924f0053 | |
parent | 56731e4b18728427f0b7188eb91afc475b36bb18 (diff) | |
download | dotty-ef0d2913a628e3ce06bc81942dc236170aa744c1.tar.gz dotty-ef0d2913a628e3ce06bc81942dc236170aa744c1.tar.bz2 dotty-ef0d2913a628e3ce06bc81942dc236170aa744c1.zip |
add error message related to missing yield/do in for-comprehensions
-rw-r--r-- | src/dotty/tools/dotc/parsing/Parsers.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/reporting/diagnostic/messages.scala | 35 |
2 files changed, 36 insertions, 1 deletions
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 + } + } |