Skip to content

Fix #3200: bind var for singleton pattern get scrutinee type #4022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,12 @@ class Typer extends Namer
case _ =>
if (tree.name == nme.WILDCARD) body1
else {
val sym = ctx.newPatternBoundSymbol(tree.name, body1.tpe.underlyingIfRepeated(isJava = false), tree.pos)
// for a singleton pattern like `x @ Nil`, `x` should get the type from the scrutinee
// see tests/neg/i3200b.scala and SI-1503
val symTp =
if (body1.tpe.isInstanceOf[TermRef]) pt1
else body1.tpe.underlyingIfRepeated(isJava = false)
val sym = ctx.newPatternBoundSymbol(tree.name, symTp, tree.pos)
if (ctx.mode.is(Mode.InPatternAlternative))
ctx.error(i"Illegal variable ${sym.name} in pattern alternative", tree.pos)
assignType(cpy.Bind(tree)(tree.name, body1), sym)
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i3200.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
case object Bob { override def equals(other: Any) = true }
def main(args: Array[String]): Unit = {
val m : Bob.type = (5: Any) match { case x @ Bob => x } // error
}
}
5 changes: 5 additions & 0 deletions tests/neg/i3200b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
val a : Nil.type = (Vector(): Any) match { case n @ Nil => n } // error
}
}