diag: add factual related borrow and task locations

This commit is contained in:
dev committed 2026-10-07 19:12:58 +03:00
1 parent dbd9763713
commit 8ee0f4c2bf
27 files changed
+659 -18

No files matched your search

+32 -6
View File
@@ -11,6 +11,7 @@ const std = @import("std");
const air = @import("../air.zig");
const verifier = @import("../verifier.zig");
const ssa = @import("ssa.zig");
const diag = @import("../diag.zig");
const Context = verifier.Context;
const ValueId = air.ValueId;
@@ -29,7 +30,15 @@ const Seg = union(enum) {
const Mode = enum { shared, exclusive };
const Loan = struct { task: ValueId, borrow: ValueId, root: ValueId, path: []const Seg, mode: Mode };
const Loan = struct {
task: ValueId,
borrow: ValueId,
root: ValueId,
path: []const Seg,
mode: Mode,
/// Diagnostic provenance only; never participates in borrow/liveness state.
spawn: air.Span,
};
/// Why a resource is no longer live (spec 06 §6.3).
const Dead = enum { none, closed, moved };
@@ -85,14 +94,27 @@ const Walker = struct {
}
fn report(w: *Walker, code: []const u8, message: []const u8, borrow: ValueId, extra: ?struct { []const u8, ValueId }) !void {
return w.reportWithOrigin(code, message, borrow, extra, null);
}
fn reportWithOrigin(w: *Walker, code: []const u8, message: []const u8, borrow: ValueId, extra: ?struct { []const u8, ValueId }, spawn: ?air.Span) !void {
if (extra) |e| {
try w.c.report(code, w.span, message, &.{
var related: [1]diag.RelatedLocation = undefined;
const context: []const diag.RelatedLocation = if ((std.mem.eql(u8, code, "E2101") or std.mem.eql(u8, code, "E2104")) and std.mem.eql(u8, e[0], "conflicts_with")) blk: {
const sp = w.f.values[e[1]].span;
related[0] = .{ .role = .conflicting_borrow, .value = try w.name(e[1]), .span = .{ .file = w.c.file, .line = sp.line, .col = sp.col } };
break :blk &related;
} else if (spawn) |sp| blk: {
related[0] = .{ .role = .task_spawn, .value = try w.name(e[1]), .span = .{ .file = w.c.file, .line = sp.line, .col = sp.col } };
break :blk &related;
} else &.{};
try w.c.reportRelated(code, w.span, message, &.{
.{ .name = "function", .value = .{ .str = w.f.name } },
.{ .name = "block", .value = .{ .str = w.f.blocks[w.block].name } },
.{ .name = "inst", .value = .{ .int = w.index } },
.{ .name = "borrow", .value = .{ .str = try w.name(borrow) } },
.{ .name = e[0], .value = .{ .str = try w.name(e[1]) } },
});
}, context);
} else {
try w.c.report(code, w.span, message, &.{
.{ .name = "function", .value = .{ .str = w.f.name } },
@@ -411,7 +433,7 @@ fn step(w: *Walker, inst: air.Inst) !void {
// Ended even when rejected, so one mistake yields one diagnostic.
defer w.live[v] = false;
for (w.loans.items) |l| if (w.live[l.task] and (l.borrow == v or w.isAncestor(v, l.borrow))) {
return w.report("E4302", "borrow ended while it is lent to a live task", v, .{ "task", l.task });
return w.reportWithOrigin("E4302", "borrow ended while it is lent to a live task", v, .{ "task", l.task }, l.spawn);
};
for (w.live, 0..) |is_live, g| if (is_live and w.guard_parent[g] != null and w.isAncestor(v, w.guard_parent[g].?)) {
return w.report("E2102", "mutex borrow ended while a guard locked through it is live", v, .{ "guard", @intCast(g) });
@@ -493,7 +515,7 @@ fn step(w: *Walker, inst: air.Inst) !void {
const ai = w.info[a].?;
// The loan must not conflict with live borrows or loans.
try w.accessCode(ai.root, ai.path, ai.mode, a, a, "E4101", "lending a borrow that conflicts with a live borrow");
try w.loans.append(w.c.arena, .{ .task = t, .borrow = a, .root = ai.root, .path = ai.path, .mode = ai.mode });
try w.loans.append(w.c.arena, .{ .task = t, .borrow = a, .root = ai.root, .path = ai.path, .mode = ai.mode, .spawn = inst.span });
} else if (w.isResource(a)) try w.consume(a, .moved);
}
w.live[t] = true;
@@ -557,7 +579,11 @@ fn step(w: *Walker, inst: air.Inst) !void {
const n = w.loans.items.len;
for (0..n) |i| {
const l = w.loans.items[i];
if (l.task == t) try w.loans.append(w.c.arena, .{ .task = back, .borrow = l.borrow, .root = l.root, .path = l.path, .mode = l.mode });
if (l.task == t) {
var transferred = l;
transferred.task = back;
try w.loans.append(w.c.arena, transferred);
}
}
try w.consume(t, .closed);
},