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
Code in scala.Int.unbox source differs from actual implementation in BoxesRunTime.unboxToInt #6710
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6710?orig=1
|
@demobox said: However, the test case is not simple to automate since the runtime behaviour of scala.Int.unbox is not determined by the code that has been changed (see attached screenshot). The test case is:
|
@heathermiller said: Here is where the backend chooses the In that case, your suggested patch looks innocent and essentially just documentation. |
@demobox said (edited on Nov 26, 2012 1:25:45 PM UTC):
Thanks for the comment, Heather. Yes, the change is indeed intended as a "documentation" update rather than any functionality change. Luc (Bourlier) had mentioned Array [1] as an example where it's perhaps clearer that the code itself "does nothing", but I was trying to keep the diff as minimal as possible. Shall I go ahead and turn the commit into a pull request? [1] https://github.com/scala/scala/blob/master/src/library/scala/Array.scala#L496 |
@magarciaEPFL said: https://github.com/scala/scala/blob/master/src/reflect/scala/reflect/internal/Definitions.scala#L101 https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/transform/Erasure.scala#L657 and it's also used in other places via One of those usages reads:
Therefore, changing object Int's unbox(Object) is "non trivial". |
@demobox said:
Aha! Interesting. Thanks for explaining, Miguel. Do you have any suggestions for creating a test that could validate that the change does not break existing behaviour? Also, even if the method is, in fact, called, is it necessary that the behaviour differ from the actual runtime behaviour of Int unboxing? |
@magarciaEPFL said:
I guess it's not necessary to change the body of object Int's unbox() . After all, the definition itself does not tell "the whole story" of when Erasure decides to leave the invocation in place vs rewrite it. If the behavior were to be documented, then it should be for "==" , as [comment-40668 in SI-602 | https://issues.scala-lang.org/browse/SI-602?focusedCommentId=40668&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-40668] makes clear. |
@magarciaEPFL said:
but the bytecode contains an invocation of
That's because -Xprint:jvm actually prints trees right after CleanUp, and the rewriting that's being chased here takes place in GenICode, whose output can be seen via
Sorry about the confusion. It's "Not a bug". |
@demobox said:
Makes sense - thanks for explaining. It's clear now why the AST does not match the bytecode. I guess the remaining question is whether the implementation of the two functions should have the same behaviour or whether that is also "not a bug"... |
@magarciaEPFL said:
because that file is automatically generated. |
@demobox said:
I was curious about that too ;-) I just was not able to find where this code is actually generated from. Do you have any hints?
How would you make this call? From the REPL I tried calling [1] [^2.10.0-RC2-repl-session.png] |
@retronym said: |
@demobox said:
Thanks, Jason! Egads: https://github.com/scala/scala/blob/master/src/compiler/scala/tools/cmd/gen/AnyVals.scala#L232 makes this look a whole lot bigger. Hm... |
@demobox said:
Perhaps one final question, if you would be so kind: if this is "as intended", what then is the purpose of the If the method is not supposed to document what actually happens when unboxing takes place, what is it for? Thanks! [1] https://github.com/scala/scala/blob/master/src/compiler/scala/tools/cmd/gen/AnyVals.scala#L232 |
@magarciaEPFL said: |
@demobox said:
Fair enough ;-) So, if anything, an "improvement". Thanks for your time on this! |
@lrytz said: |
@demobox said: Yay! Glad to see we now feel the inconsistency here is weird enough to take the risk of changing the behaviour. |
Compiling the attached Test.scala class (against 2.10.0-RC2) with the -Xprint:jvm flag indicates that the AST for the null comparisons is:
Decompiling the resulting Test.class (using Jad v1.5.8g [1]) shows that the actual runtime calls are:
However, scala.Int.unbox [2] and BoxesRunTime.unboxToInt [3] differ in their behaviour with respect to null. scala.Int.unbox behaves like Java Integer-to-int unboxing and throws a NullPointerException, whereas BoxesRunTime.unboxToInt converts null to 0.
vs
#602 contains a discussion on whether the runtime behaviour is actually desired; this issue simply suggests amending the definition of scala.Int.unbox to match runtime behaviour and to document the method's behaviour on null input.
[1] http://www.varaneckas.com/jad/jad158g.win.zip
[2] https://github.com/scala/scala/blob/master/src/library/scala/Int.scala#L622
[3] https://github.com/scala/scala/blob/master/src/library/scala/runtime/BoxesRunTime.java#L105
The text was updated successfully, but these errors were encountered: