Skip to content

Commit a5a4ec7

Browse files
committed
Add unbound placeholder parameter message.
1 parent 6070cce commit a5a4ec7

File tree

2 files changed

+57
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)