File tree 2 files changed +57
-1
lines changed
2 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -334,7 +334,7 @@ object Parsers {
334
334
try op
335
335
finally {
336
336
placeholderParams match {
337
- case vd :: _ => syntaxError(" unbound placeholder parameter " , vd.pos)
337
+ case vd :: _ => syntaxError(UnboundPlaceholderParameter () , vd.pos)
338
338
case _ =>
339
339
}
340
340
placeholderParams = savedPlaceholderParams
Original file line number Diff line number Diff line change @@ -400,4 +400,60 @@ object messages {
400
400
| (( ${nestedRepresentation})) """ .stripMargin
401
401
}
402
402
}
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
+ }
403
459
}
You can’t perform that action at this time.
0 commit comments