From 35d8e6324696a829bc5052d8f6b90ca4dc233433 Mon Sep 17 00:00:00 2001 From: mlarouche Date: Thu, 15 Oct 2026 15:07:27 +0200 Subject: [PATCH] ZON: Fix parsing from a specific node with fromZoir() (#37193) Fix parsing a specific node with fromZoir() method by passing the wanted node to the inner functions. Also added a test to make sure the use case gets tested. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37193 Reviewed-by: Mason Remaley --- lib/std/zon/parse.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/std/zon/parse.zig b/lib/std/zon/parse.zig index 3b10426fbb0f9992ea8f409503b4bb69a8cfbafd..b580e8a5067c3b38b147e1fa2233a455b2fcb696 100644 --- a/lib/std/zon/parse.zig +++ b/lib/std/zon/parse.zig @@ -435,6 +435,7 @@ fn fromZoirInner( .arena = options.arena, .ast = options.ast, .zoir = options.zoir, + .node = options.node, .errors = &errors, .ignore_unknown_fields = options.ignore_unknown_fields, }); @@ -4951,6 +4952,47 @@ test "std.zon variants" { } } +test "std.zon parse specific node with zoir" { + const gpa = std.testing.allocator; + var arena_allocator: ArenaAllocator = .init(gpa); + defer arena_allocator.deinit(); + const arena = arena_allocator.allocator(); + var diagnostics: Diagnostics = undefined; + + var ast = try std.zig.Ast.parse(gpa, ".{ .x = 1.5, .y = 2.5 }", .{ .mode = .zon }); + defer ast.deinit(gpa); + + var zoir = try ZonGen.generate(gpa, ast, .{ .parse_str_lits = false }); + defer zoir.deinit(gpa); + + const root_node: std.zig.Zoir.Node = std.zig.Zoir.Node.Index.get(.root, &zoir); + + try std.testing.expect(root_node == .struct_literal); + try std.testing.expectEqual(2, root_node.struct_literal.vals.len); + + const x_node = root_node.struct_literal.vals.at(0); + const x_result = try fromZoirNoAlloc(f32, .{ + .arena = arena, + .ast = &ast, + .zoir = &zoir, + .node = x_node, + .diagnostics = &diagnostics, + }); + + try std.testing.expectApproxEqAbs(x_result, 1.5, 0.1); + + const y_node = root_node.struct_literal.vals.at(1); + const y_result = try fromZoirNoAlloc(f32, .{ + .arena = arena, + .ast = &ast, + .zoir = &zoir, + .node = y_node, + .diagnostics = &diagnostics, + }); + + try std.testing.expectApproxEqAbs(y_result, 2.5, 0.1); +} + test { _ = Diagnostics; } -- 2.54.0