authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-22 19:21:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-22 19:21:50-07:00
log8dd737801355427c558876bbcb299926d47b8269
treeb54aa4604b6b164b56483efcfbcd4c2bd0ebb4ce
parent329a359974c29d62931c32ca85aa42aa9e4847f7

delete packed enums from the language

No need for any such thing. Instead, provide an integer tag type for the enum.

2 files changed, 7 insertions(+), 26 deletions(-)

doc/langref.html.in+1-20
...@@ -2588,7 +2588,7 @@ test "default struct initialization fields" {...@@ -2588,7 +2588,7 @@ test "default struct initialization fields" {
2588 exactly their bit width.2588 exactly their bit width.
2589 </li>2589 </li>
2590 <li>{#syntax#}bool{#endsyntax#} fields use exactly 1 bit.</li>2590 <li>{#syntax#}bool{#endsyntax#} fields use exactly 1 bit.</li>
2591 <li>A {#link|packed enum#} field uses exactly the bit width of its integer tag type.</li>2591 <li>An {#link|enum#} field uses exactly the bit width of its integer tag type.</li>
2592 <li>A {#link|packed union#} field uses exactly the bit width of the union field with2592 <li>A {#link|packed union#} field uses exactly the bit width of the union field with
2593 the largest bit width.</li>2593 the largest bit width.</li>
2594 <li>Non-ABI-aligned fields are packed into the smallest possible2594 <li>Non-ABI-aligned fields are packed into the smallest possible
...@@ -2983,25 +2983,6 @@ export fn entry(foo: Foo) void { }...@@ -2983,25 +2983,6 @@ export fn entry(foo: Foo) void { }
2983 {#code_end#}2983 {#code_end#}
2984 {#header_close#}2984 {#header_close#}
29852985
2986 {#header_open|packed enum#}
2987 <p>By default, the size of enums is not guaranteed.</p>
2988 <p>{#syntax#}packed enum{#endsyntax#} causes the size of the enum to be the same as the size of the
2989 integer tag type of the enum:</p>
2990 {#code_begin|test#}
2991const std = @import("std");
2992
2993test "packed enum" {
2994 const Number = packed enum(u8) {
2995 one,
2996 two,
2997 three,
2998 };
2999 std.testing.expect(@sizeOf(Number) == @sizeOf(u8));
3000}
3001 {#code_end#}
3002 <p>This makes the enum eligible to be in a {#link|packed struct#}.</p>
3003 {#header_close#}
3004
3005 {#header_open|Enum Literals#}2986 {#header_open|Enum Literals#}
3006 <p>2987 <p>
3007 Enum literals allow specifying the name of an enum field without specifying the enum type:2988 Enum literals allow specifying the name of an enum field without specifying the enum type:
lib/std/os/linux/bpf.zig+6-6
...@@ -398,10 +398,10 @@ pub const Insn = packed struct {...@@ -398,10 +398,10 @@ pub const Insn = packed struct {
398398
399 /// r0 - r9 are general purpose 64-bit registers, r10 points to the stack399 /// r0 - r9 are general purpose 64-bit registers, r10 points to the stack
400 /// frame400 /// frame
401 pub const Reg = packed enum(u4) { r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10 };401 pub const Reg = enum(u4) { r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10 };
402 const Source = packed enum(u1) { reg, imm };402 const Source = enum(u1) { reg, imm };
403403
404 const Mode = packed enum(u8) {404 const Mode = enum(u8) {
405 imm = IMM,405 imm = IMM,
406 abs = ABS,406 abs = ABS,
407 ind = IND,407 ind = IND,
...@@ -410,7 +410,7 @@ pub const Insn = packed struct {...@@ -410,7 +410,7 @@ pub const Insn = packed struct {
410 msh = MSH,410 msh = MSH,
411 };411 };
412412
413 const AluOp = packed enum(u8) {413 const AluOp = enum(u8) {
414 add = ADD,414 add = ADD,
415 sub = SUB,415 sub = SUB,
416 mul = MUL,416 mul = MUL,
...@@ -426,14 +426,14 @@ pub const Insn = packed struct {...@@ -426,14 +426,14 @@ pub const Insn = packed struct {
426 arsh = ARSH,426 arsh = ARSH,
427 };427 };
428428
429 pub const Size = packed enum(u8) {429 pub const Size = enum(u8) {
430 byte = B,430 byte = B,
431 half_word = H,431 half_word = H,
432 word = W,432 word = W,
433 double_word = DW,433 double_word = DW,
434 };434 };
435435
436 const JmpOp = packed enum(u8) {436 const JmpOp = enum(u8) {
437 ja = JA,437 ja = JA,
438 jeq = JEQ,438 jeq = JEQ,
439 jgt = JGT,439 jgt = JGT,