mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-04 12:44:40 +00:00
c9c80d2c5f
The only non-obvious changes: - `building/storage_live_dead_in_statics.rs` has a `#[rustfmt::skip]` attribute to avoid reformating a table of data. - Two `.mir` files have slight changes involving line numbers. - In `unusual_item_types.rs` an `EMIT_MIR` annotation is moved to outside a function, which is the usual spot, because `tidy` complains if such a comment is indented. The commit also tweaks the comments in `rustfmt.toml`.
68 lines
2.1 KiB
Rust
68 lines
2.1 KiB
Rust
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
|
|
//@ test-mir-pass: NormalizeArrayLen
|
|
//@ compile-flags: -Zmir-enable-passes=+LowerSliceLenCalls
|
|
|
|
// EMIT_MIR lower_array_len.array_bound.NormalizeArrayLen.diff
|
|
pub fn array_bound<const N: usize>(index: usize, slice: &[u8; N]) -> u8 {
|
|
// CHECK-LABEL: fn array_bound(
|
|
// CHECK: [[len:_.*]] = const N;
|
|
// CHECK: Lt(move {{_.*}}, move [[len]]);
|
|
if index < slice.len() { slice[index] } else { 42 }
|
|
}
|
|
|
|
// EMIT_MIR lower_array_len.array_bound_mut.NormalizeArrayLen.diff
|
|
pub fn array_bound_mut<const N: usize>(index: usize, slice: &mut [u8; N]) -> u8 {
|
|
// CHECK-LABEL: fn array_bound_mut(
|
|
// CHECK: [[len:_.*]] = const N;
|
|
// CHECK: Lt(move {{_.*}}, move [[len]]);
|
|
if index < slice.len() {
|
|
slice[index]
|
|
} else {
|
|
slice[0] = 42;
|
|
|
|
42
|
|
}
|
|
}
|
|
|
|
// EMIT_MIR lower_array_len.array_len.NormalizeArrayLen.diff
|
|
pub fn array_len<const N: usize>(arr: &[u8; N]) -> usize {
|
|
// CHECK-LABEL: fn array_len(
|
|
// CHECK: _0 = const N;
|
|
arr.len()
|
|
}
|
|
|
|
// EMIT_MIR lower_array_len.array_len_by_value.NormalizeArrayLen.diff
|
|
pub fn array_len_by_value<const N: usize>(arr: [u8; N]) -> usize {
|
|
// CHECK-LABEL: fn array_len_by_value(
|
|
// CHECK: _0 = const N;
|
|
arr.len()
|
|
}
|
|
|
|
// EMIT_MIR lower_array_len.array_len_reborrow.NormalizeArrayLen.diff
|
|
pub fn array_len_reborrow<const N: usize>(mut arr: [u8; N]) -> usize {
|
|
// CHECK-LABEL: fn array_len_reborrow(
|
|
// CHECK: _0 = const N;
|
|
let arr: &mut [_] = &mut arr;
|
|
let arr = &*arr;
|
|
arr.len()
|
|
}
|
|
|
|
// EMIT_MIR lower_array_len.array_len_raw.NormalizeArrayLen.diff
|
|
pub fn array_len_raw<const N: usize>(arr: [u8; N]) -> usize {
|
|
// CHECK-LABEL: fn array_len_raw(
|
|
// CHECK: _0 = const N;
|
|
let arr: &[_] = &arr;
|
|
let arr = std::ptr::addr_of!(*arr);
|
|
unsafe { &*arr }.len()
|
|
}
|
|
|
|
fn main() {
|
|
let _ = array_bound(3, &[0, 1, 2, 3]);
|
|
let mut tmp = [0, 1, 2, 3, 4];
|
|
let _ = array_bound_mut(3, &mut [0, 1, 2, 3]);
|
|
let _ = array_len(&[0]);
|
|
let _ = array_len_by_value([0, 2]);
|
|
let _ = array_len_reborrow([0, 2]);
|
|
let _ = array_len_raw([0, 2]);
|
|
}
|