diff options
| author | Gregory Oakes <2153080+gcoakes@users.noreply.github.com> | 2022-11-19 23:37:29 -0600 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-12-04 14:22:16 +0200 |
| commit | 8d17e90fcd3c54da5a271ad13eb114822c22d8da (patch) | |
| tree | 24930ced2bff0351dbd99283974eb6b6cc34185d /lib/std/meta.zig | |
| parent | f094c4bce513f2b17f9c29ae40c244626f945fb4 (diff) | |
| download | zig-8d17e90fcd3c54da5a271ad13eb114822c22d8da.tar.gz zig-8d17e90fcd3c54da5a271ad13eb114822c22d8da.zip | |
std: add a special case for empty structs in meta.FieldEnum.
Empty structs would previously result in a compilation error.
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index e75108e101..cdfb16806f 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -657,6 +657,20 @@ test "std.meta.tags" { pub fn FieldEnum(comptime T: type) type { const field_infos = fields(T); + if (field_infos.len == 0) { + // TODO simplify when stage1 is removed + if (@import("builtin").zig_backend == .stage1) @compileError("stage1 doesn't allow empty enums"); + return @Type(.{ + .Enum = .{ + .layout = .Auto, + .tag_type = u0, + .fields = &.{}, + .decls = &.{}, + .is_exhaustive = true, + }, + }); + } + if (@typeInfo(T) == .Union) { if (@typeInfo(T).Union.tag_type) |tag_type| { for (std.enums.values(tag_type)) |v, i| { @@ -728,6 +742,9 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void { } test "std.meta.FieldEnum" { + if (comptime @import("builtin").zig_backend != .stage1) { + try expectEqualEnum(enum {}, FieldEnum(struct {})); + } try expectEqualEnum(enum { a }, FieldEnum(struct { a: u8 })); try expectEqualEnum(enum { a, b, c }, FieldEnum(struct { a: u8, b: void, c: f32 })); try expectEqualEnum(enum { a, b, c }, FieldEnum(union { a: u8, b: void, c: f32 })); |
