Skip to content

Add unbound placeholder parameter message. #1618

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
Show file tree
Hide file tree
Changes from all commits
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 @@ -334,7 +334,7 @@ object Parsers {
try op
finally {
placeholderParams match {
case vd :: _ => syntaxError("unbound placeholder parameter", vd.pos)
case vd :: _ => syntaxError(UnboundPlaceholderParameter(), vd.pos)
case _ =>
}
placeholderParams = savedPlaceholderParams
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 @@ -447,4 +447,39 @@ object messages {

}

case class UnboundPlaceholderParameter()(implicit ctx:Context)
extends Message(16) {
val kind = "Syntax"

val msg = hl"unbound placeholder parameter; incorrect use of `_`"

val explanation =
hl"""The `_` placeholder syntax was used where it could not be bound.
|Consider explicitly writing the variable binding.
|
|This can be done by replacing `_` with a variable (eg. `x`)
|and adding ${"x =>"} where applicable.
|
|Example before:
|
|${"{ _ }"}
|
|Example after:
|
|${"x => { x }"}
|
|Another common occurrence for this error is defining a val with `_`:
|
|${"val a = _"}
|
|But this val definition isn't very useful, it can never be assigned
|another value. And thus will always remain uninitialized.
|Consider replacing the ${"val"} with ${"var"}:
|
|${"var a = _"}
|
|Note that this use of `_` is not placeholder syntax,
|but an uninitialized var definition
""".stripMargin
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Great job generalizing this error!

For the explanation I would inline the values codeUnboundInBlock, codeBoundInBlock, codeUnboundVal and codeVar, which would get the error message down from 55 lines to roughly 30.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that makes sense, the PR has been updated.

}