|
| 1 | +//! Regression test for <https://github.com/rust-lang/rust/issues/60044>. |
| 2 | +
|
| 3 | +//@ assembly-output: emit-asm |
| 4 | +//@ only-x86_64 |
| 5 | + |
| 6 | +// We want to check that the None case is optimized away |
| 7 | +//@ compile-flags: -O |
| 8 | + |
| 9 | +// Simplify the generated assembly |
| 10 | +//@ compile-flags: -Cforce-unwind-tables=no |
| 11 | + |
| 12 | +#![crate_type = "lib"] |
| 13 | + |
| 14 | +use std::num::NonZeroUsize; |
| 15 | +use std::sync::atomic::AtomicUsize; |
| 16 | +use std::sync::atomic::Ordering::Relaxed; |
| 17 | + |
| 18 | +pub static X: AtomicUsize = AtomicUsize::new(1); |
| 19 | + |
| 20 | +/// This function function shall look like this: |
| 21 | +/// ``` |
| 22 | +/// some_non_zero_from_atomic_get: |
| 23 | +/// movq _RNvCs7C4TuIcXqwO_25some_non_zero_from_atomic1X@GOTPCREL(%rip), %rax |
| 24 | +/// movq (%rax), %rax |
| 25 | +/// retq |
| 26 | +/// ``` |
| 27 | +// CHECK-LABEL: some_non_zero_from_atomic_get: |
| 28 | +// CHECK-NEXT: movq {{[_a-zA-Z0-9]+}}@GOTPCREL(%rip), %rax |
| 29 | +// CHECK-NEXT: movq (%rax), %rax |
| 30 | +// CHECK-NEXT: retq |
| 31 | +#[no_mangle] |
| 32 | +pub unsafe fn some_non_zero_from_atomic_get() -> Option<NonZeroUsize> { |
| 33 | + let x = X.load(Relaxed); |
| 34 | + Some(NonZeroUsize::new_unchecked(x)) |
| 35 | +} |
| 36 | + |
| 37 | +/// This function shall be identical to the above, which means: |
| 38 | +// CHECK-DAG: some_non_zero_from_atomic_get2 = some_non_zero_from_atomic_get |
| 39 | +#[no_mangle] |
| 40 | +pub unsafe fn some_non_zero_from_atomic_get2() -> usize { |
| 41 | + match some_non_zero_from_atomic_get() { |
| 42 | + Some(x) => x.get(), |
| 43 | + None => unreachable!(), // shall be optimized out |
| 44 | + } |
| 45 | +} |
0 commit comments