mirror of
https://github.com/rust-lang/rust.git
synced 2025-11-24 08:37:03 +00:00
We cannot transform `*elem` to `array[idx1]` in the following code,
as `idx1` has already been modified.
```rust
mir! {
let array;
let elem;
{
array = [*val; 5];
elem = &array[idx1];
idx1 = idx2;
RET = *elem;
Return()
}
}
```
50 lines
1.1 KiB
Rust
50 lines
1.1 KiB
Rust
//@ test-mir-pass: GVN
|
|
|
|
#![feature(custom_mir, core_intrinsics)]
|
|
|
|
// Check that we do not introduce out-of-bounds access.
|
|
|
|
use std::intrinsics::mir::*;
|
|
|
|
// EMIT_MIR gvn_repeat.repeat_place.GVN.diff
|
|
#[custom_mir(dialect = "runtime")]
|
|
pub fn repeat_place(mut idx1: usize, idx2: usize, val: &i32) -> i32 {
|
|
// CHECK-LABEL: fn repeat_place(
|
|
// CHECK: let mut [[ELEM:.*]]: &i32;
|
|
// CHECK: _0 = copy (*[[ELEM]])
|
|
mir! {
|
|
let array;
|
|
let elem;
|
|
{
|
|
array = [*val; 5];
|
|
elem = &array[idx1];
|
|
idx1 = idx2;
|
|
RET = *elem;
|
|
Return()
|
|
}
|
|
}
|
|
}
|
|
|
|
// EMIT_MIR gvn_repeat.repeat_local.GVN.diff
|
|
#[custom_mir(dialect = "runtime")]
|
|
pub fn repeat_local(mut idx1: usize, idx2: usize, val: i32) -> i32 {
|
|
// CHECK-LABEL: fn repeat_local(
|
|
// CHECK: _0 = copy _3
|
|
mir! {
|
|
let array;
|
|
let elem;
|
|
{
|
|
array = [val; 5];
|
|
elem = &array[idx1];
|
|
idx1 = idx2;
|
|
RET = *elem;
|
|
Return()
|
|
}
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
assert_eq!(repeat_place(0, 5, &0), 0);
|
|
assert_eq!(repeat_local(0, 5, 0), 0);
|
|
}
|