-
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
Guarantee the layout of structs with a single non-zero-sized field #164
Conversation
Can we make this guarantee stronger? It seems like as long as all ZST values have an alignment less than or equal to the Non-ZST value that layout should be unaffected. eg: |
|
We could. This was not discussed in the issue, but I suppose that we can make it stronger as a follow up. |
@rkruppe @RalfJung what do you think about strengthening the guarantee to:
? |
I don't think we should innovate here (has this wording ever popped up anywhere before?), especially as @Lokathor's motivation for proposing it (PhantomData) doesn't need this stronger guarantee after all. The only additional things this strengthening covers are empty arrays and user-declared ZSTs with an explicit |
Unblocking as the 1-ZST PR landed. Or was this blocked on something else? |
b0f2ef5
to
b281933
Compare
So I've updated this to use the "1-ZST" terminology and noticed two things:
struct S([0; u8], [0; u8], [0; u16]); to be the same as that of I am not sure whether that is worth doing, and tend to agree with @rkruppe that the simple guarantee is enough. Just food for thought. What isn't clear to me is how we could generalize such a guarantee in the spirit of what @Lokathor proposed here, but without mentioning that one type is a non-zero-sized type. |
I think this is what we should do. It is most consistent. The current definition leaves a weird "gap". |
I think this might be due to the mismatch mentioned/"gap" in my previous post. Or do you think this is hard to read? "The default layout of structs where only a single field is not a 1-ZST, is the same as the layout of said field". |
b281933
to
e6a2db1
Compare
008158c
to
8df48e4
Compare
Done. |
In fact, I personally would just go for the stronger statement that for struct layout, 1-ZST are ignored. I don't see any reason to guarantee/propose some instances of that without guaranteeing/proposing the general, simpler to explain and more useful, principle. Both the current content of this PR and "structs consisting only of 1-ZST are 1-ZST" then basically just fall out of that principle, and the additional orthogonal statemenets
|
struct S2(Zst2, Zst1); | ||
``` | ||
|
||
the layout of `S1` is the same as that of `i32` and the layout of `S2` as that of `Zst2`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is saying more than just "1-ZST fields are ignored".
You are also assuming here that single-field structs have layout identical to their only field. That is a separate statement, and should IMO get a separate section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This comment is still open.)
"1-ZST fields are ignored for layout" seems to go beyond what got consensus back when struct layout was discussed. For example, I think it would mean that all instantiations of this type (i.e., regardless of the unit) have the same layout: struct Vec2<Unit> {
x: f32,
y: f32,
_unit: PhantomData<Unit>
} but as was discussed extensively, there is some desire to keep open the possibilities of randomized and profile-driven type layout decision, and none of the very small restrictions on that (single-field structs, single-variant enums, etc.) that were proposed without anyone objecting cover this. Don't get me wrong, I am personally also very much in favor of layout ignoring 1-ZSTs universally (and many other things besides). But I don't think we should just "sneak in" something which was explicitly discussed and didn't get consensus. Edit: on second thought I'm not so sure anymore, since arguably |
I don't see how it would say that. Those are still all separate instances of a generic type. The 1-ZST is ignored, but the type parameter is not. |
I realized the same thing after posting and just finished editing it in 😅 |
@rkruppe in other words, we don't guarantee that the following two have the same layout: struct Vec2_1 {
x: f32,
y: f32,
}
struct Vec2_2 {
x: f32,
y: f32,
} Likewise, we don't guarantee that all instance of these have the same layout: struct Vec2Plus<T> {
x: f32,
y: f32,
plus: *mut T,
} No ZST are involved in any of this. Why would the ZST in your example make any difference? The fact that EDIT: Ah, you part-way agree. Well see this post for why we need that lawyering anyway, and the ZST question is entirely orthogonal. ;) |
Opened a PR for my remaining comments: gnzlbg#2 |
rearrange a bit and be more explicit about how our rules interact
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@rkruppe what do you think?
I'm perpetually confused by what any given person means by "layout", didn't we at some point agree to stop using that term to prevent confusion? Assuming that in this diff "layout" = size, align, field offsets and padding, possibly niches but definitely not call ABI, the content seems fine. But I'm unsure whether, editorially speaking, we should already be avoiding "layout" in new paragraphs or if that's gonna be a separate clean-up PR that goes over all the documents. |
That would be #153. I think the plan (like in the other PR) was to land this PR with the vague wording so that we have some examples to work with when finalizing the terminology. It's much easier to do that when you have some examples. |
What we mean by layout here will be precised in #153 . |
This PR guarantees the layout of structs with a single non-zero-sized field to be the same layout as that of that field if the alignment requirements of all other fields is 1.
Closes #34 .