authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-10-06 02:52:20+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-10-06 02:52:20+02:00
logac247c9943385dce3d90647e26387914f65979b9
tree3d0db53b269a4fcf06b904babcc258192af03aa7
parent406e56ab698358ee9be6746b2328689bf203022e
parent7d3e0f815d4324c504b0d98323919d1980079ffa
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21331 from bobf/std-meta-DeclEnum-empty-struct

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

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

lib/std/meta.zig+4-1
......@@ -626,7 +626,7 @@ pub fn DeclEnum(comptime T: type) type {
626626 }
627627 return @Type(.{
628628 .@"enum" = .{
629 .tag_type = std.math.IntFittingRange(0, fieldInfos.len - 1),
629 .tag_type = std.math.IntFittingRange(0, if (fieldInfos.len == 0) 0 else fieldInfos.len - 1),
630630 .fields = &enumDecls,
631631 .decls = &decls,
632632 .is_exhaustive = true,
......@@ -652,9 +652,12 @@ test DeclEnum {
652652 pub const b: void = {};
653653 pub const c: f32 = 0;
654654 };
655 const D = struct {};
656
655657 try expectEqualEnum(enum { a }, DeclEnum(A));
656658 try expectEqualEnum(enum { a, b, c }, DeclEnum(B));
657659 try expectEqualEnum(enum { a, b, c }, DeclEnum(C));
660 try expectEqualEnum(enum {}, DeclEnum(D));
658661}
659662
660663pub fn Tag(comptime T: type) type {