Skip to content

Commit bf80147

Browse files
committed
Add unbound placeholder parameter message.
1 parent a6d3723 commit bf80147

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ object Parsers {
334334
try op
335335
finally {
336336
placeholderParams match {
337-
case vd :: _ => syntaxError("unbound placeholder parameter", vd.pos)
337+
case vd :: _ => syntaxError(UnboundPlaceholderParameter(), vd.pos)
338338
case _ =>
339339
}
340340
placeholderParams = savedPlaceholderParams

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,39 @@ object messages {
447447

448448
}
449449

450+
case class UnboundPlaceholderParameter()(implicit ctx:Context)
451+
extends Message(16) {
452+
val kind = "Syntax"
453+
454+
val msg = hl"unbound placeholder parameter; incorrect use of `_`"
455+
456+
val explanation =
457+
hl"""The `_` placeholder syntax was used where it could not be bound.
458+
|Consider explicitly writing the variable binding.
459+
|
460+
|This can be done by replacing `_` with a variable (eg. `x`)
461+
|and adding ${"x =>"} where applicable.
462+
|
463+
|Example before:
464+
|
465+
|${"{ _ }"}
466+
|
467+
|Example after:
468+
|
469+
|${"x => { x }"}
470+
|
471+
|Another common occurrence for this error is defining a val with `_`:
472+
|
473+
|${"val a = _"}
474+
|
475+
|But this val definition isn't very useful, it can never be assigned
476+
|another value. And thus will always remain uninitialized.
477+
|Consider replacing the ${"val"} with ${"var"}:
478+
|
479+
|${"var a = _"}
480+
|
481+
|Note that this use of `_` is not placeholder syntax,
482+
|but an uninitialized var definition
483+
""".stripMargin
484+
}
450485
}

0 commit comments

Comments
 (0)