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

Confusing MaybeUninit documentation #65216

Open
DutchGhost opened this issue Oct 8, 2019 · 3 comments
Open

Confusing MaybeUninit documentation #65216

DutchGhost opened this issue Oct 8, 2019 · 3 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@DutchGhost
Copy link
Contributor

DutchGhost commented Oct 8, 2019

The as_ptr method on a MaybeUninit<T> perhaps could use some better wording, or at least an example.

The docs say: "Writing to memory that this pointer (non-transitively) points to is undefined behavior (except inside an UnsafeCell)."

Its unclear whether that means &MaybeUninit<UnsafeCell<T>> -> *const UnsafeCell<T> -> *mut T is okey, or that &UnsafeCell<MaybeUninit<T>> -> *mut MaybeUninit<T> -> *mut T is okey, if the resulting mutable pointer is written to.

Perhaps adding a little example of which of the two is okey, (or perhaps both) would help.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 8, 2019
@Centril
Copy link
Contributor

Centril commented Oct 9, 2019

cc @RalfJung

@RalfJung
Copy link
Member

RalfJung commented Oct 9, 2019

Yeah this can probably be improved. Note that quite a few as_ptr methods have a remark like this, e.g. also see slice::as_ptr.

I am not sure what the best place for this is though; there are lots of examples one could wonder about and we probably don't want to list all of them in each as_ptr method. It would probably make more sense to have a chapter on interior mutability in the Referene or the Nomicon, and link there.

To be clear, &MaybeUninit<UnsafeCell<T>> -> *const UnsafeCell<T> -> *mut T is okay because the UnsafeCell is present in the shared reference already. &UnsafeCell<MaybeUninit<T>> -> *mut MaybeUninit<T> -> *mut T does not even really have anything to do with MaybeUninit, that's just using UnsafeCell directly from what I can see.

@joshlf
Copy link
Contributor

joshlf commented Sep 21, 2022

Ran into a similar issue with slice::from_raw_parts. IIUC, the requirement is that, if mutation happens, it must be the case that all code paths view the bytes that are being mutated as inside of an UnsafeCell. Ie, having an &UnsafeCell<T> and a &T pointing to the same memory location and mutating via the former is unsound. Is that right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants