-
Notifications
You must be signed in to change notification settings - Fork 59
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
Is it safe to treat an arbitrary reference as an &[()]
?
#557
Comments
There's no issue with the cast in this direction, as references to zero bytes are always valid to dereference1. If the intent is to cast back in the other direction, though, that runs into the usual It's also relevant to note that not all API which deals in slices is certain to maintain the input provenance for references to ZST (although std does attempt to do so). Footnotes
|
Another possible issue for the other direction is interior mutability
i.e. if the generic source type |
Yes. For references to ZST, the only things you have to worry about are null and alignment. But if you start with a reference, it is nun-null, and
Casting back does not work at all under Stacked Borrows, even without an But the OP did not ask about casting back.
Rust does not have those. The only aliasing rules we have are the ones coming from uniqueness of |
Thanks!!
Yeah I tried to mention that in my post and I accidentally said ICU4X instead of Rust. I figured it would be fine, but I've also learned to be wary of ZST semantics. And yep, casting back is much more clearly a provenance issue, fortunately I don't care about that operation! |
The question seems to be answered then :) |
&[()]
and other ZST references/slices point to empty regions of memory, but they can have a pointer value.Would there ever be a safety hazard with the following API?
What about a generic source type T? What about cases where the produced slice is always empty?
This falls afoul of C++-style strict aliasing rules which we do not have in ICU4X Rust, but I'm not clear on what this means for pointer provenance stuff.
The text was updated successfully, but these errors were encountered: