cli: map block diagnostics to their source constructs

This commit is contained in:
dev committed 2026-10-07 21:45:57 +03:00
1 parent 1495650587
commit 1a80c42289
30 files changed
+555 -27

No files matched your search

+27 -25
View File
@@ -156,7 +156,7 @@ pub const Output = struct {
/// `lines[i]` is the source span of AIR line `i + 1`, if any.
lines: []const ?Span,
/// The same AIR with `; line:col-line:col` comments before each
/// instruction (not canonical; `aji lower --annotate`).
/// instruction and block label (not canonical; `aji lower --annotate`).
annotated: []const u8,
/// Byte-string materializations, for the normalized size metric
/// (spec 15 §11): `constant_of[i]` is 0, or `k` when AIR line `i + 1`
@@ -193,6 +193,8 @@ const Inst = struct { text: []const u8, span: ?Span, constant: u32 = 0 };
const Block = struct {
name: []const u8,
/// The source construct that creates this block (including joins).
span: Span,
params: std.ArrayList(Param) = .empty,
insts: std.ArrayList(Inst) = .empty,
terminated: bool = false,
@@ -438,7 +440,7 @@ const Lowerer = struct {
try l.out.appendSlice(l.arena, text);
try l.out.append(l.arena, '\n');
try l.lines.append(l.arena, span);
if (span) |s| if (std.mem.startsWith(u8, text, " ")) {
if (span) |s| if (std.mem.startsWith(u8, text, " ") or std.mem.endsWith(u8, text, ":")) {
if (s.file) |file| {
try l.annotated.appendSlice(l.arena, try l.fmt(" ; {s}:{d}:{d}-{d}:{d}\n", .{ file, s.start.line, s.start.col, s.end.line, s.end.col }));
} else try l.annotated.appendSlice(l.arena, try l.fmt(" ; {d}:{d}-{d}:{d}\n", .{ s.start.line, s.start.col, s.end.line, s.end.col }));
@@ -759,7 +761,7 @@ const Lowerer = struct {
try header.appendSlice(l.arena, try l.fmt(") -> {s}", .{l.tyText(sig.ret)}));
if (f.from) |origin| try header.appendSlice(l.arena, try l.fmt(" from %{s}", .{origin}));
_ = try l.newBlock("entry");
_ = try l.newBlock("entry", f.span);
l.cur = 0;
const tail = try l.blockBodyWant(f.body, sig.ret);
if (l.cur != null) {
@@ -798,7 +800,7 @@ const Lowerer = struct {
try head.append(l.arena, ')');
}
try head.appendSlice(l.arena, ":");
try l.line(head.items, null);
try l.line(head.items, b.span);
for (b.insts.items) |inst| try l.lineTagged(try l.fmt(" {s}", .{inst.text}), inst.span, inst.constant);
}
}
@@ -821,12 +823,12 @@ const Lowerer = struct {
}
}
fn newBlock(l: *Lowerer, kind: []const u8) Error!usize {
fn newBlock(l: *Lowerer, kind: []const u8, span: Span) Error!usize {
const name = if (l.blocks.items.len == 0) kind else blk: {
l.counter += 1;
break :blk try l.fmt("{s}_{d}", .{ kind, l.counter });
};
try l.blocks.append(l.arena, .{ .name = name });
try l.blocks.append(l.arena, .{ .name = name, .span = span });
return l.blocks.items.len - 1;
}
@@ -1234,9 +1236,9 @@ const Lowerer = struct {
const lp_ast: LoopAst = .{ .cond = cond, .body = body, .step = if (step) |st| st.name else null };
const vars = try l.carriedLoop(depth, span, lp_ast);
const exit_vars = try l.carriedLive(depth, span, .{ .loop = lp_ast });
const header = try l.newBlock(if (cond == null) "loop" else "while");
const exit = try l.newBlock("after");
const cont = if (step != null) try l.newBlock("step") else header;
const header = try l.newBlock(if (cond == null) "loop" else "while", span);
const exit = try l.newBlock("after", span);
const cont = if (step != null) try l.newBlock("step", span) else header;
try l.joinParams(header, vars);
try l.joinParams(exit, exit_vars);
if (step != null) try l.joinParams(cont, vars);
@@ -1247,7 +1249,7 @@ const Lowerer = struct {
if (cond) |c| {
const cv = try l.expr(c, .bool);
try l.checkTy(c.span, cv.ty, .bool);
const body_b = try l.newBlock("body");
const body_b = try l.newBlock("body", body.span);
try l.term(try l.fmt("cbr %{s}, @{s}(), @{s}{s}", .{ cv.name, l.blocks.items[body_b].name, l.blocks.items[exit].name, try l.argsOf(exit_vars, null) }), c.span);
l.cur = body_b;
}
@@ -1630,8 +1632,8 @@ const Lowerer = struct {
const lv = try l.expr(a, .bool);
try l.checkTy(a.span, lv.ty, .bool);
const short = try l.def("c", .bool, if (op == .@"and") "const.bool false" else "const.bool true", e.span);
const rhs_b = try l.newBlock(if (op == .@"and") "and_rhs" else "or_rhs");
const join = try l.newBlock(if (op == .@"and") "and_end" else "or_end");
const rhs_b = try l.newBlock(if (op == .@"and") "and_rhs" else "or_rhs", b.span);
const join = try l.newBlock(if (op == .@"and") "and_end" else "or_end", e.span);
const rname = try l.freshName(if (op == .@"and") "and" else "or");
try l.blocks.items[join].params.append(l.arena, .{ .name = rname, .ty = .bool });
const jn = l.blocks.items[join].name;
@@ -1893,7 +1895,7 @@ const Lowerer = struct {
/// Joins branches that reach the end: one block with a parameter for
/// the result (if any) and one per carried variable (spec 15 §4).
fn mergeArms(l: *Lowerer, _: Span, what: []const u8, arms: []const ArmEnd, vars: []const usize, before: []const []const u8, join_kind: []const u8) Error!Value {
fn mergeArms(l: *Lowerer, span: Span, what: []const u8, arms: []const ArmEnd, vars: []const usize, before: []const []const u8, join_kind: []const u8) Error!Value {
l.restore(before);
var result_ty: ?Ty = null;
for (arms) |a| if (a.block != null) {
@@ -1907,7 +1909,7 @@ const Lowerer = struct {
l.cur = null;
return .{ .name = "", .ty = .never };
}
const join = try l.newBlock(join_kind);
const join = try l.newBlock(join_kind, span);
const has_value = result_ty.? != .void;
var result_name: []const u8 = "";
if (has_value) {
@@ -1958,7 +1960,7 @@ const Lowerer = struct {
} else return l.fail("E7002", arm.pattern.span, "no such variant", &.{ .{ .name = "enum", .value = d.display_name orelse d.name }, .{ .name = "variant", .value = c.name } });
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.invalid(.invalid_match, arm.pattern.span, "variant matched twice", &.{.{ .name = "variant", .value = c.name }});
if (c.binds.len != vs[vi].payload.len) return l.fail("E7005", arm.pattern.span, "the pattern binds a different number of payload values", &.{ .{ .name = "variant", .value = c.name }, .{ .name = "expected", .value = try l.fmt("{d}", .{vs[vi].payload.len}) } });
blk.* = try l.newBlock(try l.fmt("case_{s}", .{c.name}));
blk.* = try l.newBlock(try l.fmt("case_{s}", .{c.name}), arm.body.span);
for (vs[vi].payload, c.binds) |pt, bn| {
const pname = try l.freshName(if (std.mem.eql(u8, bn, "_")) "unused" else bn);
try l.blocks.items[blk.*].params.append(l.arena, .{ .name = pname, .ty = try l.typeOf(pt) });
@@ -2000,7 +2002,7 @@ const Lowerer = struct {
.wildcard => {
if (default != null) return l.invalid(.invalid_match, arm.pattern.span, "`_` given twice", &.{});
default = i;
blk.* = try l.newBlock("case_default");
blk.* = try l.newBlock("case_default", arm.body.span);
},
.int => |text| {
const neg = text[0] == '-';
@@ -2013,7 +2015,7 @@ const Lowerer = struct {
const bits: u128 = if (neg) (0 -% mag) else mag;
for (seen.items) |prev| if (prev == bits) return l.invalid(.invalid_match, arm.pattern.span, "integer matched twice", &.{.{ .name = "value", .value = text }});
try seen.append(l.arena, bits);
blk.* = try l.newBlock("case");
blk.* = try l.newBlock("case", arm.body.span);
// AIR uses canonical decimal literals without source
// suffixes/separators. Validate range before formatting
// rather than truncating a pattern's bit representation.
@@ -2052,7 +2054,7 @@ const Lowerer = struct {
if (b.variantIndex(c.name) == null) return l.fail("E7002", arm.pattern.span, "no such variant", &.{ .{ .name = "enum", .value = name }, .{ .name = "variant", .value = c.name } });
if (c.binds.len != 0) return l.fail("E7005", arm.pattern.span, "built-in error variants have no payload", &.{});
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.invalid(.invalid_match, arm.pattern.span, "variant matched twice", &.{.{ .name = "variant", .value = c.name }});
blk.* = try l.newBlock(try l.fmt("case_{s}", .{c.name}));
blk.* = try l.newBlock(try l.fmt("case_{s}", .{c.name}), arm.body.span);
try head.appendSlice(l.arena, try l.fmt(", {s} -> @{s}", .{ c.name, l.blocks.items[blk.*].name }));
}
var missing: std.ArrayList(u8) = .empty;
@@ -2418,7 +2420,7 @@ const Lowerer = struct {
for (0..@intCast(v.ty.array.len)) |_| try components.append(l.arena, v.ty.array.elem);
} else return l.fail("E7005", e.span, "unpack needs the corresponding struct or array", &.{});
if (names.len != components.items.len or names.len > 64) return l.fail("E7005", s.span, "unpack must bind every component", &.{});
const next = try l.newBlock("unpacked");
const next = try l.newBlock("unpacked", s.span);
for (names, components.items, 0..) |n, t, i| {
for (names[0..i]) |previous| if (!std.mem.eql(u8, n, "_") and std.mem.eql(u8, n, previous)) return l.invalid(.invalid_unpack, s.span, "duplicate unpack binding", &.{});
const pn = try l.freshName(if (std.mem.eql(u8, n, "_")) "unused" else n);
@@ -2832,10 +2834,10 @@ const Lowerer = struct {
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, s.span, .{ .lock = .{ body, cancelled } });
const before = try l.snapshot();
const locked = try l.newBlock("locked");
const locked = try l.newBlock("locked", s.span);
const guard = try l.freshName("guard");
try l.blocks.items[locked].params.append(l.arena, .{ .name = guard, .ty = try l.wrap(.guard, elem) });
const canc = try l.newBlock("lock_cancelled");
const canc = try l.newBlock("lock_cancelled", cancelled.span);
try l.term(try l.fmt("mutex.lock %{s}, ok -> @{s}, cancelled -> @{s}", .{ mr.name, l.blocks.items[locked].name, l.blocks.items[canc].name }), s.span);
var ends: std.ArrayList(ArmEnd) = .empty;
@@ -2945,7 +2947,7 @@ const Lowerer = struct {
var text: std.ArrayList(u8) = .empty;
try text.appendSlice(l.arena, "select ");
for (edges.items) |*ed| {
ed.block = try l.newBlock(ed.role);
ed.block = try l.newBlock(ed.role, s.span);
for (ed.params) |pt| try l.blocks.items[ed.block].params.append(l.arena, .{ .name = try l.freshName(ed.role), .ty = pt });
for (ed.values) |vi| try l.blocks.items[ed.block].params.append(l.arena, .{ .name = try l.freshName(ed.role), .ty = values.items[vi].v.ty });
}
@@ -3343,7 +3345,7 @@ const Lowerer = struct {
var text: std.ArrayList(u8) = .empty;
try text.appendSlice(l.arena, f.text);
for (f.outcomes, blocks) |o, *b| {
b.* = try l.newBlock(o.role);
b.* = try l.newBlock(o.role, span);
for (o.params) |pt| {
const name = try l.freshName(o.role);
try l.blocks.items[b.*].params.append(l.arena, .{ .name = name, .ty = pt });
@@ -3458,8 +3460,8 @@ const Lowerer = struct {
try l.checkTy(x.cond.span, cv.ty, .bool);
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, span, .{ .@"if" = x });
const then_b = try l.newBlock("then");
const else_b = try l.newBlock("else");
const then_b = try l.newBlock("then", x.then.span);
const else_b = try l.newBlock("else", span);
try l.term(try l.fmt("cbr %{s}, @{s}(), @{s}()", .{ cv.name, l.blocks.items[then_b].name, l.blocks.items[else_b].name }), x.cond.span);
const before = try l.snapshot();
var ends: [2]ArmEnd = undefined;