Skip to content

Commit 0cd907d

Browse files
authored
Merge pull request #1624 from sebastianharko/master
Add error message for Parsers:1329
2 parents 40da850 + 6660729 commit 0cd907d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ object Parsers {
13271327
if (in.token == YIELD) { in.nextToken(); ForYield(enums, expr()) }
13281328
else if (in.token == DO) { in.nextToken(); ForDo(enums, expr()) }
13291329
else {
1330-
if (!wrappedEnums) syntaxErrorOrIncomplete("`yield' or `do' expected")
1330+
if (!wrappedEnums) syntaxErrorOrIncomplete(YieldOrDoExpectedInForComprehension())
13311331
ForDo(enums, expr())
13321332
}
13331333
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,36 @@ object messages {
512512
|}""".stripMargin
513513
}
514514

515+
case class YieldOrDoExpectedInForComprehension()(implicit ctx: Context) extends Message(19) {
516+
val kind = "Syntax"
517+
val msg = hl"${"yield"} or ${"do"} expected"
518+
519+
val explanation =
520+
hl"""When the enumerators in a for comprehension are not placed in parentheses or
521+
|braces, a ${"do"} or ${"yield"} statement is required after the enumerators section
522+
|of the comprehension.
523+
|
524+
|You can save some keystrokes by omitting the parentheses and writing
525+
|
526+
|${"val numbers = for i <- 1 to 3 yield i"}
527+
|
528+
| instead of
529+
|
530+
|${"val numbers = for (i <- 1 to 3) yield i"}
531+
|
532+
|but the ${"yield"} keyword is still required.
533+
|
534+
|For comprehensions that simply perform a side effect without yielding anything
535+
|can also be written without parentheses but a ${"do"} keyword has to be included.
536+
|For example,
537+
|
538+
|${"for (i <- 1 to 3) println(i)"}
539+
|
540+
| can be written as
541+
|
542+
|${"for i <- 1 to 3 do println(i) // notice the 'do' keyword"}
543+
|
544+
|""".stripMargin
545+
}
546+
515547
}

0 commit comments

Comments
 (0)