From 45e4b16caf30093bcb98b266b25d1a226efd1641 Mon Sep 17 00:00:00 2001 From: Travis Staloch <1562827+travisstaloch@users.noreply.github.com> Date: Tue, 24 Dec 2024 19:44:43 -0800 Subject: [PATCH] add `@Type` behavior tests from #19985 --- test/behavior/type.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/behavior/type.zig b/test/behavior/type.zig index 4ebcfa7eee14..19feb2d5fff3 100644 --- a/test/behavior/type.zig +++ b/test/behavior/type.zig @@ -377,6 +377,32 @@ test "Type.Enum" { try testing.expectEqual(@as(u32, 1), @intFromEnum(Bar.a)); try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b)); try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6)))); + + { // from https://github.com/ziglang/zig/issues/19985 + { // enum with single field can be initialized. + const E = @Type(.{ .@"enum" = .{ + .tag_type = u0, + .is_exhaustive = true, + .fields = &.{.{ .name = "foo", .value = 0 }}, + .decls = &.{}, + } }); + const s: struct { E } = .{.foo}; + try testing.expectEqual(.foo, s[0]); + } + + { // meta.FieldEnum() with single field + const S = struct { foo: u8 }; + const Fe = std.meta.FieldEnum(S); + var s: S = undefined; + const fe = std.meta.stringToEnum(Fe, "foo") orelse return error.InvalidField; + switch (fe) { + inline else => |tag| { + @field(s, @tagName(tag)) = 42; + }, + } + try testing.expectEqual(42, s.foo); + } + } } test "Type.Union" {