authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-14 23:10:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-14 23:10:38-07:00
log5769ed2d4428305e4478cf4e425f5df2469a7ffd
treecb36433e95ccf98a2bc97c930f46248250869837
parentb4692c9a7808caabdf474c2acc6d6d3754e5e2e4

stage2: compile log stores node offset

Previously, compile log stored full SrcLoc info, which included absolute AST node index. This becomes invalid after an incremental compilation. To make it survive incremental compilation, store an offset from parent Decl instead.

3 files changed, 13 insertions(+), 6 deletions(-)

src/Compilation.zig+3-2
......@@ -1810,8 +1810,9 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
18101810 const compile_log_items = module.compile_log_decls.items();
18111811 if (errors.items.len == 0 and compile_log_items.len != 0) {
18121812 // First one will be the error; subsequent ones will be notes.
1813 const src_loc = compile_log_items[0].key.nodeOffsetSrcLoc(compile_log_items[0].value);
18131814 const err_msg = Module.ErrorMsg{
1814 .src_loc = compile_log_items[0].value,
1815 .src_loc = src_loc,
18151816 .msg = "found compile log statement",
18161817 .notes = try self.gpa.alloc(Module.ErrorMsg, compile_log_items.len - 1),
18171818 };
......@@ -1819,7 +1820,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
18191820
18201821 for (compile_log_items[1..]) |entry, i| {
18211822 err_msg.notes[i] = .{
1822 .src_loc = entry.value,
1823 .src_loc = entry.key.nodeOffsetSrcLoc(entry.value),
18231824 .msg = "also here",
18241825 };
18251826 }
src/Module.zig+7-2
......@@ -64,7 +64,8 @@ import_table: std.StringArrayHashMapUnmanaged(*Scope.File) = .{},
6464/// a Decl can have a failed_decls entry but have analysis status of success.
6565failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
6666/// Keep track of one `@compileLog` callsite per owner Decl.
67compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, SrcLoc) = .{},
67/// The value is the AST node index offset from the Decl.
68compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, i32) = .{},
6869/// Using a map here for consistency with the other fields here.
6970/// The ErrorMsg memory is owned by the `Scope.File`, using Module's general purpose allocator.
7071failed_files: std.AutoArrayHashMapUnmanaged(*Scope.File, ?*ErrorMsg) = .{},
......@@ -407,10 +408,14 @@ pub const Decl = struct {
407408 }
408409
409410 pub fn srcLoc(decl: Decl) SrcLoc {
411 return decl.nodeOffsetSrcLoc(0);
412 }
413
414 pub fn nodeOffsetSrcLoc(decl: Decl, node_offset: i32) SrcLoc {
410415 return .{
411416 .file_scope = decl.getFileScope(),
412417 .parent_decl_node = decl.src_node,
413 .lazy = .{ .node_offset = 0 },
418 .lazy = .{ .node_offset = node_offset },
414419 };
415420 }
416421
src/Sema.zig+3-2
......@@ -1661,7 +1661,8 @@ fn zirCompileLog(
16611661 const writer = managed.writer();
16621662
16631663 const extra = sema.code.extraData(Zir.Inst.NodeMultiOp, extended.operand);
1664 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };
1664 const src_node = extra.data.src_node;
1665 const src: LazySrcLoc = .{ .node_offset = src_node };
16651666 const args = sema.code.refSlice(extra.end, extended.small);
16661667
16671668 for (args) |arg_ref, i| {
......@@ -1678,7 +1679,7 @@ fn zirCompileLog(
16781679
16791680 const gop = try sema.mod.compile_log_decls.getOrPut(sema.gpa, sema.owner_decl);
16801681 if (!gop.found_existing) {
1681 gop.entry.value = src.toSrcLoc(&block.base);
1682 gop.entry.value = src_node;
16821683 }
16831684 return sema.mod.constInst(sema.arena, src, .{
16841685 .ty = Type.initTag(.void),