Skip to content

Commit 6ce7946

Browse files
committed
Add an error message for illegal start of simple pattern
1 parent 58a3ed9 commit 6ce7946

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ object Parsers {
14361436
case _ =>
14371437
if (isLiteral) literal()
14381438
else {
1439-
syntaxErrorOrIncomplete("illegal start of simple pattern")
1439+
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern())
14401440
errorTermTree
14411441
}
14421442
}

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,4 +812,83 @@ object messages {
812812
|would give 3 as a result"""
813813
}
814814
}
815+
816+
case class IllegalStartOfSimplePattern()(implicit ctx: Context) extends Message(32) {
817+
val kind = "Syntax"
818+
val msg = "illegal start of simple pattern"
819+
val explanation = {
820+
val sipCode =
821+
"""def f(x: Int, y: Int) = x match {
822+
| case `y` => ...
823+
|}
824+
"""
825+
val constructorPatternsCode =
826+
"""case class Person(name: String, age: Int)
827+
|
828+
|def test(p: Person) = p match {
829+
| case Person(name, age) => ...
830+
|}
831+
"""
832+
val tupplePatternsCode =
833+
"""def swap(tuple: (String, Int)): (Int, String) = tuple match {
834+
| case (text, number) => (number, text)
835+
|}
836+
"""
837+
val patternSequencesCode =
838+
"""def getSecondValue(list: List[Int]): Int = list match {
839+
| case List(_, second, x:_*) => second
840+
| case _ => 0
841+
|}"""
842+
hl"""|Simple patterns can be divided into several groups:
843+
|- Variable Patterns: ${"case x => ..."}.
844+
| It matches any value, and binds the variable name to that value.
845+
| A special case is the wild-card pattern _ which is treated as if it was a fresh
846+
| variable on each occurrence.
847+
|
848+
|- Typed Patterns: ${"case x: Int => ..."} or ${"case _: Int => ..."}.
849+
| This pattern matches any value matched by the specified type; it binds the variable
850+
| name to that value.
851+
|
852+
|- Literal Patterns: ${"case 123 => ..."} or ${"case 'A' => ..."}.
853+
| This type of pattern matches any value that is equal to the specified literal.
854+
|
855+
|- Stable Identifier Patterns:
856+
|
857+
| $sipCode
858+
|
859+
| the match succeeds only if the x argument and the y argument of f are equal.
860+
|
861+
|- Constructor Patterns:
862+
|
863+
| $constructorPatternsCode
864+
|
865+
| The pattern binds all object's fields to the variable names (name and age, in this
866+
| case).
867+
|
868+
|- Tuple Patterns:
869+
|
870+
| $tupplePatternsCode
871+
|
872+
| Calling:
873+
|
874+
| ${"""swap(("Luftballons", 99)"""}
875+
|
876+
| would give ${"""(99, "Luftballons")"""} as a result.
877+
|
878+
|- Pattern Sequences:
879+
|
880+
| $patternSequencesCode
881+
|
882+
| Calling:
883+
|
884+
| ${"getSecondValue(List(1, 10, 2))"}
885+
|
886+
| would give 10 as a result.
887+
| This pattern is possible because a companion object for the List class has a method
888+
| with the following signature:
889+
|
890+
| ${"def unapplySeq[A](x: List[A]): Some[List[A]]"}
891+
|"""
892+
}
893+
}
815894
}

0 commit comments

Comments
 (0)