Skip to content

Commit 204ee3d

Browse files
bishaboshaWojciechMazur
authored andcommitted
better explain message for 'pattern expected'
[Cherry-picked 721dcad]
1 parent 17cfa2a commit 204ee3d

File tree

3 files changed

+94
-25
lines changed

3 files changed

+94
-25
lines changed

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

+22-25
Original file line numberDiff line numberDiff line change
@@ -973,66 +973,63 @@ extends SyntaxMsg(IllegalStartOfSimplePatternID) {
973973
def msg(using Context) = "pattern expected"
974974
def explain(using Context) = {
975975
val sipCode =
976-
"""def f(x: Int, y: Int) = x match {
977-
| case `y` => ...
978-
|}
979-
"""
976+
"""def f(x: Int, y: Int) = x match
977+
| case `y` => ...""".stripMargin
980978
val constructorPatternsCode =
981979
"""case class Person(name: String, age: Int)
982980
|
983-
|def test(p: Person) = p match {
984-
| case Person(name, age) => ...
985-
|}
986-
"""
987-
val tupplePatternsCode =
988-
"""def swap(tuple: (String, Int)): (Int, String) = tuple match {
989-
| case (text, number) => (number, text)
990-
|}
991-
"""
981+
| def test(p: Person) = p match
982+
| case Person(name, age) => ...""".stripMargin
983+
val tuplePatternsCode =
984+
"""def swap(tuple: (String, Int)): (Int, String) = tuple match
985+
| case (text, number) => (number, text)""".stripMargin
992986
val patternSequencesCode =
993-
"""def getSecondValue(list: List[Int]): Int = list match {
994-
| case List(_, second, x:_*) => second
995-
| case _ => 0
996-
|}"""
987+
"""def getSecondValue(list: List[Int]): Int = list match
988+
| case List(_, second, x*) => second
989+
| case _ => 0""".stripMargin
997990
i"""|Simple patterns can be divided into several groups:
998-
|- Variable Patterns: ${hl("case x => ...")}.
991+
|- Variable Patterns: ${hl("case x => ...")} or ${hl("case _ => ...")}
999992
| It matches any value, and binds the variable name to that value.
1000993
| A special case is the wild-card pattern _ which is treated as if it was a fresh
1001994
| variable on each occurrence.
1002995
|
1003-
|- Typed Patterns: ${hl("case x: Int => ...")} or ${hl("case _: Int => ...")}.
996+
|- Typed Patterns: ${hl("case x: Int => ...")} or ${hl("case _: Int => ...")}
1004997
| This pattern matches any value matched by the specified type; it binds the variable
1005998
| name to that value.
1006999
|
1007-
|- Literal Patterns: ${hl("case 123 => ...")} or ${hl("case 'A' => ...")}.
1000+
|- Given Patterns: ${hl("case given ExecutionContext => ...")}
1001+
| This pattern matches any value matched by the specified type; it binds a ${hl("given")}
1002+
| instance with the same type to that value.
1003+
|
1004+
|- Literal Patterns: ${hl("case 123 => ...")} or ${hl("case 'A' => ...")}
10081005
| This type of pattern matches any value that is equal to the specified literal.
10091006
|
10101007
|- Stable Identifier Patterns:
10111008
|
1012-
| $sipCode
1009+
| ${hl(sipCode)}
10131010
|
10141011
| the match succeeds only if the x argument and the y argument of f are equal.
10151012
|
10161013
|- Constructor Patterns:
10171014
|
1018-
| $constructorPatternsCode
1015+
| ${hl(constructorPatternsCode)}
10191016
|
10201017
| The pattern binds all object's fields to the variable names (name and age, in this
10211018
| case).
10221019
|
10231020
|- Tuple Patterns:
10241021
|
1025-
| $tupplePatternsCode
1022+
| ${hl(tuplePatternsCode)}
10261023
|
10271024
| Calling:
10281025
|
1029-
| ${hl("""swap(("Luftballons", 99)""")}
1026+
| ${hl("""swap(("Luftballons", 99))""")}
10301027
|
10311028
| would give ${hl("""(99, "Luftballons")""")} as a result.
10321029
|
10331030
|- Pattern Sequences:
10341031
|
1035-
| $patternSequencesCode
1032+
| ${hl(patternSequencesCode)}
10361033
|
10371034
| Calling:
10381035
|

tests/neg/i18750-format.check

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- [E032] Syntax Error: tests/neg/i18750-format.scala:4:7 --------------------------------------------------------------
2+
4 | case # => () // error
3+
| ^
4+
| pattern expected
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| Simple patterns can be divided into several groups:
9+
| - Variable Patterns: case x => ... or case _ => ...
10+
| It matches any value, and binds the variable name to that value.
11+
| A special case is the wild-card pattern _ which is treated as if it was a fresh
12+
| variable on each occurrence.
13+
|
14+
| - Typed Patterns: case x: Int => ... or case _: Int => ...
15+
| This pattern matches any value matched by the specified type; it binds the variable
16+
| name to that value.
17+
|
18+
| - Given Patterns: case given ExecutionContext => ...
19+
| This pattern matches any value matched by the specified type; it binds a given
20+
| instance with the same type to that value.
21+
|
22+
| - Literal Patterns: case 123 => ... or case 'A' => ...
23+
| This type of pattern matches any value that is equal to the specified literal.
24+
|
25+
| - Stable Identifier Patterns:
26+
|
27+
| def f(x: Int, y: Int) = x match
28+
| case `y` => ...
29+
|
30+
| the match succeeds only if the x argument and the y argument of f are equal.
31+
|
32+
| - Constructor Patterns:
33+
|
34+
| case class Person(name: String, age: Int)
35+
|
36+
| def test(p: Person) = p match
37+
| case Person(name, age) => ...
38+
|
39+
| The pattern binds all object's fields to the variable names (name and age, in this
40+
| case).
41+
|
42+
| - Tuple Patterns:
43+
|
44+
| def swap(tuple: (String, Int)): (Int, String) = tuple match
45+
| case (text, number) => (number, text)
46+
|
47+
| Calling:
48+
|
49+
| swap(("Luftballons", 99))
50+
|
51+
| would give (99, "Luftballons") as a result.
52+
|
53+
| - Pattern Sequences:
54+
|
55+
| def getSecondValue(list: List[Int]): Int = list match
56+
| case List(_, second, x*) => second
57+
| case _ => 0
58+
|
59+
| Calling:
60+
|
61+
| getSecondValue(List(1, 10, 2))
62+
|
63+
| would give 10 as a result.
64+
| This pattern is possible because a companion object for the List class has a method
65+
| with the following signature:
66+
|
67+
| def unapplySeq[A](x: List[A]): Some[List[A]]
68+
---------------------------------------------------------------------------------------------------------------------

tests/neg/i18750-format.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//> using options -explain
2+
3+
def test = 23 match
4+
case # => () // error

0 commit comments

Comments
 (0)