cli: add initialized buffers and safe owning vectors

Introduce linear_buffers through the specification, AIR parser/serializer,
verifier, deterministic interpreter and source lowering. A buffer owns an
initialized prefix with explicit reserve, bounded push, moving pop/views and
recoverable empty-only free. Growth charges old+new capacity; failed growth,
full push and nonempty free preserve or return complete ownership.

Implement compiler-pinned safe std.vec with nine operations for Copy values,
owners, files and channel endpoints. Permit complete Copy moves inside
specialized generic bodies without weakening monomorphic or borrow rules.
Update RFC 0024, Q-036, STD-6a/b, reference grammar, Quick Reference, bundles,
source coverage and CI. Remaining collections stay STD-6c.

Validation: unit tests and AIR round-trip; 757 conformance cases; 56 dedicated
buffer cases with per-instruction collection and 32-seed schedule stress;
extended memory test; hosted smoke; 36 module, 20 generic, 49 linear and
118 buffer CLI checks; 91/91 source constructs; Zig fmt and reference bundles.
Full source bundle: 49,589 -> 52,384 cl100k tokens (+5.6%); AIR 175,736 bytes.
The reference interpreter's push copies its initialized prefix; no native
performance or empirical MCC benefit is claimed.
This commit is contained in:
dev committed 2026-10-06 23:26:41 +03:00
1 parent 4f084decce
commit 4138a52af6
101 files changed
+2377 -71

No files matched your search

