Skip to content
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

Int shift Long - Scala does not agree with Java on the runtime semantics #9516

Closed
scabug opened this Issue Oct 15, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@scabug
Copy link

scabug commented Oct 15, 2015

For any of the three shift operators (<<, >>> and >>), when the left operand is an Int (or Byte or Short, I guess) but the right operand is a Long, the runtime semantics produced by scalac are not the same as those produced by javac for equivalent operations.

Reproduction: compile the attached Foo.java and Bar.scala with javac and scalac respectively, and execute the code. The programs are equivalent at the source level, but they don't produce the same results.

Java produces:
271601776
271601776
271601776
271601776
151323393
151323393
151323393
151323393
-117112063
-117112063
-117112063
-117112063

but Scala produces:
0
0
0
0
268435455
268435455
268435455
268435455
-1
-1
-1
-1

To make the Scala program behave the same as the Java program, I have to explicitly add .toInts to the righ-hand-side of all shift operations, e.g., x << y.toInt instead of x << y. Conversely, to make the Java program behave the same as the Scala program, I have to replace x << y by (int) ((long) x << y)).

The difference, in practice, is that, in Java, the rhs is computed modulo 32, as expected for a shift operation returning an Int, whereas in Scala, the rhs is computed modulo 64.

This misbehavior has apparently lived on in all versions of Scala. I would understand if we cannot fix this in 2.11 for compatibility reasons, but we should fix this for 2.12.

The Bar.scala file can be trivially turned into a partest test, using as checkfile the output of Java listed above.

Related bug that was fixed in 2.12.0-M1: #8462

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Oct 15, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9516?orig=1
Reporter: @sjrd
Affected Versions: 2.10.5, 2.11.7, 2.12.0-M3
Attachments:

  • Bar.scala (created on Oct 15, 2015 12:29:32 PM UTC, 966 bytes)
  • Foo.java (created on Oct 15, 2015 12:29:29 PM UTC, 959 bytes)
@scabug

This comment has been minimized.

Copy link
Author

scabug commented Apr 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.