]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/stacked_borrows/return_invalid_mut_option.rs
Rollup merge of #101664 - mejrs:similarity, r=fee1-dead
[rust.git] / src / tools / miri / tests / fail / stacked_borrows / return_invalid_mut_option.rs
1 // Make sure that we cannot return a `&mut` that got already invalidated, not even in an `Option`.
2 // Due to shallow reborrowing, the error only surfaces when we look into the `Option`.
3 fn foo(x: &mut (i32, i32)) -> Option<&mut i32> {
4     let xraw = x as *mut (i32, i32);
5     let ret = unsafe { &mut (*xraw).1 }; // let-bind to avoid 2phase
6     let ret = Some(ret);
7     let _val = unsafe { *xraw }; // invalidate xref
8     ret
9 }
10
11 fn main() {
12     match foo(&mut (1, 2)) {
13         Some(_x) => {} //~ ERROR: /retag .* tag does not exist in the borrow stack/
14         None => {}
15     }
16 }