Commit 3b2fec17a4

Mason Remaley <mason@anthropicstudios.com>
2025-02-22 06:55:36
std.zon: populate `Zoir.Node.Index` values with corresponding ZOIR node
This allows using `std.zon` to parse schemas which are not directly representable in the Zig type system; for instance, `build.zig.zon`.
1 parent 1b62a22
Changed files (1)
lib
std
lib/std/zon/parse.zig
@@ -436,6 +436,10 @@ const Parser = struct {
         T: type,
         node: Zoir.Node.Index,
     ) error{ ParseZon, OutOfMemory, WrongType }!T {
+        if (T == Zoir.Node.Index) {
+            return node;
+        }
+
         switch (@typeInfo(T)) {
             .optional => |optional| if (node.get(self.zoir) == .null) {
                 return null;
@@ -3441,3 +3445,27 @@ test "std.zon add pointers" {
         try std.testing.expectFmt("1:1: error: expected optional enum literal\n", "{}", .{status});
     }
 }
+
+test "std.zon stop on node" {
+    const gpa = std.testing.allocator;
+
+    {
+        const Vec2 = struct {
+            x: Zoir.Node.Index,
+            y: f32,
+        };
+
+        var status: Status = .{};
+        defer status.deinit(gpa);
+        const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &status, .{});
+        try std.testing.expectEqual(result.y, 2.5);
+        try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(status.zoir.?));
+    }
+
+    {
+        var status: Status = .{};
+        defer status.deinit(gpa);
+        const result = try fromSlice(Zoir.Node.Index, gpa, "1.23", &status, .{});
+        try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(status.zoir.?));
+    }
+}