Skip to content

Commit 273df6b

Browse files
committed
Add unbound placeholder parameter message.
1 parent b008ceb commit 273df6b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,40 @@ object messages {
400400
| ((${nestedRepresentation}))""".stripMargin
401401
}
402402
}
403+
404+
case class UnboundPlaceholderParameter()(implicit ctx:Context)
405+
extends Message(16) {
406+
val kind = "Syntax"
407+
408+
val msg = hl"unbound placeholder parameter; incorrect use of `_`"
409+
410+
val explanation =
411+
hl"""The `_` placeholder syntax was used where it could not be bound.
412+
|Consider explicitly writing the variable binding.
413+
|
414+
|This can be done by replacing `_` with a variable (eg. `x`)
415+
|and adding ${"x =>"} where applicable.
416+
|
417+
|Example before:
418+
|
419+
|${"{ _ }"}
420+
|
421+
|Example after:
422+
|
423+
|${"x => { x }"}
424+
|
425+
|Another common occurrence for this error is defining a val with `_`:
426+
|
427+
|${"val a = _"}
428+
|
429+
|But this val definition isn't very useful, it can never be assigned
430+
|another value. And thus will always remain uninitialized.
431+
|Consider replacing the ${"val"} with ${"var"}:
432+
|
433+
|${"var a = _"}
434+
|
435+
|Note that this use of `_` is not placeholder syntax,
436+
|but an uninitialized var definition
437+
""".stripMargin
438+
}
403439
}

0 commit comments

Comments
 (0)