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

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

Closed
toddaaro opened this issue Jun 4, 2013 · 2 comments

Comments

@toddaaro
Copy link
Contributor

toddaaro commented Jun 4, 2013

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)

@Aatch
Copy link
Contributor

Aatch commented Jun 5, 2013

We don't do conversion verification we do simple checking that a literal won't overflow.

With that in mind, I don't think this is something we'll fix, since it would require arbitrary precision numbers at compile time or the constant folder to be much more complex.

Closing as WONTFIX. Reopen if you disagree.

@Aatch Aatch closed this as completed Jun 5, 2013
@thestinger
Copy link
Contributor

Casting as float is an explicit opt-in to coercion through the float type. It would be strange if it acted differently than what you asked for.

flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 8, 2021
…1995

Fix all occurences `needless_borrow` internally

The bug that got 'needless_borrow' moved into the nursery was fixed two years ago in d4370f8.

This did trigger over a thousand times internally, so that's all the other changes. I vetted most of them, but there's a lot The only interesting change is to the lint list. `declare_tool_lint` already makes a reference, so there's no need to take a reference to the lints.

changelog: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants