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

+73
View File
@@ -136,6 +136,16 @@ pub const Span = struct {
col: u32,
};
/// Non-binding factual context (spec 10 §2); no inferred repair or name.
pub const RelatedLocation = struct {
role: enum { conflicting_borrow, task_spawn },
value: []const u8,
/// Null only when a known AIR origin has no source mapping.
span: ?Span,
/// Original AIR position for lowered source; the file is not serialized.
air_span: ?Span = null,
};
/// Structured, code-specific payload (e.g. "value": "%17").
pub const Field = struct {
name: []const u8,
@@ -156,6 +166,7 @@ pub const Diagnostic = struct {
fields: []const Field = &.{},
/// Non-binding structured suggestions (spec 10 §3).
repair_candidates: []const []const u8 = &.{},
related_locations: []const RelatedLocation = &.{},
/// Writes the diagnostic as a single JSON object (no trailing newline).
/// Key order is fixed so output is byte-for-byte deterministic.
@@ -197,6 +208,33 @@ pub const Diagnostic = struct {
}
try s.endArray();
}
if (d.related_locations.len != 0) {
try s.objectField("related_locations");
try s.beginArray();
for (d.related_locations) |related| {
try s.beginObject();
try s.objectField("role");
try s.write(@tagName(related.role));
try s.objectField("value");
try s.write(related.value);
if (related.span) |sp| {
try s.objectField("file");
try s.write(sp.file);
try s.objectField("line");
try s.write(sp.line);
try s.objectField("col");
try s.write(sp.col);
}
if (related.air_span) |sp| {
try s.objectField("air_line");
try s.write(sp.line);
try s.objectField("air_col");
try s.write(sp.col);
}
try s.endObject();
}
try s.endArray();
}
try s.endObject();
}
};
@@ -219,6 +257,41 @@ test "writeJson is deterministic and structured" {
, w.buffered());
}
test "related locations are typed, ordered and appended after existing fields" {
var buf: [1024]u8 = undefined;
var w: std.Io.Writer = .fixed(&buf);
const d: Diagnostic = .{
.code = lookup("E2101").?,
.span = .{ .file = "main.aji", .line = 9, .col = 5 },
.fields = &.{.{ .name = "conflicts_with", .value = .{ .str = "%view_4" } }},
.message = "conflict",
.repair_candidates = &.{"example"},
.related_locations = &.{.{
.role = .conflicting_borrow,
.value = "%view_4",
.span = .{ .file = "dep.aji", .line = 3, .col = 7 },
.air_span = .{ .file = "main.aji", .line = 12, .col = 5 },
}},
};
try d.writeJson(&w);
try std.testing.expectEqualStrings(
\\{"code":"E2101","kind":"borrow.mutable_alias","severity":"error","file":"main.aji","line":9,"col":5,"conflicts_with":"%view_4","message":"conflict","repair_candidates":[{"kind":"example"}],"related_locations":[{"role":"conflicting_borrow","value":"%view_4","file":"dep.aji","line":3,"col":7,"air_line":12,"air_col":5}]}
, w.buffered());
w = .fixed(&buf);
try (Diagnostic{
.code = lookup("E4302").?,
.related_locations = &.{.{
.role = .task_spawn,
.value = "%back",
.span = null,
.air_span = .{ .file = "main.aji", .line = 17, .col = 5 },
}},
}).writeJson(&w);
try std.testing.expectEqualStrings(
\\{"code":"E4302","kind":"task.borrow_escape","severity":"error","related_locations":[{"role":"task_spawn","value":"%back","air_line":17,"air_col":5}]}
, w.buffered());
}
test "registry mirrors docs/reference/error-codes.md" {
const md = @embedFile("error_codes_md");
var seen: usize = 0;