Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RalfJung opened this issue Dec 28, 2022 · 3 comments
Closed

Comments

@RalfJung
Copy link
Member

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.

@Darksonn
Copy link

This seems like it's just a weaker version of #346.

@RalfJung
Copy link
Member Author

RalfJung commented Dec 28, 2022 via email

@RalfJung
Copy link
Member Author

RalfJung commented Jun 6, 2023

Closing in favor of #346

@RalfJung RalfJung closed this as completed Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants