Skip to content

Surprising impact of unused &mut #850

Closed
@jrmuizel

Description

@jrmuizel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-aliasingArea: This affects the aliasing model (Stacked/Tree Borrows)C-supportCategory: Not necessarily a bug, but someone asking for support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions