cli: query expression types and callee paths iteratively

This commit is contained in:
dev committed 2026-10-10 17:21:30 +03:00
1 parent dd73b84085
commit e862e03f2b
5 files changed
+531 -102

No files matched your search

+445 -95
View File
@@ -1586,53 +1586,172 @@ const Lowerer = struct {
return null;
}
fn peekType(l: *Lowerer, initial: ast.Expr) ?Ty {
// Negation preserves the operand's queried type; skip its unary
// spine without host-stack growth or extra query allocations.
var e = initial;
while (e.kind == .neg) e = e.kind.neg.*;
return switch (e.kind) {
.specialize => null,
.int => |x| if (parse_mod.intSuffix(x.text)) |s| Ty{ .int = s } else null,
.boolean => .bool,
.name => |n| if (l.lookup(n)) |b| b.ty else null,
.deref => |x| if (l.peekType(x.*)) |t| t.inner() else null,
.index => |x| if (x.hi == null) (if (l.peekType(x.e.*)) |t| elemOf(t) else null) else null,
.array_rep => |x| if (l.peekType(x.e.*)) |t| (l.arrayOf(t, x.len) catch null) else null,
.array_list => |xs| if (xs.len != 0) (if (l.peekType(xs[0])) |t| (l.arrayOf(t, xs.len) catch null) else null) else null,
.neg => unreachable,
.not => .bool,
.cast => |x| l.typeOfNoFail(x.ty),
.binary => |x| switch (x.op) {
.eq, .ne, .lt, .le, .gt, .ge, .@"and", .@"or" => .bool,
.shl, .shr => l.peekType(x.l.*),
else => l.peekType(x.l.*) orelse l.peekType(x.r.*),
},
.call => |x| switch (x.callee.kind) {
.name => |n| if (l.functionSig(n, e.span)) |sig| sig.ret else null,
.variant => |v| l.enumType(v.@"enum", e.span),
else => if (l.calleeSig(x.callee.*)) |sig| sig.ret else if (pathOf(x.callee.*)) |p| l.peekPath(p, x.args) else null,
},
.move => |n| if (l.lookup(n)) |b| b.ty else null,
.variant => |v| l.enumType(v.@"enum", e.span),
.struct_lit => |x| l.enumOrStruct(x.name, e.span),
.field => |x| blk: {
const known = l.peekType(x.e.*);
if (std.mem.eql(u8, x.name, "len") and (known == null or !isNominalFieldBase(known.?))) break :blk Ty{ .int = "usize" };
var base = known orelse break :blk null;
if (base.isBorrow()) base = base.inner().?;
const d = l.decl(base) orelse break :blk null;
if (d.kind != .@"struct") break :blk null;
for (d.kind.@"struct") |f| if (std.mem.eql(u8, f.name, x.name)) break :blk l.typeOfNoFail(f.ty);
break :blk null;
},
.if_expr => |x| if (x.then.tail) |t| l.peekType(t.*) else null,
.match_expr => |x| for (x.arms) |a| {
if (l.peekType(a.body)) |t| break t;
} else null,
.block => |b| if (b.tail) |t| l.peekType(t.*) else null,
else => null,
};
// Best-effort query: output construction already treats host OOM as
// unknown. Continuation OOM follows that contract without a diagnostic.
fn peekType(l: *Lowerer, e: ast.Expr) ?Ty {
return l.queryType(.{ .expr = e }) catch null;
}
const TypeRequest = union(enum) { expr: ast.Expr, path: struct { name: []const u8, args: []const ast.Expr } };
const QueryOperation = enum { atomic_new, atomic_load_inline, atomic_load_borrow, graph_root, freeze, arc_from, arc_clone, channel_dup };
const TypeContinuation = union(enum) {
inner,
element,
array: u64,
field: []const u8,
alternative: ast.Expr,
arms: []const ast.Arm,
operation: QueryOperation,
};
fn queryType(l: *Lowerer, initial: TypeRequest) Allocator.Error!?Ty {
var pending: std.ArrayList(TypeContinuation) = .empty;
defer pending.deinit(l.arena);
var request = initial;
query: while (true) {
var result: ?Ty = switch (request) {
.expr => |e| switch (e.kind) {
.int => |x| if (parse_mod.intSuffix(x.text)) |s| Ty{ .int = s } else null,
.boolean, .not => .bool,
.name, .move => |n| if (l.lookup(n)) |b| b.ty else null,
.neg => |x| {
request = .{ .expr = x.* };
continue :query;
},
.deref => |x| {
try pending.append(l.arena, .inner);
request = .{ .expr = x.* };
continue :query;
},
.index => |x| blk: {
if (x.hi != null) break :blk null;
try pending.append(l.arena, .element);
request = .{ .expr = x.e.* };
continue :query;
},
.array_rep => |x| {
try pending.append(l.arena, .{ .array = x.len });
request = .{ .expr = x.e.* };
continue :query;
},
.array_list => |xs| blk: {
if (xs.len == 0) break :blk null;
try pending.append(l.arena, .{ .array = xs.len });
request = .{ .expr = xs[0] };
continue :query;
},
.cast => |x| l.typeOfNoFail(x.ty),
.binary => |x| switch (x.op) {
.eq, .ne, .lt, .le, .gt, .ge, .@"and", .@"or" => .bool,
else => {
if (x.op != .shl and x.op != .shr) try pending.append(l.arena, .{ .alternative = x.r.* });
request = .{ .expr = x.l.* };
continue :query;
},
},
.call => |x| switch (x.callee.kind) {
.name => |n| if (l.functionSig(n, e.span)) |sig| sig.ret else null,
.variant => |v| l.enumType(v.@"enum", e.span),
else => blk: {
if (l.calleeSig(x.callee.*)) |sig| break :blk sig.ret;
const name = pathOf(x.callee.*) orelse break :blk null;
request = .{ .path = .{ .name = name, .args = x.args } };
continue :query;
},
},
.variant => |v| l.enumType(v.@"enum", e.span),
.struct_lit => |x| l.enumOrStruct(x.name, e.span),
.field => |x| {
try pending.append(l.arena, .{ .field = x.name });
request = .{ .expr = x.e.* };
continue :query;
},
.if_expr => |x| blk: {
const tail = x.then.tail orelse break :blk null;
request = .{ .expr = tail.* };
continue :query;
},
.match_expr => |x| blk: {
if (x.arms.len == 0) break :blk null;
try pending.append(l.arena, .{ .arms = x.arms[1..] });
request = .{ .expr = x.arms[0].body };
continue :query;
},
.block => |b| blk: {
const tail = b.tail orelse break :blk null;
request = .{ .expr = tail.* };
continue :query;
},
else => null,
},
.path => |p| blk: {
const eq = std.mem.eql;
if (eq(u8, p.name, "buffer.len") or eq(u8, p.name, "buffer.capacity")) break :blk Ty{ .int = "usize" };
if (eq(u8, p.name, "task.cancelled")) break :blk Ty.bool;
if (eq(u8, p.name, "task.spawn") and p.args.len >= 2) {
const sig = l.calleeSig(p.args[1]) orelse break :blk null;
break :blk l.wrap(.task, sig.ret) catch null;
}
if ((eq(u8, p.name, "own.take") or eq(u8, p.name, "task.join")) and p.args.len == 1) {
try pending.append(l.arena, .inner);
} else if (std.mem.startsWith(u8, p.name, "atomic.") and p.args.len >= 1) {
if (eq(u8, p.name, "atomic.new")) {
try pending.append(l.arena, .{ .operation = .atomic_new });
} else if (eq(u8, p.name, "atomic.take")) {
try pending.append(l.arena, .inner);
} else if (eq(u8, p.name, "atomic.store") or eq(u8, p.name, "atomic.fence") or eq(u8, p.name, "atomic.compare_exchange")) {
break :blk null;
} else {
const inline_borrow = p.args[0].kind == .borrow;
try pending.append(l.arena, .{ .operation = if (inline_borrow) .atomic_load_inline else .atomic_load_borrow });
request = .{ .expr = if (inline_borrow) p.args[0].kind.borrow.place.* else p.args[0] };
continue :query;
}
} else if (p.args.len == 1) {
const op: QueryOperation = if (eq(u8, p.name, "island.root") or eq(u8, p.name, "frozen.root") or eq(u8, p.name, "arc.root")) .graph_root else if (eq(u8, p.name, "island.freeze")) .freeze else if (eq(u8, p.name, "arc.from_frozen")) .arc_from else if (eq(u8, p.name, "arc.clone")) .arc_clone else if (eq(u8, p.name, "channel.dup")) .channel_dup else break :blk null;
try pending.append(l.arena, .{ .operation = op });
} else break :blk null;
request = .{ .expr = p.args[0] };
continue :query;
},
};
while (pending.pop()) |frame| switch (frame) {
.inner => result = if (result) |t| t.inner() else null,
.element => result = if (result) |t| elemOf(t) else null,
.array => |len| result = if (result) |t| l.arrayOf(t, len) catch null else null,
.alternative => |e| if (result == null) {
request = .{ .expr = e };
continue :query;
},
.arms => |arms| if (result == null and arms.len != 0) {
try pending.append(l.arena, .{ .arms = arms[1..] });
request = .{ .expr = arms[0].body };
continue :query;
},
.field => |name| result = l.queryField(result, name),
.operation => |op| result = if (result) |t| switch (op) {
.atomic_new => l.wrap(.atomic, t) catch null,
.atomic_load_inline => if (t == .atomic) t.inner() else null,
.atomic_load_borrow => if (t.inner()) |c| (if (c == .atomic) c.inner() else null) else null,
.graph_root => if (t.inner()) |o| (if (o.inner()) |r| l.wrap(.iref, r) catch null else null) else null,
.freeze => if (t == .island) l.wrap(.frozen, t.inner().?) catch null else null,
.arc_from => if (t == .frozen) l.wrap(.arc, t.inner().?) catch null else null,
.arc_clone => if (t == .arc) t else if (t.inner()) |in| (if (in == .arc) in else null) else null,
.channel_dup => if (t == .sender) t else if (t.inner()) |in| (if (in == .sender) in else null) else null,
} else null,
};
return result;
}
}
fn queryField(l: *Lowerer, known: ?Ty, name: []const u8) ?Ty {
if (std.mem.eql(u8, name, "len") and (known == null or !isNominalFieldBase(known.?))) return .{ .int = "usize" };
var base = known orelse return null;
if (base.isBorrow()) base = base.inner().?;
const d = l.decl(base) orelse return null;
if (d.kind != .@"struct") return null;
for (d.kind.@"struct") |f| if (std.mem.eql(u8, f.name, name)) return l.typeOfNoFail(f.ty);
return null;
}
/// The type the following code expects for the name `n`, if any use
@@ -2356,34 +2475,6 @@ const Lowerer = struct {
return l.mergeArms(m.span, "match", ends.items, vars, before, "endmatch");
}
/// The result type of a built-in operation called plainly, if known
/// without lowering it.
fn peekPath(l: *Lowerer, p: []const u8, args: []const ast.Expr) ?Ty {
const eq = std.mem.eql;
if (eq(u8, p, "buffer.len") or eq(u8, p, "buffer.capacity")) return .{ .int = "usize" };
if ((eq(u8, p, "own.take") or eq(u8, p, "task.join")) and args.len == 1) return if (l.peekType(args[0])) |t| t.inner() else null;
if (eq(u8, p, "task.cancelled")) return .bool;
if (std.mem.startsWith(u8, p, "atomic.") and args.len >= 1) {
if (eq(u8, p, "atomic.new")) return if (l.peekType(args[0])) |t| (l.wrap(.atomic, t) catch null) else null;
if (eq(u8, p, "atomic.take")) return if (l.peekType(args[0])) |t| t.inner() else null;
if (eq(u8, p, "atomic.store") or eq(u8, p, "atomic.fence") or eq(u8, p, "atomic.compare_exchange")) return null;
// A `ref a` argument, or a `ref<atomic<T>>` binding: the cell's T.
const cell: ?Ty = if (args[0].kind == .borrow) l.peekType(args[0].kind.borrow.place.*) else if (l.peekType(args[0])) |t| t.inner() else null;
return if (cell) |c| (if (c == .atomic) c.inner() else null) else null;
}
if ((eq(u8, p, "island.root") or eq(u8, p, "frozen.root") or eq(u8, p, "arc.root")) and args.len == 1)
return if (l.peekType(args[0])) |t| (if (t.inner()) |o| (if (o.inner()) |root| l.wrap(.iref, root) catch null else null) else null) else null;
if (eq(u8, p, "island.freeze") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .island) l.wrap(.frozen, t.inner().?) catch null else null) else null;
if (eq(u8, p, "arc.from_frozen") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .frozen) l.wrap(.arc, t.inner().?) catch null else null) else null;
if (eq(u8, p, "arc.clone") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .arc) t else if (t.inner()) |in| (if (in == .arc) in else null) else null) else null;
if (eq(u8, p, "channel.dup") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .sender) t else if (t.inner()) |in| (if (in == .sender) in else null) else null) else null;
if (eq(u8, p, "task.spawn") and args.len >= 2) {
const sig = l.calleeSig(args[1]) orelse return null;
return l.wrap(.task, sig.ret) catch null;
}
return null;
}
fn armBody(l: *Lowerer, body: ast.Expr, want: ?Ty) Error!?Value {
const v = try l.expr(body, want);
if (v.ty == .void) return null;
@@ -3907,22 +3998,28 @@ fn pathOf(e: ast.Expr) ?[]const u8 {
}
fn dotted(e: ast.Expr, buf: []u8) ?[]const u8 {
switch (e.kind) {
.name => |n| {
if (n.len > buf.len) return null;
@memcpy(buf[0..n.len], n);
return buf[0..n.len];
},
.field => |f| {
const base = dotted(f.e.*, buf) orelse return null;
const len = base.len + 1 + f.name.len;
if (len > buf.len) return null;
buf[base.len] = '.';
@memcpy(buf[base.len + 1 .. len], f.name);
return buf[0..len];
},
else => return null,
var root = e;
var len: usize = 0;
while (root.kind == .field) {
const field = root.kind.field;
if (len >= buf.len or field.name.len > buf.len - len - 1) return null;
len += field.name.len + 1;
root = field.e.*;
}
if (root.kind != .name or root.kind.name.len > buf.len - len) return null;
len += root.kind.name.len;
var at = len;
var node = e;
while (node.kind == .field) {
const field = node.kind.field;
at -= field.name.len;
@memcpy(buf[at .. at + field.name.len], field.name);
at -= 1;
buf[at] = '.';
node = field.e.*;
}
@memcpy(buf[0..at], root.kind.name);
return buf[0..len];
}
fn standardPath(e: ast.Expr) bool {
@@ -4279,11 +4376,20 @@ fn localName(full: []const u8, module: []const u8, short: []const u8) bool {
}
fn expressionPath(arena: Allocator, e: ast.Expr) Allocator.Error!?[]const u8 {
return switch (e.kind) {
.name => |name| name,
.field => |field| if (try expressionPath(arena, field.e.*)) |base| try std.fmt.allocPrint(arena, "{s}.{s}", .{ base, field.name }) else null,
else => null,
};
var root = e;
while (root.kind == .field) root = root.kind.field.e.*;
if (root.kind != .name) return null;
if (e.kind == .name) return root.kind.name;
var len = root.kind.name.len;
var node = e;
while (node.kind == .field) {
len = std.math.add(usize, len, 1) catch return error.OutOfMemory;
len = std.math.add(usize, len, node.kind.field.name.len) catch return error.OutOfMemory;
node = node.kind.field.e.*;
}
const output = try arena.alloc(u8, len);
// The immutable chain was validated above; dotted fills this exact buffer.
return dotted(e, output).?;
}
test "terminated blocks reject further emission with a source diagnostic" {
@@ -4301,6 +4407,250 @@ test "terminated blocks reject further emission with a source diagnostic" {
try std.testing.expect(l.blocks.items[0].terminated);
}
// Independent pre-repair expression query and built-in result derivation.
// Name/signature/type lookup helpers are shared; the descent is not.
const ReferenceExpressionQuery = struct {
fn query(l: *Lowerer, initial: ast.Expr) ?Ty {
// Negation preserves the operand's queried type; skip its unary
// spine without host-stack growth or extra query allocations.
var e = initial;
while (e.kind == .neg) e = e.kind.neg.*;
return switch (e.kind) {
.specialize => null,
.int => |x| if (parse_mod.intSuffix(x.text)) |s| Ty{ .int = s } else null,
.boolean => .bool,
.name => |n| if (l.lookup(n)) |b| b.ty else null,
.deref => |x| if (query(l, x.*)) |t| t.inner() else null,
.index => |x| if (x.hi == null) (if (query(l, x.e.*)) |t| elemOf(t) else null) else null,
.array_rep => |x| if (query(l, x.e.*)) |t| (l.arrayOf(t, x.len) catch null) else null,
.array_list => |xs| if (xs.len != 0) (if (query(l, xs[0])) |t| (l.arrayOf(t, xs.len) catch null) else null) else null,
.neg => unreachable,
.not => .bool,
.cast => |x| l.typeOfNoFail(x.ty),
.binary => |x| switch (x.op) {
.eq, .ne, .lt, .le, .gt, .ge, .@"and", .@"or" => .bool,
.shl, .shr => query(l, x.l.*),
else => query(l, x.l.*) orelse query(l, x.r.*),
},
.call => |x| switch (x.callee.kind) {
.name => |n| if (l.functionSig(n, e.span)) |sig| sig.ret else null,
.variant => |v| l.enumType(v.@"enum", e.span),
else => if (l.calleeSig(x.callee.*)) |sig| sig.ret else if (pathOf(x.callee.*)) |p| path(l, p, x.args) else null,
},
.move => |n| if (l.lookup(n)) |b| b.ty else null,
.variant => |v| l.enumType(v.@"enum", e.span),
.struct_lit => |x| l.enumOrStruct(x.name, e.span),
.field => |x| blk: {
const known = query(l, x.e.*);
if (std.mem.eql(u8, x.name, "len") and (known == null or !Lowerer.isNominalFieldBase(known.?))) break :blk Ty{ .int = "usize" };
var base = known orelse break :blk null;
if (base.isBorrow()) base = base.inner().?;
const d = l.decl(base) orelse break :blk null;
if (d.kind != .@"struct") break :blk null;
for (d.kind.@"struct") |f| if (std.mem.eql(u8, f.name, x.name)) break :blk l.typeOfNoFail(f.ty);
break :blk null;
},
.if_expr => |x| if (x.then.tail) |t| query(l, t.*) else null,
.match_expr => |x| for (x.arms) |a| {
if (query(l, a.body)) |t| break t;
} else null,
.block => |b| if (b.tail) |t| query(l, t.*) else null,
else => null,
};
}
fn path(l: *Lowerer, p: []const u8, args: []const ast.Expr) ?Ty {
const eq = std.mem.eql;
if (eq(u8, p, "buffer.len") or eq(u8, p, "buffer.capacity")) return .{ .int = "usize" };
if ((eq(u8, p, "own.take") or eq(u8, p, "task.join")) and args.len == 1) return if (query(l, args[0])) |t| t.inner() else null;
if (eq(u8, p, "task.cancelled")) return .bool;
if (std.mem.startsWith(u8, p, "atomic.") and args.len >= 1) {
if (eq(u8, p, "atomic.new")) return if (query(l, args[0])) |t| (l.wrap(.atomic, t) catch null) else null;
if (eq(u8, p, "atomic.take")) return if (query(l, args[0])) |t| t.inner() else null;
if (eq(u8, p, "atomic.store") or eq(u8, p, "atomic.fence") or eq(u8, p, "atomic.compare_exchange")) return null;
// A `ref a` argument, or a `ref<atomic<T>>` binding: the cell's T.
const cell: ?Ty = if (args[0].kind == .borrow) query(l, args[0].kind.borrow.place.*) else if (query(l, args[0])) |t| t.inner() else null;
return if (cell) |c| (if (c == .atomic) c.inner() else null) else null;
}
if ((eq(u8, p, "island.root") or eq(u8, p, "frozen.root") or eq(u8, p, "arc.root")) and args.len == 1)
return if (query(l, args[0])) |t| (if (t.inner()) |o| (if (o.inner()) |root| l.wrap(.iref, root) catch null else null) else null) else null;
if (eq(u8, p, "island.freeze") and args.len == 1) return if (query(l, args[0])) |t| (if (t == .island) l.wrap(.frozen, t.inner().?) catch null else null) else null;
if (eq(u8, p, "arc.from_frozen") and args.len == 1) return if (query(l, args[0])) |t| (if (t == .frozen) l.wrap(.arc, t.inner().?) catch null else null) else null;
if (eq(u8, p, "arc.clone") and args.len == 1) return if (query(l, args[0])) |t| (if (t == .arc) t else if (t.inner()) |in| (if (in == .arc) in else null) else null) else null;
if (eq(u8, p, "channel.dup") and args.len == 1) return if (query(l, args[0])) |t| (if (t == .sender) t else if (t.inner()) |in| (if (in == .sender) in else null) else null) else null;
if (eq(u8, p, "task.spawn") and args.len >= 2) {
const sig = l.calleeSig(args[1]) orelse return null;
return l.wrap(.task, sig.ret) catch null;
}
return null;
}
};
test "expression type queries match the independent shallow derivation" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const parsed = try parse_mod.parse(a, "module query\nstruct Record { len: u16, arr: [u8; 7] }\nenum Choice { Empty }\n");
try std.testing.expect(parsed == .ok);
var l: Lowerer = .{ .arena = a, .m = parsed.ok };
try l.sigs.put(a, "f", .{ .params = &.{}, .ret = .{ .int = "i64" }, .air_name = "f", .display_name = "f" });
try l.sigs.put(a, "worker", .{ .params = &.{}, .ret = .bool, .air_name = "worker", .display_name = "worker" });
try l.sigs.put(a, "ns.f", .{ .params = &.{}, .ret = .{ .int = "u32" }, .air_name = "ns.f", .display_name = "ns.f" });
const bindings = [_]Binding{
.{ .name = "n", .value = "n", .mutable = false, .ty = .{ .int = "u64" } },
.{ .name = "flag", .value = "flag", .mutable = false, .ty = .bool },
.{ .name = "record", .value = "r", .mutable = false, .ty = .{ .named = 0 } },
.{ .name = "record_ref", .value = "rr", .mutable = false, .ty = try l.wrap(.ref, .{ .named = 0 }) },
.{ .name = "arr", .value = "a", .mutable = false, .ty = try l.arrayOf(.{ .int = "u8" }, 7) },
.{ .name = "slice", .value = "s", .mutable = false, .ty = try l.wrap(.slice, .{ .int = "u8" }) },
.{ .name = "own", .value = "o", .mutable = false, .ty = try l.wrap(.own, .{ .int = "u64" }) },
.{ .name = "cell", .value = "c", .mutable = false, .ty = try l.wrap(.atomic, .{ .int = "u8" }) },
.{ .name = "cell_ref", .value = "cr", .mutable = false, .ty = try l.wrap(.ref, try l.wrap(.atomic, .{ .int = "u8" })) },
.{ .name = "graph", .value = "g", .mutable = false, .ty = try l.wrap(.island, .{ .named = 0 }) },
.{ .name = "graph_ref", .value = "gr", .mutable = false, .ty = try l.wrap(.mut, try l.wrap(.island, .{ .named = 0 })) },
.{ .name = "frozen", .value = "f", .mutable = false, .ty = try l.wrap(.frozen, .{ .named = 0 }) },
.{ .name = "shared", .value = "sh", .mutable = false, .ty = try l.wrap(.arc, .{ .named = 0 }) },
.{ .name = "shared_ref", .value = "shr", .mutable = false, .ty = try l.wrap(.ref, try l.wrap(.arc, .{ .named = 0 })) },
.{ .name = "tx", .value = "tx", .mutable = false, .ty = try l.wrap(.sender, .{ .int = "u8" }) },
.{ .name = "tx_ref", .value = "txr", .mutable = false, .ty = try l.wrap(.ref, try l.wrap(.sender, .{ .int = "u8" })) },
};
try l.bindings.appendSlice(a, &bindings);
const leaves = [_][]const u8{
"unknown", "n", "flag", "1", "1i64", "true", "move own", "ref n",
"record", "record_ref", "record.len", "record_ref.arr", "arr", "slice", "f()", "ns.f()",
"worker::<u8>", "Choice::Empty", "Record { len: 1u16, arr: [0u8; 7] }", "buffer.len(unknown)", "buffer.capacity()", "task.cancelled()", "task.spawn(unknown, worker)", "atomic.load(cell_ref)",
"atomic.load(ref cell)", "atomic.new(1u64)", "atomic.fence(1u64)", "atomic.take(cell)", "atomic.store(ref cell, 1u8)", "atomic.compare_exchange(cell_ref)", "island.root(graph_ref)", "island.freeze(graph)",
"arc.from_frozen(frozen)", "arc.clone(shared_ref)", "channel.dup(tx_ref)", "own.take(own)", "task.join(unknown)",
};
const forms = [_][]const u8{
"{s}", "- {s}", "not {s}", "* {s}", "({s})[0]", "[{s}; 2]", "[{s}, unknown]",
"({s}) as! u8", "({s}).len", "({s}).arr", "({s}) + unknown", "unknown + ({s})", "({s}) << unknown", "if true {{ {s} }} else {{ 1u64 }}",
"match 0u8 {{ 0 => unknown, _ => {s} }}", "{{ {s} }}", "atomic.new({s})", "own.take({s})", "arc.clone({s})",
};
inline for (forms) |form| for (leaves) |leaf| {
const expression = try std.fmt.allocPrint(a, form, .{leaf});
const source = try std.fmt.allocPrint(a, "module query\nfn probe() -> void effects [] {{ return {s}; }}", .{expression});
const r = try parse_mod.parse(a, source);
try std.testing.expect(r == .ok);
const e = r.ok.functions[0].body.stmts[0].kind.@"return".?;
const expected = ReferenceExpressionQuery.query(&l, e);
const actual = l.peekType(e);
if (expected) |ty| try std.testing.expect(actual != null and ty.eql(actual.?)) else try std.testing.expect(actual == null);
try std.testing.expect(l.problem == null);
try std.testing.expectEqual(@as(usize, 0), l.blocks.items.len);
};
}
test "expression queries suspend deep structural, branch and builtin requests" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const span = unaryTestSpan(1);
const m: ast.Module = .{ .name = "query", .types = &.{}, .functions = &.{} };
const root: ast.Expr = .{ .span = span, .kind = .{ .int = .{ .text = "1u8" } } };
const unknown: ast.Expr = .{ .span = span, .kind = .{ .name = "unknown" } };
const index: ast.Expr = .{ .span = span, .kind = .{ .int = .{ .text = "0usize" } } };
const atomic: ast.Expr = .{ .span = span, .kind = .{ .name = "atomic" } };
const new: ast.Expr = .{ .span = span, .kind = .{ .field = .{ .e = &atomic, .name = "new" } } };
const take: ast.Expr = .{ .span = span, .kind = .{ .field = .{ .e = &atomic, .name = "take" } } };
for ([_]enum { structure, branches, builtin }{ .structure, .branches, .builtin }) |mode| {
var l: Lowerer = .{ .arena = a, .m = m };
var e = root;
for (0..16_384) |_| {
const inner = try a.create(ast.Expr);
inner.* = e;
switch (mode) {
.structure => {
const array = try a.create(ast.Expr);
array.* = .{ .span = span, .kind = .{ .array_rep = .{ .e = inner, .len = 2 } } };
e = .{ .span = span, .kind = .{ .index = .{ .e = array, .lo = &index, .hi = null } } };
},
.branches => e = .{ .span = span, .kind = .{ .binary = .{ .op = .add, .l = &unknown, .r = inner } } },
.builtin => {
const args = try a.alloc(ast.Expr, 1);
args[0] = inner.*;
const allocated = try a.alloc(ast.Expr, 1);
allocated[0] = .{ .span = span, .kind = .{ .call = .{ .callee = &new, .type_arg = null, .args = args } } };
e = .{ .span = span, .kind = .{ .call = .{ .callee = &take, .type_arg = null, .args = allocated } } };
},
}
}
const result = l.peekType(e);
try std.testing.expect(result != null and result.?.eql(.{ .int = "u8" }));
try std.testing.expect(l.problem == null);
try std.testing.expectEqual(@as(usize, 0), l.blocks.items.len);
}
}
test "best-effort expression query OOM releases all backing blocks without diagnostics" {
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer fixture.deinit();
const a = fixture.allocator();
const span = unaryTestSpan(1);
const m: ast.Module = .{ .name = "query", .types = &.{}, .functions = &.{} };
var e: ast.Expr = .{ .span = span, .kind = .{ .boolean = true } };
for (0..128) |_| {
const inner = try a.create(ast.Expr);
inner.* = e;
e = .{ .span = span, .kind = .{ .array_rep = .{ .e = inner, .len = 1 } } };
}
var fail_index: usize = 0;
while (true) : (fail_index += 1) {
try std.testing.expect(fail_index < 256);
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
var arena: std.heap.ArenaAllocator = .init(failing.allocator());
var l: Lowerer = .{ .arena = arena.allocator(), .m = m };
const result = l.peekType(e);
if (failing.has_induced_failure) try std.testing.expect(result == null) else {
var t = result.?;
for (0..128) |_| {
try std.testing.expect(t == .array and t.array.len == 1);
t = t.array.elem;
}
try std.testing.expect(t == .bool);
}
try std.testing.expect(l.problem == null);
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
arena.deinit();
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
if (!failing.has_induced_failure) break;
}
var none: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
var l: Lowerer = .{ .arena = none.allocator(), .m = m };
const leaf: ast.Expr = .{ .span = span, .kind = .{ .boolean = false } };
try std.testing.expect(l.peekType(leaf).? == .bool);
try std.testing.expect(!none.has_induced_failure);
}
test "deep callee paths own one complete string and bounded lookup stays flat" {
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer fixture.deinit();
const a = fixture.allocator();
const span = unaryTestSpan(1);
var e: ast.Expr = .{ .span = span, .kind = .{ .name = "root" } };
for (0..65_536) |_| {
const base = try a.create(ast.Expr);
base.* = e;
e = .{ .span = span, .kind = .{ .field = .{ .e = base, .name = "x" } } };
}
const text = (try expressionPath(a, e)).?;
try std.testing.expectEqual(@as(usize, 4 + 2 * 65_536), text.len);
try std.testing.expectEqualStrings("root", text[0..4]);
var i: usize = 4;
while (i != text.len) : (i += 2) try std.testing.expectEqualStrings(".x", text[i .. i + 2]);
var buf: [64]u8 = undefined;
try std.testing.expect(dotted(e, &buf) == null);
try std.testing.expect(pathOf(e) == null);
var fail: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
try std.testing.expectError(error.OutOfMemory, expressionPath(fail.allocator(), e));
try std.testing.expectEqual(@as(usize, 0), fail.allocated_bytes);
try std.testing.expectEqual(@as(usize, 0), fail.freed_bytes);
const invalid: ast.Expr = .{ .span = span, .kind = .{ .field = .{ .e = &.{ .span = span, .kind = .{ .boolean = true } }, .name = "x" } } };
fail = .init(std.testing.allocator, .{ .fail_index = 0 });
try std.testing.expect((try expressionPath(fail.allocator(), invalid)) == null);
try std.testing.expect(!fail.has_induced_failure);
}
// Independent pre-repair unary expression derivation. Non-unary leaves use
// the unchanged expression handlers; this oracle is only for shallow chains.
const ReferenceUnary = struct {