From 3b2fec17a4dfe034c28f528d003bf09b5e44c2b6 Mon Sep 17 00:00:00 2001 From: Mason Remaley Date: Fri, 21 Feb 2025 21:55:36 -0800 Subject: [PATCH] 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`. --- lib/std/zon/parse.zig | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/std/zon/parse.zig b/lib/std/zon/parse.zig index 1edb37654c..7072c7c2cb 100644 --- a/lib/std/zon/parse.zig +++ b/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.?)); + } +}