Skip to content

Commit aaae563

Browse files
committed
Improve error message formatting
1 parent a2245dd commit aaae563

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

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

Lines changed: 39 additions & 27 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}
@@ -607,63 +607,75 @@ object messages {
607607
|""".stripMargin
608608
}
609609
case class WrongNumberOfArgs(fntpe: Type, argKind: String, expectedArgs: List[TypeParamInfo], actual: List[untpd.Tree])(implicit ctx: Context)
610-
extends Message(18) {
610+
extends Message(22) {
611611
val kind = "Syntax"
612612
val expectedCount = expectedArgs.length
613613
val actualCount = actual.length
614614
val msgPrefix = if (actualCount > expectedCount) "Too many" else "Not enough"
615615

616-
val expectedArgString = (fntpe.widen match {
617-
case pt: MethodOrPoly => pt //ensure we return a type that will have useful typeParms
618-
case _ => fntpe
619-
}).typeParams.map(_.paramName.show.split("\\$").last).mkString("[", ", ", "]")
616+
//TODO add def simpleParamName to TypeParamInfo
617+
val expectedArgString = fntpe.widen.typeParams.map(_.paramName.unexpandedName.show).mkString("[", ", ", "]")
620618

621-
val actualArgString = actual.map(_.show.split("\\.").last).mkString("[", ", ", "]")
619+
val actualArgString = actual.map(_.show).mkString("[", ", ", "]")
620+
621+
val prettyName = fntpe.termSymbol match {
622+
case NoSymbol => fntpe.show
623+
case symbol => symbol.showFullName
624+
}
622625

623626
val msg =
624-
hl"""|$msgPrefix ${argKind} arguments for $fntpe
627+
hl"""|${NoColor(msgPrefix)} ${argKind} arguments for $prettyName$expectedArgString
625628
|expected: $expectedArgString
626629
|actual: $actualArgString""".stripMargin
627630

628631
val explanation = {
632+
val tooManyTypeParams =
633+
"""|val tuple2: (Int, String) = (1, "one")
634+
|val list: List[(Int, String)] = List(tuple2)""".stripMargin
635+
629636
if (actualCount > expectedCount)
630637
hl"""|You have supplied too many type parameters
631638
|
632639
|For example List takes a single type parameter (List[A])
633-
| If you need to hold more types in a list then you need to combine them
634-
| into another data type that can contain the number of types you need,
635-
| In this example one solution would be to use a Tuple:
636-
| val tuple2: Tuple2[Int, String = (1, "one)
637-
| List[(Int, String)] = List(tuple2)""".stripMargin
640+
|If you need to hold more types in a list then you need to combine them
641+
|into another data type that can contain the number of types you need,
642+
|In this example one solution would be to use a Tuple:
643+
|
644+
|${tooManyTypeParams}""".stripMargin
638645
else
639646
hl"""|You have not supplied enough type parameters
640-
| If you specify one type parameter then you need to specify every type parameter.""".stripMargin
647+
|If you specify one type parameter then you need to specify every type parameter.""".stripMargin
641648
}
642649
}
643650

644651
case class IllegalVariableInPatternAlternative()(implicit ctx: Context)
645-
extends Message(19) {
652+
extends Message(23) {
646653
val kind = "Syntax"
647654

648655
val msg = hl"""|Variables are not allowed in alternative patterns"""
649656

650657
val explanation = {
658+
val varInAlternative =
659+
"""|def g(pair: (Int,Int)): Int = pair match {
660+
| case (1, n) | (n, 1) => n
661+
| case _ => 0
662+
|}""".stripMargin
663+
664+
val fixedVarInAlternative =
665+
"""|def g(pair: (Int,Int)): Int = pair match {
666+
| case (1, n) => n
667+
| case (n, 1) => n
668+
| case _ => 0
669+
|}""".stripMargin
670+
651671
hl"""|Variables are not allowed within alternate pattern matches.
652672
|You can workaround this issue by adding additional cases for each alternative.
653673
|For example, the illegal function:
654-
| def g(pair: (Int,Int)): Int = pair match {
655-
| case (1, n) | (n, 1) => n
656-
| case _ => 0
657-
| }
658-
|
659-
| could be implemented by moving each alternative into a separate case:
660-
| def g(pair: (Int,Int)): Int = pair match {
661-
| case (1, n) => n
662-
| case (n, 1) => n
663-
| case _ => 0
664-
| }
665674
|
666-
|""".stripMargin
675+
|$varInAlternative
676+
|could be implemented by moving each alternative into a separate case:
677+
|
678+
|$fixedVarInAlternative""".stripMargin
667679
}
668680
}
669681
}

0 commit comments

Comments
 (0)