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

+41
View File
@@ -74,6 +74,16 @@ pub fn syntaxDiagnostic(arena: std.mem.Allocator, file: []const u8, f: parse.Fai
/// location stays as `air_line`/`air_col` metadata (spec 15 §10).
pub fn mapDiagnostic(arena: std.mem.Allocator, d: diag.Diagnostic, file: []const u8, lines: []const ?Span) std.mem.Allocator.Error!diag.Diagnostic {
var out = d;
if (d.related_locations.len != 0) {
const related = try arena.dupe(diag.RelatedLocation, d.related_locations);
for (related) |*r| {
const origin = r.span orelse continue;
r.air_span = origin;
const source: ?Span = if (origin.line >= 1 and origin.line <= lines.len) lines[origin.line - 1] else null;
r.span = if (source) |s| .{ .file = s.file orelse file, .line = s.start.line, .col = s.start.col } else null;
}
out.related_locations = related;
}
const sp = d.span orelse return out;
const fields = try arena.alloc(diag.Field, d.fields.len + 2);
@memcpy(fields[0..d.fields.len], d.fields);
@@ -92,6 +102,37 @@ test {
_ = format;
}
test "related AIR positions map independently with no guessed source fallback" {
var a: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer a.deinit();
const lines: []const ?Span = &.{
.{ .file = "entry.aji", .start = .{ .line = 8, .col = 9 }, .end = .{ .line = 8, .col = 10 } },
.{ .file = "dep.aji", .start = .{ .line = 3, .col = 5 }, .end = .{ .line = 3, .col = 6 } },
null,
};
const mapped = try mapDiagnostic(a.allocator(), .{
.code = diag.lookup("E2101").?,
.span = .{ .file = "lowered.air", .line = 1, .col = 5 },
.related_locations = &.{
.{ .role = .conflicting_borrow, .value = "%a", .span = .{ .file = "lowered.air", .line = 2, .col = 7 } },
.{ .role = .task_spawn, .value = "%b", .span = .{ .file = "lowered.air", .line = 3, .col = 5 } },
.{ .role = .task_spawn, .value = "%c", .span = .{ .file = "lowered.air", .line = 99, .col = 5 } },
},
}, "fallback.aji", lines);
try std.testing.expectEqualStrings("entry.aji", mapped.span.?.file);
try std.testing.expectEqual(@as(u32, 8), mapped.span.?.line);
const located = mapped.related_locations[0];
try std.testing.expectEqualStrings("dep.aji", located.span.?.file);
try std.testing.expectEqual(@as(u32, 3), located.span.?.line);
try std.testing.expectEqual(@as(u32, 5), located.span.?.col);
try std.testing.expectEqual(@as(u32, 2), located.air_span.?.line);
try std.testing.expectEqual(@as(u32, 7), located.air_span.?.col);
for (mapped.related_locations[1..], [_]u32{ 3, 99 }) |related, line| {
try std.testing.expect(related.span == null);
try std.testing.expectEqual(line, related.air_span.?.line);
}
}
pub const Size = struct { bytes: usize, lines: usize };
/// AIR size with every byte-string materialization (the instructions from