Skip to content

Commit de55510

Browse files
committed
Don't flag _ as illegal variables in pattern alternatives
I thought that `_` never appears as a bound variable in a bind but was wrong. We now rewrite `(_ @ P)` to `P`.
1 parent 3cc6559 commit de55510

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,9 +1210,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12101210
tpd.cpy.UnApply(body1)(fn, Nil,
12111211
typed(untpd.Bind(tree.name, untpd.TypedSplice(arg)).withPos(tree.pos), arg.tpe) :: Nil)
12121212
case _ =>
1213-
val sym = newPatternBoundSym(tree.name, body1.tpe, tree.pos)
1214-
if (ctx.mode.is(Mode.InPatternAlternative)) ctx.error("Illegal variable in pattern alternative", tree.pos)
1215-
assignType(cpy.Bind(tree)(tree.name, body1), sym)
1213+
if (tree.name == nme.WILDCARD) body1
1214+
else {
1215+
val sym = newPatternBoundSym(tree.name, body1.tpe, tree.pos)
1216+
if (ctx.mode.is(Mode.InPatternAlternative))
1217+
ctx.error(i"Illegal variable ${sym.name} in pattern alternative", tree.pos)
1218+
assignType(cpy.Bind(tree)(tree.name, body1), sym)
1219+
}
12161220
}
12171221
}
12181222

tests/neg/i3332.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ object Main {
1010

1111
// #1612
1212
object Test {
13+
case class A()
1314
def g(p:(Int,Int)) = p match {
1415
case (10,n) | (n,10) => println(n) // error // error (Illegal variable in pattern alternative)
1516
case _ => println("nope")
1617
}
18+
def test(x: Any) = x match {
19+
case _: String | _ @ A() => 1
20+
}
1721
}

0 commit comments

Comments
 (0)