Skip to content
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

Proposal: packed struct(Backing) -> packed(Backing) struct, support packed(Backing) union #19395

Closed
rohlem opened this issue Mar 22, 2024 · 7 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@rohlem
Copy link
Contributor

rohlem commented Mar 22, 2024

Background

In status-quo, packed struct and packed union types are guaranteed to occupy the minimum number of bits required.
For packed struct this is the sum of the number of bits of their fields,
for packed union (which status-quo requires to be untagged), it's the maximum number of bits of its states.

Status-quo allows the syntax packed struct(Backing) to specify the backing integer type,
which is a convenient way to have the compiler assert the number of bits occupied.
(side note: currently undocumented in the corresponding langref section.)
I think the same mechanism would be useful to have for packed union types.

I assume the reason this is currently not supported by the language is that
union(X) is used to specify X as the tag type,
and overloading it to instead specify X as the backing type in packed union(X) would be confusing.

Proposal

I propose the backing type to instead become the syntactic "argument" of the packed keyword.
With this, status-quo packed struct(Backing) becomes packed(Backing) struct,
and packed(Backing) union becomes unambiguously expressible.

I then secondly propose supporting packed(Backing) union in the language.
std.builtin.Type.Union would have to gain field backing_integer: ?type from std.builtin.Type.Struct
in order for @typeInfo and @Type to integrate with this additional information.

Potential drawbacks

This slightly reduces grep-ability for packed struct syntax - I'm not sure how often this comes up in workflows.
If that is a concern, a mitigation against this would be to order packed after the container type,
yielding f.e. struct packed, struct packed(u16), union packed(u12).

@InKryption
Copy link
Contributor

Small bikeshed: I think it would make sense to make ContainerLayout be a tagged union, wherein the @"packed" member is of type type, as for any state where the layout isn't packed, it's illegal to specify the backing_integer type - unless specifying null for .@"packed" is made to mean "infer the backing type", as is what happens for field alignment = 0.

@rohlem
Copy link
Contributor Author

rohlem commented Mar 22, 2024

@InKryption Sure, that's a larger change than necessary for the proposal, but it does better reflect the logic of status-quo.
Note that this would require reverting should the language ever allow an explicitly-sized extern type, f.e. packed(u16) extern struct, in the future.

@nektro
Copy link
Contributor

nektro commented Mar 22, 2024

I dont read packed struct(T) as T modifying the struct keyword. packed struct is a single entity kind that doesnt need parentheses to be instantiated. inline fn is another example.

@nektro
Copy link
Contributor

nektro commented Mar 22, 2024

if this were to change I'd prefer struct packed(T) but I feel that needlessly hurts readability.

@sin-ack
Copy link
Contributor

sin-ack commented Mar 22, 2024

  • What if packed struct was |_| struct

    • What if packed(backingInt) struct was |backingInt| struct

If this syntax is already used to capture values, maybe it can go in the other direction of wrangling them too.

That won't work. It makes the syntax require lookaheads and looks terribly inconsistent overall.

@expikr
Copy link
Contributor

expikr commented Mar 23, 2024

bits {...}

@sin-ack
Copy link
Contributor

sin-ack commented Mar 23, 2024

bits sounds incredibly vague, and doesn't solve the problem in the original issue.

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jun 2, 2024
@Vexu Vexu added this to the 0.14.0 milestone Jun 2, 2024
@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2024
@Vexu Vexu modified the milestones: 0.14.0, 0.13.0 Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

7 participants