Phase 4: islands, frozen graphs and arc through every layer (RFC 0014)
AIR: types island<T>, frozen<T>, arc<T>, iref<T>; 16 instructions (island.create/alloc/root/borrow/freeze/destroy, frozen.root/borrow/ release, arc.from_frozen/new/clone/release/root/borrow, iref.eq); features islands (depends on heap) and arc (depends on islands) published. Verifier: IslandSafe/ArcSafe (E2111 at the owner type), container child borrows (one mut access per island), Send/Share rows (island not Share), linear owners, mem.alloc effect, element rules (recursion through iref). Interpreter: graphs with never-reused identities, T0010 for a foreign iref, O(1) freeze/conversion/transfer, arc counts in a trace hook (Options.arc_events), budgets per the quota table, collector support. .aji layer 9: types, operations, island.at/frozen.at/arc.at as borrow places with explicit container borrows, iref ==/!=. Frontend fix: joins carry only variables the construct assigns (and that are read again), so an unchanged linear value is never a block argument while borrowed. Tests: 10 AIR (phase4), 6 .aji run + 12 rejects (layer9), the reference program island-pipeline in .aji, equivalent to an independently hand-written AIR program (36 on 30 schedule seeds); unit tests for the arc count trace and O(1) transfer. Quick Reference §7 graphs (checked excerpt of quickref/graphs.aji); coverage 61/61. Bundle growth: AIR +7.0%, Quick Reference +0.6k, source docs +4.2k (recorded). Spec: quota rows match the header-reserved count; idioms and iref recursion in spec 03 §22; P4-11 (air-text.ebnf gaps) recorded.
This commit is contained in:
1 parent
ca84ab4348
commit
c699529fec
58 files changed
+2189
-98
No files matched your search
+107
-3
@@ -85,7 +85,7 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
|
||||
.void, .never => pos == .ret,
|
||||
.local => false,
|
||||
.ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed,
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard => pos != .member,
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc => pos != .member,
|
||||
else => true,
|
||||
};
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
@@ -93,6 +93,15 @@ 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) } },
|
||||
});
|
||||
// A graph holds IslandSafe values only (spec 03 §22–§24): reported once,
|
||||
// as E2111, at the type.
|
||||
if (t.isGraphOwner()) {
|
||||
if (!isIslandSafe(c.module, t.inner().?)) try c.report("E2111", span, "a graph holds only plain data and iref handles", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t.inner().?) } },
|
||||
});
|
||||
return;
|
||||
}
|
||||
// The pointee of ref/mut may be a linear value (`mut<file<read>>`), and
|
||||
// a task may return one; every other compound holds value types only.
|
||||
// Channel elements and mutex contents may be linear (spec 04 §38.1);
|
||||
@@ -226,6 +235,7 @@ fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
.task_scope_begin => .scope,
|
||||
.task_cancelled => .bool,
|
||||
.task_shield_begin => .shield,
|
||||
.iref_eq => .bool,
|
||||
.intrinsic => |x| blk: {
|
||||
const reg = air.authority.intrinsics[x.index];
|
||||
if (reg.fallible or reg.results.len == 0) break :blk .void;
|
||||
@@ -327,6 +337,8 @@ fn checkIntrinsic(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "intrinsic")
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => true,
|
||||
.own, .own_array, .mutex => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
@@ -340,6 +352,9 @@ pub fn isSend(m: air.Module, t: Type) bool {
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap => true,
|
||||
.frozen, .arc, .iref => true,
|
||||
// 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.*),
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
@@ -349,6 +364,17 @@ pub fn isShare(m: air.Module, t: Type) bool {
|
||||
};
|
||||
}
|
||||
|
||||
/// `IslandSafe` (spec 02 §6): plain data and `iref` handles, at any depth.
|
||||
/// `ArcSafe` is the same set in format 0.
|
||||
pub fn isIslandSafe(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .iref => true,
|
||||
.array => |a| isIslandSafe(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
fn aggregateAll(m: air.Module, i: u32, comptime pred: fn (air.Module, Type) bool) bool {
|
||||
return switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
@@ -425,6 +451,8 @@ fn contains(m: air.Module, t: Type, comptime what: enum { endpoint, forbidden })
|
||||
.forbidden => t.isBorrow() or t.isTaskBound() or t == .guard,
|
||||
};
|
||||
if (here) return true;
|
||||
// An `iref<T>` holds no `T`: types may recurse through it (spec 03 §22).
|
||||
if (t == .iref) return false;
|
||||
if (t.inner()) |inner| if (t != .ref and t != .mut and t != .slice and t != .slice_mut) return contains(m, inner, what);
|
||||
return switch (t) {
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
@@ -497,6 +525,8 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.channel_dup, .mutex_take, .mutex_borrow => |v| fc.ty[v] orelse return null,
|
||||
.mutex_make => |v| return .{ .mutex = try boxed(a, fc.ty[v] orelse return null) },
|
||||
.slice_split => |x| fc.ty[x.slice] orelse return null,
|
||||
.island_root, .frozen_root, .arc_root, .island_freeze, .arc_from_frozen, .arc_clone => |v| fc.ty[v] orelse return null,
|
||||
inline .island_borrow, .frozen_borrow, .arc_borrow => |x| fc.ty[x.owner] orelse return null,
|
||||
else => return null,
|
||||
};
|
||||
const invalid: Type = .void;
|
||||
@@ -504,7 +534,7 @@ 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, .region, .task, .scope, .sender, .receiver, .mutex => .{ .ref = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .island, .frozen, .arc => .{ .ref = try boxed(a, t) },
|
||||
.own => |inner| .{ .ref = inner },
|
||||
.own_array => |inner| .{ .slice = inner },
|
||||
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
|
||||
@@ -512,7 +542,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
},
|
||||
.borrow_mut => switch (t) {
|
||||
.local, .mut => .{ .mut = try boxed(a, t.inner().?) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver => .{ .mut = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .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().?) },
|
||||
@@ -548,10 +578,44 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.channel_dup => if (t == .ref and t.ref.* == .sender) t.ref.* else invalid,
|
||||
.mutex_take => if (t == .mutex) t.mutex.* else invalid,
|
||||
.mutex_borrow => if (t == .guard) Type{ .mut = t.guard } else invalid,
|
||||
// Graph roots and handles (spec 03 §22–§24).
|
||||
.island_root => graphRoot(t, .island),
|
||||
.frozen_root => graphRoot(t, .frozen),
|
||||
.arc_root => graphRoot(t, .arc),
|
||||
.island_freeze => if (t == .island) Type{ .frozen = t.island } else invalid,
|
||||
.arc_from_frozen => if (t == .frozen) Type{ .arc = t.frozen } else invalid,
|
||||
.arc_clone => if (t == .ref and t.ref.* == .arc) t.ref.* else invalid,
|
||||
inline .island_borrow, .frozen_borrow, .arc_borrow => |x| blk: {
|
||||
const owner: std.meta.Tag(Type) = switch (op) {
|
||||
.island_borrow => .island,
|
||||
.frozen_borrow => .frozen,
|
||||
else => .arc,
|
||||
};
|
||||
const r = fc.ty[x.iref] orelse return null;
|
||||
if (r != .iref) break :blk invalid;
|
||||
const target = try boxed(a, r.iref.*);
|
||||
if (t == .ref and std.meta.activeTag(t.ref.*) == owner) break :blk Type{ .ref = target };
|
||||
if (owner == .island and t == .mut and t.mut.* == .island) break :blk Type{ .mut = target };
|
||||
break :blk invalid;
|
||||
},
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
/// `iref<T>` of the root of a graph reached through `ref`/`mut` of its owner.
|
||||
fn graphRoot(t: Type, owner: std.meta.Tag(Type)) Type {
|
||||
const inner = switch (t) {
|
||||
.ref, .mut => |x| x.*,
|
||||
else => return .void,
|
||||
};
|
||||
if (std.meta.activeTag(inner) != owner) return .void;
|
||||
if (t == .mut and owner != .island) return .void;
|
||||
return .{ .iref = switch (inner) {
|
||||
.island, .frozen, .arc => |x| x,
|
||||
else => unreachable,
|
||||
} };
|
||||
}
|
||||
|
||||
fn checkFunction(c: *Context, f: air.Function) ![]const Type {
|
||||
const m = c.module;
|
||||
var fc: FnCtx = .{ .c = c, .f = f, .ty = try c.arena.alloc(?Type, f.values.len) };
|
||||
@@ -817,6 +881,37 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
try fc.expectParams(span, x.ok, &.{.{ .guard = t.ref.mutex }});
|
||||
},
|
||||
.mutex_borrow, .mutex_unlock => |v| if (fc.typeOf(v) != .guard) try fc.kindError(span, v, "guard<T>"),
|
||||
inline .island_create, .arc_new => |x| {
|
||||
try fc.expectAllocCap(span, x.cap);
|
||||
const t = fc.typeOf(x.value);
|
||||
// A non-IslandSafe content is reported at the owner type (E2111).
|
||||
if (!isIslandSafe(m, t)) return;
|
||||
const b = try boxed(fc.c.arena, t);
|
||||
try fc.expectOkErr(span, x.ok, x.err, if (inst.op == .arc_new) Type{ .arc = b } else Type{ .island = b });
|
||||
},
|
||||
.island_alloc => |x| {
|
||||
const g = fc.typeOf(x.island);
|
||||
if (g != .mut or g.mut.* != .island) return fc.kindError(span, x.island, "mut<island<T>>");
|
||||
const t = fc.typeOf(x.value);
|
||||
if (!isIslandSafe(m, t)) return graphContent(fc, span, t, "IslandSafe");
|
||||
try fc.expectOkErr(span, x.ok, x.err, .{ .iref = try boxed(fc.c.arena, t) });
|
||||
},
|
||||
.island_root => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<island<T>> or mut<island<T>>"),
|
||||
.frozen_root => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<frozen<T>>"),
|
||||
.arc_root => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<arc<T>>"),
|
||||
.island_borrow => |x| if (resultOf(fc, inst) == .void) try fc.kindError(span, x.owner, "ref<island<T>> or mut<island<T>>, and an iref<U>"),
|
||||
.frozen_borrow => |x| if (resultOf(fc, inst) == .void) try fc.kindError(span, x.owner, "ref<frozen<T>> and an iref<U>"),
|
||||
.arc_borrow => |x| if (resultOf(fc, inst) == .void) try fc.kindError(span, x.owner, "ref<arc<T>> and an iref<U>"),
|
||||
.island_freeze => |v| if (fc.typeOf(v) != .island) try fc.kindError(span, v, "island<T>"),
|
||||
.island_destroy => |v| if (fc.typeOf(v) != .island) try fc.kindError(span, v, "island<T>"),
|
||||
.frozen_release, .arc_from_frozen => |v| if (fc.typeOf(v) != .frozen) try fc.kindError(span, v, "frozen<T>"),
|
||||
.arc_clone => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<arc<T>>"),
|
||||
.arc_release => |v| if (fc.typeOf(v) != .arc) try fc.kindError(span, v, "arc<T>"),
|
||||
.iref_eq => |x| {
|
||||
const a = fc.typeOf(x.a);
|
||||
if (a != .iref) return fc.kindError(span, x.a, "iref<T>");
|
||||
try fc.expectValue(span, x.b, a);
|
||||
},
|
||||
.cap_narrow => |x| {
|
||||
const t = fc.typeOf(x.cap);
|
||||
if (t != .cap) return fc.kindError(span, x.cap, "cap<K>");
|
||||
@@ -852,6 +947,15 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
}
|
||||
}
|
||||
|
||||
/// A graph's content that is not `IslandSafe`/`ArcSafe` (spec 03 §22, §24).
|
||||
fn graphContent(fc: *FnCtx, span: air.Span, t: Type, property: []const u8) !void {
|
||||
try fc.c.report("E2111", span, "a graph holds only plain data and iref handles", &.{
|
||||
.{ .name = "function", .value = .{ .str = fc.f.name } },
|
||||
.{ .name = "property", .value = .{ .str = property } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(t) } },
|
||||
});
|
||||
}
|
||||
|
||||
fn resultOf(fc: *FnCtx, inst: air.Inst) Type {
|
||||
return fc.typeOf(inst.result.?);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user