From 5769ed2d4428305e4478cf4e425f5df2469a7ffd Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 14 May 2021 23:10:38 -0700 Subject: [PATCH] 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. --- src/Compilation.zig | 5 +++-- src/Module.zig | 9 +++++++-- src/Sema.zig | 5 +++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 80c8361d8d..ac26b89926 100644 --- a/src/Compilation.zig +++ b/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", }; } diff --git a/src/Module.zig b/src/Module.zig index b4865cf34d..f31b243819 100644 --- a/src/Module.zig +++ b/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 }, }; } diff --git a/src/Sema.zig b/src/Sema.zig index 7022d2fdd3..4070eedc75 100644 --- a/src/Sema.zig +++ b/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),