]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/stacked_borrows/transmute-is-no-escape.rs
Rollup merge of #101664 - mejrs:similarity, r=fee1-dead
[rust.git] / src / tools / miri / tests / fail / stacked_borrows / transmute-is-no-escape.rs
1 // Make sure we cannot use raw ptrs that got transmuted from mutable references
2 // (i.e, no EscapeToRaw happened).
3 // We could, in principle, do EscapeToRaw lazily to allow this code, but that
4 // would no alleviate the need for EscapeToRaw (see `ref_raw_int_raw` in
5 // `run-pass/stacked-borrows.rs`), and thus increase overall complexity.
6 use std::mem;
7
8 fn main() {
9     let mut x: [i32; 2] = [42, 43];
10     let _raw: *mut i32 = unsafe { mem::transmute(&mut x[0]) };
11     // `raw` still carries a tag, so we get another pointer to the same location that does not carry a tag
12     let raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
13     unsafe { *raw = 13 }; //~ ERROR: /write access .* tag does not exist in the borrow stack/
14 }