aboutsummaryrefslogtreecommitdiff
path: root/src/Compilation.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-06-30 03:00:07 +0100
committermlugg <mlugg@mlugg.co.uk>2024-07-04 21:01:41 +0100
commitded5c759f83a4da355a128dd4d7f5e22cbd3cabe (patch)
treeb862bbdf36b892e9c39f472c6759f084c87d64b2 /src/Compilation.zig
parent089bbd6588d82ccda0646e756006cf5787eadef2 (diff)
downloadzig-ded5c759f83a4da355a128dd4d7f5e22cbd3cabe.tar.gz
zig-ded5c759f83a4da355a128dd4d7f5e22cbd3cabe.zip
Zcu: store `LazySrcLoc` in error messages
This change modifies `Zcu.ErrorMsg` to store a `Zcu.LazySrcLoc` rather than a `Zcu.SrcLoc`. Everything else is dominoes. The reason for this change is incremental compilation. If a failed `AnalUnit` is up-to-date on an update, we want to re-use the old error messages. However, the file containing the error location may have been modified, and `SrcLoc` cannot survive such a modification. `LazySrcLoc` is designed to be correct across incremental updates. Therefore, we defer source location resolution until `Compilation` gathers the compile errors into the `ErrorBundle`.
Diffstat (limited to 'src/Compilation.zig')
-rw-r--r--src/Compilation.zig91
1 files changed, 47 insertions, 44 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig
index 3c97cd3145..55084fb971 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -2629,22 +2629,24 @@ fn reportMultiModuleErrors(mod: *Module) !void {
for (notes[0..num_notes], file.references.items[0..num_notes], 0..) |*note, ref, i| {
errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
note.* = switch (ref) {
- .import => |loc| blk: {
- break :blk try Module.ErrorMsg.init(
- mod.gpa,
- loc,
- "imported from module {s}",
- .{loc.file_scope.mod.fully_qualified_name},
- );
- },
- .root => |pkg| blk: {
- break :blk try Module.ErrorMsg.init(
- mod.gpa,
- .{ .file_scope = file, .base_node = 0, .lazy = .entire_file },
- "root of module {s}",
- .{pkg.fully_qualified_name},
- );
- },
+ .import => |import| try Module.ErrorMsg.init(
+ mod.gpa,
+ .{
+ .base_node_inst = try mod.intern_pool.trackZir(mod.gpa, import.file, .main_struct_inst),
+ .offset = .{ .token_abs = import.token },
+ },
+ "imported from module {s}",
+ .{import.file.mod.fully_qualified_name},
+ ),
+ .root => |pkg| try Module.ErrorMsg.init(
+ mod.gpa,
+ .{
+ .base_node_inst = try mod.intern_pool.trackZir(mod.gpa, file, .main_struct_inst),
+ .offset = .entire_file,
+ },
+ "root of module {s}",
+ .{pkg.fully_qualified_name},
+ ),
};
}
errdefer for (notes[0..num_notes]) |*n| n.deinit(mod.gpa);
@@ -2652,7 +2654,10 @@ fn reportMultiModuleErrors(mod: *Module) !void {
if (omitted > 0) {
notes[num_notes] = try Module.ErrorMsg.init(
mod.gpa,
- .{ .file_scope = file, .base_node = 0, .lazy = .entire_file },
+ .{
+ .base_node_inst = try mod.intern_pool.trackZir(mod.gpa, file, .main_struct_inst),
+ .offset = .entire_file,
+ },
"{} more references omitted",
.{omitted},
);
@@ -2661,7 +2666,10 @@ fn reportMultiModuleErrors(mod: *Module) !void {
const err = try Module.ErrorMsg.create(
mod.gpa,
- .{ .file_scope = file, .base_node = 0, .lazy = .entire_file },
+ .{
+ .base_node_inst = try mod.intern_pool.trackZir(mod.gpa, file, .main_struct_inst),
+ .offset = .entire_file,
+ },
"file exists in multiple modules",
.{},
);
@@ -3060,7 +3068,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
const values = zcu.compile_log_sources.values();
// First one will be the error; subsequent ones will be notes.
- const src_loc = values[0].src().upgrade(zcu);
+ const src_loc = values[0].src();
const err_msg: Module.ErrorMsg = .{
.src_loc = src_loc,
.msg = "found compile log statement",
@@ -3070,7 +3078,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
for (values[1..], err_msg.notes) |src_info, *note| {
note.* = .{
- .src_loc = src_info.src().upgrade(zcu),
+ .src_loc = src_info.src(),
.msg = "also here",
};
}
@@ -3139,8 +3147,9 @@ pub fn addModuleErrorMsg(
) !void {
const gpa = eb.gpa;
const ip = &mod.intern_pool;
- const err_source = module_err_msg.src_loc.file_scope.getSource(gpa) catch |err| {
- const file_path = try module_err_msg.src_loc.file_scope.fullPath(gpa);
+ const err_src_loc = module_err_msg.src_loc.upgrade(mod);
+ const err_source = err_src_loc.file_scope.getSource(gpa) catch |err| {
+ const file_path = try err_src_loc.file_scope.fullPath(gpa);
defer gpa.free(file_path);
try eb.addRootErrorMessage(.{
.msg = try eb.printString("unable to load '{s}': {s}", .{
@@ -3149,9 +3158,9 @@ pub fn addModuleErrorMsg(
});
return;
};
- const err_span = try module_err_msg.src_loc.span(gpa);
+ const err_span = try err_src_loc.span(gpa);
const err_loc = std.zig.findLineColumn(err_source.bytes, err_span.main);
- const file_path = try module_err_msg.src_loc.file_scope.fullPath(gpa);
+ const file_path = try err_src_loc.file_scope.fullPath(gpa);
defer gpa.free(file_path);
var ref_traces: std.ArrayListUnmanaged(ErrorBundle.ReferenceTrace) = .{};
@@ -3208,7 +3217,7 @@ pub fn addModuleErrorMsg(
.span_end = err_span.end,
.line = @intCast(err_loc.line),
.column = @intCast(err_loc.column),
- .source_line = if (module_err_msg.src_loc.lazy == .entire_file)
+ .source_line = if (err_src_loc.lazy == .entire_file)
0
else
try eb.addString(err_loc.source_line),
@@ -3225,10 +3234,11 @@ pub fn addModuleErrorMsg(
defer notes.deinit(gpa);
for (module_err_msg.notes) |module_note| {
- const source = try module_note.src_loc.file_scope.getSource(gpa);
- const span = try module_note.src_loc.span(gpa);
+ const note_src_loc = module_note.src_loc.upgrade(mod);
+ const source = try note_src_loc.file_scope.getSource(gpa);
+ const span = try note_src_loc.span(gpa);
const loc = std.zig.findLineColumn(source.bytes, span.main);
- const note_file_path = try module_note.src_loc.file_scope.fullPath(gpa);
+ const note_file_path = try note_src_loc.file_scope.fullPath(gpa);
defer gpa.free(note_file_path);
const gop = try notes.getOrPutContext(gpa, .{
@@ -3522,7 +3532,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo
InternPool.AnalUnit.wrap(.{ .decl = decl_index }),
try Module.ErrorMsg.create(
gpa,
- decl.navSrcLoc(module).upgrade(module),
+ decl.navSrcLoc(module),
"unable to update line number: {s}",
.{@errorName(err)},
),
@@ -4023,9 +4033,8 @@ fn workerAstGenFile(
const res = mod.importFile(file, import_path) catch continue;
if (!res.is_pkg) {
res.file.addReference(mod.*, .{ .import = .{
- .file_scope = file,
- .base_node = 0,
- .lazy = .{ .token_abs = item.data.token },
+ .file = file,
+ .token = item.data.token,
} }) catch continue;
}
break :blk res;
@@ -4398,20 +4407,14 @@ fn reportRetryableAstGenError(
file.status = .retryable_failure;
- const src_loc: Module.SrcLoc = switch (src) {
+ const src_loc: Module.LazySrcLoc = switch (src) {
.root => .{
- .file_scope = file,
- .base_node = 0,
- .lazy = .entire_file,
+ .base_node_inst = try mod.intern_pool.trackZir(gpa, file, .main_struct_inst),
+ .offset = .entire_file,
},
- .import => |info| blk: {
- const importing_file = info.importing_file;
-
- break :blk .{
- .file_scope = importing_file,
- .base_node = 0,
- .lazy = .{ .token_abs = info.import_tok },
- };
+ .import => |info| .{
+ .base_node_inst = try mod.intern_pool.trackZir(gpa, info.importing_file, .main_struct_inst),
+ .offset = .{ .token_abs = info.import_tok },
},
};