-
Notifications
You must be signed in to change notification settings - Fork 58
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
Layout of single-field structs #34
Comments
I believe we should make this guarantee. I would further like to say that it applies to any struct with a single field of non-zero size (as determined b the other layout rules). I'm not sure if that is something we can always do, though. I'm thinking of complications around unsizing, for example.. do they potentially apply? (In particular, might that force a larger alignment or some such thing?) |
This would automatically fall out of #36, correct? |
I am a big +1 on making newtyping a layout (but not ABI) no-op Also I believe we should have a rule that layout operates as if types with (size: 0, align: 1) have been removed. (So PhantomData newtypes are also nops) |
If we adopt what @gankro said, then this is indeed a subset of #36: After removing (size: 0, align: 1)-types, a "single-field struct" (according to the definition, having a single non-ZST field where I guess we mean ZST with alignment 1) is certainly homogeneous. So if we say those are like an array, we'd get that single-field structs are like 1-element arrays. Should we guarantee that those are like their element type (except for ABI)? Seems sensible to me. |
As far as I know the C standard does not technically guarantee that an array of one |
Would the compiler be allowed to change the layout of types containing a #[repr(transparent)] type relative to the type it is wrapping?
Would this (hypothetical) function return true for all Edit:
|
Is there any particular reason why you think |
That was just in case that it made a difference. |
There's been some interesting examples of why the "single-field layout guarantee" would be useful in rust-lang/rust#53514. |
As @gankro said, I think that this issue basically proposing the answer is "no" -- and further that |
offtopic: do we have a definition of |
@gnzlbg There is no central listing of all the knobs, but that seems like a good thing for the language reference. The structs section includes some discussion of struct-specific representation. |
Could there be optimization cases where it'd be desirable to over-align a struct based on its usage? Say you have a |
PR #31 identified this as an area for continued discussion. Specifically, if you have a struct with single field (
struct Foo { x: T }
), should we guarantee that the memory layout ofFoo
is identical to the memory layout ofT
(note that ABI details around function calls may still draw a distinction, which is why#[repr(transparent)]
is needed). What about zero-sized types likePhantomData
?@rkruppe wrote:
The text was updated successfully, but these errors were encountered: