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) {...@@ -50,8 +50,7 @@ pub const Error = union(enum) {
50 switch (self.err) {50 switch (self.err) {
51 .zoir => |err| {51 .zoir => |err| {
52 if (self.index >= err.note_count) return null;52 if (self.index >= err.note_count) return null;
53 const zoir = self.diag.zoir.?;53 const note = err.getNotes(self.diag.zoir)[self.index];
54 const note = err.getNotes(zoir)[self.index];
55 self.index += 1;54 self.index += 1;
56 return .{ .zoir = note };55 return .{ .zoir = note };
57 },56 },
...@@ -82,16 +81,15 @@ pub const Error = union(enum) {...@@ -82,16 +81,15 @@ pub const Error = union(enum) {
8281
83 pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Formatter(Note.formatMessage) {82 pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Formatter(Note.formatMessage) {
84 return .{ .data = switch (self) {83 return .{ .data = switch (self) {
85 .zoir => |note| note.msg.get(diag.zoir.?),84 .zoir => |note| note.msg.get(diag.zoir),
86 .type_check => |note| note.msg,85 .type_check => |note| note.msg,
87 } };86 } };
88 }87 }
8988
90 pub fn getLocation(self: Note, diag: *const Diagnostics) Ast.Location {89 pub fn getLocation(self: Note, diag: *const Diagnostics) Ast.Location {
91 const ast = diag.ast.?;
92 switch (self) {90 switch (self) {
93 .zoir => |note| return zoirErrorLocation(ast, note.token, note.node_or_offset),91 .zoir => |note| return zoirErrorLocation(diag.ast, note.token, note.node_or_offset),
94 .type_check => |note| return ast.tokenLocation(note.offset, note.token),92 .type_check => |note| return diag.ast.tokenLocation(note.offset, note.token),
95 }93 }
96 }94 }
97 };95 };
...@@ -101,16 +99,14 @@ pub const Error = union(enum) {...@@ -101,16 +99,14 @@ pub const Error = union(enum) {
101 diag: *const Diagnostics,99 diag: *const Diagnostics,
102100
103 pub fn next(self: *@This()) ?Error {101 pub fn next(self: *@This()) ?Error {
104 const zoir = self.diag.zoir orelse return null;102 if (self.index < self.diag.zoir.compile_errors.len) {
105103 const result: Error = .{ .zoir = self.diag.zoir.compile_errors[self.index] };
106 if (self.index < zoir.compile_errors.len) {
107 const result: Error = .{ .zoir = zoir.compile_errors[self.index] };
108 self.index += 1;104 self.index += 1;
109 return result;105 return result;
110 }106 }
111107
112 if (self.diag.type_check) |err| {108 if (self.diag.type_check) |err| {
113 if (self.index == zoir.compile_errors.len) {109 if (self.index == self.diag.zoir.compile_errors.len) {
114 const result: Error = .{ .type_check = err };110 const result: Error = .{ .type_check = err };
115 self.index += 1;111 self.index += 1;
116 return result;112 return result;
...@@ -168,7 +164,7 @@ pub const Error = union(enum) {...@@ -168,7 +164,7 @@ pub const Error = union(enum) {
168 _ = f;164 _ = f;
169 _ = options;165 _ = options;
170 switch (self.err) {166 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)),
172 .type_check => |tc| try writer.writeAll(tc.message),168 .type_check => |tc| try writer.writeAll(tc.message),
173 }169 }
174 }170 }
...@@ -181,14 +177,13 @@ pub const Error = union(enum) {...@@ -181,14 +177,13 @@ pub const Error = union(enum) {
181 }177 }
182178
183 pub fn getLocation(self: @This(), diag: *const Diagnostics) Ast.Location {179 pub fn getLocation(self: @This(), diag: *const Diagnostics) Ast.Location {
184 const ast = diag.ast.?;
185 return switch (self) {180 return switch (self) {
186 .zoir => |err| return zoirErrorLocation(181 .zoir => |err| return zoirErrorLocation(
187 diag.ast.?,182 diag.ast,
188 err.token,183 err.token,
189 err.node_or_offset,184 err.node_or_offset,
190 ),185 ),
191 .type_check => |err| return ast.tokenLocation(err.offset, err.token),186 .type_check => |err| return diag.ast.tokenLocation(err.offset, err.token),
192 };187 };
193 }188 }
194189
...@@ -211,19 +206,33 @@ pub const Error = union(enum) {...@@ -211,19 +206,33 @@ pub const Error = union(enum) {
211206
212/// Information about the success or failure of a parse.207/// Information about the success or failure of a parse.
213pub const Diagnostics = struct {208pub const Diagnostics = struct {
214 ast: ?Ast = null,209 ast: Ast = .{
215 zoir: ?Zoir = null,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 },
216 type_check: ?Error.TypeCheckFailure = null,225 type_check: ?Error.TypeCheckFailure = null,
217226
218 fn assertEmpty(self: Diagnostics) void {227 fn assertEmpty(self: Diagnostics) void {
219 assert(self.ast == null);228 assert(self.ast.tokens.len == 0);
220 assert(self.zoir == null);229 assert(self.zoir.nodes.len == 0);
221 assert(self.type_check == null);230 assert(self.type_check == null);
222 }231 }
223232
224 pub fn deinit(self: *Diagnostics, gpa: Allocator) void {233 pub fn deinit(self: *Diagnostics, gpa: Allocator) void {
225 if (self.ast) |*ast| ast.deinit(gpa);234 self.ast.deinit(gpa);
226 if (self.zoir) |*zoir| zoir.deinit(gpa);235 self.zoir.deinit(gpa);
227 if (self.type_check) |tc| tc.deinit(gpa);236 if (self.type_check) |tc| tc.deinit(gpa);
228 self.* = undefined;237 self.* = undefined;
229 }238 }
...@@ -3459,13 +3468,13 @@ test "std.zon stop on node" {...@@ -3459,13 +3468,13 @@ test "std.zon stop on node" {
3459 defer diag.deinit(gpa);3468 defer diag.deinit(gpa);
3460 const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &diag, .{});3469 const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &diag, .{});
3461 try std.testing.expectEqual(result.y, 2.5);3470 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));
3463 }3472 }
34643473
3465 {3474 {
3466 var diag: Diagnostics = .{};3475 var diag: Diagnostics = .{};
3467 defer diag.deinit(gpa);3476 defer diag.deinit(gpa);
3468 const result = try fromSlice(Zoir.Node.Index, gpa, "1.23", &diag, .{});3477 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));
3470 }3479 }
3471}3480}