cli: add whole-value linear aggregates
Implement STD-5 through source, AIR, verifier and deterministic interpreter. Constructors consume nested owners; exhaustive unpack/take, explicit swap and own.pack preserve ownership on success and failure. Support owning Option and Result while keeping classification helpers Copy-only. Preserve borrow identity across whole replacement and collection; distinguish individual owned arrays from repeated heap arrays. Specify feature contracts, invariants, diagnostics, compatibility and measured source/AIR budgets in RFC 0023 and normative docs. Add canonical round trips, AIR/source acceptance and rejection fixtures, CLI integration and CI checks. Validate build, unit/extended tests, full conformance, forced-collection schedule stress (32 seeds per quantum), hosted smoke, CLI suites, source coverage (88/88), formatting and reference bundle generation. Source bundle grows 3.2%; Quick Reference grows 4.0%. No MCC improvement is claimed. Dynamic initialized linear buffers and explicit drain remain Q-036/STD-6; frame/region/repeated heap storage is Copy-only. RFC remains Proposed.
This commit is contained in:
109 files changed
+1814
-142
No files matched your search
@@ -34,7 +34,7 @@ pub const registry = [_]Feature{
|
||||
.{ .name = "guard_transfer", .depends = &.{ "cancellation", "effects", "ownership", "sync", "tasks" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "heap", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "islands", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 4 },
|
||||
.{ .name = "linear_aggregates", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "linear_aggregates", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 6 },
|
||||
.{ .name = "ownership", .depends = &.{}, .status = .published, .phase = 1 },
|
||||
.{ .name = "receiver_dup", .depends = &.{ "cancellation", "capabilities", "channels", "effects", "heap", "ownership", "resources", "tasks" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "regions", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
@@ -49,7 +49,7 @@ pub const registry = [_]Feature{
|
||||
};
|
||||
|
||||
/// Features implemented by this toolchain, sorted.
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "linear_aggregates", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
|
||||
pub const Reason = enum {
|
||||
unknown_feature,
|
||||
|
||||
+37
-7
@@ -290,6 +290,9 @@ pub const Op = union(enum) {
|
||||
store: struct { borrow: ValueId, value: ValueId },
|
||||
// Feature `arrays`.
|
||||
array_splat: struct { ty: Type, value: ValueId },
|
||||
array_make: struct { ty: Type, args: []const ValueId },
|
||||
aggregate_unpack: struct { ty: Type, value: ValueId, next: u32 },
|
||||
value_swap: struct { borrow: ValueId, value: ValueId },
|
||||
borrow_index: struct { borrow: ValueId, index: ValueId, proven: bool, unchecked: bool = false },
|
||||
slice_make: ValueId,
|
||||
slice_len: ValueId,
|
||||
@@ -318,6 +321,7 @@ pub const Op = union(enum) {
|
||||
@"switch": struct { ty: IntType, value: ValueId, cases: []const Case, default: Target },
|
||||
/// `ty` is a declared enum (`.named`) or a built-in enum (`.builtin`).
|
||||
enum_switch: struct { ty: Type, value: ValueId, arms: []const Arm, default: ?Target },
|
||||
enum_take: struct { ty: Type, value: ValueId, arms: []const Arm, default: ?Target },
|
||||
/// Fallible narrowing (spec 05 §10.4): continues at `ok` with the new
|
||||
/// capability or at `err` with a `cap_error`, as block parameters.
|
||||
cap_narrow: struct { form: []const u8, cap: ValueId, args: []const ValueId, ok: u32, err: u32 },
|
||||
@@ -327,6 +331,7 @@ pub const Op = union(enum) {
|
||||
// Features `heap` and `regions` (spec 03 §19–§20). Allocations are
|
||||
// fallible terminators delivering to `ok`/`err` block parameters.
|
||||
own_new: struct { value: ValueId, cap: ValueId, ok: u32, err: u32 },
|
||||
own_pack: struct { ty: Type, value: ValueId, cap: ValueId, ok: u32, err: u32 },
|
||||
own_array: struct { n: ValueId, value: ValueId, cap: ValueId, ok: u32, err: u32 },
|
||||
own_take: ValueId,
|
||||
own_free: ValueId,
|
||||
@@ -410,8 +415,8 @@ pub const Op = union(enum) {
|
||||
|
||||
pub fn isTerminator(op: Op) bool {
|
||||
return switch (op) {
|
||||
.br, .cbr, .@"switch", .enum_switch, .cap_narrow, .ret, .trap => true,
|
||||
.own_new, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => true,
|
||||
inline .br, .cbr, .@"switch", .enum_switch, .enum_take, .aggregate_unpack, .cap_narrow, .ret, .trap => true,
|
||||
inline .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => true,
|
||||
.task_check_cancel, .task_join_cancel, .channel_make, .channel_send, .channel_recv, .channel_close_receiver, .select, .mutex_lock => true,
|
||||
.intrinsic => |x| x.ok != null,
|
||||
else => false,
|
||||
@@ -461,6 +466,24 @@ pub const Module = struct {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Whole-value linearity; well-formed nominal ownership graphs are acyclic.
|
||||
pub fn isLinearType(m: Module, t: Type) bool {
|
||||
if (t.isLinear()) return true;
|
||||
return switch (t) {
|
||||
.array => |a| m.isLinearType(a.elem),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (m.isLinearType(f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (m.isLinearType(p)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// Variant names and payloads of a declared or built-in enum type.
|
||||
pub fn variantCount(m: Module, t: Type) usize {
|
||||
return switch (t) {
|
||||
@@ -533,7 +556,7 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
for (x.cases) |c| for (c.target.args) |v| f(ctx, v);
|
||||
for (x.default.args) |v| f(ctx, v);
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
inline .enum_switch, .enum_take => |x| {
|
||||
f(ctx, x.value);
|
||||
if (x.default) |d| for (d.args) |v| f(ctx, v);
|
||||
},
|
||||
@@ -542,7 +565,7 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
for (x.args) |v| f(ctx, v);
|
||||
},
|
||||
.intrinsic => |x| for (x.args) |v| f(ctx, v),
|
||||
.own_new => |x| {
|
||||
inline .own_new, .own_pack => |x| {
|
||||
f(ctx, x.value);
|
||||
f(ctx, x.cap);
|
||||
},
|
||||
@@ -630,6 +653,12 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
f(ctx, x.value);
|
||||
},
|
||||
.array_splat => |x| f(ctx, x.value),
|
||||
.array_make => |x| for (x.args) |v| f(ctx, v),
|
||||
.aggregate_unpack => |x| f(ctx, x.value),
|
||||
.value_swap => |x| {
|
||||
f(ctx, x.borrow);
|
||||
f(ctx, x.value);
|
||||
},
|
||||
.borrow_index => |x| {
|
||||
f(ctx, x.borrow);
|
||||
f(ctx, x.index);
|
||||
@@ -654,7 +683,7 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
/// `ok`/`err` targets of a fallible allocation terminator.
|
||||
pub fn allocTargets(op: Op) ?struct { ok: u32, err: u32 } {
|
||||
return switch (op) {
|
||||
inline .own_new, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => |x| .{ .ok = x.ok, .err = x.err },
|
||||
inline .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => |x| .{ .ok = x.ok, .err = x.err },
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
@@ -670,10 +699,11 @@ pub fn successors(op: Op, buf: *std.ArrayList(u32), gpa: std.mem.Allocator) std.
|
||||
for (x.cases) |c| try buf.append(gpa, c.target.block);
|
||||
try buf.append(gpa, x.default.block);
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
inline .enum_switch, .enum_take => |x| {
|
||||
for (x.arms) |a| try buf.append(gpa, a.block);
|
||||
if (x.default) |d| try buf.append(gpa, d.block);
|
||||
},
|
||||
.aggregate_unpack => |x| try buf.append(gpa, x.next),
|
||||
.cap_narrow => |x| {
|
||||
try buf.append(gpa, x.ok);
|
||||
try buf.append(gpa, x.err);
|
||||
@@ -682,7 +712,7 @@ pub fn successors(op: Op, buf: *std.ArrayList(u32), gpa: std.mem.Allocator) std.
|
||||
try buf.append(gpa, ok);
|
||||
try buf.append(gpa, x.err.?);
|
||||
},
|
||||
inline .own_new, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => |x| {
|
||||
inline .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => |x| {
|
||||
try buf.append(gpa, x.ok);
|
||||
try buf.append(gpa, x.err);
|
||||
},
|
||||
|
||||
+31
-7
@@ -640,7 +640,7 @@ const Parser = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// Resources inside aggregates need the reserved feature `linear_aggregates`.
|
||||
/// Resources inside aggregates need the feature `linear_aggregates`.
|
||||
fn gateLinearMember(p: *Parser, t: air.Type, span: Span) Error!void {
|
||||
if (t == .guard) return p.requireFeature("guard_transfer", span, "lock guard as a member");
|
||||
if (t.isTaskBound()) return p.requireFeature("task_transfer", span, "task handle or scope as a member");
|
||||
@@ -909,6 +909,26 @@ const Parser = struct {
|
||||
const it = std.meta.stringToEnum(air.IntType, parts[2]) orelse return p.unknownOpcode(op_tok);
|
||||
break :op .{ .cast = .{ .mode = mode, .to = it, .a = try p.useValue() } };
|
||||
}
|
||||
if (eql(op_tok.text, "array.make")) {
|
||||
try p.requireFeature("linear_aggregates", op_tok.span, op_tok.text);
|
||||
const ty = try p.parseType();
|
||||
break :op .{ .array_make = .{ .ty = ty, .args = try p.valueList() } };
|
||||
}
|
||||
if (eql(op_tok.text, "struct.unpack") or eql(op_tok.text, "array.unpack")) {
|
||||
try p.requireFeature("linear_aggregates", op_tok.span, op_tok.text);
|
||||
const ty: air.Type = if (eql(op_tok.text, "struct.unpack")) .{ .named = try p.typeRef(.@"struct") } else try p.parseType();
|
||||
if (eql(op_tok.text, "array.unpack") and ty != .array) try p.report("E1001", op_tok.span, "array.unpack requires an array type", &.{});
|
||||
const v = try p.useValue();
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
_ = try p.expectWord("next");
|
||||
_ = try p.expect(.arrow, "`->`");
|
||||
break :op .{ .aggregate_unpack = .{ .ty = ty, .value = v, .next = try p.blockRef() } };
|
||||
}
|
||||
if (eql(op_tok.text, "value.swap")) {
|
||||
try p.requireFeature("linear_aggregates", op_tok.span, op_tok.text);
|
||||
const a, const b = try p.twoOperands();
|
||||
break :op .{ .value_swap = .{ .borrow = a, .value = b } };
|
||||
}
|
||||
if (eql(op_tok.text, "struct.make")) {
|
||||
const decl = try p.typeRef(.@"struct");
|
||||
break :op .{ .struct_make = .{ .decl = decl, .args = try p.valueList() } };
|
||||
@@ -962,7 +982,9 @@ const Parser = struct {
|
||||
_ = try p.expect(.arrow, "`->`");
|
||||
break :op .{ .@"switch" = .{ .ty = it, .value = value, .cases = cases.items, .default = try p.target() } };
|
||||
}
|
||||
if (eql(op_tok.text, "enum.switch")) {
|
||||
if (eql(op_tok.text, "enum.switch") or eql(op_tok.text, "enum.take")) {
|
||||
const taking = eql(op_tok.text, "enum.take");
|
||||
if (taking) try p.requireFeature("linear_aggregates", op_tok.span, op_tok.text);
|
||||
// A declared enum (`@E`) or a built-in enum (`cap_error`).
|
||||
const ety: air.Type = if (p.peek().tag == .word) blk: {
|
||||
const t = p.next();
|
||||
@@ -990,7 +1012,7 @@ const Parser = struct {
|
||||
const blk = try p.blockRef();
|
||||
try arms.append(p.arena, .{ .variant = variant, .block = blk, .span = span });
|
||||
}
|
||||
break :op .{ .enum_switch = .{ .ty = ety, .value = value, .arms = arms.items, .default = default } };
|
||||
break :op if (taking) .{ .enum_take = .{ .ty = ety, .value = value, .arms = arms.items, .default = default } } else .{ .enum_switch = .{ .ty = ety, .value = value, .arms = arms.items, .default = default } };
|
||||
}
|
||||
if (eql(op_tok.text, "ret")) {
|
||||
break :op .{ .ret = if (p.peek().tag == .local) try p.useValue() else null };
|
||||
@@ -1055,7 +1077,7 @@ const Parser = struct {
|
||||
.atomic_new, .atomic_take, .atomic_load, .atomic_rmw, .atomic_cmpxchg => true,
|
||||
.@"const", .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.local_alloc, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .load => true,
|
||||
.array_splat, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
.array_splat, .array_make, .value_swap, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
else => false,
|
||||
};
|
||||
const may_bind = produces_value or op == .call or op == .intrinsic or op == .task_join;
|
||||
@@ -1333,14 +1355,16 @@ const Parser = struct {
|
||||
|
||||
/// Instructions of the features `heap` and `regions` (spec 03 §19–§20).
|
||||
fn parseAllocOp(p: *Parser, op_tok: Token) Error!?air.Op {
|
||||
const Name = enum { @"own.new", @"own.array", @"own.take", @"own.free", @"region.create", @"region.new", @"region.array", @"region.reset", @"region.destroy" };
|
||||
const Name = enum { @"own.new", @"own.pack", @"own.array", @"own.take", @"own.free", @"region.create", @"region.new", @"region.array", @"region.reset", @"region.destroy" };
|
||||
const name = std.meta.stringToEnum(Name, op_tok.text) orelse return null;
|
||||
try p.requireFeature(if (std.mem.startsWith(u8, op_tok.text, "own.")) "heap" else "regions", op_tok.span, op_tok.text);
|
||||
switch (name) {
|
||||
.@"own.new" => {
|
||||
.@"own.new", .@"own.pack" => {
|
||||
if (name == .@"own.pack") try p.requireFeature("linear_aggregates", op_tok.span, op_tok.text);
|
||||
const pack_ty: air.Type = if (name == .@"own.pack") try p.parseType() else .void;
|
||||
const v, const c = try p.twoOperands();
|
||||
const ok, const err = try p.okErr();
|
||||
return .{ .own_new = .{ .value = v, .cap = c, .ok = ok, .err = err } };
|
||||
return if (name == .@"own.pack") .{ .own_pack = .{ .ty = pack_ty, .value = v, .cap = c, .ok = ok, .err = err } } else .{ .own_new = .{ .value = v, .cap = c, .ok = ok, .err = err } };
|
||||
},
|
||||
.@"own.array" => {
|
||||
const n, const v = try p.twoOperands();
|
||||
|
||||
+61
-4
@@ -209,6 +209,19 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
try w.print("cast.{s}.{s} ", .{ @tagName(x.mode), @tagName(x.to) });
|
||||
try value(f, x.a, w);
|
||||
},
|
||||
.array_make => |x| {
|
||||
try w.writeAll("array.make ");
|
||||
try printType(m, x.ty, w);
|
||||
try values(f, x.args, w);
|
||||
},
|
||||
.aggregate_unpack => |x| {
|
||||
try w.writeAll(if (x.ty == .array) "array.unpack " else "struct.unpack ");
|
||||
try printType(m, x.ty, w);
|
||||
try w.writeByte(' ');
|
||||
try value(f, x.value, w);
|
||||
try w.print(", next -> @{s}", .{f.blocks[x.next].name});
|
||||
},
|
||||
.value_swap => |x| try two(f, w, "value.swap", x.borrow, x.value),
|
||||
.struct_make => |x| {
|
||||
try w.print("struct.make @{s}", .{m.types[x.decl].name});
|
||||
try values(f, x.args, w);
|
||||
@@ -251,8 +264,8 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
try w.writeAll(", default -> ");
|
||||
try target(f, x.default, w);
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
try w.writeAll("enum.switch ");
|
||||
inline .enum_switch, .enum_take => |x| {
|
||||
try w.writeAll(if (op == .enum_take) "enum.take " else "enum.switch ");
|
||||
try printType(m, x.ty, w);
|
||||
try w.writeByte(' ');
|
||||
try value(f, x.value, w);
|
||||
@@ -310,8 +323,15 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
try values(f, x.args, w);
|
||||
if (x.ok) |ok| try w.print(", ok -> @{s}, err -> @{s}", .{ f.blocks[ok].name, f.blocks[x.err.?].name });
|
||||
},
|
||||
.own_new => |x| {
|
||||
try two(f, w, "own.new", x.value, x.cap);
|
||||
inline .own_new, .own_pack => |x, tag| {
|
||||
if (tag == .own_pack) {
|
||||
try w.writeAll("own.pack ");
|
||||
try printType(m, x.ty, w);
|
||||
try w.writeByte(' ');
|
||||
try value(f, x.value, w);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.cap, w);
|
||||
} else try two(f, w, "own.new", x.value, x.cap);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
.own_array => |x| {
|
||||
@@ -635,3 +655,40 @@ test "round-trip of ownership and arrays forms" {
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "round-trip whole-value linear operations" {
|
||||
try roundTrip(
|
||||
\\module test.linear
|
||||
\\air 0
|
||||
\\lang 0.1
|
||||
\\features [arrays, capabilities, effects, heap, linear_aggregates, ownership, resources]
|
||||
\\type @Maybe = enum { some(own<u64>), none }
|
||||
\\fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
\\effects [mem.alloc]
|
||||
\\entry:
|
||||
\\ %n = const.u64 42
|
||||
\\ own.pack u64 %n, %heap, ok -> @got, err -> @failed
|
||||
\\got(%owner: own<u64>):
|
||||
\\ %items = array.make array<own<u64>,1>(%owner)
|
||||
\\ array.unpack array<own<u64>,1> %items, next -> @element
|
||||
\\element(%part: own<u64>):
|
||||
\\ %slot = enum.make @Maybe.some(%part)
|
||||
\\ %empty = enum.make @Maybe.none()
|
||||
\\ %borrow = borrow.mut %slot
|
||||
\\ %old = value.swap %borrow, %empty
|
||||
\\ borrow.end %borrow
|
||||
\\ enum.take @Maybe %slot, some -> @extra, none -> @replaced
|
||||
\\extra(%extra_owner: own<u64>):
|
||||
\\ own.free %extra_owner
|
||||
\\ enum.take @Maybe %old, some -> @answer, none -> @no_answer
|
||||
\\replaced:
|
||||
\\ enum.take @Maybe %old, some -> @answer, none -> @no_answer
|
||||
\\answer(%answer_owner: own<u64>):
|
||||
\\ %value = own.take %answer_owner
|
||||
\\ ret %value
|
||||
\\no_answer:
|
||||
\\ ret %n
|
||||
\\failed(%e: alloc_error, %original: u64):
|
||||
\\ ret %n
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user