progress towards semantic error serialization
Introduces std.zig.ErrorBundle which is a trivially serializeable set of compilation errors. This is in the standard library so that both the compiler and the build runner can use it. The idea is they will use it to communicate compilation errors over a binary protocol. The binary encoding of ErrorBundle is a bit problematic - I got a little too aggressive with compaction. I need to change it in a follow-up commit to use some indirection in the error message list, otherwise iteration is too unergonomic. In fact it's so problematic right now that the logic getAllErrorsAlloc() actually fails to produce a viable ErrorBundle because it puts SourceLocation data in between the root level ErrorMessage data. This commit has a simplification - redundant logic for rendering AST errors to stderr has been removed in favor of moving the logic for lowering AST errors into AstGen. So even if we get parse errors, the errors will get lowered into ZIR before being reported. I believe this will be useful when working on --autofix. Either way, some redundant brittle logic was happily deleted. In Compilation, updateSubCompilation() is improved to properly perform error reporting when a sub-compilation object fails. It no longer dumps directly to stderr; instead it populates an ErrorBundle object, which gets added to the parent one during getAllErrorsAlloc(). In package fetching code, instead of dumping directly to stderr, it now populates an ErrorBundle object, and gets properly reported at the CLI layer of abstraction.
This commit is contained in:
@@ -3756,67 +3756,9 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
|
||||
file.source_loaded = true;
|
||||
|
||||
file.tree = try Ast.parse(gpa, source, .zig);
|
||||
defer if (!file.tree_loaded) file.tree.deinit(gpa);
|
||||
|
||||
if (file.tree.errors.len != 0) {
|
||||
const parse_err = file.tree.errors[0];
|
||||
|
||||
var msg = std.ArrayList(u8).init(gpa);
|
||||
defer msg.deinit();
|
||||
|
||||
const token_starts = file.tree.tokens.items(.start);
|
||||
const token_tags = file.tree.tokens.items(.tag);
|
||||
|
||||
const extra_offset = file.tree.errorOffset(parse_err);
|
||||
try file.tree.renderError(parse_err, msg.writer());
|
||||
const err_msg = try gpa.create(ErrorMsg);
|
||||
err_msg.* = .{
|
||||
.src_loc = .{
|
||||
.file_scope = file,
|
||||
.parent_decl_node = 0,
|
||||
.lazy = if (extra_offset == 0) .{
|
||||
.token_abs = parse_err.token,
|
||||
} else .{
|
||||
.byte_abs = token_starts[parse_err.token] + extra_offset,
|
||||
},
|
||||
},
|
||||
.msg = try msg.toOwnedSlice(),
|
||||
};
|
||||
if (token_tags[parse_err.token + @boolToInt(parse_err.token_is_prev)] == .invalid) {
|
||||
const bad_off = @intCast(u32, file.tree.tokenSlice(parse_err.token + @boolToInt(parse_err.token_is_prev)).len);
|
||||
const byte_abs = token_starts[parse_err.token + @boolToInt(parse_err.token_is_prev)] + bad_off;
|
||||
try mod.errNoteNonLazy(.{
|
||||
.file_scope = file,
|
||||
.parent_decl_node = 0,
|
||||
.lazy = .{ .byte_abs = byte_abs },
|
||||
}, err_msg, "invalid byte: '{'}'", .{std.zig.fmtEscapes(source[byte_abs..][0..1])});
|
||||
}
|
||||
|
||||
for (file.tree.errors[1..]) |note| {
|
||||
if (!note.is_note) break;
|
||||
|
||||
try file.tree.renderError(note, msg.writer());
|
||||
err_msg.notes = try mod.gpa.realloc(err_msg.notes, err_msg.notes.len + 1);
|
||||
err_msg.notes[err_msg.notes.len - 1] = .{
|
||||
.src_loc = .{
|
||||
.file_scope = file,
|
||||
.parent_decl_node = 0,
|
||||
.lazy = .{ .token_abs = note.token },
|
||||
},
|
||||
.msg = try msg.toOwnedSlice(),
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
comp.mutex.lock();
|
||||
defer comp.mutex.unlock();
|
||||
try mod.failed_files.putNoClobber(gpa, file, err_msg);
|
||||
}
|
||||
file.status = .parse_failure;
|
||||
return error.AnalysisFail;
|
||||
}
|
||||
file.tree_loaded = true;
|
||||
|
||||
// Any potential AST errors are converted to ZIR errors here.
|
||||
file.zir = try AstGen.generate(gpa, file.tree);
|
||||
file.zir_loaded = true;
|
||||
file.status = .success_zir;
|
||||
|
||||
Reference in New Issue
Block a user