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

Inconsistent null equality check for primitive types #9671

Closed
scabug opened this issue Feb 23, 2016 · 6 comments
Closed

Inconsistent null equality check for primitive types #9671

scabug opened this issue Feb 23, 2016 · 6 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Feb 23, 2016

scala> null.asInstanceOf[Double]
res0: Double = 0.0

scala> null.asInstanceOf[Double] == null
<console>:11: warning: comparing values of types Double and Null using `==' will always yield false
       null.asInstanceOf[Double] == null
                                 ^
res1: Boolean = true

scala> 0.0 == null
<console>:11: warning: comparing values of types Double and Null using `==' will always yield false
       0.0 == null
           ^
res2: Boolean = false

scala> null.asInstanceOf[Double] == 0.0
res6: Boolean = true

scala> val a = null.asInstanceOf[Double]
a: Double = 0.0

scala> a == null
<console>:12: warning: comparing values of types Double and Null using `==' will always yield false
       a == null
         ^
res7: Boolean = false

Same for Int and Long.

At least, the warning message seems incorrect, regardless that the boxing/unboxing logic might be fine (?)

@scabug
Copy link
Author

@scabug scabug commented Feb 23, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9671?orig=1
Reporter: Dmytro Kondratiuk (dk14)
Affected Versions: 2.11.7
See #602

@scabug
Copy link
Author

@scabug scabug commented Feb 23, 2016

Dmytro Kondratiuk (dk14) said:
null.asInstanceOf[Double] == null compiles to the:

aconst_null
ifnonnull
The val-version compiles to the:

aconst_null
invokestatic unboxToDouble
putfield
aload_0
invokevirtual а
invokestatic boxToDouble
ifnonnull

So compiler just forgets to add unbox/box in the first case

@scabug
Copy link
Author

@scabug scabug commented Apr 4, 2016

@szeiger said:
This looks like more than just a problem with the warning message to me. I don't see why res1 and res7 should be different. Assigning to M5 because changing this has the potential to break code.

@scabug
Copy link
Author

@scabug scabug commented May 18, 2016

@lrytz said:
regressed here: scala/scala@60f1b4b

@scabug
Copy link
Author

@scabug scabug commented May 18, 2016

@lrytz said:
in short

def f1(a: Any) = "" + a
f1(null.asInstanceOf[Int])

first replaces the cast by an unbox. then it introduces a box to pass the argument to f1. the box(unbox(t)) is replaced by t, but should not

@scabug
Copy link
Author

@scabug scabug commented May 18, 2016

@scabug scabug closed this May 19, 2016
@scabug scabug added the quickfix label Apr 7, 2017
@scabug scabug added this to the 2.12.0-M5 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants