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

Casting a known NaN value to an integral type #371

Closed
llogiq opened this issue Oct 8, 2015 · 3 comments · Fixed by #9617
Closed

Casting a known NaN value to an integral type #371

llogiq opened this issue Oct 8, 2015 · 3 comments · Fixed by #9617
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types

Comments

@llogiq
Copy link
Contributor

llogiq commented Oct 8, 2015

This is actually undefined behavior, and doesn't even get a warning from rustc.

@Pyriphlegethon
Copy link
Contributor

Is there some list of expressions that can return NaN? The only example I found in that reddit thread was 0.0/0.0.

@llogiq
Copy link
Contributor Author

llogiq commented Oct 9, 2015

As far as I know, 0.0/0.0, -0/0, 0/-0, -0/-0 and just about every float operation that has at least one NaN operand, e.g.

let z = foo();
let x = 0/-0;
let y = x + 1 * z - 3;
y as isize; //~ERROR: Casting a known NaN value to an integral type.

So this involves keeping track of known NaNs. It's similar to what consts does (complete with path chasing), but we don't need the whole expression to be constant, one NaN is sufficient.

@llogiq llogiq added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types A-lint Area: New lints labels Oct 10, 2015
@Ryan1729
Copy link
Contributor

As of 1.45, This is no longer UB and NaN in particular is sent to 0.

Might still be worth linting, though, given it's still likely to be a bug, and older version of rust will still see use for a while yet.

This was referenced Oct 8, 2022
@bors bors closed this as completed in b8a9a50 Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants