authorgravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-09-28 00:05:30-07:00
committergravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-09-28 00:05:30-07:00
log139666586ec5a59ff4d4e6973b857c058dc9b572
treeeb1c376d022f393a4c64b5b2b71670ad77f8a5b5
parent20fd6977376c0f159a15a8b6264480f9875436c1

Revert "Better API for reporting OOM"

This reverts commit 20fd6977376c0f159a15a8b6264480f9875436c1.

1 files changed, 45 insertions(+), 75 deletions(-)

lib/std/zon/parse.zig+45-75
......@@ -94,12 +94,8 @@ pub const Error = struct {
9494/// Errors encountered while parsing ZON. See `log` and `fatal` for reporting errors to the user.
9595pub const Errors = struct {
9696 items: []const Error,
97 oom: bool,
9897
99 pub const empty: Errors = .{
100 .items = &.{},
101 .oom = false,
102 };
98 pub const empty: Errors = .{ .items = &.{} };
10399
104100 /// Log the failure with `std.log.err`.
105101 pub fn log(self: *const Errors, path: []const u8) void {
......@@ -123,9 +119,12 @@ pub const Errors = struct {
123119 .arena = arena,
124120 .source = ".{ .foo = 1, .bar = 2 }",
125121 .errors = &errors,
126 }) catch b: {
127 errors.log("input.zon");
128 break :b .{ .foo = 0, .bar = 0 };
122 }) catch |err| switch (err) {
123 error.OutOfMemory => return error.OutOfMemory,
124 error.ParseZon => b: {
125 errors.log("input.zon");
126 break :b .{ .foo = 0, .bar = 0 };
127 },
129128 };
130129 _ = parsed;
131130 }
......@@ -152,14 +151,16 @@ pub const Errors = struct {
152151 .arena = arena,
153152 .source = ".{ .foo = 1, .bar = 2 }",
154153 .errors = &errors,
155 }) catch errors.fatal("input.zon");
154 }) catch |err| switch (err) {
155 error.OutOfMemory => return error.OutOfMemory,
156 error.ParseZon => errors.fatal("input.zon"),
157 };
156158 _ = parsed;
157159 }
158160
159161 pub fn fmt(self: *const Errors, path: []const u8) Formatter {
160162 return .{
161163 .items = self.items,
162 .oom = self.oom,
163164 .path = path,
164165 };
165166 }
......@@ -167,12 +168,8 @@ pub const Errors = struct {
167168 pub const Formatter = struct {
168169 path: []const u8,
169170 items: []const Error,
170 oom: bool,
171171
172172 pub fn format(self: *const @This(), w: *std.Io.Writer) std.Io.Writer.Error!void {
173 if (self.oom) {
174 try w.print("{s}: out of memory", .{self.path});
175 }
176173 for (self.items) |e| {
177174 try w.print("{s}:", .{self.path});
178175 try w.print("{d}:{d}: error: {s}\n", .{
......@@ -192,11 +189,6 @@ pub const Errors = struct {
192189 }
193190 }
194191 };
195
196 fn checkOom(self: *Errors, result: anytype) @TypeOf(result) {
197 self.oom |= result == error.OutOfMemory;
198 return result;
199 }
200192};
201193
202194pub const Options = struct {
......@@ -246,9 +238,12 @@ test fromSlice {
246238 .arena = arena,
247239 .source = source,
248240 .errors = &errors,
249 }) catch |err| {
250 errors.log("texture_options.zon");
251 return err;
241 }) catch |err| switch (err) {
242 error.OutOfMemory => return err,
243 error.ParseZon => {
244 errors.log("texture_options.zon");
245 return err;
246 },
252247 };
253248
254249 try std.testing.expectEqualDeep(TextureOptions{
......@@ -318,18 +313,24 @@ test updateFromSlice {
318313 .arena = arena,
319314 .source = global_config,
320315 .errors = &errors,
321 }) catch |err| {
322 errors.log("global_config.zon");
323 return err;
316 }) catch |err| switch (err) {
317 error.OutOfMemory => return err,
318 error.ParseZon => {
319 errors.log("global_config.zon");
320 return err;
321 },
324322 };
325323 updateFromSlice(MyTextEditorConfig, &config, .{
326324 .gpa = gpa,
327325 .arena = arena,
328326 .source = project_config,
329327 .errors = &errors,
330 }) catch |err| {
331 errors.log("project_config.zon");
332 return err;
328 }) catch |err| switch (err) {
329 error.OutOfMemory => return err,
330 error.ParseZon => {
331 errors.log("project_config.zon");
332 return err;
333 },
333334 };
334335
335336 try std.testing.expectEqualDeep(MyTextEditorConfig{
......@@ -357,32 +358,23 @@ fn fromSliceInner(
357358 initialized: bool,
358359 options: Options,
359360) error{ OutOfMemory, ParseZon }!void {
360 options.errors.oom = false;
361361 var errors: std.ArrayList(Error) = .empty;
362362 defer options.errors.items = errors.items;
363363
364 var ast = try options.errors.checkOom(std.zig.Ast.parse(
365 options.gpa,
366 options.source,
367 .{ .mode = .zon },
368 ));
364 var ast = try std.zig.Ast.parse(options.gpa, options.source, .{ .mode = .zon });
369365 defer ast.deinit(options.gpa);
370366
371 var zoir = try options.errors.checkOom(ZonGen.generate(
372 options.gpa,
373 ast,
374 .{ .parse_str_lits = false },
375 ));
367 var zoir = try ZonGen.generate(options.gpa, ast, .{ .parse_str_lits = false });
376368 defer zoir.deinit(options.gpa);
377369
378 try options.errors.checkOom(inner(T, value, initialized, .{
370 try inner(T, value, initialized, .{
379371 .arena = options.arena,
380372 .ast = &ast,
381373 .zoir = &zoir,
382374 .node = .root,
383375 .errors = &errors,
384376 .ignore_unknown_fields = options.ignore_unknown_fields,
385 }));
377 });
386378}
387379
388380pub const FromZoirOptions = struct {
......@@ -439,16 +431,15 @@ fn fromZoirInner(
439431 initialized: bool,
440432 options: FromZoirOptions,
441433) error{ OutOfMemory, ParseZon }!void {
442 options.errors.oom = false;
443434 var errors: std.ArrayList(Error) = .empty;
444435 defer options.errors.items = errors.items;
445 return options.errors.checkOom(inner(T, value, initialized, .{
436 return inner(T, value, initialized, .{
446437 .arena = options.arena,
447438 .ast = options.ast,
448439 .zoir = options.zoir,
449440 .errors = &errors,
450441 .ignore_unknown_fields = options.ignore_unknown_fields,
451 }));
442 });
452443}
453444
454445const InnerOptions = struct {
......@@ -1450,38 +1441,17 @@ test "std.zon failure/oom formatting" {
14501441 var arena_allocator: ArenaAllocator = .init(gpa);
14511442 defer arena_allocator.deinit();
14521443 const arena = arena_allocator.allocator();
1444 var errors: Errors = .empty;
14531445
1454 {
1455 var errors: Errors = .empty;
1456 try std.testing.expectError(error.OutOfMemory, fromSlice([]const u8, .{
1457 .gpa = .failing,
1458 .arena = arena,
1459 .source = "\"foo\"",
1460 .errors = &errors,
1461 }));
1462 try std.testing.expectFmt(
1463 \\input.zon: out of memory
1464 , "{f}", .{errors.fmt("input.zon")});
1465 }
1466
1467 {
1468 var ast = try std.zig.Ast.parse(gpa, "\"foo\"", .{ .mode = .zon });
1469 defer ast.deinit(gpa);
1470
1471 var zoir = try ZonGen.generate(gpa, ast, .{ .parse_str_lits = false });
1472 defer zoir.deinit(gpa);
1473
1474 var errors: Errors = .empty;
1475 try std.testing.expectError(error.OutOfMemory, fromZoir([]const u8, .{
1476 .arena = .failing,
1477 .ast = &ast,
1478 .zoir = &zoir,
1479 .errors = &errors,
1480 }));
1481 try std.testing.expectFmt(
1482 \\input.zon: out of memory
1483 , "{f}", .{errors.fmt("input.zon")});
1484 }
1446 try std.testing.expectError(error.OutOfMemory, fromSlice([]const u8, .{
1447 .gpa = .failing,
1448 .arena = arena,
1449 .source = "\"foo\"",
1450 .errors = &errors,
1451 }));
1452 try std.testing.expectFmt(
1453 \\
1454 , "{f}", .{errors.fmt("input.zon")});
14851455}
14861456
14871457test "std.zon fromSlice syntax error" {