authorgravatar for michael.larouche@gmail.commlarouche <michael.larouche@gmail.com> 2026-10-15 15:07:27+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-15 15:07:27+02:00
logcb7a773ab8d8eb1838a3f764a9bfb735f30bfb5e
tree850e4dfafe68b4b36ebd6542c8dddca425444ed7
parent38dd6a68b702f7b34bf888a047c7b7cb93a1ae79

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 <masonremaley@noreply.codeberg.org>

1 files changed, 42 insertions(+), 0 deletions(-)

lib/std/zon/parse.zig+42
......@@ -436,6 +436,7 @@ fn fromZoirInner(
436436 .arena = options.arena,
437437 .ast = options.ast,
438438 .zoir = options.zoir,
439 .node = options.node,
439440 .errors = &errors,
440441 .ignore_unknown_fields = options.ignore_unknown_fields,
441442 });
......@@ -4854,6 +4855,47 @@ test "std.zon variants" {
48544855 }
48554856}
48564857
4858test "std.zon parse specific node with zoir" {
4859 const gpa = std.testing.allocator;
4860 var arena_allocator: ArenaAllocator = .init(gpa);
4861 defer arena_allocator.deinit();
4862 const arena = arena_allocator.allocator();
4863 var diagnostics: Diagnostics = undefined;
4864
4865 var ast = try std.zig.Ast.parse(gpa, ".{ .x = 1.5, .y = 2.5 }", .{ .mode = .zon });
4866 defer ast.deinit(gpa);
4867
4868 var zoir = try ZonGen.generate(gpa, ast, .{ .parse_str_lits = false });
4869 defer zoir.deinit(gpa);
4870
4871 const root_node: std.zig.Zoir.Node = std.zig.Zoir.Node.Index.get(.root, &zoir);
4872
4873 try std.testing.expect(root_node == .struct_literal);
4874 try std.testing.expectEqual(2, root_node.struct_literal.vals.len);
4875
4876 const x_node = root_node.struct_literal.vals.at(0);
4877 const x_result = try fromZoirNoAlloc(f32, .{
4878 .arena = arena,
4879 .ast = &ast,
4880 .zoir = &zoir,
4881 .node = x_node,
4882 .diagnostics = &diagnostics,
4883 });
4884
4885 try std.testing.expectApproxEqAbs(x_result, 1.5, 0.1);
4886
4887 const y_node = root_node.struct_literal.vals.at(1);
4888 const y_result = try fromZoirNoAlloc(f32, .{
4889 .arena = arena,
4890 .ast = &ast,
4891 .zoir = &zoir,
4892 .node = y_node,
4893 .diagnostics = &diagnostics,
4894 });
4895
4896 try std.testing.expectApproxEqAbs(y_result, 2.5, 0.1);
4897}
4898
48574899test {
48584900 _ = Diagnostics;
48594901}