-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
&[!] should have zero size #2754
Comments
Surely there is unsafe code out there which relies on the fact that |
Is there somewhere I should look to see the guarantees of object layout (before filing bugs like this)? edit: or is it the case that regardless of written guarantee, as a rule Rust doesn't change object layout in ways like this? |
Unfortunately layout guarantees are still a work in progress. https://doc.rust-lang.org/beta/reference/type-layout.html is the closest thing to an official source, but it's explicitly marked as non-authoritative, and is far from comprehensive. Work to improve this situation is mostly being done at the UCG repo https://github.com/rust-lang/unsafe-code-guidelines. In particular, I believe this specific issue is a partial duplicate of rust-lang/unsafe-code-guidelines#165. If I'm interpreting arrays-and-slices.md and pointers.md correctly, these draft documents (which are also not authoritative and would need to go through an RFC to become official guarantees of the language, but do represent the "state of the discussion" so far) together imply that |
I personally think this is getting too much into unspecified details so I'm going to move this over to the RFCs repo (and even that, IMO, is a bit questionable). |
I also just noticed a subtle mistake in the original post, which in this context might be helpful to point out:
You might have gotten |
Oops, I meant without the &, yes. :)
I agree. It outright says "The size of &[T] is two words." which IMO is enough to close this issue. I hope nobody minds if I do that myself.
Well, you could check if it has a size of 0. Almost anything unsafe you do with a slice of uninhabited (except for just copying / writing / reading in-place with no changes) will be UB already. And exact knowledge of the layout is actually more difficult: transmute instead of from_raw_parts/len/as_ptr, or a hardcoded constant instead of size_of... I would expect generic unsafe code to use the provided APIs rather than transmuting all over the place, because it's easier, clearer, safer, and doesn't rely on un-finalized documentation. Maybe I am being optimistic.
I think so -- the result of this issue would be substantially informed by what the decision is on the layout of |
I'm not sure how reasonable this is, but since
&[!]
can only ever be the empty slice, I was hoping it would be represented the same way as&[!; 0]
and()
, with zero size and knowledge that it must be empty.e.g. I was hoping this would become a function taking a parameter of zero size and executing
xor eax, eax
:I have no real use case except showing off cool tricks with
!
. Maybe this would be useful in generic code, although at least for simple cases where the actual value can get threaded through, it already optimizes fine. (e.g. a call oflength_never_slice(&vec![])
will become a hardcoded constant as one expects.)The text was updated successfully, but these errors were encountered: