Phase 2: heap, regions and allocation budgets (RFC 0008)

- air: own<T>, own_array<T>, region, alloc_error; own.new/array/take/
  free, region.create/new/array/reset/destroy; mem.alloc budget form;
  linear contents of owners gated by linear_aggregates
- verifier: allocation types and capability kind (E1001, E3201),
  mem.alloc effect (E3101), ok/err counts; borrow-typed block params
  checked in the CFG stage with the region-allocation exception; linear
  owners and regions (E3002, E3001, E2001, E2002, E2101); region
  allocations as disjoint exclusive children of the shared region
  borrow; E2104 when reset/destroy would invalidate a live reference
- interp: live-byte budgets charged along the narrowing chain, shared by
  copies, mandatory in the deterministic profile; heap cells; regions
  return quota only on reset/destroy
- conformance: 25 heap/region tests including a 64 KiB region file copy;
  4000 mutants without a crash
This commit is contained in:
dev committed 2026-10-04 14:26:34 +03:00
1 parent b8d4ab4d4c
commit 7f4ce71d44
38 files changed
+1341 -48

No files matched your search

+76 -5
View File
@@ -72,7 +72,7 @@ fn visitDecl(c: *Context, state: anytype, i: u32) !void {
state[i] = .black;
}
const Position = enum { member, param, ret, block_param };
const Position = enum { member, param, ret, block_param, borrowed };
/// Where a type may appear (spec 07 §8.3, §8.8):
/// - `void`/`never` only as return types;
@@ -84,7 +84,8 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
const ok = switch (t) {
.void, .never => pos == .ret,
.local => false,
.ref, .mut, .slice, .slice_mut, .resource => pos != .member,
.ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed,
.resource, .own, .own_array, .region => pos != .member,
else => true,
};
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
@@ -92,7 +93,9 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
.{ .name = "position", .value = .{ .str = @tagName(pos) } },
.{ .name = "found", .value = .{ .str = try c.typeName(t) } },
});
if (t.inner()) |inner| try wellFormed(c, span, inner, .member, owner);
// The pointee of ref/mut may be a linear value (`mut<file<read>>`);
// every other compound holds value types only.
if (t.inner()) |inner| try wellFormed(c, span, inner, if (t == .ref or t == .mut) .borrowed else .member, owner);
}
/// `local.alloc` results are checked separately: their own type is storage.
@@ -135,6 +138,31 @@ const FnCtx = struct {
});
}
/// A `cap<mem.alloc>` operand: wrong kind is E3201, not a capability E1001.
fn expectAllocCap(fc: *FnCtx, span: air.Span, v: air.ValueId) !void {
const t = fc.typeOf(v);
if (t != .cap) return fc.kindError(span, v, "cap<mem.alloc>");
const kind = air.authority.kinds[t.cap].name;
if (!std.mem.eql(u8, kind, "mem.alloc")) try fc.c.report("E3201", span, "allocation needs cap<mem.alloc>", &.{
.{ .name = "function", .value = .{ .str = fc.f.name } },
.{ .name = "expected", .value = .{ .str = "mem.alloc" } },
.{ .name = "found", .value = .{ .str = kind } },
});
}
fn expectRegionRef(fc: *FnCtx, span: air.Span, v: air.ValueId) !void {
const t = fc.typeOf(v);
if (t != .ref or t.ref.* != .region) try fc.kindError(span, v, "ref<region>");
}
/// `ok` receives `result`, `err` an `alloc_error` (counts checked by the CFG stage).
fn expectOkErr(fc: *FnCtx, span: air.Span, ok: u32, err: u32, result: Type) !void {
const p = fc.f.blocks[ok].params[0];
if (!p.ty.eql(result)) try fc.mismatch(span, try fc.c.valueName(fc.f, p.value), result, p.ty);
const e = fc.f.blocks[err].params[0];
if (!e.ty.eql(.{ .builtin = .alloc_error })) try fc.mismatch(span, try fc.c.valueName(fc.f, e.value), .{ .builtin = .alloc_error }, e.ty);
}
fn kindError(fc: *FnCtx, span: air.Span, v: air.ValueId, expected: []const u8) !void {
try fc.c.report("E1001", span, "operand has the wrong kind of type", &.{
.{ .name = "function", .value = .{ .str = fc.f.name } },
@@ -299,6 +327,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
.borrow_field => |x| fc.ty[x.borrow] orelse return null,
.borrow_index => |x| fc.ty[x.borrow] orelse return null,
.slice_sub => |x| fc.ty[x.slice] orelse return null,
.own_take => |v| fc.ty[v] orelse return null,
.slice_split => |x| fc.ty[x.slice] orelse return null,
else => return null,
};
@@ -307,13 +336,17 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
.local_load => if (t == .local) t.local.* else invalid,
.borrow_ref => switch (t) {
.local, .ref, .mut => .{ .ref = try boxed(a, t.inner().?) },
.resource => .{ .ref = try boxed(a, t) },
.resource, .region => .{ .ref = try boxed(a, t) },
.own => |inner| .{ .ref = inner },
.own_array => |inner| .{ .slice = inner },
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
else => invalid,
},
.borrow_mut => switch (t) {
.local, .mut => .{ .mut = try boxed(a, t.inner().?) },
.resource => .{ .mut = try boxed(a, t) },
.resource, .region => .{ .mut = try boxed(a, t) },
.own => |inner| .{ .mut = inner },
.own_array => |inner| .{ .slice_mut = inner },
.slice_mut => .{ .slice_mut = try boxed(a, t.inner().?) },
else => invalid,
},
@@ -342,6 +375,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
else => invalid,
},
.slice_sub, .slice_split => if (t.isSlice()) t else invalid,
.own_take => if (t == .own) t.own.* else invalid,
else => unreachable,
};
}
@@ -522,6 +556,39 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
if (x.default) |d| try fc.expectTarget(span, d);
},
.intrinsic => |x| try checkIntrinsic(fc, inst, x),
.own_new => |x| {
try fc.expectAllocCap(span, x.cap);
const t = fc.typeOf(x.value);
if (!t.isValue()) return fc.kindError(span, x.value, "a value type");
try fc.expectOkErr(span, x.ok, x.err, .{ .own = try boxed(fc.c.arena, t) });
},
.own_array => |x| {
try fc.expectValue(span, x.n, .{ .int = .usize });
try fc.expectAllocCap(span, x.cap);
const t = fc.typeOf(x.value);
if (!t.isValue()) return fc.kindError(span, x.value, "a value type");
try fc.expectOkErr(span, x.ok, x.err, .{ .own_array = try boxed(fc.c.arena, t) });
},
.own_take => |v| if (fc.typeOf(v) != .own) try fc.kindError(span, v, "own<T>"),
.own_free => |v| if (fc.typeOf(v) != .own and fc.typeOf(v) != .own_array) try fc.kindError(span, v, "own<T> or own_array<T>"),
.region_create => |x| {
try fc.expectAllocCap(span, x.cap);
try fc.expectOkErr(span, x.ok, x.err, .region);
},
.region_new => |x| {
try fc.expectRegionRef(span, x.region);
const t = fc.typeOf(x.value);
if (!t.isValue()) return fc.kindError(span, x.value, "a value type");
try fc.expectOkErr(span, x.ok, x.err, .{ .mut = try boxed(fc.c.arena, t) });
},
.region_array => |x| {
try fc.expectRegionRef(span, x.region);
try fc.expectValue(span, x.n, .{ .int = .usize });
const t = fc.typeOf(x.value);
if (!t.isValue()) return fc.kindError(span, x.value, "a value type");
try fc.expectOkErr(span, x.ok, x.err, .{ .slice_mut = try boxed(fc.c.arena, t) });
},
.region_reset, .region_destroy => |v| if (fc.typeOf(v) != .region) try fc.kindError(span, v, "region"),
.cap_narrow => |x| {
const t = fc.typeOf(x.cap);
if (t != .cap) return fc.kindError(span, x.cap, "cap<K>");
@@ -532,6 +599,10 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
.{ .name = "kind", .value = .{ .str = air.authority.kinds[t.cap].name } },
.{ .name = "form", .value = .{ .str = x.form } },
});
if (std.mem.eql(u8, x.form, "budget")) {
if (x.args.len != 1) return fc.c.report("E1001", span, "cap.narrow.budget takes one byte-count operand", &.{.{ .name = "function", .value = .{ .str = f.name } }});
try fc.expectValue(span, x.args[0], .{ .int = .usize });
}
if (std.mem.eql(u8, x.form, "prefix")) {
if (x.args.len != 1) return fc.c.report("E1001", span, "cap.narrow.prefix takes one path operand", &.{.{ .name = "function", .value = .{ .str = f.name } }});
if (!operandMatches(.bytes, fc.typeOf(x.args[0]))) try fc.kindError(span, x.args[0], "slice<u8>");