cli: add whole-value linear aggregates

Implement STD-5 through source, AIR, verifier and deterministic interpreter.
Constructors consume nested owners; exhaustive unpack/take, explicit swap and
own.pack preserve ownership on success and failure. Support owning Option and
Result while keeping classification helpers Copy-only. Preserve borrow identity
across whole replacement and collection; distinguish individual owned arrays
from repeated heap arrays.

Specify feature contracts, invariants, diagnostics, compatibility and measured
source/AIR budgets in RFC 0023 and normative docs. Add canonical round trips,
AIR/source acceptance and rejection fixtures, CLI integration and CI checks.

Validate build, unit/extended tests, full conformance, forced-collection schedule
stress (32 seeds per quantum), hosted smoke, CLI suites, source coverage (88/88),
formatting and reference bundle generation. Source bundle grows 3.2%; Quick
Reference grows 4.0%. No MCC improvement is claimed.

Dynamic initialized linear buffers and explicit drain remain Q-036/STD-6;
frame/region/repeated heap storage is Copy-only. RFC remains Proposed.
This commit is contained in:
dev committed 2026-10-06 21:23:52 +03:00
1 parent 7eea31518d
commit 4f084decce
109 files changed
+1814 -142

No files matched your search

+43 -9
View File
@@ -191,7 +191,7 @@ const Walker = struct {
/// Linear values: resources, owners and regions (spec 06 §6, spec 03 §19–§20).
fn isResource(w: *Walker, v: ValueId) bool {
return w.types[v].isLinear();
return w.c.module.isLinearType(w.types[v]);
}
/// A non-consuming use of resource `v` (a borrow of it).
@@ -252,10 +252,11 @@ fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void
for (x.cases) |c| try out.append(arena, .{ .target = c.target.block, .args = c.target.args });
try out.append(arena, .{ .target = x.default.block, .args = x.default.args });
},
.enum_switch => |x| {
inline .enum_switch, .enum_take => |x| {
for (x.arms) |a| try out.append(arena, .{ .target = a.block, .args = &.{} });
if (x.default) |d| try out.append(arena, .{ .target = d.block, .args = d.args });
},
.aggregate_unpack => |x| try out.append(arena, .{ .target = x.next, .args = &.{} }),
.cap_narrow => |x| {
try out.append(arena, .{ .target = x.ok, .args = &.{} });
try out.append(arena, .{ .target = x.err, .args = &.{} });
@@ -264,7 +265,7 @@ fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void
try out.append(arena, .{ .target = ok, .args = &.{} });
try out.append(arena, .{ .target = x.err.?, .args = &.{} });
},
.own_new, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => {
inline .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => {
const t = air.allocTargets(op).?;
try out.append(arena, .{ .target = t.ok, .args = &.{} });
try out.append(arena, .{ .target = t.err, .args = &.{} });
@@ -302,7 +303,7 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
.parent = null,
};
// Linear parameters were moved in: the function owns them (spec 06 §6.3).
if (p.ty.isLinear()) w.live[p.value] = true;
if (c.module.isLinearType(p.ty)) w.live[p.value] = true;
}
const dom = try ssa.dominators(c.arena, f);
@@ -318,7 +319,7 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
const blk = f.blocks[b];
// Linear block parameters own their argument from here on; the
// parameter of a region allocation's `ok` target is a live borrow.
for (blk.params) |p| if (p.ty.isLinear() or (p.ty.isBorrow() and w.info[p.value] != null)) {
for (blk.params) |p| if (c.module.isLinearType(p.ty) or (p.ty.isBorrow() and w.info[p.value] != null)) {
w.live[p.value] = true;
w.dead[p.value] = .none;
};
@@ -337,19 +338,19 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
for (out.items) |e| {
@memcpy(w.live, base);
@memcpy(w.dead, base_dead);
for (e.args) |a| if (types[a].isLinear()) try w.consume(a, .moved);
for (e.args) |a| if (c.module.isLinearType(types[a])) try w.consume(a, .moved);
// A linear parameter of the target that is still live on this
// edge would be overwritten: that instance leaks. Parameters are
// not part of the incoming state; they come alive in the block.
for (f.blocks[e.target].params) |p| {
if (p.ty.isLinear() and w.live[p.value]) try w.report("E3002", "linear value not consumed before it is redefined", p.value, null);
if (c.module.isLinearType(p.ty) and w.live[p.value]) try w.report("E3002", "linear value not consumed before it is redefined", p.value, null);
w.live[p.value] = false;
}
if (entry_state[e.target]) |expected| {
if (!std.mem.eql(bool, expected, w.live) and !reported_join[e.target]) {
reported_join[e.target] = true;
for (expected, w.live, 0..) |a, x, v| if (a != x) {
const is_res = types[v].isLinear();
const is_res = c.module.isLinearType(types[v]);
try c.report(if (is_res) "E3002" else "E2105", f.blocks[e.target].span, if (is_res) "resource state differs between edges into this block" else "borrow state differs between edges into this block", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "block", .value = .{ .str = f.blocks[e.target].name } },
@@ -433,7 +434,40 @@ fn step(w: *Walker, inst: air.Inst) !void {
w.live[r] = true;
};
},
.own_free, .own_take => |v| try w.consume(v, .closed),
inline .struct_make, .enum_make, .array_make => |x| {
for (x.args) |v| if (w.isResource(v)) try w.consume(v, .moved);
if (inst.result) |r| {
if (w.isResource(r)) w.live[r] = true;
}
},
.struct_get => |x| {
if (w.isResource(x.a)) {
_ = try w.resourceUsable(x.a);
try w.access(x.a, &.{.{ .field = x.field }}, .shared, null, x.a);
}
},
.aggregate_unpack => |x| {
if (w.isResource(x.value)) try w.consume(x.value, .moved);
},
.enum_take => |x| {
if (w.isResource(x.value)) try w.consume(x.value, .moved);
},
.own_pack => |x| {
if (w.isResource(x.value)) try w.consume(x.value, .moved);
},
.value_swap => |x| {
try w.useThrough(x.borrow, .exclusive);
if (w.isResource(x.value)) try w.consume(x.value, .moved);
if (inst.result) |r| {
if (w.isResource(r)) w.live[r] = true;
}
},
.own_free, .own_take => |v| {
try w.consume(v, .closed);
if (inst.result) |r| {
if (w.isResource(r)) w.live[r] = true;
}
},
.task_scope_begin => w.live[inst.result.?] = true,
.task_spawn => |x| {
try noWaitUnderLock(w, "task.spawn");