Phase 3: channels, mutex, select and cancellation points (RFC 0012)
- features cancellation, channels, sync supported - AIR model/parser/printer: shield, sender<T>, receiver<T>, mutex<T>, guard<T>; task.check_cancel, task.join.cancel, task.shield.*, channel.*, select (bracketed arms), mutex.* - verifier: arities (E5002), element rules (E4001/E1001, E0006 channel_transfer), guard_transfer gating, task.wait effect (gated for task.join by cancellation), per-edge consumption with values and handles returned as block parameters, loans moved to the handle on task.join.cancel's cancelled edge, shield order (E4305), guard children (E2102), critical-section rule (E4304) - interpreter: waiting-operation rule (ready, else cancelled if observable, else wait), shield depth, bounded and rendezvous channels with FIFO offers, drain-then-close receiver, budget release at the last close, select with seeded draw among ready arms, mutex wakeups; schedule record also after a select draw - report counts task.spawn, task.wait and channel allocations - spec erratum: mutex<T> is linear (format 0 has no droppable move-only class) - 30 new conformance tests (10 run with schedules, 4 accept, 16 reject); 6000-run schedule stress and 4350 mutants without failure
This commit is contained in:
1 parent
b82bc35c5f
commit
aaf32b6853
45 files changed
+2077
-43
No files matched your search
+146
-8
@@ -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 => pos != .member,
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard => pos != .member,
|
||||
else => true,
|
||||
};
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
@@ -95,7 +95,12 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
|
||||
});
|
||||
// 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.
|
||||
if (t.inner()) |inner| try wellFormed(c, span, inner, if (t == .ref or t == .mut or t == .task) .borrowed else .member, owner);
|
||||
// Channel elements and mutex contents may be linear (spec 04 §38.1);
|
||||
// their remaining rules are the element rules of `checkElement`.
|
||||
if (t.inner()) |inner| try wellFormed(c, span, inner, switch (t) {
|
||||
.ref, .mut, .task, .sender, .receiver, .mutex, .guard => .borrowed,
|
||||
else => .member,
|
||||
}, owner);
|
||||
}
|
||||
|
||||
/// `local.alloc` results are checked separately: their own type is storage.
|
||||
@@ -191,6 +196,14 @@ const FnCtx = struct {
|
||||
for (args, params) |a, p| try fc.expectValue(span, a, p);
|
||||
}
|
||||
|
||||
/// The parameters of `block` have exactly the types `want` (counts were
|
||||
/// checked by the CFG stage).
|
||||
fn expectParams(fc: *FnCtx, span: air.Span, block: u32, want: []const Type) !void {
|
||||
for (fc.f.blocks[block].params, want) |p, t| {
|
||||
if (!p.ty.eql(t)) try fc.mismatch(span, try fc.c.valueName(fc.f, p.value), t, p.ty);
|
||||
}
|
||||
}
|
||||
|
||||
fn expectTarget(fc: *FnCtx, span: air.Span, t: air.Target) !void {
|
||||
const params = fc.f.blocks[t.block].params;
|
||||
for (t.args, params) |a, p| try fc.expectValue(span, a, p.ty);
|
||||
@@ -212,6 +225,7 @@ fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
.slice_len => .{ .int = .usize },
|
||||
.task_scope_begin => .scope,
|
||||
.task_cancelled => .bool,
|
||||
.task_shield_begin => .shield,
|
||||
.intrinsic => |x| blk: {
|
||||
const reg = air.authority.intrinsics[x.index];
|
||||
if (reg.fallible or reg.results.len == 0) break :blk .void;
|
||||
@@ -312,13 +326,13 @@ fn checkIntrinsic(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "intrinsic")
|
||||
/// `Send` (spec 04 §35): may move to another task.
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region => true,
|
||||
.own, .own_array => |inner| isSend(m, inner.*),
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => true,
|
||||
.own, .own_array, .mutex => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
.array => |a| isSend(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isSend),
|
||||
.scope, .task, .local, .void, .never => false,
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -326,8 +340,10 @@ 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,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .local, .void, .never => 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).
|
||||
.mutex => |inner| isSend(m, inner.*),
|
||||
.array => |a| isShare(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isShare),
|
||||
};
|
||||
@@ -383,6 +399,78 @@ fn checkSpawn(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "task_spawn")) !
|
||||
}
|
||||
}
|
||||
|
||||
/// Element rules of channels and mutexes (spec 04 §38.1).
|
||||
fn checkElement(fc: *FnCtx, span: air.Span, t: Type) !void {
|
||||
const m = fc.c.module;
|
||||
if (contains(m, t, .endpoint)) return fc.c.report("E0006", span, "construct belongs to a feature the module does not declare", &.{
|
||||
.{ .name = "reason", .value = .{ .str = "feature_not_declared" } },
|
||||
.{ .name = "feature", .value = .{ .str = "channel_transfer" } },
|
||||
.{ .name = "construct", .value = .{ .str = "channel endpoint inside a channel element or mutex" } },
|
||||
});
|
||||
if (contains(m, t, .forbidden)) return fc.c.report("E1001", span, "a channel element or mutex content contains no borrow, scope, task, shield or guard", &.{
|
||||
.{ .name = "function", .value = .{ .str = fc.f.name } },
|
||||
.{ .name = "type", .value = .{ .str = try fc.c.typeName(t) } },
|
||||
});
|
||||
if (!isSend(m, t)) try fc.c.report("E4001", span, "a channel element or mutex content must be Send", &.{
|
||||
.{ .name = "function", .value = .{ .str = fc.f.name } },
|
||||
.{ .name = "type", .value = .{ .str = try fc.c.typeName(t) } },
|
||||
});
|
||||
}
|
||||
|
||||
/// Whether `t` transitively contains an endpoint, or a borrow or
|
||||
/// function-bound value.
|
||||
fn contains(m: air.Module, t: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
const here = switch (what) {
|
||||
.endpoint => t.isEndpoint(),
|
||||
.forbidden => t.isBorrow() or t.isTaskBound() or t == .guard,
|
||||
};
|
||||
if (here) return true;
|
||||
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) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (contains(m, f.ty, what)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| for (vs) |v| {
|
||||
for (v.payload) |p| if (contains(m, p, what)) return true;
|
||||
} else false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// `select` (spec 04 §39): each arm is a channel operation; each target
|
||||
/// takes its own parameters, then the send values not sent on its edge.
|
||||
fn checkSelect(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "select")) !void {
|
||||
const f = fc.f;
|
||||
const a = fc.c.arena;
|
||||
const buf = try a.alloc(air.ValueId, x.arms.len);
|
||||
const vs = air.Op.selectValues(x.arms, buf);
|
||||
const v_types = try a.alloc(Type, vs.len);
|
||||
for (vs, v_types) |v, *t| t.* = fc.typeOf(v);
|
||||
for (x.arms) |arm| {
|
||||
const t = fc.typeOf(arm.endpoint);
|
||||
const want: std.meta.Tag(Type) = if (arm.value == null) .receiver else .sender;
|
||||
if (t != .mut or std.meta.activeTag(t.mut.*) != want) {
|
||||
try fc.kindError(arm.span, arm.endpoint, if (arm.value == null) "mut<receiver<T>>" else "mut<sender<T>>");
|
||||
continue;
|
||||
}
|
||||
const e = t.mut.inner().?;
|
||||
var ok_types: std.ArrayList(Type) = .empty;
|
||||
if (arm.value) |v| {
|
||||
try fc.expectValue(arm.span, v, e);
|
||||
for (vs, v_types) |w, wt| if (w != v) try ok_types.append(a, wt);
|
||||
} else {
|
||||
try ok_types.append(a, e);
|
||||
try ok_types.appendSlice(a, v_types);
|
||||
}
|
||||
try fc.expectParams(arm.span, arm.ok, ok_types.items);
|
||||
try fc.expectParams(arm.span, arm.closed, v_types);
|
||||
}
|
||||
try fc.expectParams(inst.span, x.cancelled, v_types);
|
||||
_ = f;
|
||||
}
|
||||
|
||||
fn boxed(arena: std.mem.Allocator, t: Type) !*const Type {
|
||||
const b = try arena.create(Type);
|
||||
b.* = t;
|
||||
@@ -406,6 +494,8 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.own_take => |v| fc.ty[v] orelse return null,
|
||||
.task_join => |v| fc.ty[v] orelse return null,
|
||||
.task_spawn => |x| return .{ .task = try boxed(a, m.functions[x.callee].ret) },
|
||||
.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,
|
||||
else => return null,
|
||||
};
|
||||
@@ -414,7 +504,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 => .{ .ref = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .mutex => .{ .ref = try boxed(a, t) },
|
||||
.own => |inner| .{ .ref = inner },
|
||||
.own_array => |inner| .{ .slice = inner },
|
||||
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
|
||||
@@ -422,7 +512,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 => .{ .mut = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver => .{ .mut = try boxed(a, t) },
|
||||
.own => |inner| .{ .mut = inner },
|
||||
.own_array => |inner| .{ .slice_mut = inner },
|
||||
.slice_mut => .{ .slice_mut = try boxed(a, t.inner().?) },
|
||||
@@ -455,6 +545,9 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.slice_sub, .slice_split => if (t.isSlice()) t else invalid,
|
||||
.own_take => if (t == .own) t.own.* else invalid,
|
||||
.task_join => if (t == .task) t.task.* else invalid,
|
||||
.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,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
@@ -679,6 +772,51 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
if (inst.result == null and r != .void and r != .never) try fc.c.report("E1001", span, "the result of task.join must be bound", &.{.{ .name = "function", .value = .{ .str = f.name } }});
|
||||
},
|
||||
.task_spawn => |x| try checkSpawn(fc, inst, x),
|
||||
.task_check_cancel, .task_shield_begin => {},
|
||||
.task_shield_end => |v| if (fc.typeOf(v) != .shield) try fc.kindError(span, v, "shield"),
|
||||
.task_join_cancel => |x| {
|
||||
const t = fc.typeOf(x.task);
|
||||
if (t != .task) return fc.kindError(span, x.task, "task<T>");
|
||||
try fc.expectParams(span, x.ok, &.{t.task.*});
|
||||
try fc.expectParams(span, x.cancelled, &.{t});
|
||||
},
|
||||
.channel_make => |x| {
|
||||
try fc.expectValue(span, x.cap, .{ .int = .usize });
|
||||
try fc.expectAllocCap(span, x.mem);
|
||||
try checkElement(fc, span, x.elem);
|
||||
const e = try boxed(fc.c.arena, x.elem);
|
||||
try fc.expectParams(span, x.ok, &.{ .{ .sender = e }, .{ .receiver = e } });
|
||||
try fc.expectParams(span, x.err, &.{.{ .builtin = .alloc_error }});
|
||||
},
|
||||
.channel_dup => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<sender<T>>"),
|
||||
.channel_send => |x| {
|
||||
const t = fc.typeOf(x.sender);
|
||||
if (t != .mut or t.mut.* != .sender) return fc.kindError(span, x.sender, "mut<sender<T>>");
|
||||
const e = t.mut.sender.*;
|
||||
try fc.expectValue(span, x.value, e);
|
||||
try fc.expectParams(span, x.closed, &.{e});
|
||||
try fc.expectParams(span, x.cancelled, &.{e});
|
||||
},
|
||||
.channel_recv => |x| {
|
||||
const t = fc.typeOf(x.receiver);
|
||||
if (t != .mut or t.mut.* != .receiver) return fc.kindError(span, x.receiver, "mut<receiver<T>>");
|
||||
try fc.expectParams(span, x.ok, &.{t.mut.receiver.*});
|
||||
},
|
||||
.channel_close_sender => |v| if (fc.typeOf(v) != .sender) try fc.kindError(span, v, "sender<T>"),
|
||||
.channel_close_receiver => |x| {
|
||||
const t = fc.typeOf(x.receiver);
|
||||
if (t != .receiver) return fc.kindError(span, x.receiver, "receiver<T>");
|
||||
try fc.expectParams(span, x.pending, &.{t});
|
||||
},
|
||||
.select => |x| try checkSelect(fc, inst, x),
|
||||
.mutex_make => |v| try checkElement(fc, span, fc.typeOf(v)),
|
||||
.mutex_take => |v| if (fc.typeOf(v) != .mutex) try fc.kindError(span, v, "mutex<T>"),
|
||||
.mutex_lock => |x| {
|
||||
const t = fc.typeOf(x.mutex);
|
||||
if (t != .ref or t.ref.* != .mutex) return fc.kindError(span, x.mutex, "ref<mutex<T>>");
|
||||
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>"),
|
||||
.cap_narrow => |x| {
|
||||
const t = fc.typeOf(x.cap);
|
||||
if (t != .cap) return fc.kindError(span, x.cap, "cap<K>");
|
||||
|
||||
Reference in new issue
Block a user