diff options
| author | bors <bors@rust-lang.org> | 2020-07-02 23:18:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-07-02 23:18:15 +0000 |
| commit | 5f4abc16e1b2cb035eee6a5079ce45ce924c1f33 (patch) | |
| tree | 327e15fe3a91f01c5b4cf23b2a31366b384e97bd /src/test/ui | |
| parent | 3503f565e1fb7296983757d2716346f48a4a262b (diff) | |
| parent | 7bd2f97cb61d4578ac55d54bfdfc7c4ade752374 (diff) | |
Auto merge of #73977 - Manishearth:rollup-2x4s7c6, r=Manishearth
Rollup of 8 pull requests Successful merges: - #73454 (Move contributing.md to rustc-dev-guide and point at getting started) - #73724 (Use WASM's saturating casts if they are available) - #73726 (resolve: disallow labelled breaks/continues through closures/async blocks) - #73753 (Use 'tcx for references to AccessLevels wherever possible.) - #73781 (Update psm version) - #73952 (Add option for local docker testing.) - #73957 (disable BTree min_max test in Miri for now) - #73975 (Document rustc_ast::ast::Pat) Failed merges: r? @ghost
Diffstat (limited to 'src/test/ui')
22 files changed, 728 insertions, 529 deletions
diff --git a/src/test/ui/break-outside-loop.rs b/src/test/ui/break-outside-loop.rs index a6f9d0423d0..26769b30dd5 100644 --- a/src/test/ui/break-outside-loop.rs +++ b/src/test/ui/break-outside-loop.rs @@ -27,7 +27,9 @@ fn main() { // not the `loop`, which failed in the call to `find_breakable`. (#65383) 'lab: loop { || { - break 'lab; //~ ERROR `break` inside of a closure + break 'lab; + //~^ ERROR use of unreachable label `'lab` + //~| ERROR `break` inside of a closure }; } } diff --git a/src/test/ui/break-outside-loop.stderr b/src/test/ui/break-outside-loop.stderr index 8e300fd848d..287bf9af62e 100644 --- a/src/test/ui/break-outside-loop.stderr +++ b/src/test/ui/break-outside-loop.stderr @@ -1,3 +1,14 @@ +error[E0767]: use of unreachable label `'lab` + --> $DIR/break-outside-loop.rs:30:19 + | +LL | 'lab: loop { + | ---- unreachable label defined here +LL | || { +LL | break 'lab; + | ^^^^ unreachable label `'lab` + | + = note: labels are unreachable through functions, closures, async blocks and modules + error[E0268]: `break` outside of a loop --> $DIR/break-outside-loop.rs:10:15 | @@ -41,7 +52,7 @@ LL | || { LL | break 'lab; | ^^^^^^^^^^ cannot `break` inside of a closure -error: aborting due to 6 previous errors +error: aborting due to 7 previous errors -Some errors have detailed explanations: E0267, E0268. +Some errors have detailed explanations: E0267, E0268, E0767. For more information about an error, try `rustc --explain E0267`. diff --git a/src/test/ui/error-codes/E0767.rs b/src/test/ui/error-codes/E0767.rs new file mode 100644 index 00000000000..6c6cb746e6c --- /dev/null +++ b/src/test/ui/error-codes/E0767.rs @@ -0,0 +1,7 @@ +fn main () { + 'a: loop { + || { + loop { break 'a; } //~ ERROR E0767 + } + } +} diff --git a/src/test/ui/error-codes/E0767.stderr b/src/test/ui/error-codes/E0767.stderr new file mode 100644 index 00000000000..2429823306b --- /dev/null +++ b/src/test/ui/error-codes/E0767.stderr @@ -0,0 +1,14 @@ +error[E0767]: use of unreachable label `'a` + --> $DIR/E0767.rs:4:26 + | +LL | 'a: loop { + | -- unreachable label defined here +LL | || { +LL | loop { break 'a; } + | ^^ unreachable label `'a` + | + = note: labels are unreachable through functions, closures, async blocks and modules + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0767`. diff --git a/src/test/ui/issues/issue-62480.rs b/src/test/ui/issues/issue-62480.rs index bc3e6c69a60..5c3be3e64ee 100644 --- a/src/test/ui/issues/issue-62480.rs +++ b/src/test/ui/issues/issue-62480.rs @@ -5,6 +5,8 @@ fn main() { // `propagate_through_expr` would be the closure and not the `loop`, which wouldn't be found in // `self.break_ln`. (#62480) 'a: { - || break 'a //~ ERROR `break` to unknown label + || break 'a + //~^ ERROR use of unreachable label `'a` + //~| ERROR `break` inside of a closure } } diff --git a/src/test/ui/issues/issue-62480.stderr b/src/test/ui/issues/issue-62480.stderr index de8451ad7df..17085ef908b 100644 --- a/src/test/ui/issues/issue-62480.stderr +++ b/src/test/ui/issues/issue-62480.stderr @@ -1,8 +1,22 @@ -error: `break` to unknown label +error[E0767]: use of unreachable label `'a` + --> $DIR/issue-62480.rs:8:18 + | +LL | 'a: { + | -- unreachable label defined here +LL | || break 'a + | ^^ unreachable label `'a` + | + = note: labels are unreachable through functions, closures, async blocks and modules + +error[E0267]: `break` inside of a closure --> $DIR/issue-62480.rs:8:12 | LL | || break 'a - | ^^^^^^^^ + | -- ^^^^^^^^ cannot `break` inside of a closure + | | + | enclosing closure -error: aborting due to previous error +error: aborting due to 2 previous errors +Some errors have detailed explanations: E0267, E0767. +For more information about an error, try `rustc --explain E0267`. diff --git a/src/test/ui/issues/issue-66702-break-outside-loop-val.rs b/src/test/ui/issues/issue-66702-break-outside-loop-val.rs index bd3c00d2621..05b86cbbf8f 100644 --- a/src/test/ui/issues/issue-66702-break-outside-loop-val.rs +++ b/src/test/ui/issues/issue-66702-break-outside-loop-val.rs @@ -2,6 +2,8 @@ fn main() { 'some_label: loop { - || break 'some_label (); //~ ERROR: `break` inside of a closure + || break 'some_label (); + //~^ ERROR: use of unreachable label `'some_label` + //~| ERROR: `break` inside of a closure } } diff --git a/src/test/ui/issues/issue-66702-break-outside-loop-val.stderr b/src/test/ui/issues/issue-66702-break-outside-loop-val.stderr index 83bde9775b2..f92ede311af 100644 --- a/src/test/ui/issues/issue-66702-break-outside-loop-val.stderr +++ b/src/test/ui/issues/issue-66702-break-outside-loop-val.stderr @@ -1,3 +1,13 @@ +error[E0767]: use of unreachable label `'some_label` + --> $DIR/issue-66702-break-outside-loop-val.rs:5:18 + | +LL | 'some_label: loop { + | ----------- unreachable label defined here +LL | || break 'some_label (); + | ^^^^^^^^^^^ unreachable label `'some_label` + | + = note: labels are unreachable through functions, closures, async blocks and modules + error[E0267]: `break` inside of a closure --> $DIR/issue-66702-break-outside-loop-val.rs:5:12 | @@ -6,6 +16,7 @@ LL | || break 'some_label (); | | | enclosing closure -error: aborting due to previous error +error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0267`. +Some errors have detailed explanations: E0267, E0767. +For more information about an error, try `rustc --explain E0267`. diff --git a/src/test/ui/issues/issue-73541-1.rs b/src/test/ui/issues/issue-73541-1.rs new file mode 100644 index 00000000000..7fb0d6c39ff --- /dev/null +++ b/src/test/ui/issues/issue-73541-1.rs @@ -0,0 +1,12 @@ +// edition:2018 + +fn main() { + 'a: loop { + async { + loop { + continue 'a + //~^ ERROR use of unreachable label `'a` + } + }; + } +} diff --git a/src/test/ui/issues/issue-73541-1.stderr b/src/test/ui/issues/issue-73541-1.stderr new file mode 100644 index 00000000000..80c1fdf002a --- /dev/null +++ b/src/test/ui/issues/issue-73541-1.stderr @@ -0,0 +1,14 @@ +error[E0767]: use of unreachable label `'a` + --> $DIR/issue-73541-1.rs:7:26 + | +LL | 'a: loop { + | -- unreachable label defined here +... +LL | continue 'a + | ^^ unreachable label `'a` + | + = note: labels are unreachable through functions, closures, async blocks and modules + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0767`. diff --git a/src/test/ui/issues/issue-73541-2.rs b/src/test/ui/issues/issue-73541-2.rs new file mode 100644 index 00000000000..70b4ab25376 --- /dev/null +++ b/src/test/ui/issues/issue-73541-2.rs @@ -0,0 +1,20 @@ +// edition:2018 + +async fn c() { + 'a: loop { + macro_rules! b { + () => { + continue 'a + //~^ ERROR use of unreachable label `'a` + } + } + + async { + loop { + b!(); + } + }; + } +} + +fn main() { } diff --git a/src/test/ui/issues/issue-73541-2.stderr b/src/test/ui/issues/issue-73541-2.stderr new file mode 100644 index 00000000000..75d769c4e15 --- /dev/null +++ b/src/test/ui/issues/issue-73541-2.stderr @@ -0,0 +1,18 @@ +error[E0767]: use of unreachable label `'a` + --> $DIR/issue-73541-2.rs:7:26 + | +LL | 'a: loop { + | -- unreachable label defined here +... +LL | continue 'a + | ^^ unreachable label `'a` +... +LL | b!(); + | ----- in this macro invocation + | + = note: labels are unreachable through functions, closures, async blocks and modules + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0767`. diff --git a/src/test/ui/issues/issue-73541-3.rs b/src/test/ui/issues/issue-73541-3.rs new file mode 100644 index 00000000000..02ca02da8ed --- /dev/null +++ b/src/test/ui/issues/issue-73541-3.rs @@ -0,0 +1,9 @@ +fn main() { + 'aaaaab: loop { + || { + loop { continue 'aaaaaa } + //~^ ERROR use of undeclared label `'aaaaaa` + }; + + } +} diff --git a/src/test/ui/issues/issue-73541-3.stderr b/src/test/ui/issues/issue-73541-3.stderr new file mode 100644 index 00000000000..53487aaca99 --- /dev/null +++ b/src/test/ui/issues/issue-73541-3.stderr @@ -0,0 +1,12 @@ +error[E0426]: use of undeclared label `'aaaaaa` + --> $DIR/issue-73541-3.rs:4:29 + | +LL | 'aaaaab: loop { + | ------- a label with a similar name exists but is unreachable +LL | || { +LL | loop { continue 'aaaaaa } + | ^^^^^^^ undeclared label `'aaaaaa` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0426`. diff --git a/src/test/ui/issues/issue-73541.rs b/src/test/ui/issues/issue-73541.rs new file mode 100644 index 00000000000..399a07cd3fc --- /dev/null +++ b/src/test/ui/issues/issue-73541.rs @@ -0,0 +1,9 @@ +fn main() { + 'a: loop { + || { + loop { continue 'a } + //~^ ERROR use of unreachable label `'a` + }; + + } +} diff --git a/src/test/ui/issues/issue-73541.stderr b/src/test/ui/issues/issue-73541.stderr new file mode 100644 index 00000000000..4bb466ff16c --- /dev/null +++ b/src/test/ui/issues/issue-73541.stderr @@ -0,0 +1,14 @@ +error[E0767]: use of unreachable label `'a` + --> $DIR/issue-73541.rs:4:29 + | +LL | 'a: loop { + | -- unreachable label defined here +LL | || { +LL | loop { continue 'a } + | ^^ unreachable label `'a` + | + = note: labels are unreachable through functions, closures, async blocks and modules + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0767`. diff --git a/src/test/ui/numbers-arithmetic/saturating-float-casts-impl.rs b/src/test/ui/numbers-arithmetic/saturating-float-casts-impl.rs new file mode 100644 index 00000000000..6c3e503693c --- /dev/null +++ b/src/test/ui/numbers-arithmetic/saturating-float-casts-impl.rs @@ -0,0 +1,507 @@ +// ignore-test + +// Tests saturating float->int casts. See u128-as-f32.rs for the opposite direction. +// +// Some of these tests come from a similar file in miri, +// tests/run-pass/float.rs. Individual test cases are potentially duplicated +// with the previously existing tests, but since this runs so quickly anyway, +// we're not spending the time to figure out exactly which ones should be +// merged. + +extern crate test; + +use self::test::black_box; +use std::{f32, f64}; +#[cfg(not(target_os = "emscripten"))] +use std::{i128, u128}; +use std::{i16, i32, i64, i8, u16, u32, u64, u8}; + +macro_rules! test { + ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ( + // black_box disables constant evaluation to test run-time conversions: + assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected, + "run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty)); + + { + const X: $src_ty = $val; + const Y: $dest_ty = X as $dest_ty; + assert_eq!(Y, $expected, + "const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty)); + } + ); + + ($fval:expr, f* -> $ity:ident, $ival:expr) => ( + test!($fval, f32 -> $ity, $ival); + test!($fval, f64 -> $ity, $ival); + ) +} + +macro_rules! common_fptoi_tests { + ($fty:ident -> $($ity:ident)+) => ({ $( + test!($fty::NAN, $fty -> $ity, 0); + test!($fty::INFINITY, $fty -> $ity, $ity::MAX); + test!($fty::NEG_INFINITY, $fty -> $ity, $ity::MIN); + // These two tests are not solely float->int tests, in particular the latter relies on + // `u128::MAX as f32` not being UB. But that's okay, since this file tests int->float + // as well, the test is just slightly misplaced. + test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN); + test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX); + test!(0., $fty -> $ity, 0); + test!($fty::MIN_POSITIVE, $fty -> $ity, 0); + test!(-0.9, $fty -> $ity, 0); + test!(1., $fty -> $ity, 1); + test!(42., $fty -> $ity, 42); + )+ }); + + (f* -> $($ity:ident)+) => ({ + common_fptoi_tests!(f32 -> $($ity)+); + common_fptoi_tests!(f64 -> $($ity)+); + }) +} + +macro_rules! fptoui_tests { + ($fty: ident -> $($ity: ident)+) => ({ $( + test!(-0., $fty -> $ity, 0); + test!(-$fty::MIN_POSITIVE, $fty -> $ity, 0); + test!(-0.99999994, $fty -> $ity, 0); + test!(-1., $fty -> $ity, 0); + test!(-100., $fty -> $ity, 0); + test!(#[allow(overflowing_literals)] -1e50, $fty -> $ity, 0); + test!(#[allow(overflowing_literals)] -1e130, $fty -> $ity, 0); + )+ }); + + (f* -> $($ity:ident)+) => ({ + fptoui_tests!(f32 -> $($ity)+); + fptoui_tests!(f64 -> $($ity)+); + }) +} + +use std::fmt::Debug; + +// Helper function to avoid promotion so that this tests "run-time" casts, not CTFE. +#[track_caller] +#[inline(never)] +fn assert_eq<T: PartialEq + Debug>(x: T, y: T) { + assert_eq!(x, y); +} + +trait FloatToInt<Int>: Copy { + fn cast(self) -> Int; + unsafe fn cast_unchecked(self) -> Int; +} + +impl FloatToInt<i8> for f32 { + fn cast(self) -> i8 { + self as _ + } + unsafe fn cast_unchecked(self) -> i8 { + self.to_int_unchecked() + } +} +impl FloatToInt<i32> for f32 { + fn cast(self) -> i32 { + self as _ + } + unsafe fn cast_unchecked(self) -> i32 { + self.to_int_unchecked() + } +} +impl FloatToInt<u32> for f32 { + fn cast(self) -> u32 { + self as _ + } + unsafe fn cast_unchecked(self) -> u32 { + self.to_int_unchecked() + } +} +impl FloatToInt<i64> for f32 { + fn cast(self) -> i64 { + self as _ + } + unsafe fn cast_unchecked(self) -> i64 { + self.to_int_unchecked() + } +} +impl FloatToInt<u64> for f32 { + fn cast(self) -> u64 { + self as _ + } + unsafe fn cast_unchecked(self) -> u64 { + self.to_int_unchecked() + } +} + +impl FloatToInt<i8> for f64 { + fn cast(self) -> i8 { + self as _ + } + unsafe fn cast_unchecked(self) -> i8 { + self.to_int_unchecked() + } +} +impl FloatToInt<i32> for f64 { + fn cast(self) -> i32 { + self as _ + } + unsafe fn cast_unchecked(self) -> i32 { + self.to_int_unchecked() + } +} +impl FloatToInt<u32> for f64 { + fn cast(self) -> u32 { + self as _ + } + unsafe fn cast_unchecked(self) -> u32 { + self.to_int_unchecked() + } +} +impl FloatToInt<i64> for f64 { + fn cast(self) -> i64 { + self as _ + } + unsafe fn cast_unchecked(self) -> i64 { + self.to_int_unchecked() + } +} +impl FloatToInt<u64> for f64 { + fn cast(self) -> u64 { + self as _ + } + unsafe fn cast_unchecked(self) -> u64 { + self.to_int_unchecked() + } +} +// FIXME emscripten does not support i128 +#[cfg(not(target_os = "emscripten"))] +impl FloatToInt<i128> for f64 { + fn cast(self) -> i128 { + self as _ + } + unsafe fn cast_unchecked(self) -> i128 { + self.to_int_unchecked() + } +} +// FIXME emscripten does not support i128 +#[cfg(not(target_os = "emscripten"))] +impl FloatToInt<u128> for f64 { + fn cast(self) -> u128 { + self as _ + } + unsafe fn cast_unchecked(self) -> u128 { + self.to_int_unchecked() + } +} + +/// Test this cast both via `as` and via `to_int_unchecked` (i.e., it must not saturate). +#[track_caller] +#[inline(never)] +fn test_both_cast<F, I>(x: F, y: I) +where + F: FloatToInt<I>, + I: PartialEq + Debug, +{ + assert_eq!(x.cast(), y); + assert_eq!(unsafe { x.cast_unchecked() }, y); +} + +fn casts() { + // f32 -> i8 + test_both_cast::<f32, i8>(127.99, 127); + test_both_cast::<f32, i8>(-128.99, -128); + + // f32 -> i32 + test_both_cast::<f32, i32>(0.0, 0); + test_both_cast::<f32, i32>(-0.0, 0); + test_both_cast::<f32, i32>(/*0x1p-149*/ f32::from_bits(0x00000001), 0); + test_both_cast::<f32, i32>(/*-0x1p-149*/ f32::from_bits(0x80000001), 0); + test_both_cast::<f32, i32>(/*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd), 1); + test_both_cast::<f32, i32>(/*-0x1.19999ap+0*/ f32::from_bits(0xbf8ccccd), -1); + test_both_cast::<f32, i32>(1.9, 1); + test_both_cast::<f32, i32>(-1.9, -1); + test_both_cast::<f32, i32>(5.0, 5); + test_both_cast::<f32, i32>(-5.0, -5); + test_both_cast::<f32, i32>(2147483520.0, 2147483520); + test_both_cast::<f32, i32>(-2147483648.0, -2147483648); + // unrepresentable casts + assert_eq::<i32>(2147483648.0f32 as i32, i32::MAX); + assert_eq::<i32>(-2147483904.0f32 as i32, i32::MIN); + assert_eq::<i32>(f32::MAX as i32, i32::MAX); + assert_eq::<i32>(f32::MIN as i32, i32::MIN); + assert_eq::<i32>(f32::INFINITY as i32, i32::MAX); + assert_eq::<i32>(f32::NEG_INFINITY as i32, i32::MIN); + assert_eq::<i32>(f32::NAN as i32, 0); + assert_eq::<i32>((-f32::NAN) as i32, 0); + + // f32 -> u32 + test_both_cast::<f32, u32>(0.0, 0); + test_both_cast::<f32, u32>(-0.0, 0); + test_both_cast::<f32, u32>(-0.9999999, 0); + test_both_cast::<f32, u32>(/*0x1p-149*/ f32::from_bits(0x1), 0); + test_both_cast::<f32, u32>(/*-0x1p-149*/ f32::from_bits(0x80000001), 0); + test_both_cast::<f32, u32>(/*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd), 1); + test_both_cast::<f32, u32>(1.9, 1); + test_both_cast::<f32, u32>(5.0, 5); + test_both_cast::<f32, u32>(2147483648.0, 0x8000_0000); + test_both_cast::<f32, u32>(4294967040.0, 0u32.wrapping_sub(256)); + test_both_cast::<f32, u32>(/*-0x1.ccccccp-1*/ f32::from_bits(0xbf666666), 0); + test_both_cast::<f32, u32>(/*-0x1.fffffep-1*/ f32::from_bits(0xbf7fffff), 0); + test_both_cast::<f32, u32>((u32::MAX - 128) as f32, u32::MAX - 255); // rounding loss + + // unrepresentable casts: + + // rounds up and then becomes unrepresentable + assert_eq::<u32>((u32::MAX - 127) as f32 as u32, u32::MAX); + + assert_eq::<u32>(4294967296.0f32 as u32, u32::MAX); + assert_eq::<u32>(-5.0f32 as u32, 0); + assert_eq::<u32>(f32::MAX as u32, u32::MAX); + assert_eq::<u32>(f32::MIN as u32, 0); + assert_eq::<u32>(f32::INFINITY as u32, u32::MAX); + assert_eq::<u32>(f32::NEG_INFINITY as u32, 0); + assert_eq::<u32>(f32::NAN as u32, 0); + assert_eq::<u32>((-f32::NAN) as u32, 0); + + // f32 -> i64 + test_both_cast::<f32, i64>(4294967296.0, 4294967296); + test_both_cast::<f32, i64>(-4294967296.0, -4294967296); + test_both_cast::<f32, i64>(9223371487098961920.0, 9223371487098961920); + test_both_cast::<f32, i64>(-9223372036854775808.0, -9223372036854775808); + + // f64 -> i8 + test_both_cast::<f64, i8>(127.99, 127); + test_both_cast::<f64, i8>(-128.99, -128); + + // f64 -> i32 + test_both_cast::<f64, i32>(0.0, 0); + test_both_cast::<f64, i32>(-0.0, 0); + test_both_cast::<f64, i32>(/*0x1.199999999999ap+0*/ f64::from_bits(0x3ff199999999999a), 1); + test_both_cast::<f64, i32>( + /*-0x1.199999999999ap+0*/ f64::from_bits(0xbff199999999999a), + -1, + ); + test_both_cast::<f64, i32>(1.9, 1); + test_both_cast::<f64, i32>(-1.9, -1); + test_both_cast::<f64, i32>(1e8, 100_000_000); + test_both_cast::<f64, i32>(2147483647.0, 2147483647); + test_both_cast::<f64, i32>(-2147483648.0, -2147483648); + // unrepresentable casts + assert_eq::<i32>(2147483648.0f64 as i32, i32::MAX); + assert_eq::<i32>(-2147483649.0f64 as i32, i32::MIN); + + // f64 -> i64 + test_both_cast::<f64, i64>(0.0, 0); + test_both_cast::<f64, i64>(-0.0, 0); + test_both_cast::<f64, i64>(/*0x0.0000000000001p-1022*/ f64::from_bits(0x1), 0); + test_both_cast::<f64, i64>( + /*-0x0.0000000000001p-1022*/ f64::from_bits(0x8000000000000001), + 0, + ); + test_both_cast::<f64, i64>(/*0x1.199999999999ap+0*/ f64::from_bits(0x3ff199999999999a), 1); + test_both_cast::<f64, i64>( + /*-0x1.199999999999ap+0*/ f64::from_bits(0xbff199999999999a), + -1, + ); + test_both_cast::<f64, i64>(5.0, 5); + test_both_cast::<f64, i64>(5.9, 5); + test_both_cast::<f64, i64>(-5.0, -5); + test_both_cast::<f64, i64>(-5.9, -5); + test_both_cast::<f64, i64>(4294967296.0, 4294967296); + test_both_cast::<f64, i64>(-4294967296.0, -4294967296); + test_both_cast::<f64, i64>(9223372036854774784.0, 9223372036854774784); + test_both_cast::<f64, i64>(-9223372036854775808.0, -9223372036854775808); + // unrepresentable casts + assert_eq::<i64>(9223372036854775808.0f64 as i64, i64::MAX); + assert_eq::<i64>(-9223372036854777856.0f64 as i64, i64::MIN); + assert_eq::<i64>(f64::MAX as i64, i64::MAX); + assert_eq::<i64>(f64::MIN as i64, i64::MIN); + assert_eq::<i64>(f64::INFINITY as i64, i64::MAX); + assert_eq::<i64>(f64::NEG_INFINITY as i64, i64::MIN); + assert_eq::<i64>(f64::NAN as i64, 0); + assert_eq::<i64>((-f64::NAN) as i64, 0); + + // f64 -> u64 + test_both_cast::<f64, u64>(0.0, 0); + test_both_cast::<f64, u64>(-0.0, 0); + test_both_cast::<f64, u64>(-0.99999999999, 0); + test_both_cast::<f64, u64>(5.0, 5); + test_both_cast::<f64, u64>(1e16, 10000000000000000); + test_both_cast::<f64, u64>((u64::MAX - 1024) as f64, u64::MAX - 2047); // rounding loss + test_both_cast::<f64, u64>(9223372036854775808.0, 9223372036854775808); + // unrepresentable casts + assert_eq::<u64>(-5.0f64 as u64, 0); + // rounds up and then becomes unrepresentable + assert_eq::<u64>((u64::MAX - 1023) as f64 as u64, u64::MAX); + assert_eq::<u64>(18446744073709551616.0f64 as u64, u64::MAX); + assert_eq::<u64>(f64::MAX as u64, u64::MAX); + assert_eq::<u64>(f64::MIN as u64, 0); + assert_eq::<u64>(f64::INFINITY as u64, u64::MAX); + assert_eq::<u64>(f64::NEG_INFINITY as u64, 0); + assert_eq::<u64>(f64::NAN as u64, 0); + assert_eq::<u64>((-f64::NAN) as u64, 0); + + // FIXME emscripten does not support i128 + #[cfg(not(target_os = "emscripten"))] + { + // f64 -> i128 + assert_eq::<i128>(f64::MAX as i128, i128::MAX); + assert_eq::<i128>(f64::MIN as i128, i128::MIN); + + // f64 -> u128 + assert_eq::<u128>(f64::MAX as u128, u128::MAX); + assert_eq::<u128>(f64::MIN as u128, 0); + } + + // int -> f32 + assert_eq::<f32>(127i8 as f32, 127.0); + assert_eq::<f32>(2147483647i32 as f32, 2147483648.0); + assert_eq::<f32>((-2147483648i32) as f32, -2147483648.0); + assert_eq::<f32>(1234567890i32 as f32, /*0x1.26580cp+30*/ f32::from_bits(0x4e932c06)); + assert_eq::<f32>(16777217i32 as f32, 16777216.0); + assert_eq::<f32>((-16777217i32) as f32, -16777216.0); + assert_eq::<f32>(16777219i32 as f32, 16777220.0); + assert_eq::<f32>((-16777219i32) as f32, -16777220.0); + assert_eq::<f32>( + 0x7fffff4000000001i64 as f32, + /*0x1.fffffep+62*/ f32::from_bits(0x5effffff), + ); + assert_eq::<f32>( + 0x8000004000000001u64 as i64 as f32, + /*-0x1.fffffep+62*/ f32::from_bits(0xdeffffff), + ); + assert_eq::<f32>( + 0x0020000020000001i64 as f32, + /*0x1.000002p+53*/ f32::from_bits(0x5a000001), + ); + assert_eq::<f32>( + 0xffdfffffdfffffffu64 as i64 as f32, + /*-0x1.000002p+53*/ f32::from_bits(0xda000001), + ); + // FIXME emscripten does not support i128 + #[cfg(not(target_os = "emscripten"))] + { + assert_eq::<f32>(i128::MIN as f32, -170141183460469231731687303715884105728.0f32); + assert_eq::<f32>(u128::MAX as f32, f32::INFINITY); // saturation + } + + // int -> f64 + assert_eq::<f64>(127i8 as f64, 127.0); + assert_eq::<f64>(i16::MIN as f64, -32768.0f64); + assert_eq::<f64>(2147483647i32 as f64, 2147483647.0); + assert_eq::<f64>(-2147483648i32 as f64, -2147483648.0); + assert_eq::<f64>(987654321i32 as f64, 987654321.0); + assert_eq::<f64>(9223372036854775807i64 as f64, 9223372036854775807.0); + assert_eq::<f64>(-9223372036854775808i64 as f64, -9223372036854775808.0); + assert_eq::<f64>(4669201609102990i64 as f64, 4669201609102990.0); // Feigenbaum (?) + assert_eq::<f64>(9007199254740993i64 as f64, 9007199254740992.0); + assert_eq::<f64>(-9007199254740993i64 as f64, -9007199254740992.0); + assert_eq::<f64>(9007199254740995i64 as f64, 9007199254740996.0); + assert_eq::<f64>(-9007199254740995i64 as f64, -9007199254740996.0); + // FIXME emscripten does not support i128 + #[cfg(not(target_os = "emscripten"))] + { + // even that fits... + assert_eq::<f64>(u128::MAX as f64, 340282366920938463463374607431768211455.0f64); + } + + // f32 -> f64 + assert_eq::<u64>((0.0f32 as f64).to_bits(), 0.0f64.to_bits()); + assert_eq::<u64>(((-0.0f32) as f64).to_bits(), (-0.0f64).to_bits()); + assert_eq::<f64>(5.0f32 as f64, 5.0f64); + assert_eq::<f64>( + /*0x1p-149*/ f32::from_bits(0x1) as f64, + /*0x1p-149*/ f64::from_bits(0x36a0000000000000), + ); + assert_eq::<f64>( + /*-0x1p-149*/ f32::from_bits(0x80000001) as f64, + /*-0x1p-149*/ f64::from_bits(0xb6a0000000000000), + ); + assert_eq::<f64>( + /*0x1.fffffep+127*/ f32::from_bits(0x7f7fffff) as f64, + /*0x1.fffffep+127*/ f64::from_bits(0x47efffffe0000000), + ); + assert_eq::<f64>( + /*-0x1.fffffep+127*/ (-f32::from_bits(0x7f7fffff)) as f64, + /*-0x1.fffffep+127*/ -f64::from_bits(0x47efffffe0000000), + ); + assert_eq::<f64>( + /*0x1p-119*/ f32::from_bits(0x4000000) as f64, + /*0x1p-119*/ f64::from_bits(0x3880000000000000), + ); + assert_eq::<f64>( + /*0x1.8f867ep+125*/ f32::from_bits(0x7e47c33f) as f64, + 6.6382536710104395e+37, + ); + assert_eq::<f64>(f32::INFINITY as f64, f64::INFINITY); + assert_eq::<f64>(f32::NEG_INFINITY as f64, f64::NEG_INFINITY); + + // f64 -> f32 + assert_eq::<u32>((0.0f64 as f32).to_bits(), 0.0f32.to_bits()); + assert_eq::<u32>(((-0.0f64) as f32).to_bits(), (-0.0f32).to_bits()); + assert_eq::<f32>(5.0f64 as f32, 5.0f32); + assert_eq::<f32>(/*0x0.0000000000001p-1022*/ f64::from_bits(0x1) as f32, 0.0); + assert_eq::<f32>(/*-0x0.0000000000001p-1022*/ (-f64::from_bits(0x1)) as f32, -0.0); + assert_eq::<f32>( + /*0x1.fffffe0000000p-127*/ f64::from_bits(0x380fffffe0000000) as f32, + /*0x1p-149*/ f32::from_bits(0x800000), + ); + assert_eq::<f32>( + /*0x1.4eae4f7024c7p+108*/ f64::from_bits(0x46b4eae4f7024c70) as f32, + /*0x1.4eae5p+108*/ f32::from_bits(0x75a75728), + ); + assert_eq::<f32>(f64::MAX as f32, f32::INFINITY); + assert_eq::<f32>(f64::MIN as f32, f32::NEG_INFINITY); + assert_eq::<f32>(f64::INFINITY as f32, f32::INFINITY); + assert_eq::<f32>(f64::NEG_INFINITY as f32, f32::NEG_INFINITY); +} + +pub fn run() { + casts(); // from miri's tests + + common_fptoi_tests!(f* -> i8 i16 i32 i64 u8 u16 u32 u64); + fptoui_tests!(f* -> u8 u16 u32 u64); + // FIXME emscripten does not support i128 + #[cfg(not(target_os = "emscripten"))] + { + common_fptoi_tests!(f* -> i128 u128); + fptoui_tests!(f* -> u128); + } + + // The following tests cover edge cases for some integer types. + + // # u8 + test!(254., f* -> u8, 254); + test!(256., f* -> u8, 255); + + // # i8 + test!(-127., f* -> i8, -127); + test!(-129., f* -> i8, -128); + test!(126., f* -> i8, 126); + test!(128., f* -> i8, 127); + + // # i32 + // -2147483648. is i32::MIN (exactly) + test!(-2147483648., f* -> i32, i32::MIN); + // 2147483648. is i32::MAX rounded up + test!(2147483648., f32 -> i32, 2147483647); + // With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to + // multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128: + test!(2147483520., f32 -> i32, 2147483520); + // Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7 + test!(-2147483904., f* -> i32, i32::MIN); + test!(-2147483520., f* -> i32, -2147483520); + + // # u32 + // round(MAX) and nextUp(round(MAX)) + test!(4294967040., f* -> u32, 4294967040); + test!(4294967296., f* -> u32, 4294967295); + + // # u128 + #[cfg(not(target_os = "emscripten"))] + { + // float->int: + test!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000); + // nextDown(f32::MAX) = 2^128 - 2 * 2^104 + const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.; + test!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000); + } +} diff --git a/src/test/ui/numbers-arithmetic/saturating-float-casts-wasm.rs b/src/test/ui/numbers-arithmetic/saturating-float-casts-wasm.rs new file mode 100644 index 00000000000..cad05917391 --- /dev/null +++ b/src/test/ui/numbers-arithmetic/saturating-float-casts-wasm.rs @@ -0,0 +1,13 @@ +// run-pass +// only-wasm32 +// compile-flags: -Zmir-opt-level=0 -C target-feature=+nontrapping-fptoint + +#![feature(test, stmt_expr_attributes)] +#![deny(overflowing_literals)] + +#[path = "saturating-float-casts-impl.rs"] +mod implementation; + +pub fn main() { + implementation::run(); +} diff --git a/src/test/ui/numbers-arithmetic/saturating-float-casts.rs b/src/test/ui/numbers-arithmetic/saturating-float-casts.rs index 825a11972ae..cc248a9bea0 100644 --- a/src/test/ui/numbers-arithmetic/saturating-float-casts.rs +++ b/src/test/ui/numbers-arithmetic/saturating-float-casts.rs @@ -1,509 +1,12 @@ // run-pass // compile-flags:-Zmir-opt-level=0 -// Tests saturating float->int casts. See u128-as-f32.rs for the opposite direction. -// -// Some of these tests come from a similar file in miri, -// tests/run-pass/float.rs. Individual test cases are potentially duplicated -// with the previously existing tests, but since this runs so quickly anyway, -// we're not spending the time to figure out exactly which ones should be -// merged. #![feature(test, stmt_expr_attributes)] #![deny(overflowing_literals)] -extern crate test; -use std::{f32, f64}; -#[cfg(not(target_os = "emscripten"))] -use std::{i128, u128}; -use std::{i16, i32, i64, i8, u16, u32, u64, u8}; -use test::black_box; - -macro_rules! test { - ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ( - // black_box disables constant evaluation to test run-time conversions: - assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected, - "run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty)); - - { - const X: $src_ty = $val; - const Y: $dest_ty = X as $dest_ty; - assert_eq!(Y, $expected, - "const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty)); - } - ); - - ($fval:expr, f* -> $ity:ident, $ival:expr) => ( - test!($fval, f32 -> $ity, $ival); - test!($fval, f64 -> $ity, $ival); - ) -} - -macro_rules! common_fptoi_tests { - ($fty:ident -> $($ity:ident)+) => ({ $( - test!($fty::NAN, $fty -> $ity, 0); - test!($fty::INFINITY, $fty -> $ity, $ity::MAX); - test!($fty::NEG_INFINITY, $fty -> $ity, $ity::MIN); - // These two tests are not solely float->int tests, in particular the latter relies on - // `u128::MAX as f32` not being UB. But that's okay, since this file tests int->float - // as well, the test is just slightly misplaced. - test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN); - test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX); - test!(0., $fty -> $ity, 0); - test!($fty::MIN_POSITIVE, $fty -> $ity, 0); - test!(-0.9, $fty -> $ity, 0); - test!(1., $fty -> $ity, 1); - test!(42., $fty -> $ity, 42); - )+ }); - - (f* -> $($ity:ident)+) => ({ - common_fptoi_tests!(f32 -> $($ity)+); - common_fptoi_tests!(f64 -> $($ity)+); - }) -} - -macro_rules! fptoui_tests { - ($fty: ident -> $($ity: ident)+) => ({ $( - test!(-0., $fty -> $ity, 0); - test!(-$fty::MIN_POSITIVE, $fty -> $ity, 0); - test!(-0.99999994, $fty -> $ity, 0); - test!(-1., $fty -> $ity, 0); - test!(-100., $fty -> $ity, 0); - test!(#[allow(overflowing_literals)] -1e50, $fty -> $ity, 0); - test!(#[allow(overflowing_literals)] -1e130, $fty -> $ity, 0); - )+ }); - - (f* -> $($ity:ident)+) => ({ - fptoui_tests!(f32 -> $($ity)+); - fptoui_tests!(f64 -> $($ity)+); - }) -} - -use std::fmt::Debug; - -// Helper function to avoid promotion so that this tests "run-time" casts, not CTFE. -#[track_caller] -#[inline(never)] -fn assert_eq<T: PartialEq + Debug>(x: T, y: T) { - assert_eq!(x, y); -} - -trait FloatToInt<Int>: Copy { - fn cast(self) -> Int; - unsafe fn cast_unchecked(self) -> Int; -} - -impl FloatToInt<i8> for f32 { - fn cast(self) -> i8 { - self as _ - } - unsafe fn cast_unchecked(self) -> i8 { - self.to_int_unchecked() - } -} -impl FloatToInt<i32> for f32 { - fn cast(self) -> i32 { - self as _ - } - unsafe fn cast_unchecked(self) -> i32 { - self.to_int_unchecked() - } -} -impl FloatToInt<u32> for f32 { - fn cast(self) -> u32 { - self as _ - } - unsafe fn cast_unchecked(self) -> u32 { - self.to_int_unchecked() - } -} -impl FloatToInt<i64> for f32 { - fn cast(self) -> i64 { - self as _ - } - unsafe fn cast_unchecked(self) -> i64 { - self.to_int_unchecked() - } -} -impl FloatToInt<u64> for f32 { - fn cast(self) -> u64 { - self as _ - } - unsafe fn cast_unchecked(self) -> u64 { - self.to_int_unchecked() - } -} - -impl FloatToInt<i8> for f64 { - fn cast(self) -> i8 { - self as _ - } - unsafe fn cast_unchecked(self) -> i8 { - self.to_int_unchecked() - } -} -impl FloatToInt<i32> for f64 { - fn cast(self) -> i32 { - self as _ - } - unsafe fn cast_unchecked(self) -> i32 { - self.to_int_unchecked() - } -} -impl FloatToInt<u32> for f64 { - fn cast(self) -> u32 { - self as _ - } - unsafe fn cast_unchecked(self) -> u32 { - self.to_int_unchecked() - } -} -impl FloatToInt<i64> for f64 { - fn cast(self) -> i64 { - self as _ - } - unsafe fn cast_unchecked(self) -> i64 { - self.to_int_unchecked() - } -} -impl FloatToInt<u64> for f64 { - fn cast(self) -> u64 { - self as _ - } - unsafe fn cast_unchecked(self) -> u64 { - self.to_int_unchecked() - } -} -// FIXME emscripten does not support i128 -#[cfg(not(target_os = "emscripten"))] -impl FloatToInt<i128> for f64 { - fn cast(self) -> i128 { - self as _ - } - unsafe fn cast_unchecked(self) -> i128 { - self.to_int_unchecked() - } -} -// FIXME emscripten does not support i128 -#[cfg(not(target_os = "emscripten"))] -impl FloatToInt<u128> for f64 { - fn cast(self) -> u128 { - self as _ - } - unsafe fn cast_unchecked(self) -> u128 { - self.to_int_unchecked() - } -} - -/// Test this cast both via `as` and via `to_int_unchecked` (i.e., it must not saturate). -#[track_caller] -#[inline(never)] -fn test_both_cast<F, I>(x: F, y: I) -where - F: FloatToInt<I>, - I: PartialEq + Debug, -{ - assert_eq!(x.cast(), y); - assert_eq!(unsafe { x.cast_unchecked() }, y); -} - -fn casts() { - // f32 -> i8 - test_both_cast::<f32, i8>(127.99, 127); - test_both_cast::<f32, i8>(-128.99, -128); - - // f32 -> i32 - test_both_cast::<f32, i32>(0.0, 0); - test_both_cast::<f32, i32>(-0.0, 0); - test_both_cast::<f32, i32>(/*0x1p-149*/ f32::from_bits(0x00000001), 0); - test_both_cast::<f32, i32>(/*-0x1p-149*/ f32::from_bits(0x80000001), 0); - test_both_cast::<f32, i32>(/*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd), 1); - test_both_cast::<f32, i32>(/*-0x1.19999ap+0*/ f32::from_bits(0xbf8ccccd), -1); - test_both_cast::<f32, i32>(1.9, 1); - test_both_cast::<f32, i32>(-1.9, -1); - test_both_cast::<f32, i32>(5.0, 5); - test_both_cast::<f32, i32>(-5.0, -5); - test_both_cast::<f32, i32>(2147483520.0, 2147483520); - test_both_cast::<f32, i32>(-2147483648.0, -2147483648); - // unrepresentable casts - assert_eq::<i32>(2147483648.0f32 as i32, i32::MAX); - assert_eq::<i32>(-2147483904.0f32 as i32, i32::MIN); - assert_eq::<i32>(f32::MAX as i32, i32::MAX); - assert_eq::<i32>(f32::MIN as i32, i32::MIN); - assert_eq::<i32>(f32::INFINITY as i32, i32::MAX); - assert_eq::<i32>(f32::NEG_INFINITY as i32, i32::MIN); - assert_eq::<i32>(f32::NAN as i32, 0); - assert_eq::<i32>((-f32::NAN) as i32, 0); - - // f32 -> u32 - test_both_cast::<f32, u32>(0.0, 0); - test_both_cast::<f32, u32>(-0.0, 0); - test_both_cast::<f32, u32>(-0.9999999, 0); - test_both_cast::<f32, u32>(/*0x1p-149*/ f32::from_bits(0x1), 0); - test_both_cast::<f32, u32>(/*-0x1p-149*/ f32::from_bits(0x80000001), 0); - test_both_cast::<f32, u32>(/*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd), 1); - test_both_cast::<f32, u32>(1.9, 1); - test_both_cast::<f32, u32>(5.0, 5); - test_both_cast::<f32, u32>(2147483648.0, 0x8000_0000); - test_both_cast::<f32, u32>(4294967040.0, 0u32.wrapping_sub(256)); - test_both_cast::<f32, u32>(/*-0x1.ccccccp-1*/ f32::from_bits(0xbf666666), 0); - test_both_cast::<f32, u32>(/*-0x1.fffffep-1*/ f32::from_bits(0xbf7fffff), 0); - test_both_cast::<f32, u32>((u32::MAX - 128) as f32, u32::MAX - 255); // rounding loss - - // unrepresentable casts: - - // rounds up and then becomes unrepresentable - assert_eq::<u32>((u32::MAX - 127) as f32 as u32, u32::MAX); - - assert_eq::<u32>(4294967296.0f32 as u32, u32::MAX); - assert_eq::<u32>(-5.0f32 as u32, 0); - assert_eq::<u32>(f32::MAX as u32, u32::MAX); - assert_eq::<u32>(f32::MIN as u32, 0); - assert_eq::<u32>(f32::INFINITY as u32, u32::MAX); - assert_eq::<u32>(f32::NEG_INFINITY as u32, 0); - assert_eq::<u32>(f32::NAN as u32, 0); - assert_eq::<u32>((-f32::NAN) as u32, 0); - - // f32 -> i64 - test_both_cast::<f32, i64>(4294967296.0, 4294967296); - test_both_cast::<f32, i64>(-4294967296.0, -4294967296); - test_both_cast::<f32, i64>(9223371487098961920.0, 9223371487098961920); - test_both_cast::<f32, i64>(-9223372036854775808.0, -9223372036854775808); - - // f64 -> i8 - test_both_cast::<f64, i8>(127.99, 127); - test_both_cast::<f64, i8>(-128.99, -128); - - // f64 -> i32 - test_both_cast::<f64, i32>(0.0, 0); - test_both_cast::<f64, i32>(-0.0, 0); - test_both_cast::<f64, i32>(/*0x1.199999999999ap+0*/ f64::from_bits(0x3ff199999999999a), 1); - test_both_cast::<f64, i32>( - /*-0x1.199999999999ap+0*/ f64::from_bits(0xbff199999999999a), - -1, - ); - test_both_cast::<f64, i32>(1.9, 1); - test_both_cast::<f64, i32>(-1.9, -1); - test_both_cast::<f64, i32>(1e8, 100_000_000); - test_both_cast::<f64, i32>(2147483647.0, 2147483647); - test_both_cast::<f64, i32>(-2147483648.0, -2147483648); - // unrepresentable casts - assert_eq::<i32>(2147483648.0f64 as i32, i32::MAX); - assert_eq::<i32>(-2147483649.0f64 as i32, i32::MIN); - - // f64 -> i64 - test_both_cast::<f64, i64>(0.0, 0); - test_both_cast::<f64, i64>(-0.0, 0); - test_both_cast::<f64, i64>(/*0x0.0000000000001p-1022*/ f64::from_bits(0x1), 0); - test_both_cast::<f64, i64>( - /*-0x0.0000000000001p-1022*/ f64::from_bits(0x8000000000000001), - 0, - ); - test_both_cast::<f64, i64>(/*0x1.199999999999ap+0*/ f64::from_bits(0x3ff199999999999a), 1); - test_both_cast::<f64, i64>( - /*-0x1.199999999999ap+0*/ f64::from_bits(0xbff199999999999a), - -1, - ); - test_both_cast::<f64, i64>(5.0, 5); - test_both_cast::<f64, i64>(5.9, 5); - test_both_cast::<f64, i64>(-5.0, -5); - test_both_cast::<f64, i64>(-5.9, -5); - test_both_cast::<f64, i64>(4294967296.0, 4294967296); - test_both_cast::<f64, i64>(-4294967296.0, -4294967296); - test_both_cast::<f64, i64>(9223372036854774784.0, 9223372036854774784); - test_both_cast::<f64, i64>(-9223372036854775808.0, -9223372036854775808); - // unrepresentable casts - assert_eq::<i64>(9223372036854775808.0f64 as i64, i64::MAX); - assert_eq::<i64>(-9223372036854777856.0f64 as i64, i64::MIN); - assert_eq::<i64>(f64::MAX as i64, i64::MAX); - assert_eq::<i64>(f64::MIN as i64, i64::MIN); - assert_eq::<i64>(f64::INFINITY as i64, i64::MAX); - assert_eq::<i64>(f64::NEG_INFINITY as i64, i64::MIN); - assert_eq::<i64>(f64::NAN as i64, 0); - assert_eq::<i64>((-f64::NAN) as i64, 0); - - // f64 -> u64 - test_both_cast::<f64, u64>(0.0, 0); - test_both_cast::<f64, u64>(-0.0, 0); - test_both_cast::<f64, u64>(-0.99999999999, 0); - test_both_cast::<f64, u64>(5.0, 5); - test_both_cast::<f64, u64>(1e16, 10000000000000000); - test_both_cast::<f64, u64>((u64::MAX - 1024) as f64, u64::MAX - 2047); // rounding loss - test_both_cast::<f64, u64>(9223372036854775808.0, 9223372036854775808); - // unrepresentable casts - assert_eq::<u64>(-5.0f64 as u64, 0); - // rounds up and then becomes unrepresentable - assert_eq::<u64>((u64::MAX - 1023) as f64 as u64, u64::MAX); - assert_eq::<u64>(18446744073709551616.0f64 as u64, u64::MAX); - assert_eq::<u64>(f64::MAX as u64, u64::MAX); - assert_eq::<u64>(f64::MIN as u64, 0); - assert_eq::<u64>(f64::INFINITY as u64, u64::MAX); - assert_eq::<u64>(f64::NEG_INFINITY as u64, 0); - assert_eq::<u64>(f64::NAN as u64, 0); - assert_eq::<u64>((-f64::NAN) as u64, 0); - - // FIXME emscripten does not support i128 - #[cfg(not(target_os = "emscripten"))] - { - // f64 -> i128 - assert_eq::<i128>(f64::MAX as i128, i128::MAX); - assert_eq::<i128>(f64::MIN as i128, i128::MIN); - - // f64 -> u128 - assert_eq::<u128>(f64::MAX as u128, u128::MAX); - assert_eq::<u128>(f64::MIN as u128, 0); - } - - // int -> f32 - assert_eq::<f32>(127i8 as f32, 127.0); - assert_eq::<f32>(2147483647i32 as f32, 2147483648.0); - assert_eq::<f32>((-2147483648i32) as f32, -2147483648.0); - assert_eq::<f32>(1234567890i32 as f32, /*0x1.26580cp+30*/ f32::from_bits(0x4e932c06)); - assert_eq::<f32>(16777217i32 as f32, 16777216.0); - assert_eq::<f32>((-16777217i32) as f32, -16777216.0); - assert_eq::<f32>(16777219i32 as f32, 16777220.0); - assert_eq::<f32>((-16777219i32) as f32, -16777220.0); - assert_eq::<f32>( - 0x7fffff4000000001i64 as f32, - /*0x1.fffffep+62*/ f32::from_bits(0x5effffff), - ); - assert_eq::<f32>( - 0x8000004000000001u64 as i64 as f32, - /*-0x1.fffffep+62*/ f32::from_bits(0xdeffffff), - ); - assert_eq::<f32>( - 0x0020000020000001i64 as f32, - /*0x1.000002p+53*/ f32::from_bits(0x5a000001), - ); - assert_eq::<f32>( - 0xffdfffffdfffffffu64 as i64 as f32, - /*-0x1.000002p+53*/ f32::from_bits(0xda000001), - ); - // FIXME emscripten does not support i128 - #[cfg(not(target_os = "emscripten"))] - { - assert_eq::<f32>(i128::MIN as f32, -170141183460469231731687303715884105728.0f32); - assert_eq::<f32>(u128::MAX as f32, f32::INFINITY); // saturation - } - - // int -> f64 - assert_eq::<f64>(127i8 as f64, 127.0); - assert_eq::<f64>(i16::MIN as f64, -32768.0f64); - assert_eq::<f64>(2147483647i32 as f64, 2147483647.0); - assert_eq::<f64>(-2147483648i32 as f64, -2147483648.0); - assert_eq::<f64>(987654321i32 as f64, 987654321.0); - assert_eq::<f64>(9223372036854775807i64 as f64, 9223372036854775807.0); - assert_eq::<f64>(-9223372036854775808i64 as f64, -9223372036854775808.0); - assert_eq::<f64>(4669201609102990i64 as f64, 4669201609102990.0); // Feigenbaum (?) - assert_eq::<f64>(9007199254740993i64 as f64, 9007199254740992.0); - assert_eq::<f64>(-9007199254740993i64 as f64, -9007199254740992.0); - assert_eq::<f64>(9007199254740995i64 as f64, 9007199254740996.0); - assert_eq::<f64>(-9007199254740995i64 as f64, -9007199254740996.0); - // FIXME emscripten does not support i128 - #[cfg(not(target_os = "emscripten"))] - { - // even that fits... - assert_eq::<f64>(u128::MAX as f64, 340282366920938463463374607431768211455.0f64); - } - - // f32 -> f64 - assert_eq::<u64>((0.0f32 as f64).to_bits(), 0.0f64.to_bits()); - assert_eq::<u64>(((-0.0f32) as f64).to_bits(), (-0.0f64).to_bits()); - assert_eq::<f64>(5.0f32 as f64, 5.0f64); - assert_eq::<f64>( - /*0x1p-149*/ f32::from_bits(0x1) as f64, - /*0x1p-149*/ f64::from_bits(0x36a0000000000000), - ); - assert_eq::<f64>( - /*-0x1p-149*/ f32::from_bits(0x80000001) as f64, - /*-0x1p-149*/ f64::from_bits(0xb6a0000000000000), - ); - assert_eq::<f64>( - /*0x1.fffffep+127*/ f32::from_bits(0x7f7fffff) as f64, - /*0x1.fffffep+127*/ f64::from_bits(0x47efffffe0000000), - ); - assert_eq::<f64>( - /*-0x1.fffffep+127*/ (-f32::from_bits(0x7f7fffff)) as f64, - /*-0x1.fffffep+127*/ -f64::from_bits(0x47efffffe0000000), - ); - assert_eq::<f64>( - /*0x1p-119*/ f32::from_bits(0x4000000) as f64, - /*0x1p-119*/ f64::from_bits(0x3880000000000000), - ); - assert_eq::<f64>( - /*0x1.8f867ep+125*/ f32::from_bits(0x7e47c33f) as f64, - 6.6382536710104395e+37, - ); - assert_eq::<f64>(f32::INFINITY as f64, f64::INFINITY); - assert_eq::<f64>(f32::NEG_INFINITY as f64, f64::NEG_INFINITY); - - // f64 -> f32 - assert_eq::<u32>((0.0f64 as f32).to_bits(), 0.0f32.to_bits()); - assert_eq::<u32>(((-0.0f64) as f32).to_bits(), (-0.0f32).to_bits()); - assert_eq::<f32>(5.0f64 as f32, 5.0f32); - assert_eq::<f32>(/*0x0.0000000000001p-1022*/ f64::from_bits(0x1) as f32, 0.0); - assert_eq::<f32>(/*-0x0.0000000000001p-1022*/ (-f64::from_bits(0x1)) as f32, -0.0); - assert_eq::<f32>( - /*0x1.fffffe0000000p-127*/ f64::from_bits(0x380fffffe0000000) as f32, - /*0x1p-149*/ f32::from_bits(0x800000), - ); - assert_eq::<f32>( - /*0x1.4eae4f7024c7p+108*/ f64::from_bits(0x46b4eae4f7024c70) as f32, - /*0x1.4eae5p+108*/ f32::from_bits(0x75a75728), - ); - assert_eq::<f32>(f64::MAX as f32, f32::INFINITY); - assert_eq::<f32>(f64::MIN as f32, f32::NEG_INFINITY); - assert_eq::<f32>(f64::INFINITY as f32, f32::INFINITY); - assert_eq::<f32>(f64::NEG_INFINITY as f32, f32::NEG_INFINITY); -} +#[path = "saturating-float-casts-impl.rs"] +mod implementation; pub fn main() { - casts(); // from miri's tests - - common_fptoi_tests!(f* -> i8 i16 i32 i64 u8 u16 u32 u64); - fptoui_tests!(f* -> u8 u16 u32 u64); - // FIXME emscripten does not support i128 - #[cfg(not(target_os = "emscripten"))] - { - common_fptoi_tests!(f* -> i128 u128); - fptoui_tests!(f* -> u128); - } - - // The following tests cover edge cases for some integer types. - - // # u8 - test!(254., f* -> u8, 254); - test!(256., f* -> u8, 255); - - // # i8 - test!(-127., f* -> i8, -127); - test!(-129., f* -> i8, -128); - test!(126., f* -> i8, 126); - test!(128., f* -> i8, 127); - - // # i32 - // -2147483648. is i32::MIN (exactly) - test!(-2147483648., f* -> i32, i32::MIN); - // 2147483648. is i32::MAX rounded up - test!(2147483648., f32 -> i32, 2147483647); - // With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to - // multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128: - test!(2147483520., f32 -> i32, 2147483520); - // Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7 - test!(-2147483904., f* -> i32, i32::MIN); - test!(-2147483520., f* -> i32, -2147483520); - - // # u32 - // round(MAX) and nextUp(round(MAX)) - test!(4294967040., f* -> u32, 4294967040); - test!(4294967296., f* -> u32, 4294967295); - - // # u128 - #[cfg(not(target_os = "emscripten"))] - { - // float->int: - test!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000); - // nextDown(f32::MAX) = 2^128 - 2 * 2^104 - const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.; - test!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000); - } + implementation::run(); } diff --git a/src/test/ui/resolve/resolve-label.rs b/src/test/ui/resolve/resolve-label.rs index 965ee450104..ac5410cd355 100644 --- a/src/test/ui/resolve/resolve-label.rs +++ b/src/test/ui/resolve/resolve-label.rs @@ -2,7 +2,7 @@ fn f() { 'l: loop { fn g() { loop { - break 'l; //~ ERROR use of undeclared label + break 'l; //~ ERROR use of unreachable label } } } diff --git a/src/test/ui/resolve/resolve-label.stderr b/src/test/ui/resolve/resolve-label.stderr index 72a8e443bac..5729348ef21 100644 --- a/src/test/ui/resolve/resolve-label.stderr +++ b/src/test/ui/resolve/resolve-label.stderr @@ -1,9 +1,14 @@ -error[E0426]: use of undeclared label `'l` +error[E0767]: use of unreachable label `'l` --> $DIR/resolve-label.rs:5:23 | +LL | 'l: loop { + | -- unreachable label defined here +... LL | break 'l; - | ^^ undeclared label `'l` + | ^^ unreachable label `'l` + | + = note: labels are unreachable through functions, closures, async blocks and modules error: aborting due to previous error -For more information about this error, try `rustc --explain E0426`. +For more information about this error, try `rustc --explain E0767`. diff --git a/src/test/ui/suggestions/suggest-labels.stderr b/src/test/ui/suggestions/suggest-labels.stderr index 079821e6490..aac6515b527 100644 --- a/src/test/ui/suggestions/suggest-labels.stderr +++ b/src/test/ui/suggestions/suggest-labels.stderr @@ -1,35 +1,35 @@ error[E0426]: use of undeclared label `'fo` --> $DIR/suggest-labels.rs:4:15 | +LL | 'foo: loop { + | ---- a label with a similar name is reachable LL | break 'fo; | ^^^ - | -help: a label with a similar name exists in this scope - | -LL | break 'foo; - | ^^^^ + | | + | undeclared label `'fo` + | help: try using similarly named label: `'foo` error[E0426]: use of undeclared label `'bor` --> $DIR/suggest-labels.rs:8:18 | +LL | 'bar: loop { + | ---- a label with a similar name is reachable LL | continue 'bor; | ^^^^ - | -help: a label with a similar name exists in this scope - | -LL | continue 'bar; - | ^^^^ + | | + | undeclared label `'bor` + | help: try using similarly named label: `'bar` error[E0426]: use of undeclared label `'longlable` --> $DIR/suggest-labels.rs:13:19 | +LL | 'longlabel1: loop { + | ----------- a label with a similar name is reachable LL | break 'longlable; | ^^^^^^^^^^ - | -help: a label with a similar name exists in this scope - | -LL | break 'longlabel1; - | ^^^^^^^^^^^ + | | + | undeclared label `'longlable` + | help: try using similarly named label: `'longlabel1` error: aborting due to 3 previous errors |
