Skip to content

Commit a83725a

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

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

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

Lines changed: 38 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,75 @@ 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(22) {
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+
//TODO add def simpleParamName to TypeParamInfo
524+
val expectedArgString = fntpe.widen.typeParams.map(_.paramName.unexpandedName.show).mkString("[", ", ", "]")
527525

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

530533
val msg =
531-
hl"""|$msgPrefix ${argKind} arguments for $fntpe
534+
hl"""|${NoColor(msgPrefix)} ${argKind} arguments for $prettyName$expectedArgString
532535
|expected: $expectedArgString
533536
|actual: $actualArgString""".stripMargin
534537

535538
val explanation = {
539+
val tooManyTypeParams =
540+
"""|val tuple2: (Int, String) = (1, "one")
541+
|val list: List[(Int, String)] = List(tuple2)""".stripMargin
542+
536543
if (actualCount > expectedCount)
537544
hl"""|You have supplied too many type parameters
538545
|
539546
|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
547+
|If you need to hold more types in a list then you need to combine them
548+
|into another data type that can contain the number of types you need,
549+
|In this example one solution would be to use a Tuple:
550+
|
551+
|${tooManyTypeParams}""".stripMargin
545552
else
546553
hl"""|You have not supplied enough type parameters
547-
| If you specify one type parameter then you need to specify every type parameter.""".stripMargin
554+
|If you specify one type parameter then you need to specify every type parameter.""".stripMargin
548555
}
549556
}
550557

551558
case class IllegalVariableInPatternAlternative()(implicit ctx: Context)
552-
extends Message(19) {
559+
extends Message(23) {
553560
val kind = "Syntax"
554561

555562
val msg = hl"""|Variables are not allowed in alternative patterns"""
556563

557564
val explanation = {
565+
val varInAlternative =
566+
"""|def g(pair: (Int,Int)): Int = pair match {
567+
| case (1, n) | (n, 1) => n
568+
| case _ => 0
569+
|}""".stripMargin
570+
571+
val fixedVarInAlternative =
572+
"""|def g(pair: (Int,Int)): Int = pair match {
573+
| case (1, n) => n
574+
| case (n, 1) => n
575+
| case _ => 0
576+
|}""".stripMargin
577+
558578
hl"""|Variables are not allowed within alternate pattern matches.
559579
|You can workaround this issue by adding additional cases for each alternative.
560580
|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-
| }
565581
|
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-
| }
582+
|$varInAlternative
583+
|could be implemented by moving each alternative into a separate case:
572584
|
573-
|""".stripMargin
585+
|$fixedVarInAlternative""".stripMargin
574586
}
575587
}
576588
}

0 commit comments

Comments
 (0)