Commit c89bb3e141

PanSashko <155240273+PanSashko@users.noreply.github.com>
2023-12-30 15:03:15
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.
1 parent e19219f
Changed files (1)
lib
lib/std/enums.zig
@@ -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));
+}