Skip to content

Commit

Permalink
Merge pull request #21331 from bobf/std-meta-DeclEnum-empty-struct
Browse files Browse the repository at this point in the history
Prevent failure with empty struct in `std.meta.DeclEnum`
  • Loading branch information
alexrp authored Oct 6, 2024
2 parents 406e56a + 7d3e0f8 commit ac247c9
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,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,
Expand All @@ -652,9 +652,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 {
Expand Down

0 comments on commit ac247c9

Please sign in to comment.