diag: preserve envelope fields and reject JSON key collisions in tests

This commit is contained in:
dev committed 2026-10-07 19:28:36 +03:00
1 parent 8ee0f4c2bf
commit 364eb57bbc
16 files changed
+166 -12

No files matched your search

+56
View File
@@ -254,6 +254,62 @@ test "mapped E4302 keeps spawn through repeated cancelled joins and missing mapp
}
}
test "diagnostic payloads preserve stable kinds and distinguish guards from borrows" {
const inputs = [_]struct { file: []const u8, source: []const u8, code: []const u8, field: []const u8, value: []const u8 }{
.{ .file = "unknown.aji", .source = "module t fn main(c:cap<unknown>)->void effects [] {}", .code = "E7002", .field = "capability_kind", .value = "unknown" },
.{ .file = "narrow.air", .source =
\\module t
\\air 0
\\lang 0.1
\\features [capabilities, effects, ownership]
\\fn @f(%c: cap<io.write>, %n: usize) -> void
\\effects []
\\entry:
\\ cap.narrow.budget %c, %n, ok -> @ok, err -> @err
\\ok(%d: cap<io.write>):
\\ ret
\\err(%e: cap_error):
\\ ret
\\
, .code = "E3202", .field = "capability_kind", .value = "io.write" },
.{ .file = "guard.air", .source =
\\module t
\\air 0
\\lang 0.1
\\features [cancellation, effects, ownership, sync, tasks]
\\fn @f(%a: ref<mutex<u64>>) -> void
\\effects [task.wait]
\\entry:
\\ mutex.lock %a, ok -> @locked, cancelled -> @out
\\locked(%g: guard<u64>):
\\ %p = mutex.borrow %g
\\ mutex.unlock %g
\\ borrow.end %p
\\ ret
\\out:
\\ ret
\\
, .code = "E2102", .field = "guard", .value = "%g" },
};
var a: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer a.deinit();
const arena = a.allocator();
for (inputs) |input| {
const report = try checkFile(arena, input.file, input.source);
try std.testing.expectEqual(@as(usize, 1), report.diagnostics.len);
const d = report.diagnostics[0];
try std.testing.expectEqualStrings(input.code, d.code.id);
try diag.expectUniqueFields(d);
var writer: std.Io.Writer.Allocating = .init(arena);
try d.writeJson(&writer.writer);
const parsed = try std.json.parseFromSlice(std.json.Value, arena, writer.written(), .{ .duplicate_field_behavior = .@"error" });
defer parsed.deinit();
try std.testing.expectEqualStrings(d.code.kind, parsed.value.object.get("kind").?.string);
try std.testing.expectEqualStrings(input.value, parsed.value.object.get(input.field).?.string);
if (std.mem.eql(u8, input.code, "E2102")) try std.testing.expectEqualStrings("%p", parsed.value.object.get("borrow").?.string);
}
}
test "standard type diagnostics use the public source name" {
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
defer a.deinit();
+17
View File
@@ -292,6 +292,23 @@ test "related locations are typed, ordered and appended after existing fields" {
, w.buffered());
}
/// Regression invariant used by verifier/pipeline tests; never a runtime
/// language check. Payload names cannot hide the stable envelope or each other.
pub fn expectUniqueFields(d: Diagnostic) !void {
try std.testing.expect(hasUniqueFields(d));
}
/// Diagnostic contract validation for test tooling, including final source
/// mappings; not used to make any verifier/runtime acceptance decision.
pub fn hasUniqueFields(d: Diagnostic) bool {
const envelope = [_][]const u8{ "code", "kind", "severity", "file", "line", "col", "message", "repair_candidates", "related_locations" };
for (d.fields, 0..) |f, i| {
for (envelope) |reserved| if (std.mem.eql(u8, f.name, reserved)) return false;
for (d.fields[0..i]) |previous| if (std.mem.eql(u8, previous.name, f.name)) return false;
}
return true;
}
test "registry mirrors docs/reference/error-codes.md" {
const md = @embedFile("error_codes_md");
var seen: usize = 0;
+1 -1
View File
@@ -716,7 +716,7 @@ const Lowerer = struct {
};
},
.cap => |k| {
if (authority.kindIndex(k) == null) return l.fail("E7002", t.span, "unknown capability kind", &.{.{ .name = "kind", .value = k }});
if (authority.kindIndex(k) == null) return l.fail("E7002", t.span, "unknown capability kind", &.{.{ .name = "capability_kind", .value = k }});
return .{ .cap = k };
},
.file => |m| {
+4 -1
View File
@@ -120,7 +120,10 @@ pub fn expectCodes(src: []const u8, codes: []const []const u8) !void {
for (ds) |d| std.debug.print("got {s} {s} at {d}:{d}\n", .{ d.code.id, d.message, d.span.?.line, d.span.?.col });
return error.TestExpectedEqual;
}
for (codes, ds) |c, d| try std.testing.expectEqualStrings(c, d.code.id);
for (codes, ds) |c, d| {
try std.testing.expectEqualStrings(c, d.code.id);
try diag.expectUniqueFields(d);
}
}
pub const test_header = "module t\nair 0\nlang 0.1\nfeatures []\n";
+1 -1
View File
@@ -654,7 +654,7 @@ fn step(w: *Walker, inst: air.Inst) !void {
.mutex_unlock => |g| {
if (!w.live[g]) return w.report("E2002", "linear value consumed twice", g, null);
for (w.live, 0..) |is_live, b| if (is_live and w.info[b] != null and w.info[b].?.root == g) {
try w.report("E2102", "guard unlocked while a borrow from it is live", g, .{ "borrow", @intCast(b) });
try w.report("E2102", "guard unlocked while a borrow from it is live", @intCast(b), .{ "guard", g });
break;
};
w.live[g] = false;
+1 -1
View File
@@ -1165,7 +1165,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
// an attempt to widen (spec 05 §10.4).
if (!air.authority.hasForm(t.cap, x.form)) try fc.c.report("E3202", span, "narrowing form not defined for this capability kind", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "kind", .value = .{ .str = air.authority.kinds[t.cap].name } },
.{ .name = "capability_kind", .value = .{ .str = air.authority.kinds[t.cap].name } },
.{ .name = "form", .value = .{ .str = x.form } },
});
if (std.mem.eql(u8, x.form, "budget")) {