Skip to content

Commit 32ece43

Browse files
committed
fix #3200: bind var for singleton pattern get scrutinee type
Related issues: https://issues.scala-lang.org/browse/SI-1503 #1463
1 parent d5ee92b commit 32ece43

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,12 @@ class Typer extends Namer
13201320
case _ =>
13211321
if (tree.name == nme.WILDCARD) body1
13221322
else {
1323-
val sym = ctx.newPatternBoundSymbol(tree.name, body1.tpe.underlyingIfRepeated(isJava = false), tree.pos)
1323+
// for a singleton pattern like `x @ Nil`, `x` should get the type from the scrutinee
1324+
// see tests/neg/3200b.scala and SI-1503
1325+
val symTp =
1326+
if (body1.tpe.isInstanceOf[TermRef]) pt1
1327+
else body1.tpe.underlyingIfRepeated(isJava = false)
1328+
val sym = ctx.newPatternBoundSymbol(tree.name, symTp, tree.pos)
13241329
if (ctx.mode.is(Mode.InPatternAlternative))
13251330
ctx.error(i"Illegal variable ${sym.name} in pattern alternative", tree.pos)
13261331
assignType(cpy.Bind(tree)(tree.name, body1), sym)

tests/neg/i3200.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
case object Bob { override def equals(other: Any) = true }
3+
def main(args: Array[String]): Unit = {
4+
val m : Bob.type = (5: Any) match { case x @ Bob => x } // error
5+
}
6+
}

tests/neg/i3200b.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val a : Nil.type = (Vector(): Any) match { case n @ Nil => n } // error
4+
}
5+
}

0 commit comments

Comments
 (0)