Skip to content

Commit 9907ad6

Browse files
Define UB in float-to-int casts to saturate
- Round to zero, and representable values cast directly. - `NaN` goes to 0 - Values beyond the limits of the type are saturated to the "nearest value" (essentially rounding to zero, in some sense) in the integral type, so e.g. `f32::INFINITY` would go to `{u,i}N::MAX.`
1 parent a0c61a9 commit 9907ad6

File tree

5 files changed

+10
-11
lines changed

5 files changed

+10
-11
lines changed

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ fn cast_float_to_int<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
768768
) -> Bx::Value {
769769
let fptosui_result = if signed { bx.fptosi(x, int_ty) } else { bx.fptoui(x, int_ty) };
770770

771-
if !bx.cx().sess().opts.debugging_opts.saturating_float_casts {
771+
if let Some(false) = bx.cx().sess().opts.debugging_opts.saturating_float_casts {
772772
return fptosui_result;
773773
}
774774

src/librustc_interface/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ fn test_debugging_options_tracking_hash() {
558558
tracked!(sanitizer, Some(Sanitizer::Address));
559559
tracked!(sanitizer_memory_track_origins, 2);
560560
tracked!(sanitizer_recover, vec![Sanitizer::Address]);
561-
tracked!(saturating_float_casts, true);
561+
tracked!(saturating_float_casts, Some(true));
562562
tracked!(share_generics, Some(true));
563563
tracked!(show_span, Some(String::from("abc")));
564564
tracked!(src_hash_algorithm, Some(SourceFileHashAlgorithm::Sha1));

src/librustc_session/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,9 +936,9 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
936936
"enable origins tracking in MemorySanitizer"),
937937
sanitizer_recover: Vec<Sanitizer> = (vec![], parse_sanitizer_list, [TRACKED],
938938
"enable recovery for selected sanitizers"),
939-
saturating_float_casts: bool = (false, parse_bool, [TRACKED],
939+
saturating_float_casts: Option<bool> = (None, parse_opt_bool, [TRACKED],
940940
"make float->int casts UB-free: numbers outside the integer type's range are clipped to \
941-
the max/min integer respectively, and NaN is mapped to 0 (default: no)"),
941+
the max/min integer respectively, and NaN is mapped to 0 (default: yes)"),
942942
save_analysis: bool = (false, parse_bool, [UNTRACKED],
943943
"write syntax and type analysis (in JSON format) information, in \
944944
addition to normal output (default: no)"),
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// compile-flags: -C no-prepopulate-passes
1+
// This file tests that we don't generate any code for saturation when using the
2+
// unchecked intrinsics.
23

3-
// This file tests that we don't generate any code for saturation if
4-
// -Z saturating-float-casts is not enabled.
4+
// compile-flags: -C opt-level=3
55

66
#![crate_type = "lib"]
77

@@ -12,7 +12,7 @@ pub fn f32_to_u32(x: f32) -> u32 {
1212
// CHECK-NOT: fcmp
1313
// CHECK-NOT: icmp
1414
// CHECK-NOT: select
15-
x as u32
15+
unsafe { x.to_int_unchecked() }
1616
}
1717

1818
// CHECK-LABEL: @f32_to_i32
@@ -22,7 +22,7 @@ pub fn f32_to_i32(x: f32) -> i32 {
2222
// CHECK-NOT: fcmp
2323
// CHECK-NOT: icmp
2424
// CHECK-NOT: select
25-
x as i32
25+
unsafe { x.to_int_unchecked() }
2626
}
2727

2828
#[no_mangle]
@@ -31,5 +31,5 @@ pub fn f64_to_u16(x: f64) -> u16 {
3131
// CHECK-NOT: fcmp
3232
// CHECK-NOT: icmp
3333
// CHECK-NOT: select
34-
x as u16
34+
unsafe { x.to_int_unchecked() }
3535
}

src/test/ui/numbers-arithmetic/saturating-float-casts.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-pass
22
// Tests saturating float->int casts. See u128-as-f32.rs for the opposite direction.
3-
// compile-flags: -Z saturating-float-casts
43

54
#![feature(test, stmt_expr_attributes)]
65
#![deny(overflowing_literals)]

0 commit comments

Comments
 (0)