diff options
| author | Alex Rønne Petersen <alex@alexrp.com> | 2024-10-06 02:52:20 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-06 02:52:20 +0200 |
| commit | ac247c9943385dce3d90647e26387914f65979b9 (patch) | |
| tree | 3d0db53b269a4fcf06b904babcc258192af03aa7 /lib/std/meta.zig | |
| parent | 406e56ab698358ee9be6746b2328689bf203022e (diff) | |
| parent | 7d3e0f815d4324c504b0d98323919d1980079ffa (diff) | |
| download | zig-ac247c9943385dce3d90647e26387914f65979b9.tar.gz zig-ac247c9943385dce3d90647e26387914f65979b9.zip | |
Merge pull request #21331 from bobf/std-meta-DeclEnum-empty-struct
Prevent failure with empty struct in `std.meta.DeclEnum`
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index ff0cec1b18..ea81c87648 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -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, @@ -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 { |
