scala/scala — pull request #3294 by gzm0 (2013-12-20 15:58:35+00:00)
Title: SI-1448 Unify casting behavior for AnyVals

Casts of AnyVals had inconsitent behavior depending on whether they were boxed:

1L.asInstanceOf[Int] // -> 1
(1L: Any).asInstanceOf[Int] // -> ClassCastException

This PR fixes this by adapting the unboxing helpers in `BoxesRunTime`. A couple of caveats:

- Unboxing requires an additional instanceof check. (Reason: Casting of Character, which is not subtype of Number).
- A wrong ClassCastException is raised on a failed cast to a primitive numeric value. It says "cannot cast to Number" instead of Integer etc.

Where the latter issue can easily be fixed by adding another `instanceof` check, the former is inherent to the fact that `java.lang.Character` does not extend `java.lang.Number` but conversion must be performed. It can only be fixed by integrating (part) of this behaviour in the compiler.

Maybe we should reconsider changing the behaviour the other way around. It is easy to emit a deprecation warning in 2.11 for casts that convert ([in the erasure phase](https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/transform/Erasure.scala#L39)) and put the behaviour behind a flag in 2.12.

All partests except for the instrumentation tests succeed on this PR.