Skip to content

Commit 0b20a04

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 2a019f3 commit 0b20a04

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,10 @@ class Typer extends Namer
13001300
case _ =>
13011301
if (tree.name == nme.WILDCARD) body1
13021302
else {
1303-
val sym = ctx.newPatternBoundSymbol(tree.name, body1.tpe.underlyingIfRepeated(isJava = false), tree.pos)
1303+
val symTp =
1304+
if (body1.tpe.isInstanceOf[TermRef]) pt1
1305+
else body1.tpe.underlyingIfRepeated(isJava = false)
1306+
val sym = ctx.newPatternBoundSymbol(tree.name, symTp, tree.pos)
13041307
if (ctx.mode.is(Mode.InPatternAlternative))
13051308
ctx.error(i"Illegal variable ${sym.name} in pattern alternative", tree.pos)
13061309
assignType(cpy.Bind(tree)(tree.name, body1), sym)

tests/neg/3200b.scala

+5
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+
}

tests/neg/i3200.scala

+6
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+
}

0 commit comments

Comments
 (0)