diff options
| author | Bob Farrell <git@bob.frl> | 2024-09-07 15:20:55 +0100 |
|---|---|---|
| committer | Bob Farrell <git@bob.frl> | 2024-09-07 15:20:55 +0100 |
| commit | 7d3e0f815d4324c504b0d98323919d1980079ffa (patch) | |
| tree | f80b75d358627e42df20cda9f15439fa76b2bd15 /lib/std | |
| parent | 5f3d9e0b7a67b8a23b659ca7dada8641f55b8503 (diff) | |
| download | zig-7d3e0f815d4324c504b0d98323919d1980079ffa.tar.gz zig-7d3e0f815d4324c504b0d98323919d1980079ffa.zip | |
Prevent failure with empty struct in `std.meta.DeclEnum`
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/meta.zig | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index e7ea5b5f0e..64d2cbdbab 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -628,7 +628,7 @@ pub fn DeclEnum(comptime T: type) type { } return @Type(.{ .@"enum" = .{ - .tag_type = std.math.IntFittingRange(0, fieldInfos.len - 1), + .tag_type = std.math.IntFittingRange(0, if (fieldInfos.len == 0) 0 else fieldInfos.len - 1), .fields = &enumDecls, .decls = &decls, .is_exhaustive = true, @@ -654,9 +654,12 @@ test DeclEnum { pub const b: void = {}; pub const c: f32 = 0; }; + const D = struct {}; + try expectEqualEnum(enum { a }, DeclEnum(A)); try expectEqualEnum(enum { a, b, c }, DeclEnum(B)); try expectEqualEnum(enum { a, b, c }, DeclEnum(C)); + try expectEqualEnum(enum {}, DeclEnum(D)); } pub fn Tag(comptime T: type) type { |
