authorgravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2025-08-20 23:10:34+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-21 11:50:03-07:00
log1594c8055500c595e1b47c3985c035142b824280
treedbb9138fd68c26b4bc062e7ba34a4b1f4f9f51c9
parent52de06c3b0720223a8ee2d337152df9e214388f8

zig reduce: adapt to new Writer API


2 files changed, 29 insertions(+), 29 deletions(-)

lib/compiler/reduce.zig+19-20
...@@ -114,10 +114,10 @@ pub fn main() !void {...@@ -114,10 +114,10 @@ pub fn main() !void {
114 interestingness_argv.appendAssumeCapacity(checker_path);114 interestingness_argv.appendAssumeCapacity(checker_path);
115 interestingness_argv.appendSliceAssumeCapacity(argv);115 interestingness_argv.appendSliceAssumeCapacity(argv);
116116
117 var rendered = std.array_list.Managed(u8).init(gpa);117 var rendered: std.Io.Writer.Allocating = .init(gpa);
118 defer rendered.deinit();118 defer rendered.deinit();
119119
120 var astgen_input = std.array_list.Managed(u8).init(gpa);120 var astgen_input: std.Io.Writer.Allocating = .init(gpa);
121 defer astgen_input.deinit();121 defer astgen_input.deinit();
122122
123 var tree = try parse(gpa, root_source_file_path);123 var tree = try parse(gpa, root_source_file_path);
...@@ -138,10 +138,10 @@ pub fn main() !void {...@@ -138,10 +138,10 @@ pub fn main() !void {
138 }138 }
139 }139 }
140140
141 var fixups: Ast.Fixups = .{};141 var fixups: Ast.Render.Fixups = .{};
142 defer fixups.deinit(gpa);142 defer fixups.deinit(gpa);
143143
144 var more_fixups: Ast.Fixups = .{};144 var more_fixups: Ast.Render.Fixups = .{};
145 defer more_fixups.deinit(gpa);145 defer more_fixups.deinit(gpa);
146146
147 var rng = std.Random.DefaultPrng.init(seed);147 var rng = std.Random.DefaultPrng.init(seed);
...@@ -188,15 +188,14 @@ pub fn main() !void {...@@ -188,15 +188,14 @@ pub fn main() !void {
188 try transformationsToFixups(gpa, arena, root_source_file_path, this_set, &fixups);188 try transformationsToFixups(gpa, arena, root_source_file_path, this_set, &fixups);
189189
190 rendered.clearRetainingCapacity();190 rendered.clearRetainingCapacity();
191 try tree.renderToArrayList(&rendered, fixups);191 try tree.render(gpa, &rendered.writer, fixups);
192192
193 // The transformations we applied may have resulted in unused locals,193 // The transformations we applied may have resulted in unused locals,
194 // in which case we would like to add the respective discards.194 // in which case we would like to add the respective discards.
195 {195 {
196 try astgen_input.resize(rendered.items.len);196 try astgen_input.writer.writeAll(rendered.written());
197 @memcpy(astgen_input.items, rendered.items);197 try astgen_input.writer.writeByte(0);
198 try astgen_input.append(0);198 const source_with_null = astgen_input.written()[0..(astgen_input.written().len - 1) :0];
199 const source_with_null = astgen_input.items[0 .. astgen_input.items.len - 1 :0];
200 var astgen_tree = try Ast.parse(gpa, source_with_null, .zig);199 var astgen_tree = try Ast.parse(gpa, source_with_null, .zig);
201 defer astgen_tree.deinit(gpa);200 defer astgen_tree.deinit(gpa);
202 if (astgen_tree.errors.len != 0) {201 if (astgen_tree.errors.len != 0) {
...@@ -228,12 +227,12 @@ pub fn main() !void {...@@ -228,12 +227,12 @@ pub fn main() !void {
228 }227 }
229 if (more_fixups.count() != 0) {228 if (more_fixups.count() != 0) {
230 rendered.clearRetainingCapacity();229 rendered.clearRetainingCapacity();
231 try astgen_tree.renderToArrayList(&rendered, more_fixups);230 try astgen_tree.render(gpa, &rendered.writer, more_fixups);
232 }231 }
233 }232 }
234 }233 }
235234
236 try std.fs.cwd().writeFile(.{ .sub_path = root_source_file_path, .data = rendered.items });235 try std.fs.cwd().writeFile(.{ .sub_path = root_source_file_path, .data = rendered.written() });
237 // std.debug.print("trying this code:\n{s}\n", .{rendered.items});236 // std.debug.print("trying this code:\n{s}\n", .{rendered.items});
238237
239 const interestingness = try runCheck(arena, interestingness_argv.items);238 const interestingness = try runCheck(arena, interestingness_argv.items);
...@@ -273,8 +272,8 @@ pub fn main() !void {...@@ -273,8 +272,8 @@ pub fn main() !void {
273 // Revert the source back to not be transformed.272 // Revert the source back to not be transformed.
274 fixups.clearRetainingCapacity();273 fixups.clearRetainingCapacity();
275 rendered.clearRetainingCapacity();274 rendered.clearRetainingCapacity();
276 try tree.renderToArrayList(&rendered, fixups);275 try tree.render(gpa, &rendered.writer, fixups);
277 try std.fs.cwd().writeFile(.{ .sub_path = root_source_file_path, .data = rendered.items });276 try std.fs.cwd().writeFile(.{ .sub_path = root_source_file_path, .data = rendered.written() });
278277
279 return std.process.cleanExit();278 return std.process.cleanExit();
280 }279 }
...@@ -318,7 +317,7 @@ fn transformationsToFixups(...@@ -318,7 +317,7 @@ fn transformationsToFixups(
318 arena: Allocator,317 arena: Allocator,
319 root_source_file_path: []const u8,318 root_source_file_path: []const u8,
320 transforms: []const Walk.Transformation,319 transforms: []const Walk.Transformation,
321 fixups: *Ast.Fixups,320 fixups: *Ast.Render.Fixups,
322) !void {321) !void {
323 fixups.clearRetainingCapacity();322 fixups.clearRetainingCapacity();
324323
...@@ -359,7 +358,7 @@ fn transformationsToFixups(...@@ -359,7 +358,7 @@ fn transformationsToFixups(
359 other_file_ast.deinit(gpa);358 other_file_ast.deinit(gpa);
360 }359 }
361360
362 var inlined_fixups: Ast.Fixups = .{};361 var inlined_fixups: Ast.Render.Fixups = .{};
363 defer inlined_fixups.deinit(gpa);362 defer inlined_fixups.deinit(gpa);
364 if (std.fs.path.dirname(inline_imported_file.imported_string)) |dirname| {363 if (std.fs.path.dirname(inline_imported_file.imported_string)) |dirname| {
365 inlined_fixups.rebase_imported_paths = dirname;364 inlined_fixups.rebase_imported_paths = dirname;
...@@ -382,16 +381,16 @@ fn transformationsToFixups(...@@ -382,16 +381,16 @@ fn transformationsToFixups(
382 }381 }
383 }382 }
384383
385 var other_source = std.array_list.Managed(u8).init(gpa);384 var other_source: std.io.Writer.Allocating = .init(gpa);
386 defer other_source.deinit();385 defer other_source.deinit();
387 try other_source.appendSlice("struct {\n");386 try other_source.writer.writeAll("struct {\n");
388 try other_file_ast.renderToArrayList(&other_source, inlined_fixups);387 try other_file_ast.render(gpa, &other_source.writer, inlined_fixups);
389 try other_source.appendSlice("}");388 try other_source.writer.writeAll("}");
390389
391 try fixups.replace_nodes_with_string.put(390 try fixups.replace_nodes_with_string.put(
392 gpa,391 gpa,
393 inline_imported_file.builtin_call_node,392 inline_imported_file.builtin_call_node,
394 try arena.dupe(u8, other_source.items),393 try arena.dupe(u8, other_source.written()),
395 );394 );
396 },395 },
397 };396 };
lib/compiler/reduce/Walk.zig+10-9
...@@ -501,6 +501,10 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {...@@ -501,6 +501,10 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {
501 .@"asm",501 .@"asm",
502 => return walkAsm(w, ast.fullAsm(node).?),502 => return walkAsm(w, ast.fullAsm(node).?),
503503
504 .asm_legacy => {
505 return walkAsmLegacy(w, ast.legacyAsm(node).?);
506 },
507
504 .enum_literal => {508 .enum_literal => {
505 return walkIdentifier(w, ast.nodeMainToken(node)); // name509 return walkIdentifier(w, ast.nodeMainToken(node)); // name
506 },510 },
...@@ -665,7 +669,7 @@ fn walkStructInit(...@@ -665,7 +669,7 @@ fn walkStructInit(
665669
666fn walkCall(w: *Walk, call: Ast.full.Call) Error!void {670fn walkCall(w: *Walk, call: Ast.full.Call) Error!void {
667 try walkExpression(w, call.ast.fn_expr);671 try walkExpression(w, call.ast.fn_expr);
668 try walkParamList(w, call.ast.params);672 try walkExpressions(w, call.ast.params);
669}673}
670674
671fn walkSlice(675fn walkSlice(
...@@ -830,7 +834,7 @@ fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) E...@@ -830,7 +834,7 @@ fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) E
830}834}
831835
832fn walkFor(w: *Walk, for_node: Ast.full.For) Error!void {836fn walkFor(w: *Walk, for_node: Ast.full.For) Error!void {
833 try walkParamList(w, for_node.ast.inputs);837 try walkExpressions(w, for_node.ast.inputs);
834 try walkExpression(w, for_node.ast.then_expr);838 try walkExpression(w, for_node.ast.then_expr);
835 if (for_node.ast.else_expr.unwrap()) |else_expr| {839 if (for_node.ast.else_expr.unwrap()) |else_expr| {
836 try walkExpression(w, else_expr);840 try walkExpression(w, else_expr);
...@@ -874,15 +878,12 @@ fn walkIf(w: *Walk, node_index: Ast.Node.Index, if_node: Ast.full.If) Error!void...@@ -874,15 +878,12 @@ fn walkIf(w: *Walk, node_index: Ast.Node.Index, if_node: Ast.full.If) Error!void
874878
875fn walkAsm(w: *Walk, asm_node: Ast.full.Asm) Error!void {879fn walkAsm(w: *Walk, asm_node: Ast.full.Asm) Error!void {
876 try walkExpression(w, asm_node.ast.template);880 try walkExpression(w, asm_node.ast.template);
877 for (asm_node.ast.items) |item| {881 try walkExpressions(w, asm_node.ast.items);
878 try walkExpression(w, item);
879 }
880}882}
881883
882fn walkParamList(w: *Walk, params: []const Ast.Node.Index) Error!void {884fn walkAsmLegacy(w: *Walk, asm_node: Ast.full.AsmLegacy) Error!void {
883 for (params) |param_node| {885 try walkExpression(w, asm_node.ast.template);
884 try walkExpression(w, param_node);886 try walkExpressions(w, asm_node.ast.items);
885 }
886}887}
887888
888/// Check if it is already gutted (i.e. its body replaced with `@trap()`).889/// Check if it is already gutted (i.e. its body replaced with `@trap()`).