From a2245dde469e29b0a93479a73961511e269b7440 Mon Sep 17 00:00:00 2001 From: Shane Delmore Date: Mon, 24 Oct 2016 18:20:29 -0700 Subject: Update IllegalVariableInPatternAlternative error message --- src/dotty/tools/dotc/ast/Desugar.scala | 2 +- .../tools/dotc/reporting/diagnostic/messages.scala | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/ast/Desugar.scala b/src/dotty/tools/dotc/ast/Desugar.scala index 8b8e0b318..639dac930 100644 --- a/src/dotty/tools/dotc/ast/Desugar.scala +++ b/src/dotty/tools/dotc/ast/Desugar.scala @@ -1051,7 +1051,7 @@ object desugar { elems foreach collect case Alternative(trees) => for (tree <- trees; (vble, _) <- getVariables(tree)) - ctx.error("illegal variable in pattern alternative", vble.pos) + ctx.error(IllegalVariableInPatternAlternative(), vble.pos) case Annotated(arg, _) => collect(arg) case InterpolatedString(_, segments) => diff --git a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 57d96224a..7e897f081 100644 --- a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -640,4 +640,30 @@ object messages { | If you specify one type parameter then you need to specify every type parameter.""".stripMargin } } + + case class IllegalVariableInPatternAlternative()(implicit ctx: Context) + extends Message(19) { + val kind = "Syntax" + + val msg = hl"""|Variables are not allowed in alternative patterns""" + + val explanation = { + hl"""|Variables are not allowed within alternate pattern matches. + |You can workaround this issue by adding additional cases for each alternative. + |For example, the illegal function: + | def g(pair: (Int,Int)): Int = pair match { + | case (1, n) | (n, 1) => n + | case _ => 0 + | } + | + | could be implemented by moving each alternative into a separate case: + | def g(pair: (Int,Int)): Int = pair match { + | case (1, n) => n + | case (n, 1) => n + | case _ => 0 + | } + | + |""".stripMargin + } + } } -- cgit v1.2.3