@@ -4,12 +4,13 @@ package reporting
4
4
package diagnostic
5
5
6
6
import dotc .core ._
7
- import Contexts .Context , Decorators ._ , Symbols ._ , Names ._ , Types ._
7
+ import Contexts .Context , Decorators ._ , Symbols ._ , Names ._ , NameOps . _ , Types ._
8
8
import ast .untpd .{Modifiers , ModuleDef }
9
9
import util .{SourceFile , NoSource }
10
10
import util .{SourcePosition , NoSourcePosition }
11
11
import config .Settings .Setting
12
12
import interfaces .Diagnostic .{ERROR , WARNING , INFO }
13
+ import dotty .tools .dotc .ast .tpd
13
14
import printing .Highlighting ._
14
15
import printing .Formatting
15
16
@@ -605,4 +606,76 @@ object messages {
605
606
| ${" func(bool => // do something...)" }
606
607
| """ .stripMargin
607
608
}
609
+ case class WrongNumberOfArgs (fntpe : Type , argKind : String , expectedArgs : List [TypeParamInfo ], actual : List [untpd.Tree ])(implicit ctx : Context )
610
+ extends Message (22 ) {
611
+ val kind = " Syntax"
612
+ val expectedCount = expectedArgs.length
613
+ val actualCount = actual.length
614
+ val msgPrefix = if (actualCount > expectedCount) " Too many" else " Not enough"
615
+
616
+ // TODO add def simpleParamName to TypeParamInfo
617
+ val expectedArgString = fntpe.widen.typeParams.map(_.paramName.unexpandedName.show).mkString(" [" , " , " , " ]" )
618
+
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
+ }
625
+
626
+ val msg =
627
+ hl """ | ${NoColor (msgPrefix)} ${argKind} arguments for $prettyName$expectedArgString
628
+ |expected: $expectedArgString
629
+ |actual: $actualArgString""" .stripMargin
630
+
631
+ val explanation = {
632
+ val tooManyTypeParams =
633
+ """ |val tuple2: (Int, String) = (1, "one")
634
+ |val list: List[(Int, String)] = List(tuple2)""" .stripMargin
635
+
636
+ if (actualCount > expectedCount)
637
+ hl """ |You have supplied too many type parameters
638
+ |
639
+ |For example List takes a single type parameter (List[A])
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
645
+ else
646
+ hl """ |You have not supplied enough type parameters
647
+ |If you specify one type parameter then you need to specify every type parameter. """ .stripMargin
648
+ }
649
+ }
650
+
651
+ case class IllegalVariableInPatternAlternative ()(implicit ctx : Context )
652
+ extends Message (23 ) {
653
+ val kind = " Syntax"
654
+
655
+ val msg = hl """ |Variables are not allowed in alternative patterns """
656
+
657
+ 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
+
671
+ hl """ |Variables are not allowed within alternate pattern matches.
672
+ |You can workaround this issue by adding additional cases for each alternative.
673
+ |For example, the illegal function:
674
+ |
675
+ | $varInAlternative
676
+ |could be implemented by moving each alternative into a separate case:
677
+ |
678
+ | $fixedVarInAlternative""" .stripMargin
679
+ }
680
+ }
608
681
}
0 commit comments