diff options
| author | Mitchell Hashimoto <m@mitchellh.com> | 2025-09-27 14:42:59 -0700 |
|---|---|---|
| committer | Mitchell Hashimoto <m@mitchellh.com> | 2025-09-27 14:43:01 -0700 |
| commit | 6a0a94c82728d93440fc6f693154ec5aa4c79dc9 (patch) | |
| tree | 235c8c24134d727fa5604dd24b4004039009ec36 /src/lib | |
| parent | 397e47c274fb5077e16b37f28c8f9327cc995b2e (diff) | |
lib: fix holes handling for C
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/enum.zig | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/lib/enum.zig b/src/lib/enum.zig index 063232176..c3971ebde 100644 --- a/src/lib/enum.zig +++ b/src/lib/enum.zig @@ -22,15 +22,25 @@ pub fn Enum( ) type { var fields: [keys.len]std.builtin.Type.EnumField = undefined; var fields_i: usize = 0; - for (keys, 0..) |key_, key_i| { - const key: [:0]const u8 = key_ orelse switch (target) { - .c => std.fmt.comptimePrint("__unused_{d}", .{key_i}), - .zig => continue, + var holes: usize = 0; + for (keys) |key_| { + const key: [:0]const u8 = key_ orelse { + switch (target) { + // For Zig we don't track holes because the enum value + // isn't guaranteed to be stable and we want to use the + // smallest possible backing type. + .zig => {}, + + // For C we must track holes to preserve ABI compatibility + // with subsequent values. + .c => holes += 1, + } + continue; }; fields[fields_i] = .{ .name = key, - .value = fields_i, + .value = fields_i + holes, }; fields_i += 1; } |
