Closed
Description
The following code fails with miri
use std::ptr::NonNull;
use std::cell::Cell;
struct F {
x: Cell<u32>,
y: u32
}
impl F {
fn new() -> F {
F { x: Cell::new(5), y: 9 }
}
}
#[test]
fn it_works() {
let mut b = Box::new(F::new());
let p: NonNull<F> = (&*b).into();
fn f(rr: &F, mut p: NonNull<F>) {
let r = unsafe { p.as_ref() };
r.x.set(9);
}
{
let m = &mut *b;
}
f(&b, p);
b.y = 5;
}
error[E0080]: Miri evaluation error: not granting access to tag <untagged> because incompatible item is protected: [SharedReadWrite for <79372> (call 36446)]
--> /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:430:31
|
430 | mem::replace(unsafe { &mut *self.value.get() }, val)
| ^^^^^^^^^^^^^^^^^^^^^^ Miri evaluation error: not granting access to tag <untagged> because incompatible item is protected: [SharedReadWrite for <79372> (call 36446)]
|
= note: inside call to `std::cell::Cell::<u32>::replace` at /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:387:19
note: inside call to `std::cell::Cell::<u32>::set` at src/lib.rs:23:9
--> src/lib.rs:23:9
|
23 | r.x.set(9);
| ^^^^^^^^^^
note: inside call to `it_works::f` at src/lib.rs:29:5
--> src/lib.rs:29:5
|
29 | f(&b, p);
| ^^^^^^^^
note: inside call to `it_works` at src/lib.rs:17:1
--> src/lib.rs:17:1
|
17 | / fn it_works() {
18 | | let mut b = Box::new(F::new());
19 | | let p: NonNull<F> = (&*b).into();
20 | |
... |
30 | | b.y = 5;
31 | | }
| |_^
Commenting out let m = &mut *b;
causes miri to succeed. I found this surprising as I wouldn't have expected let m = &mut *b; to have an effect.