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
+43
-9
@@ -191,7 +191,7 @@ const Walker = struct {
|
||||
|
||||
/// Linear values: resources, owners and regions (spec 06 §6, spec 03 §19–§20).
|
||||
fn isResource(w: *Walker, v: ValueId) bool {
|
||||
return w.types[v].isLinear();
|
||||
return w.c.module.isLinearType(w.types[v]);
|
||||
}
|
||||
|
||||
/// A non-consuming use of resource `v` (a borrow of it).
|
||||
@@ -252,10 +252,11 @@ fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void
|
||||
for (x.cases) |c| try out.append(arena, .{ .target = c.target.block, .args = c.target.args });
|
||||
try out.append(arena, .{ .target = x.default.block, .args = x.default.args });
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
inline .enum_switch, .enum_take => |x| {
|
||||
for (x.arms) |a| try out.append(arena, .{ .target = a.block, .args = &.{} });
|
||||
if (x.default) |d| try out.append(arena, .{ .target = d.block, .args = d.args });
|
||||
},
|
||||
.aggregate_unpack => |x| try out.append(arena, .{ .target = x.next, .args = &.{} }),
|
||||
.cap_narrow => |x| {
|
||||
try out.append(arena, .{ .target = x.ok, .args = &.{} });
|
||||
try out.append(arena, .{ .target = x.err, .args = &.{} });
|
||||
@@ -264,7 +265,7 @@ fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void
|
||||
try out.append(arena, .{ .target = ok, .args = &.{} });
|
||||
try out.append(arena, .{ .target = x.err.?, .args = &.{} });
|
||||
},
|
||||
.own_new, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => {
|
||||
inline .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => {
|
||||
const t = air.allocTargets(op).?;
|
||||
try out.append(arena, .{ .target = t.ok, .args = &.{} });
|
||||
try out.append(arena, .{ .target = t.err, .args = &.{} });
|
||||
@@ -302,7 +303,7 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
.parent = null,
|
||||
};
|
||||
// Linear parameters were moved in: the function owns them (spec 06 §6.3).
|
||||
if (p.ty.isLinear()) w.live[p.value] = true;
|
||||
if (c.module.isLinearType(p.ty)) w.live[p.value] = true;
|
||||
}
|
||||
|
||||
const dom = try ssa.dominators(c.arena, f);
|
||||
@@ -318,7 +319,7 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
const blk = f.blocks[b];
|
||||
// Linear block parameters own their argument from here on; the
|
||||
// parameter of a region allocation's `ok` target is a live borrow.
|
||||
for (blk.params) |p| if (p.ty.isLinear() or (p.ty.isBorrow() and w.info[p.value] != null)) {
|
||||
for (blk.params) |p| if (c.module.isLinearType(p.ty) or (p.ty.isBorrow() and w.info[p.value] != null)) {
|
||||
w.live[p.value] = true;
|
||||
w.dead[p.value] = .none;
|
||||
};
|
||||
@@ -337,19 +338,19 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
for (out.items) |e| {
|
||||
@memcpy(w.live, base);
|
||||
@memcpy(w.dead, base_dead);
|
||||
for (e.args) |a| if (types[a].isLinear()) try w.consume(a, .moved);
|
||||
for (e.args) |a| if (c.module.isLinearType(types[a])) try w.consume(a, .moved);
|
||||
// A linear parameter of the target that is still live on this
|
||||
// edge would be overwritten: that instance leaks. Parameters are
|
||||
// not part of the incoming state; they come alive in the block.
|
||||
for (f.blocks[e.target].params) |p| {
|
||||
if (p.ty.isLinear() and w.live[p.value]) try w.report("E3002", "linear value not consumed before it is redefined", p.value, null);
|
||||
if (c.module.isLinearType(p.ty) and w.live[p.value]) try w.report("E3002", "linear value not consumed before it is redefined", p.value, null);
|
||||
w.live[p.value] = false;
|
||||
}
|
||||
if (entry_state[e.target]) |expected| {
|
||||
if (!std.mem.eql(bool, expected, w.live) and !reported_join[e.target]) {
|
||||
reported_join[e.target] = true;
|
||||
for (expected, w.live, 0..) |a, x, v| if (a != x) {
|
||||
const is_res = types[v].isLinear();
|
||||
const is_res = c.module.isLinearType(types[v]);
|
||||
try c.report(if (is_res) "E3002" else "E2105", f.blocks[e.target].span, if (is_res) "resource state differs between edges into this block" else "borrow state differs between edges into this block", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "block", .value = .{ .str = f.blocks[e.target].name } },
|
||||
@@ -433,7 +434,40 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
w.live[r] = true;
|
||||
};
|
||||
},
|
||||
.own_free, .own_take => |v| try w.consume(v, .closed),
|
||||
inline .struct_make, .enum_make, .array_make => |x| {
|
||||
for (x.args) |v| if (w.isResource(v)) try w.consume(v, .moved);
|
||||
if (inst.result) |r| {
|
||||
if (w.isResource(r)) w.live[r] = true;
|
||||
}
|
||||
},
|
||||
.struct_get => |x| {
|
||||
if (w.isResource(x.a)) {
|
||||
_ = try w.resourceUsable(x.a);
|
||||
try w.access(x.a, &.{.{ .field = x.field }}, .shared, null, x.a);
|
||||
}
|
||||
},
|
||||
.aggregate_unpack => |x| {
|
||||
if (w.isResource(x.value)) try w.consume(x.value, .moved);
|
||||
},
|
||||
.enum_take => |x| {
|
||||
if (w.isResource(x.value)) try w.consume(x.value, .moved);
|
||||
},
|
||||
.own_pack => |x| {
|
||||
if (w.isResource(x.value)) try w.consume(x.value, .moved);
|
||||
},
|
||||
.value_swap => |x| {
|
||||
try w.useThrough(x.borrow, .exclusive);
|
||||
if (w.isResource(x.value)) try w.consume(x.value, .moved);
|
||||
if (inst.result) |r| {
|
||||
if (w.isResource(r)) w.live[r] = true;
|
||||
}
|
||||
},
|
||||
.own_free, .own_take => |v| {
|
||||
try w.consume(v, .closed);
|
||||
if (inst.result) |r| {
|
||||
if (w.isResource(r)) w.live[r] = true;
|
||||
}
|
||||
},
|
||||
.task_scope_begin => w.live[inst.result.?] = true,
|
||||
.task_spawn => |x| {
|
||||
try noWaitUnderLock(w, "task.spawn");
|
||||
|
||||
@@ -87,6 +87,7 @@ pub fn check(c: *verifier.Context) !void {
|
||||
for (b.insts) |inst| switch (inst.op) {
|
||||
.call => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
||||
.struct_make => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
||||
.array_make => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
||||
.enum_make => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
||||
.@"switch" => |x| try limit(c, inst.span, "cases per switch", Limits.cases, x.cases.len),
|
||||
else => {},
|
||||
@@ -150,7 +151,7 @@ fn checkValueSizes(c: *verifier.Context) !void {
|
||||
for (b.params) |p| try v.visit(b.span, p.ty);
|
||||
for (b.insts) |inst| switch (inst.op) {
|
||||
.local_alloc => |t| try v.visit(inst.span, t),
|
||||
.array_splat => |x| try v.visit(inst.span, x.ty),
|
||||
inline .array_splat, .array_make, .aggregate_unpack, .own_pack => |x| try v.visit(inst.span, x.ty),
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
|
||||
+14
-1
@@ -53,7 +53,8 @@ fn checkTerminator(c: *Context, f: air.Function, inst: air.Inst) !void {
|
||||
}
|
||||
try arity(c, f, inst.span, x.default);
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
inline .enum_switch, .enum_take => |x| {
|
||||
if (inst.op == .enum_take and x.default != null) try invalid(c, f, inst.span, "enum.take cannot have a default", null);
|
||||
const m = c.module;
|
||||
for (x.arms, 0..) |arm, i| {
|
||||
for (x.arms[0..i]) |prev| if (prev.variant == arm.variant)
|
||||
@@ -74,6 +75,18 @@ fn checkTerminator(c: *Context, f: air.Function, inst: air.Inst) !void {
|
||||
}
|
||||
}
|
||||
},
|
||||
.aggregate_unpack => |x| {
|
||||
const n: usize = switch (x.ty) {
|
||||
.array => |a| @intCast(a.len),
|
||||
.named => |i| if (c.module.types[i].kind == .@"struct") c.module.types[i].kind.@"struct".len else 0,
|
||||
else => 0,
|
||||
};
|
||||
try paramCount(c, f, inst.span, x.next, n, "unpack target must receive every component");
|
||||
},
|
||||
.own_pack => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 1, "own.pack success takes owner");
|
||||
try paramCount(c, f, inst.span, x.err, 2, "own.pack failure takes error and original value");
|
||||
},
|
||||
// `ok` receives the narrowed capability, `err` the `cap_error` (spec 05 §10.4).
|
||||
.cap_narrow => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 1, "cap.narrow `ok` target takes exactly one parameter");
|
||||
|
||||
@@ -57,7 +57,7 @@ pub fn check(c: *Context) !void {
|
||||
.atomic_store => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
|
||||
.atomic_rmw => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
|
||||
.atomic_cmpxchg => |x| try needAtomic(c, f, inst, x.success != .seq_cst or x.failure != .seq_cst),
|
||||
.own_new, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => if (!contains(f.effects, "mem.alloc")) {
|
||||
.own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => if (!contains(f.effects, "mem.alloc")) {
|
||||
try c.report("E3101", inst.span, "allocation requires the effect mem.alloc", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "effect", .value = .{ .str = "mem.alloc" } },
|
||||
|
||||
+75
-13
@@ -23,6 +23,10 @@ pub fn check(c: *Context) !void {
|
||||
|
||||
fn checkDeclarations(c: *Context) !void {
|
||||
const m = c.module;
|
||||
if (m.declares("linear_aggregates")) {
|
||||
try checkFiniteLayouts(c);
|
||||
if (c.diags.items.len != 0) return;
|
||||
}
|
||||
for (m.types) |d| switch (d.kind) {
|
||||
.@"struct" => |fs| for (fs) |fld| try wellFormed(c, d.span, fld.ty, .member, d.name),
|
||||
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| try wellFormed(c, d.span, t, .member, d.name),
|
||||
@@ -44,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),
|
||||
.array_splat => |x| try wellFormed(c, inst.span, x.ty, .member, f.name),
|
||||
inline .array_splat, .array_make, .aggregate_unpack, .own_pack => |x| try wellFormed(c, inst.span, x.ty, .param, f.name),
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
@@ -81,6 +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.*),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
@@ -98,9 +103,11 @@ 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, .island, .frozen, .arc, .atomic => pos != .member,
|
||||
.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"),
|
||||
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", &.{});
|
||||
// An atomic cell holds an AtomicScalar (spec 04 §23).
|
||||
if (t == .atomic and !isAtomicScalar(t.atomic.*)) try c.report("E1001", span, "atomic<T> needs bool or an integer of at most 64 bits", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
@@ -269,7 +276,7 @@ fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
if (reg.fallible or reg.results.len == 0) break :blk .void;
|
||||
break :blk intrinsicResult(reg.results[0]);
|
||||
},
|
||||
.array_splat => |x| x.ty,
|
||||
inline .array_splat, .array_make => |x| x.ty,
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
@@ -595,6 +602,10 @@ fn checkSelect(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "select")) !voi
|
||||
_ = f;
|
||||
}
|
||||
|
||||
pub fn complete(t: Type) bool {
|
||||
return t != .void and t != .never and t != .local and !t.isBorrow() and !t.isTaskBound() and t != .guard;
|
||||
}
|
||||
|
||||
fn boxed(arena: std.mem.Allocator, t: Type) !*const Type {
|
||||
const b = try arena.create(Type);
|
||||
b.* = t;
|
||||
@@ -612,6 +623,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.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,
|
||||
.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,
|
||||
.slice_sub => |x| fc.ty[x.slice] orelse return null,
|
||||
@@ -632,9 +644,11 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
};
|
||||
const invalid: Type = .void;
|
||||
return switch (op) {
|
||||
.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) },
|
||||
.own => |inner| .{ .ref = inner },
|
||||
.own_array => |inner| .{ .slice = inner },
|
||||
@@ -643,6 +657,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) },
|
||||
.own => |inner| .{ .mut = inner },
|
||||
.own_array => |inner| .{ .slice_mut = inner },
|
||||
@@ -771,11 +786,12 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
const span = inst.span;
|
||||
switch (inst.op) {
|
||||
.@"const", .trap => {},
|
||||
.copy => |v| if (!fc.typeOf(v).isValue()) try fc.c.report("E1001", span, "only value types can be copied", &.{
|
||||
.copy => |v| if (!fc.typeOf(v).isValue() or m.isLinearType(fc.typeOf(v))) try fc.c.report("E1001", span, "only value types can be copied", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(fc.typeOf(v)) } },
|
||||
}),
|
||||
.local_alloc, .slice_len => {},
|
||||
.local_alloc => |t| if (m.isLinearType(t)) try fc.c.report("E1001", span, "frame storage requires Copy content", &.{}),
|
||||
.slice_len => {},
|
||||
.local_store => |x| {
|
||||
const t = fc.typeOf(x.local);
|
||||
if (t != .local) return fc.kindError(span, x.local, "local<T>");
|
||||
@@ -789,7 +805,9 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(t.local.*) } },
|
||||
});
|
||||
},
|
||||
.local_load => |v| if (fc.typeOf(v) != .local) try fc.kindError(span, v, "local<T>"),
|
||||
.local_load => |v| {
|
||||
if (fc.typeOf(v) != .local) try fc.kindError(span, v, "local<T>");
|
||||
},
|
||||
.borrow_ref => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, a resource, ref<T>, mut<T>, slice<T> or slice_mut<T>"),
|
||||
.borrow_mut => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, a resource, mut<T> or slice_mut<T>"),
|
||||
.borrow_field => |x| if (resultOf(fc, inst) == .void) try fc.kindError(span, x.borrow, "ref or mut of the named struct"),
|
||||
@@ -797,12 +815,38 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
.load => |v| {
|
||||
if (atomicPointee(fc.typeOf(v))) return ordinaryAtomicAccess(fc, span, v, "atomic.load");
|
||||
if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<T> or mut<T>");
|
||||
if (m.isLinearType(resultOf(fc, inst))) try fc.c.report("E1001", span, "load cannot copy linear content; exchange or unpack", &.{});
|
||||
},
|
||||
.store => |x| {
|
||||
const t = fc.typeOf(x.borrow);
|
||||
if (atomicPointee(t)) return ordinaryAtomicAccess(fc, span, x.borrow, "atomic.store");
|
||||
if (t != .mut) return fc.kindError(span, x.borrow, "mut<T>");
|
||||
try fc.expectValue(span, x.value, t.mut.*);
|
||||
if (m.isLinearType(t.mut.*)) try fc.c.report("E1001", span, "store cannot overwrite linear content; use value.swap", &.{});
|
||||
},
|
||||
.value_swap => |x| {
|
||||
const t = fc.typeOf(x.borrow);
|
||||
if (atomicPointee(t)) return ordinaryAtomicAccess(fc, span, x.borrow, "atomic operations");
|
||||
if (t != .mut) return fc.kindError(span, x.borrow, "mut<T>");
|
||||
try fc.expectValue(span, x.value, t.mut.*);
|
||||
if (!complete(t.mut.*)) try fc.kindError(span, x.value, "a complete value");
|
||||
},
|
||||
.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", &.{});
|
||||
for (x.args) |arg| try fc.expectValue(span, arg, x.ty.array.elem);
|
||||
},
|
||||
.aggregate_unpack => |x| {
|
||||
try fc.expectValue(span, x.value, x.ty);
|
||||
switch (x.ty) {
|
||||
.array => |a| for (f.blocks[x.next].params) |p| {
|
||||
if (!p.ty.eql(a.elem)) try fc.mismatch(span, try fc.c.valueName(f, p.value), a.elem, p.ty);
|
||||
},
|
||||
.named => |i| if (m.types[i].kind == .@"struct") {
|
||||
for (f.blocks[x.next].params, m.types[i].kind.@"struct") |p, field| if (!p.ty.eql(field.ty)) try fc.mismatch(span, try fc.c.valueName(f, p.value), field.ty, p.ty);
|
||||
} else try fc.c.report("E1001", span, "unpack requires a struct or array", &.{}),
|
||||
else => try fc.c.report("E1001", span, "unpack requires a struct or array", &.{}),
|
||||
}
|
||||
},
|
||||
.array_splat => |x| {
|
||||
if (x.ty != .array) return fc.c.report("E1001", span, "array.splat needs an array type", &.{
|
||||
@@ -810,6 +854,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(x.ty) } },
|
||||
});
|
||||
try fc.expectValue(span, x.value, x.ty.array.elem);
|
||||
if (m.isLinearType(x.ty.array.elem)) try fc.c.report("E1001", span, "array.splat cannot duplicate linear elements", &.{});
|
||||
},
|
||||
.borrow_index => |x| {
|
||||
if (resultOf(fc, inst) == .void) return fc.kindError(span, x.borrow, "a borrow of an array, or a slice");
|
||||
@@ -827,6 +872,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
},
|
||||
.slice_copy => |x| {
|
||||
const d = fc.typeOf(x.dst);
|
||||
if (d == .slice_mut and m.isLinearType(d.inner().?)) return fc.c.report("E1001", span, "slice.copy cannot copy linear elements", &.{});
|
||||
if (d != .slice_mut) return fc.kindError(span, x.dst, "slice_mut<T>");
|
||||
const s = fc.typeOf(x.src);
|
||||
if (!s.isSlice() or !s.inner().?.eql(d.inner().?)) return fc.kindError(span, x.src, try std.fmt.allocPrint(fc.c.arena, "slice<{s}> or slice_mut<{s}>", .{ try fc.c.typeName(d.inner().?), try fc.c.typeName(d.inner().?) }));
|
||||
@@ -859,7 +905,10 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
for (fields, tys) |fld, *t| t.* = fld.ty;
|
||||
try fc.expectArgs(span, x.args, tys);
|
||||
},
|
||||
.struct_get => |x| try fc.expectValue(span, x.a, .{ .named = x.decl }),
|
||||
.struct_get => |x| {
|
||||
try fc.expectValue(span, x.a, .{ .named = x.decl });
|
||||
if (m.isLinearType(m.types[x.decl].kind.@"struct"[x.field].ty)) try fc.c.report("E1001", span, "linear fields require whole unpack or borrowed exchange", &.{});
|
||||
},
|
||||
.enum_make => |x| try fc.expectArgs(span, x.args, m.types[x.decl].kind.@"enum"[x.variant].payload),
|
||||
.call => |x| {
|
||||
const callee = m.functions[x.callee];
|
||||
@@ -889,7 +938,9 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
for (x.cases) |case| try fc.expectTarget(case.span, case.target);
|
||||
try fc.expectTarget(span, x.default);
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
inline .enum_switch, .enum_take => |x| {
|
||||
if (inst.op == .enum_take and x.ty != .named) try fc.c.report("E1001", span, "enum.take requires a declared enum", &.{});
|
||||
if (inst.op == .enum_switch and m.isLinearType(x.ty)) try fc.c.report("E1001", span, "linear enums require enum.take", &.{});
|
||||
try fc.expectValue(span, x.value, x.ty);
|
||||
for (x.arms) |arm| {
|
||||
const params = f.blocks[arm.block].params;
|
||||
@@ -900,21 +951,32 @@ 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_pack => |x| {
|
||||
try fc.expectAllocCap(span, x.cap);
|
||||
try fc.expectValue(span, x.value, x.ty);
|
||||
if (!complete(x.ty)) return fc.kindError(span, x.value, "a complete value");
|
||||
try fc.expectParams(span, x.ok, &.{.{ .own = try boxed(fc.c.arena, x.ty) }});
|
||||
try fc.expectParams(span, x.err, &.{ .{ .builtin = .alloc_error }, x.ty });
|
||||
},
|
||||
.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");
|
||||
if (!t.isValue() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy 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");
|
||||
if (!t.isValue() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy 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>"),
|
||||
.own_free => |v| {
|
||||
const t = fc.typeOf(v);
|
||||
if (t != .own and t != .own_array) return fc.kindError(span, v, "own<T> or own_array<T>");
|
||||
if (m.isLinearType(t.inner().?)) try fc.c.report("E1001", span, "own.free cannot destroy linear content; use own.take", &.{});
|
||||
},
|
||||
.region_create => |x| {
|
||||
try fc.expectAllocCap(span, x.cap);
|
||||
try fc.expectOkErr(span, x.ok, x.err, .region);
|
||||
@@ -922,14 +984,14 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
.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");
|
||||
if (!t.isValue() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy 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");
|
||||
if (!t.isValue() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy 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"),
|
||||
|
||||
Reference in new issue
Block a user