authorgravatar for git@bob.frlBob Farrell <git@bob.frl> 2024-09-07 15:20:55+01:00
committergravatar for git@bob.frlBob Farrell <git@bob.frl> 2024-09-07 15:20:55+01:00
log7d3e0f815d4324c504b0d98323919d1980079ffa
treef80b75d358627e42df20cda9f15439fa76b2bd15
parent5f3d9e0b7a67b8a23b659ca7dada8641f55b8503

Prevent failure with empty struct in `std.meta.DeclEnum`


1 files changed, 4 insertions(+), 1 deletions(-)

lib/std/meta.zig+4-1
......@@ -628,7 +628,7 @@ pub fn DeclEnum(comptime T: type) type {
628628 }
629629 return @Type(.{
630630 .@"enum" = .{
631 .tag_type = std.math.IntFittingRange(0, fieldInfos.len - 1),
631 .tag_type = std.math.IntFittingRange(0, if (fieldInfos.len == 0) 0 else fieldInfos.len - 1),
632632 .fields = &enumDecls,
633633 .decls = &decls,
634634 .is_exhaustive = true,
......@@ -654,9 +654,12 @@ test DeclEnum {
654654 pub const b: void = {};
655655 pub const c: f32 = 0;
656656 };
657 const D = struct {};
658
657659 try expectEqualEnum(enum { a }, DeclEnum(A));
658660 try expectEqualEnum(enum { a, b, c }, DeclEnum(B));
659661 try expectEqualEnum(enum { a, b, c }, DeclEnum(C));
662 try expectEqualEnum(enum {}, DeclEnum(D));
660663}
661664
662665pub fn Tag(comptime T: type) type {