mirror of
https://github.com/ziglang/zig.git
synced 2026-02-15 13:58:27 +00:00
Fix std.enums.values
Current implementation fails to handle the following enum
```zig
const E = enum {
X,
pub const X = 1;
}
```
because `@field(type, name)` prefers declarations over enum fields.
This commit is contained in:
parent
e19219fa0e
commit
c89bb3e141
@ -35,8 +35,8 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
|
||||
pub inline fn valuesFromFields(comptime E: type, comptime fields: []const EnumField) []const E {
|
||||
comptime {
|
||||
var result: [fields.len]E = undefined;
|
||||
for (fields, 0..) |f, i| {
|
||||
result[i] = @field(E, f.name);
|
||||
for (&result, fields) |*r, f| {
|
||||
r.* = @enumFromInt(f.value);
|
||||
}
|
||||
return &result;
|
||||
}
|
||||
@ -1534,3 +1534,13 @@ test "std.enums.EnumIndexer empty" {
|
||||
try testing.expectEqual(E, Indexer.Key);
|
||||
try testing.expectEqual(0, Indexer.count);
|
||||
}
|
||||
|
||||
test "enumValues" {
|
||||
const E = enum {
|
||||
X,
|
||||
Y,
|
||||
Z,
|
||||
pub const X = 1;
|
||||
};
|
||||
try testing.expectEqualSlices(E, &.{ .X, .Y, .Z }, values(E));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user