authorgravatar for mason@anthropicstudios.comMason Remaley <mason@anthropicstudios.com> 2025-02-21 23:09:18-08:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-04-02 06:09:21+01:00
logfa1695a8b0f611a67166732a4fb0d0eff2c6db42
tree47493a121e8f330438f75c06249ebc80b47b9d12
parent65649576dbd3900103d3d96cdad94509f2217545
signaturelock-open Commit is signed but in an unrecognized format.

std.zon.parse: make `ast` and `zoir` fields of `Diagnostics` non-optional


1 files changed, 32 insertions(+), 23 deletions(-)

lib/std/zon/parse.zig+32-23
......@@ -50,8 +50,7 @@ pub const Error = union(enum) {
5050 switch (self.err) {
5151 .zoir => |err| {
5252 if (self.index >= err.note_count) return null;
53 const zoir = self.diag.zoir.?;
54 const note = err.getNotes(zoir)[self.index];
53 const note = err.getNotes(self.diag.zoir)[self.index];
5554 self.index += 1;
5655 return .{ .zoir = note };
5756 },
......@@ -82,16 +81,15 @@ pub const Error = union(enum) {
8281
8382 pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Formatter(Note.formatMessage) {
8483 return .{ .data = switch (self) {
85 .zoir => |note| note.msg.get(diag.zoir.?),
84 .zoir => |note| note.msg.get(diag.zoir),
8685 .type_check => |note| note.msg,
8786 } };
8887 }
8988
9089 pub fn getLocation(self: Note, diag: *const Diagnostics) Ast.Location {
91 const ast = diag.ast.?;
9290 switch (self) {
93 .zoir => |note| return zoirErrorLocation(ast, note.token, note.node_or_offset),
94 .type_check => |note| return ast.tokenLocation(note.offset, note.token),
91 .zoir => |note| return zoirErrorLocation(diag.ast, note.token, note.node_or_offset),
92 .type_check => |note| return diag.ast.tokenLocation(note.offset, note.token),
9593 }
9694 }
9795 };
......@@ -101,16 +99,14 @@ pub const Error = union(enum) {
10199 diag: *const Diagnostics,
102100
103101 pub fn next(self: *@This()) ?Error {
104 const zoir = self.diag.zoir orelse return null;
105
106 if (self.index < zoir.compile_errors.len) {
107 const result: Error = .{ .zoir = zoir.compile_errors[self.index] };
102 if (self.index < self.diag.zoir.compile_errors.len) {
103 const result: Error = .{ .zoir = self.diag.zoir.compile_errors[self.index] };
108104 self.index += 1;
109105 return result;
110106 }
111107
112108 if (self.diag.type_check) |err| {
113 if (self.index == zoir.compile_errors.len) {
109 if (self.index == self.diag.zoir.compile_errors.len) {
114110 const result: Error = .{ .type_check = err };
115111 self.index += 1;
116112 return result;
......@@ -168,7 +164,7 @@ pub const Error = union(enum) {
168164 _ = f;
169165 _ = options;
170166 switch (self.err) {
171 .zoir => |err| try writer.writeAll(err.msg.get(self.diag.zoir.?)),
167 .zoir => |err| try writer.writeAll(err.msg.get(self.diag.zoir)),
172168 .type_check => |tc| try writer.writeAll(tc.message),
173169 }
174170 }
......@@ -181,14 +177,13 @@ pub const Error = union(enum) {
181177 }
182178
183179 pub fn getLocation(self: @This(), diag: *const Diagnostics) Ast.Location {
184 const ast = diag.ast.?;
185180 return switch (self) {
186181 .zoir => |err| return zoirErrorLocation(
187 diag.ast.?,
182 diag.ast,
188183 err.token,
189184 err.node_or_offset,
190185 ),
191 .type_check => |err| return ast.tokenLocation(err.offset, err.token),
186 .type_check => |err| return diag.ast.tokenLocation(err.offset, err.token),
192187 };
193188 }
194189
......@@ -211,19 +206,33 @@ pub const Error = union(enum) {
211206
212207/// Information about the success or failure of a parse.
213208pub const Diagnostics = struct {
214 ast: ?Ast = null,
215 zoir: ?Zoir = null,
209 ast: Ast = .{
210 .source = "",
211 .tokens = .empty,
212 .nodes = .empty,
213 .extra_data = &.{},
214 .mode = .zon,
215 .errors = &.{},
216 },
217 zoir: Zoir = .{
218 .nodes = .empty,
219 .extra = &.{},
220 .limbs = &.{},
221 .string_bytes = &.{},
222 .compile_errors = &.{},
223 .error_notes = &.{},
224 },
216225 type_check: ?Error.TypeCheckFailure = null,
217226
218227 fn assertEmpty(self: Diagnostics) void {
219 assert(self.ast == null);
220 assert(self.zoir == null);
228 assert(self.ast.tokens.len == 0);
229 assert(self.zoir.nodes.len == 0);
221230 assert(self.type_check == null);
222231 }
223232
224233 pub fn deinit(self: *Diagnostics, gpa: Allocator) void {
225 if (self.ast) |*ast| ast.deinit(gpa);
226 if (self.zoir) |*zoir| zoir.deinit(gpa);
234 self.ast.deinit(gpa);
235 self.zoir.deinit(gpa);
227236 if (self.type_check) |tc| tc.deinit(gpa);
228237 self.* = undefined;
229238 }
......@@ -3459,13 +3468,13 @@ test "std.zon stop on node" {
34593468 defer diag.deinit(gpa);
34603469 const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &diag, .{});
34613470 try std.testing.expectEqual(result.y, 2.5);
3462 try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(diag.zoir.?));
3471 try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(diag.zoir));
34633472 }
34643473
34653474 {
34663475 var diag: Diagnostics = .{};
34673476 defer diag.deinit(gpa);
34683477 const result = try fromSlice(Zoir.Node.Index, gpa, "1.23", &diag, .{});
3469 try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(diag.zoir.?));
3478 try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(diag.zoir));
34703479 }
34713480}