You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking Issue for pointer methods returning MaybeUninit<T> #75402
This is a tracking issue for methods on pointer types returning MaybeUninit: as_uninit_ref/mut and as_uninit_slice(_mut) on mutable and const raw pointers and NonNull.
The feature gate for the issue is #![feature(ptr_as_uninit)].
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
Merge Implementation (cc @rust-lang/T-libs, cc @RalfJung)
Currently, there is no unchecked non-null version for raw pointers without pointer casts. Either we want as_uninit_ref_unchecked or Option::unwrap_unchecked
A possible concern for stabilization is that these methods will be somewhat redundant if we end up deciding that references do not have to point to valid data -- though maybe reflecting the initialization state is a good idea even in that case? Also it seems likely that some T will not allow uninitialized references, i.e., there is demand for making &! uninhabited.
Having such cast on pointers rather than references could be useful in situations like in #93653 (although having cast on Box is even better in that case).
@RalfJung any good resource explaining why it'd be useful to allow uninitialized references? Anyway, I do like tracking (un)initializedness in type even for documentation value.
For documentation, it is the safety invariant of a type that is most relevant, and those certainly still apply recursively below references.
For the validity invariant, see rust-lang/unsafe-code-guidelines#77 for the discussion. Basically, specifying the language becomes a hell of a lot simpler if the validity invariant depends solely on the bit pattern of the value itself, and not on other state of the system such as the current contents of memory. I would argue that directly translates to making life easier for unsafe code authors (as one of the consumers of such a specification). Also generally I think we should require reasons to make something UB, the default should be not-UB. :)
I noticed that as_uninit_* on pointers take self by value (and pointers are Copy), e.g. see as_uninit_mut.
However, on NonNull, these functions take self by reference, e.g. see the function with the same name by for NonNull: as_uninit_mut takes self by mutable reference. Even more inconsistent, as_uninit_slice_mut returns a mutable reference, but takes self by immutable reference.
I think these methods should take self by value for consistency. The returned lifetime is unbounded anyways and not tied to the pointer/NonNull value anyways
Activity
RalfJung commentedon Aug 17, 2020
A possible concern for stabilization is that these methods will be somewhat redundant if we end up deciding that references do not have to point to valid data -- though maybe reflecting the initialization state is a good idea even in that case? Also it seems likely that some
Twill not allow uninitialized references, i.e., there is demand for making&!uninhabited.Rollup merge of rust-lang#75392 - TimDiekmann:non-null-uninit-slice, …
Kixunil commentedon Feb 4, 2022
Having such cast on pointers rather than references could be useful in situations like in #93653 (although having cast on
Boxis even better in that case).@RalfJung any good resource explaining why it'd be useful to allow uninitialized references? Anyway, I do like tracking (un)initializedness in type even for documentation value.
RalfJung commentedon Feb 4, 2022
For documentation, it is the safety invariant of a type that is most relevant, and those certainly still apply recursively below references.
For the validity invariant, see rust-lang/unsafe-code-guidelines#77 for the discussion. Basically, specifying the language becomes a hell of a lot simpler if the validity invariant depends solely on the bit pattern of the value itself, and not on other state of the system such as the current contents of memory. I would argue that directly translates to making life easier for unsafe code authors (as one of the consumers of such a specification). Also generally I think we should require reasons to make something UB, the default should be not-UB. :)
Raekye commentedon Apr 16, 2022
I noticed that
as_uninit_*on pointers takeselfby value (and pointers areCopy), e.g. seeas_uninit_mut.However, on
NonNull, these functions takeselfby reference, e.g. see the function with the same name by forNonNull:as_uninit_muttakesselfby mutable reference. Even more inconsistent,as_uninit_slice_mutreturns a mutable reference, but takesselfby immutable reference.I think these methods should take
selfby value for consistency. The returned lifetime is unbounded anyways and not tied to the pointer/NonNull value anywaysRalfJung commentedon Apr 16, 2022
NonNull::as_reftakes&self. It would seem odd forNonNull::as_uninit_refto diverge from that?Auto merge of rust-lang#96100 - Raekye:master, r=dtolnay
Auto merge of #96100 - Raekye:master, r=dtolnay
pnkfelix commentedon Sep 23, 2022
Visiting for T-compiler backlog bonanza
@RalfJung 's comments above were somewhat echo'ed by @m-ou-se in the meeting
@rustbot label: S-tracking-design-concerns
Rollup merge of rust-lang#130164 - RalfJung:const_ptr_as_ref, r=dtolnay
Rollup merge of rust-lang#130164 - RalfJung:const_ptr_as_ref, r=dtolnay
Unrolled build for rust-lang#130164
Auto merge of #96100 - Raekye:master, r=dtolnay
Rollup merge of rust-lang#158804 - yilin0518:fix_as_uninit_mut, r=Law…
Rollup merge of rust-lang#158804 - yilin0518:fix_as_uninit_mut, r=Law…
Unrolled build for #158804
Rollup merge of #158804 - yilin0518:fix_as_uninit_mut, r=LawnGnome