Skip to content

Commit 8f0dc96

Browse files
committed
Improve error message formatting
1 parent 3790e93 commit 8f0dc96

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

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

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package reporting
44
package diagnostic
55

66
import dotc.core._
7-
import Contexts.Context, Decorators._, Symbols._, Names._, Types._
7+
import Contexts.Context, Decorators._, Symbols._, Names._, NameOps._, Types._
88
import ast.untpd.{Modifiers, ModuleDef}
99
import util.{SourceFile, NoSource}
1010
import util.{SourcePosition, NoSourcePosition}
@@ -514,63 +514,74 @@ object messages {
514514
}
515515

516516
case class WrongNumberOfArgs(fntpe: Type, argKind: String, expectedArgs: List[TypeParamInfo], actual: List[untpd.Tree])(implicit ctx: Context)
517-
extends Message(18) {
517+
extends Message(19) {
518518
val kind = "Syntax"
519519
val expectedCount = expectedArgs.length
520520
val actualCount = actual.length
521521
val msgPrefix = if (actualCount > expectedCount) "Too many" else "Not enough"
522522

523-
val expectedArgString = (fntpe.widen match {
524-
case pt: MethodOrPoly => pt //ensure we return a type that will have useful typeParms
525-
case _ => fntpe
526-
}).typeParams.map(_.paramName.show.split("\\$").last).mkString("[", ", ", "]")
523+
val expectedArgString = fntpe.widen.typeParams.map(_.paramName.unexpandedName.show).mkString("[", ", ", "]")
524+
525+
val actualArgString = actual.map(_.show).mkString("[", ", ", "]")
527526

528-
val actualArgString = actual.map(_.show.split("\\.").last).mkString("[", ", ", "]")
527+
val prettyName = fntpe.termSymbol match {
528+
case NoSymbol => fntpe.show
529+
case symbol => symbol.showFullName
530+
}
529531

530532
val msg =
531-
hl"""|$msgPrefix ${argKind} arguments for $fntpe
533+
hl"""|${NoColor(msgPrefix)} ${argKind} arguments for $prettyName$expectedArgString
532534
|expected: $expectedArgString
533535
|actual: $actualArgString""".stripMargin
534536

535537
val explanation = {
538+
val tooManyTypeParams =
539+
"""|val tuple2: (Int, String) = (1, "one")
540+
|val list: List[(Int, String)] = List(tuple2)""".stripMargin
541+
536542
if (actualCount > expectedCount)
537543
hl"""|You have supplied too many type parameters
538544
|
539545
|For example List takes a single type parameter (List[A])
540-
| If you need to hold more types in a list then you need to combine them
541-
| into another data type that can contain the number of types you need,
542-
| In this example one solution would be to use a Tuple:
543-
| val tuple2: Tuple2[Int, String = (1, "one)
544-
| List[(Int, String)] = List(tuple2)""".stripMargin
546+
|If you need to hold more types in a list then you need to combine them
547+
|into another data type that can contain the number of types you need,
548+
|In this example one solution would be to use a Tuple:
549+
|
550+
|${tooManyTypeParams}""".stripMargin
545551
else
546552
hl"""|You have not supplied enough type parameters
547-
| If you specify one type parameter then you need to specify every type parameter.""".stripMargin
553+
|If you specify one type parameter then you need to specify every type parameter.""".stripMargin
548554
}
549555
}
550556

551557
case class IllegalVariableInPatternAlternative()(implicit ctx: Context)
552-
extends Message(19) {
558+
extends Message(20) {
553559
val kind = "Syntax"
554560

555561
val msg = hl"""|Variables are not allowed in alternative patterns"""
556562

557563
val explanation = {
564+
val varInAlternative =
565+
"""|def g(pair: (Int,Int)): Int = pair match {
566+
| case (1, n) | (n, 1) => n
567+
| case _ => 0
568+
|}""".stripMargin
569+
570+
val fixedVarInAlternative =
571+
"""|def g(pair: (Int,Int)): Int = pair match {
572+
| case (1, n) => n
573+
| case (n, 1) => n
574+
| case _ => 0
575+
|}""".stripMargin
576+
558577
hl"""|Variables are not allowed within alternate pattern matches.
559578
|You can workaround this issue by adding additional cases for each alternative.
560579
|For example, the illegal function:
561-
| def g(pair: (Int,Int)): Int = pair match {
562-
| case (1, n) | (n, 1) => n
563-
| case _ => 0
564-
| }
565580
|
566-
| could be implemented by moving each alternative into a separate case:
567-
| def g(pair: (Int,Int)): Int = pair match {
568-
| case (1, n) => n
569-
| case (n, 1) => n
570-
| case _ => 0
571-
| }
581+
|$varInAlternative
582+
|could be implemented by moving each alternative into a separate case:
572583
|
573-
|""".stripMargin
584+
|$fixedVarInAlternative""".stripMargin
574585
}
575586
}
576587
}

0 commit comments

Comments
 (0)