So I was reading the Stacked Borrows Implemented blog post, and realized that the following is not considered UB by stacked borrows:
fn main() {
let x = &mut 1u8;
// Create raw pointer
let y = x as *mut u8;
// Assert uniqueness of x
*x = 7;
// Create another raw pointer
x as *mut u8;
// Read x through y, which was created before we asserted uniqueness.
let _val = unsafe { *y };
}
I found it really surprising that reading through y
is not considered UB here, because in my mental model, y
would have been invalidated by asserting uniqueness with the write.
Note that if you remove the immediately-ignored raw pointer, miri will complain. I don't have any questions, I just found this surprising and wanted to share it.