Skip to content

is it UB or not if using cell's underlying data raw ptr after cell is moved #2796

Closed
@gftea

Description

@gftea

rust playground

I am a bit confused about the "Cell::as_ptr()", user can escape the compiler check, and may still use the raw pointer after the cell is moved.

#[derive(Debug)]
struct Inner(isize);

impl Drop for Inner {
    fn drop(&mut self) {
        println!("drop inner {:?}", self as *const _);
    }
}

let m = Cell::new(Inner(0));
let m_ptr = m.as_ptr();

println!("before move: {:?}", m.as_ptr());

let h = thread::spawn(move || {
    // the `m` cell underlying data has different address after move
    println!("after move: {:?}", m.as_ptr());
    // `m` drop
});

// we can still read/write the original address after moving `m`
// should it be UB? 
// if yes, miri does not report it
// If not, what does the address really means?
unsafe {println!("m_ptr: {:?}, value: {:?}",  m_ptr, *m_ptr)};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions