Commit 0486aa5081

mlugg <mlugg@mlugg.co.uk>
2024-06-18 05:41:02
Zir: provide absolute node for `reify`
Since we track `reify` instructions across incremental updates, it is acceptable to treat it as the baseline for a relative source location. This turns out to be a good idea, since it makes it easy to define the source location for a reified type.
1 parent edf1477
lib/std/zig/AstGen.zig
@@ -9361,9 +9361,10 @@ fn builtinCall(
             try gz.instructions.ensureUnusedCapacity(gpa, 1);
             try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
 
-            const payload_index = try gz.astgen.addExtra(Zir.Inst.UnNode{
-                .node = gz.nodeIndexToRelative(node),
+            const payload_index = try gz.astgen.addExtra(Zir.Inst.Reify{
+                .node = node, // Absolute node index -- see the definition of `Reify`.
                 .operand = operand,
+                .src_line = astgen.source_line,
             });
             const new_index: Zir.Inst.Index = @enumFromInt(gz.astgen.instructions.len);
             gz.astgen.instructions.appendAssumeCapacity(.{
lib/std/zig/Zir.zig
@@ -2835,7 +2835,9 @@ pub const Inst = struct {
     };
 
     pub const Reify = struct {
-        node: i32,
+        /// This node is absolute, because `reify` instructions are tracked across updates, and
+        /// this simplifies the logic for getting source locations for types.
+        node: Ast.Node.Index,
         operand: Ref,
         src_line: u32,
     };
src/Module.zig
@@ -2374,6 +2374,7 @@ pub const LazySrcLoc = struct {
                 .union_decl => zir.extraData(Zir.Inst.UnionDecl, inst.data.extended.operand).data.src_node,
                 .enum_decl => zir.extraData(Zir.Inst.EnumDecl, inst.data.extended.operand).data.src_node,
                 .opaque_decl => zir.extraData(Zir.Inst.OpaqueDecl, inst.data.extended.operand).data.src_node,
+                .reify => zir.extraData(Zir.Inst.Reify, inst.data.extended.operand).data.node,
                 else => unreachable,
             },
             else => unreachable,
src/print_zir.zig
@@ -586,7 +586,10 @@ const Writer = struct {
                 try stream.print("{d}, ", .{inst_data.src_line});
                 try self.writeInstRef(stream, inst_data.operand);
                 try stream.writeAll(")) ");
-                try self.writeSrcNode(stream, inst_data.node);
+                const prev_parent_decl_node = self.parent_decl_node;
+                self.parent_decl_node = inst_data.node;
+                defer self.parent_decl_node = prev_parent_decl_node;
+                try self.writeSrcNode(stream, 0);
             },
 
             .builtin_extern,
src/Sema.zig
@@ -21210,10 +21210,22 @@ fn zirReify(
     const ip = &mod.intern_pool;
     const name_strategy: Zir.Inst.NameStrategy = @enumFromInt(extended.small);
     const extra = sema.code.extraData(Zir.Inst.Reify, extended.operand).data;
-    const src = block.nodeOffset(extra.node);
+    const tracked_inst = try ip.trackZir(gpa, block.getFileScope(mod), inst);
+    const src: LazySrcLoc = .{
+        .base_node_inst = tracked_inst,
+        .offset = LazySrcLoc.Offset.nodeOffset(0),
+    };
+    const operand_src: LazySrcLoc = .{
+        .base_node_inst = tracked_inst,
+        .offset = .{
+            .node_offset_builtin_call_arg = .{
+                .builtin_call_node = 0, // `tracked_inst` is precisely the `reify` instruction, so offset is 0
+                .arg_index = 0,
+            },
+        },
+    };
     const type_info_ty = try sema.getBuiltinType("Type");
     const uncasted_operand = try sema.resolveInst(extra.operand);
-    const operand_src = block.builtinCallArgSrc(extra.node, 0);
     const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);
     const val = try sema.resolveConstDefinedValue(block, operand_src, type_info, .{
         .needed_comptime_reason = "operand to @Type must be comptime-known",
src/type.zig
@@ -3336,22 +3336,11 @@ pub const Type = struct {
         const ip = &zcu.intern_pool;
         return .{
             .base_node_inst = switch (ip.indexToKey(ty.toIntern())) {
-                .struct_type => |info| switch (info) {
-                    .declared => ip.loadStructType(ty.toIntern()).zir_index.unwrap() orelse return null,
-                    else => return null,
-                },
-                .union_type => |info| switch (info) {
-                    .declared => ip.loadUnionType(ty.toIntern()).zir_index,
-                    else => return null,
-                },
-                .opaque_type => |info| switch (info) {
-                    .declared => ip.loadOpaqueType(ty.toIntern()).zir_index,
-                    else => return null,
-                },
-                .enum_type => |info| switch (info) {
-                    .declared => ip.loadEnumType(ty.toIntern()).zir_index.unwrap().?,
+                .struct_type, .union_type, .opaque_type, .enum_type => |info| switch (info) {
+                    .declared => |d| d.zir_index,
+                    .reified => |r| r.zir_index,
                     .generated_tag => |gt| ip.loadUnionType(gt.union_type).zir_index, // must be declared since we can't generate tags when reifying
-                    else => return null,
+                    .empty_struct => return null,
                 },
                 else => return null,
             },