Skip to content

Commit 179a0fe

Browse files
committed
Update IllegalVariableInPatternAlternative error message
1 parent ef085d5 commit 179a0fe

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ object desugar {
10511051
elems foreach collect
10521052
case Alternative(trees) =>
10531053
for (tree <- trees; (vble, _) <- getVariables(tree))
1054-
ctx.error("illegal variable in pattern alternative", vble.pos)
1054+
ctx.error(IllegalVariableInPatternAlternative(), vble.pos)
10551055
case Annotated(arg, _) =>
10561056
collect(arg)
10571057
case InterpolatedString(_, segments) =>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,30 @@ object messages {
547547
| If you specify one type parameter then you need to specify every type parameter.""".stripMargin
548548
}
549549
}
550+
551+
case class IllegalVariableInPatternAlternative(implicit ctx: Context)
552+
extends Message(19) {
553+
val kind = "Syntax"
554+
555+
val msg = hl"""|Variables are not allowed in alternative patterns"""
556+
557+
val explanation = {
558+
hl"""|Variables are not allowed within alternate pattern matches.
559+
|You can workaround this issue by adding additional cases for each alternative.
560+
|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+
| }
565+
|
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+
| }
572+
|
573+
|""".stripMargin
574+
}
575+
}
550576
}

0 commit comments

Comments
 (0)