Skip to content

Commit a2245dd

Browse files
committed
Update IllegalVariableInPatternAlternative error message
1 parent 63f1a3c commit a2245dd

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
@@ -640,4 +640,30 @@ object messages {
640640
| If you specify one type parameter then you need to specify every type parameter.""".stripMargin
641641
}
642642
}
643+
644+
case class IllegalVariableInPatternAlternative()(implicit ctx: Context)
645+
extends Message(19) {
646+
val kind = "Syntax"
647+
648+
val msg = hl"""|Variables are not allowed in alternative patterns"""
649+
650+
val explanation = {
651+
hl"""|Variables are not allowed within alternate pattern matches.
652+
|You can workaround this issue by adding additional cases for each alternative.
653+
|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+
| }
665+
|
666+
|""".stripMargin
667+
}
668+
}
643669
}

0 commit comments

Comments
 (0)