rust/tests/fail/stacked_borrows/illegal_read7.rs
Ben Kimock 14e72e7ffa Improve information sharing across SB diagnostics
Previous Stacked Borrows diagnostics were missing a lot of information
about the state of the interpreter, and it was difficult to add
additional state because it was threaded through all the intervening
function signatures.

This change factors a lot of the arguments which used to be passed
individually to many stacked borrows functions into a single
`DiagnosticCx`, which is built in `Stacks::for_each`, and since it
wraps a handle to `AllocHistory`, we can now handle more nuanced
things like heterogeneous borrow of `!Freeze` types.
2022-08-18 15:01:03 -04:00

23 lines
911 B
Rust

// Creating a shared reference does not leak the data to raw pointers,
// not even when interior mutability is involved.
use std::cell::Cell;
use std::ptr;
fn main() {
unsafe {
let x = &mut Cell::new(0);
let raw = x as *mut Cell<i32>;
let x = &mut *raw;
let _shr = &*x;
// The state here is interesting because the top of the stack is [Unique, SharedReadWrite],
// just like if we had done `x as *mut _`.
// If we said that reading from a lower item is fine if the top item is `SharedReadWrite`
// (one way to maybe preserve a stack discipline), then we could now read from `raw`
// without invalidating `x`. That would be bad! It would mean that creating `shr`
// leaked `x` to `raw`.
let _val = ptr::read(raw);
let _val = *x.get_mut(); //~ ERROR: /retag .* tag does not exist in the borrow stack/
}
}