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

ICE when casting float to integer (out-of-bounds) #45134

Closed
samnardoni opened this issue Oct 9, 2017 · 3 comments · Fixed by #45205
Closed

ICE when casting float to integer (out-of-bounds) #45134

samnardoni opened this issue Oct 9, 2017 · 3 comments · Fixed by #45205
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@samnardoni
Copy link

The following program (with perhaps undefined behaviour) causes an ICE:

fn ice() {
    println!("{:?}", Some(257.0f32 as u8));
}

Interestingly, if not wrapping the result in a Some, then there's no ICE.

fn no_ice() {
    println!("{:?}", 257.0f32 as u8);
}

Here's the result from running it on the playground:

   Compiling playground v0.0.1 (file:///playground)
error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.20.0 (f3d6973f4 2017-08-27) running on x86_64-unknown-linux-gnu

thread 'rustc' panicked at 'assertion failed: !is_undef(val)', /checkout/src/librustc_trans/mir/constant.rs:1083:8
note: Run with `RUST_BACKTRACE=1` for a backtrace.

error: Could not compile `playground`.

To learn more, run the command again with --verbose.
@scottmcm
Copy link
Member

scottmcm commented Oct 9, 2017

Looks like #10184 (which is approaching its fourth birthday)

@hanna-kruppe
Copy link
Contributor

Same underlying issue, but a different code path than the one that's been exclusively (as far as I can tell) discussed over there. So this report was very valuable for my in-progress patch addressing #10184, thank you @samnardoni!

@jdm jdm added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Oct 10, 2017
@samnardoni
Copy link
Author

@rkruppe No problem at all. I'm glad it's useful.

@TimNN TimNN added the C-bug Category: This is a bug. label Oct 10, 2017
bors added a commit that referenced this issue Nov 8, 2017
Saturating casts between integers and floats

Introduces a new flag, `-Z saturating-float-casts`, which makes code generation for int->float and float->int casts safe (`undef`-free), implementing [the saturating semantics laid out by](#10184 (comment)) @jorendorff for float->int casts and overflowing to infinity for `u128::MAX` -> `f32`.
Constant evaluation in trans was changed to behave like HIR const eval already did, i.e., saturate for u128->f32 and report an error for problematic float->int casts.

Many thanks to @eddyb, whose APFloat port simplified many parts of this patch, and made HIR constant evaluation recognize dangerous float casts as mentioned above.
Also thanks to @ActuallyaDeviloper whose branchless implementation served as inspiration for this implementation.

cc #10184 #41799
fixes #45134
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants