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

>> behaves like >>> when inlined #10037

Closed
scabug opened this issue Nov 9, 2016 · 4 comments
Closed

>> behaves like >>> when inlined #10037

scabug opened this issue Nov 9, 2016 · 4 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Nov 9, 2016

Hey all,

It looks like >> is acting like >>> when inlined.

This bug showed up in an sbt 0.13.12 project running Scala 2.11.8. I compiled a file containing this object, with -optimize enabled:

object Bug {
  @inline def inlineShift(x: Int): Int = x >> 1
  def shift(x: Int): Int = x >> 1
}

Then started a console with sbt console, and found the following discrepancy between the behavior of the two functions:

scala> Bug.inlineShift(Int.MinValue)
res0: Int = 1073741824

scala> Bug.shift(Int.MinValue)
res1: Int = -1073741824

scala> Int.MinValue
res2: Int = -2147483648

The functions both returned Int.MinValue when I removed the -optimize flag.

@scabug
Copy link
Author

scabug commented Nov 9, 2016

Imported From: https://issues.scala-lang.org/browse/SI-10037?orig=1
Reporter: Sam Ritchie (sritchie)
Affected Versions: 2.11.8

@scabug
Copy link
Author

scabug commented Nov 9, 2016

Sam Ritchie (sritchie) said:
Sorry for all the edits. I can't figure out how to format the first block of code.

@scabug
Copy link
Author

scabug commented Nov 9, 2016

@som-snytt said:

GenASM and separate compilation:

$ ~/scala-2.11.8/bin/scalac -optimize -Ybackend:GenBCode shifted.scala && ~/scala-2.11.8/bin/scala -optimize -Ybackend:GenBCode
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.

scala> shifted.Bug.inlineShift(Int.MinValue)
res0: Int = -1073741824

scala> :quit
$ rm shifted/*.class
$ ~/scala-2.11.8/bin/scalac -optimize -Ybackend:GenASM shifted.scala && ~/scala-2.11.8/bin/scala -optimize -Ybackend:GenASM
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.

scala> shifted.Bug.inlineShift(Int.MinValue)
res0: Int = 1073741824

scala> :quit
amarki@amarki-462836:~/tmp$ ~/scala-2.11.8/bin/scalac -optimize shifted.scala && ~/scala-2.11.8/bin/scala shifted.Test
-1073741824
-1073741824
amarki@amarki-462836:~/tmp$ cat shifted.scala

package shifted

object Bug {
  @inline def inlineShift(x: Int): Int = x >> 1
  def shift(x: Int): Int = x >> 1
  def f(x: Int): Int = inlineShift(x)    // ok
}

object Test extends App {
  println(Bug.inlineShift(Int.MinValue))   // ok
  println(Bug.shift(Int.MinValue))
}

@scabug
Copy link
Author

scabug commented Nov 10, 2016

@som-snytt said:
scala/scala#5518

@scabug scabug closed this as completed Nov 11, 2016
@scabug scabug added this to the 2.11.9 milestone Apr 7, 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

2 participants