Commit 5769ed2d44

Andrew Kelley <andrew@ziglang.org>
2021-05-15 08:10:38
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.
1 parent b4692c9
src/Compilation.zig
@@ -1810,8 +1810,9 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
         const compile_log_items = module.compile_log_decls.items();
         if (errors.items.len == 0 and compile_log_items.len != 0) {
             // First one will be the error; subsequent ones will be notes.
+            const src_loc = compile_log_items[0].key.nodeOffsetSrcLoc(compile_log_items[0].value);
             const err_msg = Module.ErrorMsg{
-                .src_loc = compile_log_items[0].value,
+                .src_loc = src_loc,
                 .msg = "found compile log statement",
                 .notes = try self.gpa.alloc(Module.ErrorMsg, compile_log_items.len - 1),
             };
@@ -1819,7 +1820,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
 
             for (compile_log_items[1..]) |entry, i| {
                 err_msg.notes[i] = .{
-                    .src_loc = entry.value,
+                    .src_loc = entry.key.nodeOffsetSrcLoc(entry.value),
                     .msg = "also here",
                 };
             }
src/Module.zig
@@ -64,7 +64,8 @@ import_table: std.StringArrayHashMapUnmanaged(*Scope.File) = .{},
 /// a Decl can have a failed_decls entry but have analysis status of success.
 failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
 /// Keep track of one `@compileLog` callsite per owner Decl.
-compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, SrcLoc) = .{},
+/// The value is the AST node index offset from the Decl.
+compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, i32) = .{},
 /// Using a map here for consistency with the other fields here.
 /// The ErrorMsg memory is owned by the `Scope.File`, using Module's general purpose allocator.
 failed_files: std.AutoArrayHashMapUnmanaged(*Scope.File, ?*ErrorMsg) = .{},
@@ -407,10 +408,14 @@ pub const Decl = struct {
     }
 
     pub fn srcLoc(decl: Decl) SrcLoc {
+        return decl.nodeOffsetSrcLoc(0);
+    }
+
+    pub fn nodeOffsetSrcLoc(decl: Decl, node_offset: i32) SrcLoc {
         return .{
             .file_scope = decl.getFileScope(),
             .parent_decl_node = decl.src_node,
-            .lazy = .{ .node_offset = 0 },
+            .lazy = .{ .node_offset = node_offset },
         };
     }
 
src/Sema.zig
@@ -1661,7 +1661,8 @@ fn zirCompileLog(
     const writer = managed.writer();
 
     const extra = sema.code.extraData(Zir.Inst.NodeMultiOp, extended.operand);
-    const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };
+    const src_node = extra.data.src_node;
+    const src: LazySrcLoc = .{ .node_offset = src_node };
     const args = sema.code.refSlice(extra.end, extended.small);
 
     for (args) |arg_ref, i| {
@@ -1678,7 +1679,7 @@ fn zirCompileLog(
 
     const gop = try sema.mod.compile_log_decls.getOrPut(sema.gpa, sema.owner_decl);
     if (!gop.found_existing) {
-        gop.entry.value = src.toSrcLoc(&block.base);
+        gop.entry.value = src_node;
     }
     return sema.mod.constInst(sema.arena, src, .{
         .ty = Type.initTag(.void),