Skip to content

malformed number literal 0x7FFF_FFFF : Float #8398

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

Closed
som-snytt opened this issue Feb 27, 2020 · 2 comments
Closed

malformed number literal 0x7FFF_FFFF : Float #8398

som-snytt opened this issue Feb 27, 2020 · 2 comments

Comments

@som-snytt
Copy link
Contributor

minimized code

Dotty compiler version 0.22.0-RC1 -- Copyright 2002-2020, LAMP/EPFL
scala> 0x7FFF_FFFF
val res0: Int = 2147483647

scala> 0x7FFF_FFFF : Float
1 |0x7FFF_FFFF : Float
  |^^^^^^^^^^^
  |malformed number literal

scala> Seq(3.14f, 0x7FFF_FFFF)
val res1: Seq[Float] = List(3.14, 2.14748365E9)

scala> Seq(3.14f, 0x7FFF_FFFE)                                                                                                                    
val res2: Seq[AnyVal] = List(3.14, 2147483646)

scala> Array(3.14f, 0x7FFF_FFFE)                                                                                                                  
val res3: Array[Float] = Array(3.14, 2.14748365E9)

scala> 2147483647 : Float                                                                                                                         
val res4: Float = 2.14748365E9

Compilation output

per transcript

expectation

0x7FFF_FFFF : Float is the same as 2147483647 : Float.

Would be nice to warn or indicate that Array(3.14f, 0x7FFF_FFFE) is inferred Float element, so that the int literal is lossy. Alternatively, infer boxed AnyVal, which is not intended but will catch the eye of a linter.

Similarly, Seq(3.14f, 0x7FFF_FFFE): Seq[Float] is tricky if you're innocently passing the arg to a method f(xs: Seq[Float]).

Also repeating parameter works as above:

scala> def sumall(xs: Float*) = xs.sum
def sumall(xs: Float*): Float

scala> sumall(3.14f, 0x7FFF_FFFF)                                                                                                                 
1 |sumall(3.14f, 0x7FFF_FFFF)
  |              ^^^^^^^^^^^
  |              malformed number literal

I have to do more cleaning, probably I ran .22 and also .23.

scala> sumall(3.14f, 0x7FFF_FFFF)
error while loading test$,
class file f/test.class is broken, reading aborted with class dotty.tools.tasty.UnpickleException
TASTy signature has wrong version.
 expected: 19.0
 found   : 18.0
error while loading C$,
class file f/C.class is broken, reading aborted with class dotty.tools.tasty.UnpickleException
TASTy signature has wrong version.
 expected: 19.0
 found   : 18.0
1 |sumall(3.14f, 0x7FFF_FFFF)
  |       ^^^^^^^^^^^^^^^^^^
  |       Found:    (Float, Int)
  |       Required: Seq[Float]
@bishabosha
Copy link
Member

bishabosha commented Feb 28, 2020

so what's happening here is the ascription to float then causes the compiler to call FromDigits.floatFromDigits("0x7FFF_FFFF") to inline the constant, which delegates to java.lang.Float.parseFloat("0x7FFF_FFFF")

@bishabosha
Copy link
Member

to get the original behaviour where implicit conversions take over, the following should catch errors and fall through to the default
https://github.com/lampepfl/dotty/blob/63343d281e44d29e9db6a895d9ccf3c0daaa9cce/compiler/src/dotty/tools/dotc/typer/Typer.scala#L561

odersky added a commit that referenced this issue Feb 29, 2020
fix #8398 - dont parse hex literals as floating point
robstoll pushed a commit to tegonal/dotty that referenced this issue Mar 2, 2020
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