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

+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", &.{});