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:
dev committed 2026-10-05 22:14:05 +03:00
1 parent ca84ab4348
commit c699529fec
58 files changed
+2189 -98

No files matched your search

+229 -19
View File
@@ -21,7 +21,8 @@
//! 6. intrinsics (I/O, files, TCP, clock, random), resources, capability
//! narrowing, unsafe operations;
//! 7. task scopes, spawn and join, channels;
//! 8. `lock` and mutexes, `select`, cancellation points, shields.
//! 8. `lock` and mutexes, `select`, cancellation points, shields;
//! 9. islands, frozen graphs, arcs and `iref` handles (spec 03 §22–§24).
const std = @import("std");
const ast = @import("ast.zig");
@@ -67,13 +68,18 @@ pub const Ty = union(enum) {
guard: *const Ty,
/// A shield (`shield { … }`, never a source value).
shield,
/// Graph owners and handles (spec 03 §22–§24).
island: *const Ty,
frozen: *const Ty,
arc: *const Ty,
iref: *const Ty,
pub const Arr = struct { elem: Ty, len: u64 };
/// Linear: consumed exactly once, by `move` (spec 15 §5).
fn isLinear(t: Ty) bool {
return switch (t) {
.own, .own_array, .region, .resource, .task, .sender, .receiver, .scope, .mutex, .guard, .shield => true,
.own, .own_array, .region, .resource, .task, .sender, .receiver, .scope, .mutex, .guard, .shield, .island, .frozen, .arc => true,
else => false,
};
}
@@ -84,7 +90,7 @@ pub const Ty = union(enum) {
.int => |x| std.mem.eql(u8, x, b.int),
.named => |i| i == b.named,
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard => |x| x.eql(inner(b).?),
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| x.eql(inner(b).?),
.cap => |k| std.mem.eql(u8, k, b.cap),
.builtin => |e| e == b.builtin,
.resource => |r| r == b.resource,
@@ -94,7 +100,7 @@ pub const Ty = union(enum) {
fn inner(t: Ty) ?Ty {
return switch (t) {
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard => |x| x.*,
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| x.*,
.array => |a| a.elem,
else => null,
};
@@ -321,7 +327,7 @@ const Lowerer = struct {
.never => "never",
.named => |i| l.typeNames[i],
.array => |a| std.fmt.allocPrint(l.arena, "array<{s}, {d}>", .{ l.tyText(a.elem), a.len }) catch "array",
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard => |x| std.fmt.allocPrint(l.arena, "{s}<{s}>", .{ @tagName(t), l.tyText(x.*) }) catch "borrow",
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| std.fmt.allocPrint(l.arena, "{s}<{s}>", .{ @tagName(t), l.tyText(x.*) }) catch "borrow",
.scope => "scope",
.shield => "shield",
.region => "region",
@@ -441,6 +447,8 @@ const Lowerer = struct {
.{ "channels", &.{ "channel.", "sender<", "receiver<", "select [" } },
.{ "sync", &.{ "mutex.", "mutex<", "guard<" } },
.{ "cancellation", &.{ "task.shield", "task.check_cancel", "task.join.cancel" } },
.{ "islands", &.{ "island.", "island<", "frozen.", "frozen<", "iref.", "iref<" } },
.{ "arc", &.{ "arc.", "arc<" } },
};
for (rules) |r| {
const used = for (texts.items) |t| {
@@ -511,7 +519,7 @@ const Lowerer = struct {
},
.array => |a| return l.arrayOf(try l.memberType(a.elem.*), a.len),
.wrapped => |w| {
const Wr = enum { ref, mut, slice, slice_mut, own, own_array, task, sender, receiver, mutex };
const Wr = enum { ref, mut, slice, slice_mut, own, own_array, task, sender, receiver, mutex, island, frozen, arc, iref };
const which = std.meta.stringToEnum(Wr, w.wrapper) orelse return l.notYet(t.span, "this type (later layers)");
const in = try l.typeOf(w.inner.*);
return switch (which) {
@@ -525,6 +533,10 @@ const Lowerer = struct {
.sender => l.wrap(.sender, in),
.receiver => l.wrap(.receiver, in),
.mutex => l.wrap(.mutex, in),
.island => l.wrap(.island, in),
.frozen => l.wrap(.frozen, in),
.arc => l.wrap(.arc, in),
.iref => l.wrap(.iref, in),
};
},
.cap => |k| {
@@ -682,9 +694,12 @@ const Lowerer = struct {
/// that may be read after it (pruned SSA, spec 15 §4). A variable that is
/// not read again is not passed on, so a linear value consumed before the
/// join does not travel to it.
fn carriedLive(l: *Lowerer, depth: usize, construct: Span) Error![]const usize {
fn carriedLive(l: *Lowerer, depth: usize, construct: Span, region: Region) Error![]const usize {
var out: std.ArrayList(usize) = .empty;
for (try l.carried(depth)) |i| if (l.liveAfter(l.bindings.items[i].name, construct)) try out.append(l.arena, i);
for (try l.carried(depth)) |i| {
const n = l.bindings.items[i].name;
if (region.assigns(n) and l.liveAfter(n, construct)) try out.append(l.arena, i);
}
return out.items;
}
@@ -692,9 +707,10 @@ const Lowerer = struct {
/// itself reads (they flow around the back edge).
fn carriedLoop(l: *Lowerer, depth: usize, construct: Span, lp: LoopAst) Error![]const usize {
var out: std.ArrayList(usize) = .empty;
const region: Region = .{ .loop = lp };
for (try l.carried(depth)) |i| {
const n = l.bindings.items[i].name;
if (loopReads(lp, n) or l.liveAfter(n, construct)) try out.append(l.arena, i);
if (region.assigns(n) and (loopReads(lp, n) or l.liveAfter(n, construct))) try out.append(l.arena, i);
}
return out.items;
}
@@ -915,6 +931,8 @@ const Lowerer = struct {
const scope_start = l.scopeBindingStart();
for (l.bindings.items[scope_start..]) |b| if (b.kind == .borrow and std.mem.eql(u8, b.name, x.name))
return l.fail("E7006", s.span, "a borrow of this name is already active in this scope", &.{.{ .name = "name", .value = x.name }});
if (x.place.kind == .call) if (pathOf(x.place.kind.call.callee.*)) |p| if (std.mem.endsWith(u8, p, ".at"))
return l.graphAccess(s, x.name, x.mutable, x.place);
const p = try l.uncheckedPlace(x.place, x.mutable, x.unchecked, s.span);
if (p.ends.len == 0) {
// Borrowing a borrow binding: a reborrow, ended at scope end.
@@ -991,7 +1009,7 @@ const Lowerer = struct {
const depth = l.bindings.items.len;
const lp_ast: LoopAst = .{ .cond = cond, .body = body, .step = if (step) |st| st.name else null };
const vars = try l.carriedLoop(depth, span, lp_ast);
const exit_vars = try l.carriedLive(depth, span);
const exit_vars = try l.carriedLive(depth, span, .{ .loop = lp_ast });
const header = try l.newBlock(if (cond == null) "loop" else "while");
const exit = try l.newBlock("after");
const cont = if (step != null) try l.newBlock("step") else header;
@@ -1339,6 +1357,14 @@ const Lowerer = struct {
try l.checkTy(a.span, lv.ty, ty);
const rv = try l.expr(b, ty);
try l.checkTy(b.span, rv.ty, ty);
// `iref` identity (spec 03 §22): equal graph and slot; never a trap.
if (ty == .iref) {
if (op != .eq and op != .ne) return l.fail("E7005", e.span, "`iref` handles are compared with `==` and `!=` only", &.{});
const c = try l.def("cmp", .bool, try l.fmt("iref.eq %{s}, %{s}", .{ lv.name, rv.name }), e.span);
if (op == .eq) return c;
const t = try l.def("c", .bool, "const.bool true", e.span);
return l.def("cmp", .bool, try l.fmt("xor.bool %{s}, %{s}", .{ c.name, t.name }), e.span);
}
if (ty == .bool and !(op == .eq or op == .ne or op == .bit_and or op == .bit_or or op == .bit_xor))
return l.fail("E7005", e.span, "this operation needs integer operands", &.{});
if (ty != .bool and ty != .int) return l.fail("E7005", e.span, "this operation needs integer or boolean operands", &.{});
@@ -1565,7 +1591,7 @@ const Lowerer = struct {
if (st == .builtin) return l.builtinMatch(m, st.builtin, want);
const sv = try l.expr(m.scrutinee, st);
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, m.span);
const vars = try l.carriedLive(depth, m.span, .{ .match = m });
const before = try l.snapshot();
var ends: std.ArrayList(ArmEnd) = .empty;
@@ -1659,7 +1685,7 @@ const Lowerer = struct {
const name = @tagName(b);
const sv = try l.expr(m.scrutinee, .{ .builtin = b });
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, m.span);
const vars = try l.carriedLive(depth, m.span, .{ .match = m });
const before = try l.snapshot();
const blocks = try l.arena.alloc(usize, m.arms.len);
var head: std.ArrayList(u8) = .empty;
@@ -1699,6 +1725,11 @@ const Lowerer = struct {
const eq = std.mem.eql;
if ((eq(u8, p, "own.take") or eq(u8, p, "task.join")) and args.len == 1) return if (l.peekType(args[0])) |t| t.inner() else null;
if (eq(u8, p, "task.cancelled")) return .bool;
if ((eq(u8, p, "island.root") or eq(u8, p, "frozen.root") or eq(u8, p, "arc.root")) and args.len == 1)
return if (l.peekType(args[0])) |t| (if (t.inner()) |o| (if (o.inner()) |root| l.wrap(.iref, root) catch null else null) else null) else null;
if (eq(u8, p, "island.freeze") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .island) l.wrap(.frozen, t.inner().?) catch null else null) else null;
if (eq(u8, p, "arc.from_frozen") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .frozen) l.wrap(.arc, t.inner().?) catch null else null) else null;
if (eq(u8, p, "arc.clone") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .arc) t else if (t.inner()) |in| (if (in == .arc) in else null) else null) else null;
if (eq(u8, p, "channel.dup") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .sender) t else if (t.inner()) |in| (if (in == .sender) in else null) else null) else null;
if (eq(u8, p, "task.spawn") and args.len >= 2 and args[1].kind == .name) {
const sig = l.sigs.get(args[1].kind.name) orelse return null;
@@ -1860,7 +1891,8 @@ const Lowerer = struct {
.own => |in| if (mutable) try l.wrap(.mut, in.*) else try l.wrap(.ref, in.*),
.own_array => |in| if (mutable) try l.wrap(.slice_mut, in.*) else try l.wrap(.slice, in.*),
.region => if (mutable) try l.wrap(.mut, .region) else try l.wrap(.ref, .region),
.resource, .mutex, .sender, .receiver, .task => if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty),
.resource, .mutex, .sender, .receiver, .task, .island => if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty),
.frozen, .arc => if (mutable) return l.fail("E7005", place.span, "frozen graphs and arcs are immutable: borrow them with `ref`", &.{.{ .name = "name", .value = n }}) else try l.wrap(.ref, b.ty),
else => null,
};
const t = bt orelse return l.fail("E7006", place.span, "this value is not addressable here; borrow a variable, a field or an element", &.{.{ .name = "name", .value = n }});
@@ -2117,7 +2149,7 @@ const Lowerer = struct {
fn fallible(l: *Lowerer, path: []const u8) ?void {
_ = l;
const ops = [_][]const u8{ "own.new", "own.array", "region.create", "region.new", "region.array", "cap.narrow.budget", "cap.narrow.prefix", "channel.make", "channel.send", "channel.recv", "channel.close_receiver", "task.check_cancel", "task.join_cancel" };
const ops = [_][]const u8{ "own.new", "own.array", "region.create", "region.new", "region.array", "cap.narrow.budget", "cap.narrow.prefix", "channel.make", "channel.send", "channel.recv", "channel.close_receiver", "task.check_cancel", "task.join_cancel", "island.create", "island.alloc", "arc.new" };
for (ops) |o| if (std.mem.eql(u8, o, path)) return {};
if (authority.intrinsicIndex(path)) |i| if (authority.intrinsics[i].fallible) return {};
return null;
@@ -2206,6 +2238,41 @@ const Lowerer = struct {
try l.endNow(temps.items, e.span);
return v;
}
if (eq(p, "island.root") or eq(p, "frozen.root") or eq(p, "arc.root")) {
if (args.len != 1) return l.fail("E7005", e.span, "`.root` takes the graph's borrow binding", &.{});
const g = try l.graphBorrow(args[0], p[0..std.mem.indexOfScalar(u8, p, '.').?], false);
return try l.def("root", try l.wrap(.iref, g.ty.inner().?.inner().?), try l.fmt("{s} %{s}", .{ p, g.name }), e.span);
}
if (eq(p, "island.freeze") or eq(p, "arc.from_frozen")) {
if (args.len != 1) return l.fail("E7005", e.span, "this operation takes one graph owner", &.{});
const o = try l.consuming(args[0], null);
const want: std.meta.Tag(Ty) = if (eq(p, "island.freeze")) .island else .frozen;
if (std.meta.activeTag(o.ty) != want) return l.fail("E7005", args[0].span, if (want == .island) "`island.freeze` takes an `island<T>`" else "`arc.from_frozen` takes a `frozen<T>`", &.{.{ .name = "found", .value = l.tyText(o.ty) }});
const out = if (want == .island) try l.wrap(.frozen, o.ty.inner().?) else try l.wrap(.arc, o.ty.inner().?);
return try l.def(if (want == .island) "frozen" else "arc", out, try l.fmt("{s} %{s}", .{ p, o.name }), e.span);
}
if (eq(p, "island.destroy") or eq(p, "frozen.release") or eq(p, "arc.release")) {
if (args.len != 1) return l.fail("E7005", e.span, "this operation takes one graph owner", &.{});
const o = try l.consuming(args[0], null);
const want: std.meta.Tag(Ty) = if (eq(p, "island.destroy")) .island else if (eq(p, "frozen.release")) .frozen else .arc;
if (std.meta.activeTag(o.ty) != want) return l.fail("E7005", args[0].span, "this operation takes another graph owner", &.{ .{ .name = "operation", .value = p }, .{ .name = "found", .value = l.tyText(o.ty) } });
try l.emit(try l.fmt("{s} %{s}", .{ p, o.name }), e.span);
return .{ .name = "", .ty = .void };
}
if (eq(p, "arc.clone")) {
if (args.len != 1 or args[0].kind != .name) return l.fail("E7005", e.span, "`arc.clone` takes an arc by name", &.{});
const b = l.lookup(args[0].kind.name) orelse return l.unknown(args[0].span, args[0].kind.name);
if (b.ty == .arc) {
const r = try l.def(args[0].kind.name, try l.wrap(.ref, b.ty), try l.fmt("borrow.ref %{s}", .{b.value}), args[0].span);
const v = try l.def("arc", b.ty, try l.fmt("arc.clone %{s}", .{r.name}), e.span);
try l.emit(try l.fmt("borrow.end %{s}", .{r.name}), e.span);
return v;
}
if (b.ty == .ref and b.ty.inner().? == .arc) return try l.def("arc", b.ty.inner().?, try l.fmt("arc.clone %{s}", .{b.value}), e.span);
return l.fail("E7005", args[0].span, "`arc.clone` takes an `arc<T>`", &.{.{ .name = "found", .value = l.tyText(b.ty) }});
}
if (eq(p, "island.at") or eq(p, "frozen.at") or eq(p, "arc.at"))
return l.fail("E7006", e.span, "a graph access is a borrow: bind it with `ref x = island.at(g, r);` or `mut x = island.at(g, r);`", &.{});
if (eq(p, "mutex.make")) {
if (args.len != 1) return l.fail("E7005", e.span, "`mutex.make` takes the initial value", &.{});
const v = try l.consuming(args[0], null);
@@ -2248,7 +2315,7 @@ const Lowerer = struct {
};
const elem = mr.ty.ref.mutex.*;
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, s.span);
const vars = try l.carriedLive(depth, s.span, .{ .lock = .{ body, cancelled } });
const before = try l.snapshot();
const locked = try l.newBlock("locked");
const guard = try l.freshName("guard");
@@ -2358,7 +2425,7 @@ const Lowerer = struct {
}
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, s.span);
const vars = try l.carriedLive(depth, s.span, .{ .select = sel });
const before = try l.snapshot();
var text: std.ArrayList(u8) = .empty;
try text.appendSlice(l.arena, "select ");
@@ -2436,6 +2503,37 @@ const Lowerer = struct {
};
}
/// The named borrow binding of a graph owner that `.root`, `.at` and
/// `island.alloc` take (spec 15 §8: container borrows are explicit).
fn graphBorrow(l: *Lowerer, a: ast.Expr, owner: []const u8, mutable: bool) Error!Value {
if (a.kind == .borrow) return l.fail("E7006", a.span, "the graph's borrow is bound first (`mut g = isl;`), so it outlives the borrows taken from it", &.{});
if (a.kind != .name) return l.fail("E7005", a.span, "the graph's borrow binding is expected here, by name", &.{});
const b = l.lookup(a.kind.name) orelse return l.unknown(a.span, a.kind.name);
const tag = std.meta.stringToEnum(std.meta.Tag(Ty), owner).?;
if (std.meta.activeTag(b.ty) == tag) return l.fail("E7005", a.span, try l.fmt("this takes a borrow of the {s}, not the owner: bind `{s} g = {s};` in an enclosing block and pass `g`", .{ owner, if (mutable) "mut" else "ref", a.kind.name }), &.{.{ .name = "name", .value = a.kind.name }});
const ok = (b.ty == .ref or (b.ty == .mut and !mutable) or (b.ty == .mut and mutable)) and std.meta.activeTag(b.ty.inner().?) == tag;
if (!ok or (mutable and b.ty != .mut)) return l.fail("E7005", a.span, try l.fmt("a {s} borrow of an `{s}<T>` is expected here", .{ if (mutable) "`mut`" else "`ref` or `mut`", owner }), &.{.{ .name = "found", .value = l.tyText(b.ty) }});
return .{ .name = b.value, .ty = b.ty };
}
/// `ref x = island.at(g, r);` / `mut x = island.at(g, r);`, and
/// `frozen.at`, `arc.at` (`ref` only): a child borrow of `g`.
fn graphAccess(l: *Lowerer, s: ast.Stmt, name: []const u8, mutable: bool, place: ast.Expr) Error!void {
const ac = place.kind.call;
const p = pathOf(ac.callee.*).?;
const owner = p[0..std.mem.indexOfScalar(u8, p, '.').?];
if (mutable and !std.mem.eql(u8, owner, "island")) return l.fail("E7005", s.span, "frozen graphs and arcs give `ref` access only", &.{});
if (ac.args.len != 2) return l.fail("E7005", place.span, "`.at` takes the graph's borrow binding and an `iref`", &.{});
const g = try l.graphBorrow(ac.args[0], owner, mutable);
const r = try l.expr(ac.args[1], null);
if (r.ty != .iref) return l.fail("E7005", ac.args[1].span, "an `iref` is expected here", &.{.{ .name = "found", .value = l.tyText(r.ty) }});
const op = if (std.mem.eql(u8, owner, "island")) "island.borrow" else if (std.mem.eql(u8, owner, "frozen")) "frozen.borrow" else "arc.borrow";
const target = r.ty.inner().?;
const v = try l.def(name, if (mutable) try l.wrap(.mut, target) else try l.wrap(.ref, target), try l.fmt("{s} %{s}, %{s}", .{ op, g.name, r.name }), s.span);
try l.scopeEnds(&.{v.name}, s.span);
try l.bindings.append(l.arena, .{ .name = name, .mutable = false, .value = v.name, .ty = v.ty, .kind = .borrow });
}
fn resultTy(r: authority.ResultType) Ty {
return switch (r) {
.usize => .{ .int = "usize" },
@@ -2575,6 +2673,19 @@ const Lowerer = struct {
.temps = temps.items,
};
}
if (std.mem.eql(u8, path, "island.create") or std.mem.eql(u8, path, "arc.new")) {
try want(l, e.span, args, 2, path);
const v = try l.consuming(args[0], null);
const c = try l.expectCap(args[1], "mem.alloc");
const owner = if (std.mem.eql(u8, path, "arc.new")) try l.wrap(.arc, v.ty) else try l.wrap(.island, v.ty);
return .{ .text = try l.fmt("{s} %{s}, %{s}", .{ path, v.name, c.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{owner} }, .{ .role = "err", .params = &.{alloc_err} } }) };
}
if (std.mem.eql(u8, path, "island.alloc")) {
try want(l, e.span, args, 2, path);
const g = try l.graphBorrow(args[0], "island", true);
const v = try l.consuming(args[1], null);
return .{ .text = try l.fmt("island.alloc %{s}, %{s}", .{ g.name, v.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.iref, v.ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) };
}
if (std.mem.eql(u8, path, "task.check_cancel")) {
try want(l, e.span, args, 0, path);
return .{ .text = "task.check_cancel", .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{} }, .{ .role = "cancelled", .params = &.{} } }) };
@@ -2690,7 +2801,7 @@ const Lowerer = struct {
if (missing.items.len != 0) return l.fail("E7004", m.span, "the match does not handle every outcome of the operation", &.{.{ .name = "missing", .value = missing.items }});
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, m.span);
const vars = try l.carriedLive(depth, m.span, .{ .match = m });
const before = try l.snapshot();
const blocks = try l.outcomeBlocks(f, m.scrutinee.span);
var ends: std.ArrayList(ArmEnd) = .empty;
@@ -2763,7 +2874,7 @@ const Lowerer = struct {
};
try l.checkTy(x.cond.span, cv.ty, .bool);
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, span);
const vars = try l.carriedLive(depth, span, .{ .@"if" = x });
const then_b = try l.newBlock("then");
const else_b = try l.newBlock("else");
try l.term(try l.fmt("cbr %{s}, @{s}(), @{s}()", .{ cv.name, l.blocks.items[then_b].name, l.blocks.items[else_b].name }), x.cond.span);
@@ -2813,7 +2924,10 @@ fn pathOf(e: ast.Expr) ?[]const u8 {
"cap.narrow.budget", "cap.narrow.prefix", "task.spawn", "task.join", "task.cancel",
"task.cancelled", "channel.make", "channel.dup", "channel.send", "channel.recv",
"channel.close_sender", "channel.close_receiver", "mutex.make", "mutex.take", "task.check_cancel",
"task.join_cancel",
"task.join_cancel", "island.create", "island.alloc", "island.root", "island.at",
"island.freeze", "island.destroy", "frozen.root", "frozen.at", "frozen.release",
"arc.from_frozen", "arc.new", "arc.clone", "arc.release", "arc.root",
"arc.at",
};
var buf: [64]u8 = undefined;
const text = dotted(e, &buf) orelse return null;
@@ -3077,3 +3191,99 @@ const After = struct {
};
}
};
/// The code of a branching construct, for deciding which variables it
/// assigns: a join carries only variables that may change inside.
const Region = union(enum) {
loop: LoopAst,
@"if": ast.If,
match: ast.Match,
lock: struct { ast.Block, ast.Block },
select: ast.Select,
fn assigns(r: Region, name: []const u8) bool {
return switch (r) {
.loop => |lp| (if (lp.step) |st| std.mem.eql(u8, st, name) else false) or (if (lp.cond) |c| exprAssigns(c, name) else false) or blockAssigns(lp.body, name),
.@"if" => |x| ifAssigns(x, name),
.match => |m| matchAssigns(m, name),
.lock => |b| blockAssigns(b[0], name) or blockAssigns(b[1], name),
.select => |x| selectAssigns(x, name),
};
}
};
/// Whether code assigns variable `name`: `name = e;`, or a pattern of an
/// outcome or `select` edge rebinding it (spec 15 §6).
fn blockAssigns(b: ast.Block, name: []const u8) bool {
for (b.stmts) |st| if (stmtAssigns(st, name)) return true;
if (b.tail) |t| if (exprAssigns(t.*, name)) return true;
return false;
}
fn stmtAssigns(st: ast.Stmt, name: []const u8) bool {
return switch (st.kind) {
.assign => |x| (x.place.kind == .name and std.mem.eql(u8, x.place.kind.name, name)) or exprAssigns(x.e, name),
.let => |x| exprAssigns(x.e, name),
.@"var" => |x| if (x.init) |e| exprAssigns(e, name) else false,
.borrow => |x| exprAssigns(x.place, name),
.@"return" => |e| if (e) |x| exprAssigns(x, name) else false,
.expr => |e| exprAssigns(e, name),
.@"while" => |x| exprAssigns(x.cond, name) or blockAssigns(x.body, name),
.@"for" => |x| std.mem.eql(u8, x.name, name) or blockAssigns(x.body, name),
.loop, .shield => |b| blockAssigns(b, name),
.scope => |x| blockAssigns(x.body, name),
.lock => |x| blockAssigns(x.body, name) or blockAssigns(x.cancelled, name),
.select => |x| selectAssigns(x, name),
.@"break", .@"continue", .trap => false,
};
}
fn exprAssigns(e: ast.Expr, name: []const u8) bool {
return switch (e.kind) {
.if_expr => |x| ifAssigns(x.*, name),
.match_expr => |x| matchAssigns(x.*, name),
.block => |b| blockAssigns(b.*, name),
.neg, .not, .deref, .or_trap => |x| exprAssigns(x.*, name),
.binary => |x| exprAssigns(x.l.*, name) or exprAssigns(x.r.*, name),
.cast => |x| exprAssigns(x.e.*, name),
.call => |x| blk: {
for (x.args) |a| if (exprAssigns(a, name)) break :blk true;
break :blk false;
},
.struct_lit => |x| blk: {
for (x.fields) |f| if (exprAssigns(f.e, name)) break :blk true;
break :blk false;
},
.field => |x| exprAssigns(x.e.*, name),
.index => |x| exprAssigns(x.e.*, name) or exprAssigns(x.lo.*, name) or (if (x.hi) |h| exprAssigns(h.*, name) else false),
.array_rep => |x| exprAssigns(x.e.*, name),
else => false,
};
}
fn ifAssigns(x: ast.If, name: []const u8) bool {
if (exprAssigns(x.cond, name) or blockAssigns(x.then, name)) return true;
if (x.@"else") |els| return switch (els) {
.block => |b| blockAssigns(b, name),
.@"if" => |inner| ifAssigns(inner.*, name),
};
return false;
}
fn matchAssigns(m: ast.Match, name: []const u8) bool {
if (exprAssigns(m.scrutinee, name)) return true;
for (m.arms) |a| {
if (a.pattern.kind == .ctor) for (a.pattern.kind.ctor.binds) |b| if (std.mem.eql(u8, b, name)) return true;
if (exprAssigns(a.body, name)) return true;
}
return false;
}
fn selectAssigns(x: ast.Select, name: []const u8) bool {
for (x.arms) |a| for (a.arms) |arm| {
if (arm.pattern.kind == .ctor) for (arm.pattern.kind.ctor.binds) |b| if (std.mem.eql(u8, b, name)) return true;
if (exprAssigns(arm.body, name)) return true;
};
for (x.cancelled_binds) |b| if (std.mem.eql(u8, b, name)) return true;
return exprAssigns(x.cancelled, name);
}