Skip to content

Bad code for pattern matches with values. #1463

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

Closed
odersky opened this issue Aug 23, 2016 · 2 comments
Closed

Bad code for pattern matches with values. #1463

odersky opened this issue Aug 23, 2016 · 2 comments
Assignees

Comments

@odersky
Copy link
Contributor

odersky commented Aug 23, 2016

Consider the following test case adapted from pending/run/t3150:

object Test {
  case object Bob { override def equals(other: Any) = true }

  class Bob2 {
    override def equals(other: Any) = true
  }
  val Bob2 = new Bob2

  def f0(x: Any) = x match { case Bob2 => Bob2 } // class cast exception at runtime, dotc only
  def f1(x: Any) = x match { case Bob => Bob } // class cast exception at runtime, dotc only
  def f2(x: Any): Bob.type = x match { case x @ Bob => x } // class cast exception at runtime, dotc and javac.

  def main(args: Array[String]): Unit = {
    assert(f0(Bob2) eq Bob2)
    assert(f0(0) eq Bob2)  // only dotty fails here
    assert(f0(Nil) eq Bob2)

    assert(f1(Bob) eq Bob)
    assert(f1(0) eq Bob)  // only dotty fails here
    assert(f1(Nil) eq Bob)

    assert(f2(Bob) eq Bob)
    assert(f2(0) eq Bob) // both dotty and scalac fail here
    assert(f2(Nil) eq Bob)
  }
}

Dotty causes a class cast exception in three scenarios, scalac only in the last. Each class cast exception is a bug. We should decide what the precise typing and comparison rules for pattern matches involving equals tests are.

@smarter
Copy link
Member

smarter commented Aug 23, 2016

See the discussion in https://issues.scala-lang.org/browse/SI-1503 for more context, note that this is one of these cases where you can't trust the spec: it was updated following some discussions in this bug report and the proposed solution was later found out to be incorrect and reverted, but the spec wasn't updated. Anyway the only sane and correct thing to do here is that in:

(Vector(): Any) match { case n @ Nil => n }

n should not be cast to anything, the only thing we know about n is that it has type Any and it's == to Nil, no safe casting is possible here.

@smarter
Copy link
Member

smarter commented Aug 23, 2016

n should not be cast to anything

This is also be the behaviour of Scala 2.12 and Scala 2.11 with -Xfuture

liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 16, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 16, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 16, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 16, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 16, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 18, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 22, 2017
liufengyun added a commit that referenced this issue Sep 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants