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

Prevent failure with empty struct in `std.meta.DeclEnum`
This commit is contained in:
Alex Rønne Petersen 2024-10-06 02:52:20 +02:00 committed by GitHub
commit ac247c9943
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -626,7 +626,7 @@ pub fn DeclEnum(comptime T: type) type {
} }
return @Type(.{ return @Type(.{
.@"enum" = .{ .@"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, .fields = &enumDecls,
.decls = &decls, .decls = &decls,
.is_exhaustive = true, .is_exhaustive = true,
@ -652,9 +652,12 @@ test DeclEnum {
pub const b: void = {}; pub const b: void = {};
pub const c: f32 = 0; pub const c: f32 = 0;
}; };
const D = struct {};
try expectEqualEnum(enum { a }, DeclEnum(A)); try expectEqualEnum(enum { a }, DeclEnum(A));
try expectEqualEnum(enum { a, b, c }, DeclEnum(B)); try expectEqualEnum(enum { a, b, c }, DeclEnum(B));
try expectEqualEnum(enum { a, b, c }, DeclEnum(C)); try expectEqualEnum(enum { a, b, c }, DeclEnum(C));
try expectEqualEnum(enum {}, DeclEnum(D));
} }
pub fn Tag(comptime T: type) type { pub fn Tag(comptime T: type) type {