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

+57 -25
View File
@@ -109,6 +109,9 @@ pub const Value = union(enum) {
pub const Cell = struct {
value: Value,
ty: air.Type,
/// An SSA root borrowed in place; its frame outlives every loan.
backing: ?struct { values: []Value, index: u32 } = null,
owner_array: bool = false,
/// Accesses seen by the race detector (`Options.detect_races`).
shadow: ?*race.Shadow = null,
};
@@ -374,6 +377,8 @@ const Copier = struct {
const n = try c.to.create(Value.Cell);
try c.cells.put(c.tmp, old, n);
n.* = .{ .value = .void, .ty = old.ty, .shadow = if (old.shadow) |sh| try sh.copy(c.to) else null };
n.owner_array = old.owner_array;
if (old.backing) |b| n.backing = .{ .values = try c.items(b.values), .index = b.index };
n.value = try c.value(old.value);
return n;
}
@@ -858,6 +863,12 @@ const Machine = struct {
return bind(vals, inst.result, .{ .int = .{ .ty = x.to, .bits = bits } });
},
.assert => |v| return if (vals[v].boolean) .next else .{ .trap = .assert },
.array_make => |x| return bind(vals, inst.result, .{ .array = try m.gatherMut(vals, x.args) }),
.aggregate_unpack => |x| {
const fields = if (x.ty == .array) vals[x.value].array else vals[x.value].aggregate.fields;
for (f.blocks[x.next].params, fields) |p, v| vals[p.value] = v;
return .{ .jump = x.next };
},
.struct_make => |x| return bind(vals, inst.result, .{ .aggregate = .{
.decl = x.decl,
.variant = 0,
@@ -877,7 +888,7 @@ const Machine = struct {
for (x.cases) |c| if (c.bits == v) return m.jump(f, vals, c.target);
return m.jump(f, vals, x.default);
},
.enum_switch => |x| {
inline .enum_switch, .enum_take => |x| {
const variant: u32, const fields: []const Value = switch (vals[x.value]) {
.aggregate => |agg| .{ agg.variant, agg.fields },
.builtin => |b| .{ b.variant, &.{} },
@@ -934,13 +945,13 @@ const Machine = struct {
.task_check_cancel, .task_join_cancel, .task_shield_begin, .task_shield_end => return m.execCancellation(f, vals, inst),
.channel_make, .channel_dup, .channel_send, .channel_recv, .channel_close_sender, .channel_close_receiver, .select => return m.execChannel(f, vals, inst),
.mutex_make, .mutex_take, .mutex_lock, .mutex_borrow, .mutex_unlock => return m.execMutex(f, vals, inst),
.own_new, .own_array, .own_take, .own_free, .region_create, .region_new, .region_array, .region_reset, .region_destroy => return m.execAlloc(f, vals, {}, inst),
inline .own_new, .own_pack, .own_array, .own_take, .own_free, .region_create, .region_new, .region_array, .region_reset, .region_destroy => return m.execAlloc(f, vals, {}, inst),
.island_create, .island_alloc, .island_root, .island_borrow, .island_freeze, .island_destroy, .frozen_root, .frozen_borrow, .frozen_release, .arc_from_frozen, .arc_new, .arc_clone, .arc_release, .arc_root, .arc_borrow, .iref_eq => return m.execGraph(f, vals, inst),
.atomic_new, .atomic_take, .atomic_load, .atomic_store, .atomic_rmw, .atomic_cmpxchg, .atomic_fence => return m.execAtomic(vals, inst),
.intrinsic => |x| return m.intrinsic(f, vals, inst, x),
.ret => |v| return .{ .ret = if (v) |x| vals[x] else .void },
.trap => return .{ .trap = .explicit },
.local_alloc, .local_store, .local_zero, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .borrow_end, .load, .store, .array_splat, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split, .slice_copy, .guard, .assume => return m.execMemory(vals, inst),
.local_alloc, .local_store, .local_zero, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .borrow_end, .load, .store, .array_splat, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split, .slice_copy, .value_swap, .guard, .assume => return m.execMemory(vals, inst),
}
}
@@ -968,7 +979,7 @@ const Machine = struct {
const ch = endpointOf(vals[arm.endpoint]);
if ((if (arm.value == null) m.recvState(ch) else m.sendState(ch)) != .none) break true;
} else false,
.mutex_lock => |x| !vals[x.mutex].mutex.locked,
.mutex_lock => |x| !mutexOf(vals[x.mutex]).locked,
else => unreachable,
};
}
@@ -976,7 +987,7 @@ const Machine = struct {
fn endpointOf(v: Value) u32 {
return switch (v) {
.sender, .receiver => |c| c,
.place => |p| endpointOf(p.cell.value),
.place => |p| endpointOf(placePtr(p).*),
else => unreachable,
};
}
@@ -1453,7 +1464,7 @@ const Machine = struct {
fn mutexOf(v: Value) *Value.MutexCell {
return switch (v) {
.mutex => |mc| mc,
.place => |p| mutexOf(p.cell.value),
.place => |p| mutexOf(placePtr(p).*),
else => unreachable,
};
}
@@ -1501,7 +1512,7 @@ const Machine = struct {
}
fn placePtr(p: Value.Place) *Value {
var v = &p.cell.value;
var v = if (p.cell.backing) |b| &b.values[b.index] else &p.cell.value;
for (p.path) |i| v = switch (v.*) {
.aggregate => |a| &a.fields[i],
.array => |items| &items[i],
@@ -1567,7 +1578,7 @@ const Machine = struct {
fn resourceOf(v: Value) u32 {
return switch (v) {
.resource => |hd| hd,
.place => |p| p.cell.value.resource,
.place => |p| placePtr(p).resource,
else => unreachable,
};
}
@@ -1761,6 +1772,19 @@ const Machine = struct {
fn execAlloc(m: *Machine, f: air.Function, vals: []Value, types_of: anytype, inst: air.Inst) !Step {
_ = types_of;
switch (inst.op) {
.own_pack => |x| {
const a = vals[x.cap].cap;
const bytes = budget.accountedSize(m.module, x.ty, 0);
if (!m.charge(a, bytes)) {
vals[f.blocks[x.err].params[1].value] = vals[x.value];
return allocFailed(f, vals, x.err);
}
const cell = try m.heap().create(Value.Cell);
cell.* = .{ .value = try m.deepCopy(vals[x.value]), .ty = x.ty };
try m.owners.put(m.heap(), cell, .{ .authority = a, .bytes = bytes.? });
vals[f.blocks[x.ok].params[0].value] = .{ .own = cell };
return .{ .jump = x.ok };
},
.own_new => |x| {
const v = vals[x.value];
const bytes = m.valueBytes(vals[x.value]);
@@ -1780,7 +1804,7 @@ const Machine = struct {
const items = try m.heap().alloc(Value, n);
for (items) |*it| it.* = try m.deepCopy(vals[x.value]);
const cell = try m.heap().create(Value.Cell);
cell.* = .{ .value = .{ .array = items }, .ty = .void };
cell.* = .{ .value = .{ .array = items }, .ty = .void, .owner_array = true };
try m.owners.put(m.heap(), cell, .{ .authority = a, .bytes = bytes.? });
vals[f.blocks[x.ok].params[0].value] = .{ .own = cell };
return .{ .jump = x.ok };
@@ -1949,6 +1973,20 @@ const Machine = struct {
return @truncate(v.int.bits);
}
fn ssaCell(m: *Machine, vals: []Value, index: u32) !*Value.Cell {
for (vals) |v| {
const cell: ?*Value.Cell = switch (v) {
.place => |p| p.cell,
.slice => |s| s.place.cell,
else => null,
};
if (cell) |c| if (c.backing) |b| if (b.values.ptr == vals.ptr and b.index == index) return c;
}
const cell = try m.heap().create(Value.Cell);
cell.* = .{ .value = .void, .ty = .void, .backing = .{ .values = vals, .index = index } };
return cell;
}
fn execMemory(m: *Machine, vals: []Value, inst: air.Inst) std.mem.Allocator.Error!Step {
switch (inst.op) {
.local_alloc => |t| {
@@ -1973,22 +2011,9 @@ const Machine = struct {
},
.borrow_ref, .borrow_mut => |v| return bind(vals, inst.result, switch (vals[v]) {
.local => |c| .{ .place = .{ .cell = c, .path = &.{} } },
// A borrowed resource: a place whose cell holds the handle.
// Owners: the heap cell itself; arrays are borrowed as slices.
.own => |cell| switch (cell.value) {
.array => |items| .{ .slice = .{ .place = .{ .cell = cell, .path = &.{} }, .start = 0, .len = items.len } },
else => .{ .place = .{ .cell = cell, .path = &.{} } },
},
.region => |r| blk: {
const cell = try m.heap().create(Value.Cell);
cell.* = .{ .value = .{ .region = r }, .ty = .void };
break :blk .{ .place = .{ .cell = cell, .path = &.{} } };
},
.resource => |hd| blk: {
const cell = try m.heap().create(Value.Cell);
cell.* = .{ .value = .{ .resource = hd }, .ty = .void };
break :blk .{ .place = .{ .cell = cell, .path = &.{} } };
},
.own => |cell| if (cell.owner_array) .{ .slice = .{ .place = .{ .cell = cell, .path = &.{} }, .start = 0, .len = cell.value.array.len } } else .{ .place = .{ .cell = cell, .path = &.{} } },
.aggregate, .array, .resource, .region => .{ .place = .{ .cell = try m.ssaCell(vals, v), .path = &.{} } },
.graph => if (inst.op == .borrow_mut) .{ .place = .{ .cell = try m.ssaCell(vals, v), .path = &.{} } } else vals[v],
else => |x| x,
}),
.borrow_field => |x| return bind(vals, inst.result, .{ .place = try m.extend(asPlace(vals[x.borrow]), x.field) }),
@@ -2004,6 +2029,13 @@ const Machine = struct {
placePtr(p).* = try m.deepCopy(vals[x.value]);
return .next;
},
.value_swap => |x| {
const p = asPlace(vals[x.borrow]);
if (try m.raceCheck(p.cell, p.path, true)) |r| return r;
const old = placePtr(p).*;
placePtr(p).* = try m.deepCopy(vals[x.value]);
return bind(vals, inst.result, old);
},
.array_splat => |x| {
const items = try m.heap().alloc(Value, x.ty.array.len);
for (items) |*it| it.* = try m.deepCopy(vals[x.value]);