+15 -1
View File
@@ -242,6 +242,11 @@ pub fn overlap(a: []const Seg, b: []const Seg) bool {
const Edge = struct { target: u32, args: []const ValueId };
fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void {
if (op.bufferTargets()) |t| {
try out.append(arena, .{ .target = t.ok, .args = &.{} });
try out.append(arena, .{ .target = t.err, .args = &.{} });
return;
}
switch (op) {
.br => |t| try out.append(arena, .{ .target = t.block, .args = t.args }),
.cbr => |x| {
@@ -385,7 +390,7 @@ fn step(w: *Walker, inst: air.Inst) !void {
},
.borrow_field => |x| try w.derive(inst.result.?, x.borrow, .{ .field = x.field }),
.borrow_index => |x| try w.derive(inst.result.?, x.borrow, .elem),
.slice_make => |v| try w.derive(inst.result.?, v, null),
.buffer_view, .slice_make => |v| try w.derive(inst.result.?, v, null),
.slice_sub => |x| try w.derive(inst.result.?, x.slice, .elem),
.slice_split => |x| {
const l = inst.result.?;
@@ -452,6 +457,15 @@ fn step(w: *Walker, inst: air.Inst) !void {
.enum_take => |x| {
if (w.isResource(x.value)) try w.consume(x.value, .moved);
},
.buffer_new => {},
.buffer_len, .buffer_capacity => |v| try w.useThrough(v, .shared),
.buffer_reserve => |x| try w.useThrough(x.borrow, .exclusive),
.buffer_push => |x| {
try w.useThrough(x.borrow, .exclusive);
if (w.isResource(x.value)) try w.consume(x.value, .moved);
},
.buffer_pop => |x| try w.useThrough(x.value, .exclusive),
.buffer_free => |x| try w.consume(x.value, .moved),
.own_pack => |x| {
if (w.isResource(x.value)) try w.consume(x.value, .moved);
},
+2 -1
View File
@@ -33,6 +33,7 @@ pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
.atomic => |inner| accountedSize(m, inner.*, depth + 1),
.mutex => |inner| std.math.add(u64, 8, accountedSize(m, inner.*, depth + 1) orelse return null) catch null,
.own_array => 16,
.buffer => 24,
.slice, .slice_mut => 16,
.builtin => |b| tagSize(b.variants().len),
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1) orelse return null) catch null,
@@ -151,7 +152,7 @@ fn checkValueSizes(c: *verifier.Context) !void {
for (b.params) |p| try v.visit(b.span, p.ty);
for (b.insts) |inst| switch (inst.op) {
.local_alloc => |t| try v.visit(inst.span, t),
inline .array_splat, .array_make, .aggregate_unpack, .own_pack => |x| try v.visit(inst.span, x.ty),
inline .array_splat, .array_make, .aggregate_unpack, .own_pack, .buffer_new => |x| try v.visit(inst.span, x.ty),
else => {},
};
}
+20
View File
@@ -83,6 +83,26 @@ fn checkTerminator(c: *Context, f: air.Function, inst: air.Inst) !void {
};
try paramCount(c, f, inst.span, x.next, n, "unpack target must receive every component");
},
.buffer_new => |x| {
try paramCount(c, f, inst.span, x.ok, 1, "buffer.new success takes buffer");
try paramCount(c, f, inst.span, x.err, 1, "buffer.new failure takes alloc_error");
},
.buffer_reserve => |x| {
try paramCount(c, f, inst.span, x.ok, 0, "buffer.reserve success takes no values");
try paramCount(c, f, inst.span, x.err, 1, "buffer.reserve failure takes alloc_error");
},
.buffer_push => |x| {
try paramCount(c, f, inst.span, x.ok, 0, "buffer.push success takes no values");
try paramCount(c, f, inst.span, x.err, 1, "buffer.push failure returns value");
},
.buffer_pop => |x| {
try paramCount(c, f, inst.span, x.ok, 1, "buffer.pop success takes value");
try paramCount(c, f, inst.span, x.err, 0, "buffer.pop empty takes no values");
},
.buffer_free => |x| {
try paramCount(c, f, inst.span, x.ok, 0, "buffer.free success takes no values");
try paramCount(c, f, inst.span, x.err, 1, "buffer.free nonempty returns buffer");
},
.own_pack => |x| {
try paramCount(c, f, inst.span, x.ok, 1, "own.pack success takes owner");
try paramCount(c, f, inst.span, x.err, 2, "own.pack failure takes error and original value");
+1 -1
View File
@@ -57,7 +57,7 @@ pub fn check(c: *Context) !void {
.atomic_store => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
.atomic_rmw => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
.atomic_cmpxchg => |x| try needAtomic(c, f, inst, x.success != .seq_cst or x.failure != .seq_cst),
.own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => if (!contains(f.effects, "mem.alloc")) {
.buffer_new, .buffer_reserve, .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => if (!contains(f.effects, "mem.alloc")) {
try c.report("E3101", inst.span, "allocation requires the effect mem.alloc", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "effect", .value = .{ .str = "mem.alloc" } },
+48 -10
View File
@@ -48,7 +48,7 @@ fn checkDeclarations(c: *Context) !void {
for (b.params) |p| try wellFormed(c, b.span, p.ty, .block_param, f.name);
for (b.insts) |inst| switch (inst.op) {
.local_alloc => |t| try wellFormed(c, inst.span, t, .member, f.name),
inline .array_splat, .array_make, .aggregate_unpack, .own_pack => |x| try wellFormed(c, inst.span, x.ty, .param, f.name),
inline .array_splat, .array_make, .aggregate_unpack, .own_pack, .buffer_new => |x| try wellFormed(c, inst.span, x.ty, .param, f.name),
else => {},
};
}
@@ -85,7 +85,7 @@ fn visitValue(c: *Context, state: anytype, t: Type) std.mem.Allocator.Error!void
switch (t) {
.named => |i| try visitDecl(c, state, i),
.array => |a| try visitValue(c, state, a.elem),
.own, .own_array, .sender, .receiver, .mutex, .ref, .mut, .slice, .slice_mut, .guard, .task, .local => |inner| if (c.module.declares("linear_aggregates")) try visitValue(c, state, inner.*),
.own, .own_array, .buffer, .sender, .receiver, .mutex, .ref, .mut, .slice, .slice_mut, .guard, .task, .local => |inner| if (c.module.declares("linear_aggregates")) try visitValue(c, state, inner.*),
else => {},
}
}
@@ -104,7 +104,7 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
.local => false,
.ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed,
.scope, .task, .shield, .guard => pos != .member,
.resource, .own, .own_array, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or c.module.declares("linear_aggregates"),
.resource, .own, .own_array, .buffer, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or c.module.declares("linear_aggregates"),
else => true,
};
if (t == .own_array and c.module.declares("linear_aggregates") and c.module.isLinearType(t.own_array.*)) try c.report("E1001", span, "dynamic heap arrays require Copy content", &.{});
@@ -266,7 +266,7 @@ fn resultType(m: air.Module, op: air.Op) ?Type {
.struct_get => |x| m.types[x.decl].kind.@"struct"[x.field].ty,
.enum_make => |x| .{ .named = x.decl },
.call => |x| m.functions[x.callee].ret,
.slice_len => .{ .int = .usize },
.slice_len, .buffer_len, .buffer_capacity => .{ .int = .usize },
.task_scope_begin => .scope,
.task_cancelled => .bool,
.task_shield_begin => .shield,
@@ -382,7 +382,7 @@ pub fn isCopy(m: air.Module, t: Type) bool {
pub fn isLocalType(m: air.Module, t: Type) bool {
return switch (t) {
.local, .task, .scope, .shield, .guard => true,
.ref, .mut, .slice, .slice_mut, .own, .own_array, .mutex => |inner| isLocalType(m, inner.*),
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| isLocalType(m, inner.*),
.array => |a| isLocalType(m, a.elem),
.named => |i| switch (m.types[i].kind) {
.@"struct" => |fs| for (fs) |f| {
@@ -405,7 +405,7 @@ pub fn isSend(m: air.Module, t: Type) bool {
.island, .frozen, .arc, .iref => true,
// Every access to a cell is an atomic operation (spec 04 §23).
.atomic => true,
.own, .own_array, .mutex => |inner| isSend(m, inner.*),
.own, .own_array, .buffer, .mutex => |inner| isSend(m, inner.*),
.ref, .slice => |inner| isShare(m, inner.*),
.mut, .slice_mut => |inner| isSend(m, inner.*),
.array => |a| isSend(m, a.elem),
@@ -422,7 +422,7 @@ pub fn isShare(m: air.Module, t: Type) bool {
// Its owner may mutate it: freeze to share (spec 04 §35).
.island => false,
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => false,
.own, .own_array, .ref, .slice => |inner| isShare(m, inner.*),
.own, .own_array, .buffer, .ref, .slice => |inner| isShare(m, inner.*),
// The lock synchronizes access (spec 04 §35, §40).
.mutex => |inner| isSend(m, inner.*),
.array => |a| isShare(m, a.elem),
@@ -602,6 +602,13 @@ fn checkSelect(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "select")) !voi
_ = f;
}
fn bufferElement(fc: *FnCtx, span: air.Span, v: air.ValueId, mutable: bool) !?Type {
const t = fc.typeOf(v);
if ((t == .mut or (!mutable and t == .ref)) and t.inner().? == .buffer) return t.inner().?.buffer.*;
try fc.kindError(span, v, if (mutable) "mut<buffer<T>>" else "ref<buffer<T>> or mut<buffer<T>>");
return null;
}
pub fn complete(t: Type) bool {
return t != .void and t != .never and t != .local and !t.isBorrow() and !t.isTaskBound() and t != .guard;
}
@@ -622,7 +629,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
.copy => |v| return fc.ty[v],
.local_alloc => |inner| return .{ .local = try boxed(a, inner) },
.local_load => |v| fc.ty[v] orelse return null,
.borrow_ref, .borrow_mut, .load, .slice_make => |v| fc.ty[v] orelse return null,
.buffer_view, .borrow_ref, .borrow_mut, .load, .slice_make => |v| fc.ty[v] orelse return null,
.value_swap => |x| fc.ty[x.borrow] orelse return null,
.borrow_field => |x| fc.ty[x.borrow] orelse return null,
.borrow_index => |x| fc.ty[x.borrow] orelse return null,
@@ -644,12 +651,13 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
};
const invalid: Type = .void;
return switch (op) {
.buffer_view => if ((t == .ref or t == .mut) and t.inner().? == .buffer) (if (t == .mut) .{ .slice_mut = try boxed(a, t.inner().?.buffer.*) } else .{ .slice = try boxed(a, t.inner().?.buffer.*) }) else invalid,
.value_swap => if (t == .mut) t.mut.* else invalid,
.local_load => if (t == .local) t.local.* else invalid,
.borrow_ref => switch (t) {
.local, .ref, .mut => .{ .ref = try boxed(a, t.inner().?) },
.named, .array => if (m.isLinearType(t)) .{ .ref = try boxed(a, t) } else invalid,
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => .{ .ref = try boxed(a, t) },
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .buffer, .island, .frozen, .arc, .atomic => .{ .ref = try boxed(a, t) },
.own => |inner| .{ .ref = inner },
.own_array => |inner| .{ .slice = inner },
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
@@ -658,7 +666,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
.borrow_mut => switch (t) {
.local, .mut => .{ .mut = try boxed(a, t.inner().?) },
.named, .array => if (m.isLinearType(t)) .{ .mut = try boxed(a, t) } else invalid,
.resource, .region, .task, .scope, .sender, .receiver, .island => .{ .mut = try boxed(a, t) },
.resource, .region, .task, .scope, .sender, .receiver, .buffer, .island => .{ .mut = try boxed(a, t) },
.own => |inner| .{ .mut = inner },
.own_array => |inner| .{ .slice_mut = inner },
.slice_mut => .{ .slice_mut = try boxed(a, t.inner().?) },
@@ -831,6 +839,36 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
try fc.expectValue(span, x.value, t.mut.*);
if (!complete(t.mut.*)) try fc.kindError(span, x.value, "a complete value");
},
.buffer_new => |x| {
try fc.expectValue(span, x.n, .{ .int = .usize });
try fc.expectAllocCap(span, x.cap);
if (!complete(x.ty)) return fc.kindError(span, x.n, "complete buffer content");
try fc.expectParams(span, x.ok, &.{.{ .buffer = try boxed(fc.c.arena, x.ty) }});
try fc.expectParams(span, x.err, &.{.{ .builtin = .alloc_error }});
},
.buffer_len, .buffer_capacity, .buffer_view => |v| {
_ = try bufferElement(fc, span, v, false);
},
.buffer_reserve => |x| {
_ = try bufferElement(fc, span, x.borrow, true);
try fc.expectValue(span, x.n, .{ .int = .usize });
try fc.expectAllocCap(span, x.cap);
try fc.expectParams(span, x.err, &.{.{ .builtin = .alloc_error }});
},
.buffer_push => |x| {
const t = try bufferElement(fc, span, x.borrow, true) orelse return;
try fc.expectValue(span, x.value, t);
try fc.expectParams(span, x.err, &.{t});
},
.buffer_pop => |x| {
const t = try bufferElement(fc, span, x.value, true) orelse return;
try fc.expectParams(span, x.ok, &.{t});
},
.buffer_free => |x| {
const t = fc.typeOf(x.value);
if (t != .buffer) return fc.kindError(span, x.value, "buffer<T>");
try fc.expectParams(span, x.err, &.{t});
},
.array_make => |x| {
if (x.ty != .array) return fc.c.report("E1001", span, "array.make needs an array type", &.{});
if (x.args.len != x.ty.array.len) return fc.c.report("E1001", span, "array.make requires every element", &.{});