Aladdin - Scala Bugtracking
[#1256] project: compiler priority: medium category: bug
submitter assigned to status date submitted
Philipp Burak fixed 2007-08-07 16:53:52.0
subject unapply throws ClassCastException
code
package test

object unapplyJoins extends Application {

  class Sync {
    def apply(): Int = 42
    def unapply(scrut: Any): Boolean = false
  }

  class Buffer {
    val Get = new Sync

    val jp: PartialFunction[Any, Any] = {
      case Get() =>
    }
  }

  println((new Buffer).jp.isDefinedAt(42))
}
what happened
It compiles, but when run throws a ClassCastException:

java.lang.ExceptionInInitializerError
        at test.unapplyJoins.main(stripped5.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.ObjectRunner$$anonfun$0.apply(ObjectRunner.scala:75)
        at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:154)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.ClassCastException: test.unapplyJoins$ cannot be cast to test.unapplyJoins$Buffer
        at test.unapplyJoins$Buffer$$anonfun$0.isDefinedAt(stripped5.scala:16)
        at test.unapplyJoins$.(stripped5.scala:20)
        at test.unapplyJoins$.(stripped5.scala)
        ... 10 more

For method isDefinedAt of the jp PartialFunction the following (wrong) code is generated:

final def isDefinedAt(x$1: java.lang.Object): Boolean = if (unapplyJoins.this.$asInstanceOf[test.unapplyJoins$Bu\
ffer]().Get().unapply(x$1))
      true
    else
      false;
what expected No exception thrown when run.
[back to overview]
Changes of this bug report
Burak  edited on  2007-08-14 18:07:08.0
Philipp  edited on  2007-08-15 18:20:52.0
Burak  edited on  2007-08-15 18:42:10.0
in the Typer, we seem to assign the wrong symbol to the unapply :/
Burak  edited on  2007-08-15 22:36:18.0
ok, i am assigning the proper fn symbol to the unapply now, i think.