Skip to content

Integer -> Float -> Integer conversion can succeed while changing what the value is #6931

Closed
@toddaaro

Description

@toddaaro

This code works well, despite doing a conversion of an integer constant to a float and then back to an integer the fact that the integer is too large triggers an error.

    let bar: int = (120120120120120120120120 as float) as int;
$ rustc prims.rs
prims.rs:10:19: 10:20 error: int literal is too large
prims.rs:10     let bar: int = (120120120120120120120120 as float) as int;

Now in this example the number is shorter, and it does not produce an error. But the printed value isn't the same as the original number.

let bar: int = (120120120120120120 as float) as int;
println(fmt!("%?", bar));
120120120120120128

It looks like the first int -> float conversion resulted in some kind of information loss due to floating point precision/representation issues, and this loss was propogated when the value was converted back to an int.

It seems like there already is constant conversion verification, so it likely could be extended to deal with floating point issues like this.

(tested using the rust trunk)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions