]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-float-bits-reject-conv.rs
Rollup merge of #99479 - Enselic:import-can-be-without-id, r=camelid
[rust.git] / src / test / ui / consts / const-float-bits-reject-conv.rs
1 // compile-flags: -Zmir-opt-level=0
2 #![feature(const_float_bits_conv)]
3 #![feature(const_float_classify)]
4
5 // Don't promote
6 const fn nop<T>(x: T) -> T { x }
7
8 macro_rules! const_assert {
9     ($a:expr) => {
10         {
11             const _: () = assert!($a);
12             assert!(nop($a));
13         }
14     };
15     ($a:expr, $b:expr) => {
16         {
17             const _: () = assert!($a == $b);
18             assert_eq!(nop($a), nop($b));
19         }
20     };
21 }
22
23 fn f32() {
24     // Check that NaNs roundtrip their bits regardless of signalingness
25     // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
26     // ...actually, let's just check that these break. :D
27     const MASKED_NAN1: u32 = f32::NAN.to_bits() ^ 0x002A_AAAA;
28     const MASKED_NAN2: u32 = f32::NAN.to_bits() ^ 0x0055_5555;
29
30     const_assert!(f32::from_bits(MASKED_NAN1).is_nan());
31     //~^ ERROR any use of this value will cause an error
32     //~| WARNING this was previously accepted
33     const_assert!(f32::from_bits(MASKED_NAN1).is_nan());
34     //~^ ERROR any use of this value will cause an error
35     //~| WARNING this was previously accepted
36
37     // LLVM does not guarantee that loads and stores of NaNs preserve their exact bit pattern.
38     // In practice, this seems to only cause a problem on x86, since the most widely used calling
39     // convention mandates that floating point values are returned on the x87 FPU stack. See #73328.
40     if !cfg!(target_arch = "x86") {
41         const_assert!(f32::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1);
42         //~^ ERROR any use of this value will cause an error
43         //~| WARNING this was previously accepted
44         const_assert!(f32::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2);
45         //~^ ERROR any use of this value will cause an error
46         //~| WARNING this was previously accepted
47     }
48 }
49
50 fn f64() {
51     // Check that NaNs roundtrip their bits regardless of signalingness
52     // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
53     // ...actually, let's just check that these break. :D
54     const MASKED_NAN1: u64 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
55     const MASKED_NAN2: u64 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
56
57     const_assert!(f64::from_bits(MASKED_NAN1).is_nan());
58     //~^ ERROR any use of this value will cause an error
59     //~| WARNING this was previously accepted
60     const_assert!(f64::from_bits(MASKED_NAN1).is_nan());
61     //~^ ERROR any use of this value will cause an error
62     //~| WARNING this was previously accepted
63
64     // See comment above.
65     if !cfg!(target_arch = "x86") {
66         const_assert!(f64::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1);
67         //~^ ERROR any use of this value will cause an error
68         //~| WARNING this was previously accepted
69         const_assert!(f64::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2);
70         //~^ ERROR any use of this value will cause an error
71         //~| WARNING this was previously accepted
72     }
73 }
74
75 fn main() {
76     f32();
77     f64();
78 }