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

incorrect if, 0d, and -0d optimization #6331

Closed
scabug opened this issue Sep 6, 2012 · 3 comments
Closed

incorrect if, 0d, and -0d optimization #6331

scabug opened this issue Sep 6, 2012 · 3 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Sep 6, 2012

Scala incorrectly optimizes away the if statement (without -optimize) in the following:

scala> val x = if(error("x")) 0d else -0d
x: Double = 0.0

scala> val x = if(error("x")) 1d else 1d
x: Double = 1.0

The main problem is that although 0d == -0d is true, it is wrong to treat them as the same. It seems plausible this kind of constant equality check might be applied elsewhere as well.

Second, the side-effect in the if statement is discarded, although I don't know if there is a scenario where this could possibly matter.

@scabug
Copy link
Author

scabug commented Sep 6, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6331?orig=1
Reporter: @harrah
Affected Versions: 2.10.0-M7
Other Milestones: 2.10.0

@scabug
Copy link
Author

scabug commented Sep 7, 2012

@retronym said (edited on Sep 7, 2012 3:11:13 PM UTC):
typedIf lubs the branches of the conditional:

https://github.com/scala/scala/blob/2.10.x/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L4072

adapt replaces <expr>: ConstantType\(x) with x

https://github.com/scala/scala/blob/2.10.x/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L1067

Probable fixes:

  1. call .deconst before lubbing the types of the branches; make sure treatment is consistent in other places lubs are taken. (Or maybe: Do this in lub/glb itself)
  2. Update Const#equals to differentiate +-0d (it already has similar handling for NaN). I think we can use Double.doubleToRawLongBits(this.value) == Double.doubleToRawLongBits(that.value) (and the equivalent for Float) to handle all the floating point esoterica correctly.

@scabug
Copy link
Author

scabug commented Sep 8, 2012

@retronym said:
scala/scala#1271

My last comment wasn't accurate: deconst was already called by ptOrLub. The real problem was a new fast path that avoids lubbing, which was added by virtpatmat.

@scabug scabug closed this as completed Oct 1, 2012
@scabug scabug added the backend label Apr 7, 2017
@scabug scabug added this to the 2.10.0-M7 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants