authorgravatar for 2153080+gcoakes@users.noreply.github.comGregory Oakes <2153080+gcoakes@users.noreply.github.com> 2022-11-19 23:37:29-06:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-04 14:22:16+02:00
log8d17e90fcd3c54da5a271ad13eb114822c22d8da
tree24930ced2bff0351dbd99283974eb6b6cc34185d
parentf094c4bce513f2b17f9c29ae40c244626f945fb4

std: add a special case for empty structs in meta.FieldEnum.

Empty structs would previously result in a compilation error.

1 files changed, 17 insertions(+), 0 deletions(-)

lib/std/meta.zig+17
......@@ -657,6 +657,20 @@ test "std.meta.tags" {
657657pub fn FieldEnum(comptime T: type) type {
658658 const field_infos = fields(T);
659659
660 if (field_infos.len == 0) {
661 // TODO simplify when stage1 is removed
662 if (@import("builtin").zig_backend == .stage1) @compileError("stage1 doesn't allow empty enums");
663 return @Type(.{
664 .Enum = .{
665 .layout = .Auto,
666 .tag_type = u0,
667 .fields = &.{},
668 .decls = &.{},
669 .is_exhaustive = true,
670 },
671 });
672 }
673
660674 if (@typeInfo(T) == .Union) {
661675 if (@typeInfo(T).Union.tag_type) |tag_type| {
662676 for (std.enums.values(tag_type)) |v, i| {
......@@ -728,6 +742,9 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {
728742}
729743
730744test "std.meta.FieldEnum" {
745 if (comptime @import("builtin").zig_backend != .stage1) {
746 try expectEqualEnum(enum {}, FieldEnum(struct {}));
747 }
731748 try expectEqualEnum(enum { a }, FieldEnum(struct { a: u8 }));
732749 try expectEqualEnum(enum { a, b, c }, FieldEnum(struct { a: u8, b: void, c: f32 }));
733750 try expectEqualEnum(enum { a, b, c }, FieldEnum(union { a: u8, b: void, c: f32 }));