-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
NonNull methods for pointer offset, add, sub #72429
Comments
Why would they? Or you you mean non-nullness (but you said that is not a concern)? |
I mean non-null-ness yes. Non-null-ness is not a concern as long as we stick to the rules we have, which are partly based on requirements from the llvm backend. The whole paragraph tries to look beyond just LLVM |
I'm in favor of adding these methods since it will make |
I'd like these too. Is anyone willing to get the ball rolling on this? |
It looks like this has been implemented, tracking issue is #117691 |
Okay seems like we can close this issue then :) |
It would be beneficial to low-level code to add methods like
offset
,add
,sub
nonNonNull<T>
, so that pointer offsetting can be done without converting back and forth to raw pointers. These methods would beunsafe
just like the corresponding raw pointer methods, returnNonNull<T>
and have the same semantics.These methods will work well because such pointer offsetting is only valid inside the same allocated object; when offsetting it is not allowed to "leave" the current object, it can because of that never result in a null pointer. (For example, LLVM documents that an inbounds GEP on a non-null pointer must not result in a null pointer.)
For this reason, the safety rules that need to be followed for correct use of
<NonNull<T>>::offset
are the same as for<*mut T>::offset
, and the method can be offered on the same terms, as an unsafe method.This is for the moment implemented in at least one crate -
rawpointer
and was a necessary feature for usingNonNull<T>
inndarray
.Example implementation
The drawback of these methods is that while raw pointer
offset
has tricky requirements (offset inside the same allocation) due to the code generation back-end, the new nonnulloffset
methods will add extra requirements on top of that (Rust-level value validity); that these two restrictions go hand in hand, is just a consequence of the current back-end. Would it be possible to imagine a "nicer" Rust that didn't have these UB traps for pointer offsetting?The text was updated successfully, but these errors were encountered: