Skip to content

Add error message for Parsers:1329 #1624

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 2 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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())
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you break this line so that it is max 80 chars long?

|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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with this guy :)

|but a ${"do"} keyword has to be included. For example,
|
|$code3
|
| can be written as
|
|$code4
|
|""".stripMargin
}

}