Skip to content

Document that it is okay to temporarily have invalid data behind an &mut T #386

Closed
@RalfJung

Description

@RalfJung

As part of this discussion, it seems we have general consensus that "it is acceptable to write bytes through a pointer derived from &mut T even if those bytes make the T invalid so long as the T is made valid again before the lifetime that would be given to the pointer if it were a reference expires and the parent &mut T is usable again."

Specifically, this is fine:

fn main() {
    let mut b = false;
    let ptr = &mut b as *mut bool as *mut u8;
    unsafe {
        *ptr = 2;
        *ptr -= 1;
    }
    assert!(b);
}

We should probably document this as UCG consensus, and maybe even find a way to get t-lang to sign off on this, though it seems unclear how to best do that.

This is helpful because it means the code in rust-lang/rfcs#2195 is fine without having to weaken the notion of validity of those enums specifically.

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