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 20:13:49+02:00
log35d8e6324696a829bc5052d8f6b90ca4dc233433
tree315da618fbae6fd5d08b4ff1540854eda57efa4f
parent5cba8339049cc70c3fabd457188fbdb397ab9321
signature Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

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
...@@ -435,6 +435,7 @@ fn fromZoirInner(...@@ -435,6 +435,7 @@ fn fromZoirInner(
435 .arena = options.arena,435 .arena = options.arena,
436 .ast = options.ast,436 .ast = options.ast,
437 .zoir = options.zoir,437 .zoir = options.zoir,
438 .node = options.node,
438 .errors = &errors,439 .errors = &errors,
439 .ignore_unknown_fields = options.ignore_unknown_fields,440 .ignore_unknown_fields = options.ignore_unknown_fields,
440 });441 });
...@@ -4951,6 +4952,47 @@ test "std.zon variants" {...@@ -4951,6 +4952,47 @@ test "std.zon variants" {
4951 }4952 }
4952}4953}
49534954
4955test "std.zon parse specific node with zoir" {
4956 const gpa = std.testing.allocator;
4957 var arena_allocator: ArenaAllocator = .init(gpa);
4958 defer arena_allocator.deinit();
4959 const arena = arena_allocator.allocator();
4960 var diagnostics: Diagnostics = undefined;
4961
4962 var ast = try std.zig.Ast.parse(gpa, ".{ .x = 1.5, .y = 2.5 }", .{ .mode = .zon });
4963 defer ast.deinit(gpa);
4964
4965 var zoir = try ZonGen.generate(gpa, ast, .{ .parse_str_lits = false });
4966 defer zoir.deinit(gpa);
4967
4968 const root_node: std.zig.Zoir.Node = std.zig.Zoir.Node.Index.get(.root, &zoir);
4969
4970 try std.testing.expect(root_node == .struct_literal);
4971 try std.testing.expectEqual(2, root_node.struct_literal.vals.len);
4972
4973 const x_node = root_node.struct_literal.vals.at(0);
4974 const x_result = try fromZoirNoAlloc(f32, .{
4975 .arena = arena,
4976 .ast = &ast,
4977 .zoir = &zoir,
4978 .node = x_node,
4979 .diagnostics = &diagnostics,
4980 });
4981
4982 try std.testing.expectApproxEqAbs(x_result, 1.5, 0.1);
4983
4984 const y_node = root_node.struct_literal.vals.at(1);
4985 const y_result = try fromZoirNoAlloc(f32, .{
4986 .arena = arena,
4987 .ast = &ast,
4988 .zoir = &zoir,
4989 .node = y_node,
4990 .diagnostics = &diagnostics,
4991 });
4992
4993 try std.testing.expectApproxEqAbs(y_result, 2.5, 0.1);
4994}
4995
4954test {4996test {
4955 _ = Diagnostics;4997 _ = Diagnostics;
4956}4998}