]> git.lizzy.rs Git - rust.git/blob - tests/codegen/issue-96497-slice-size-nowrap.rs
Rollup merge of #107700 - jyn514:tools-builder, r=Mark-Simulacrum
[rust.git] / tests / codegen / issue-96497-slice-size-nowrap.rs
1 // This test case checks that LLVM is aware that computing the size of a slice cannot wrap.
2 // The possibility of wrapping results in an additional branch when dropping boxed slices
3 // in some situations, see https://github.com/rust-lang/rust/issues/96497#issuecomment-1112865218
4
5 // compile-flags: -O
6 // min-llvm-version: 15.0
7
8 #![crate_type="lib"]
9
10 // CHECK-LABEL: @simple_size_of_nowrap
11 #[no_mangle]
12 pub fn simple_size_of_nowrap(x: &[u32]) -> usize {
13     // Make sure the shift used to compute the size has a nowrap flag.
14
15     // CHECK: [[A:%.*]] = shl nsw {{.*}}, 2
16     // CHECK-NEXT: ret {{.*}} [[A]]
17     core::mem::size_of_val(x)
18 }
19
20 // CHECK-LABEL: @drop_write
21 #[no_mangle]
22 pub fn drop_write(mut x: Box<[u32]>) {
23     // Check that this write is optimized out.
24     // This depends on the size calculation not wrapping,
25     // since otherwise LLVM can't tell that the memory is always deallocated if the slice len > 0.
26
27     // CHECK-NOT: store i32 42
28     x[1] = 42;
29 }