air: query cache-free nominal properties without recursion
This commit is contained in:
1 parent
897f538840
commit
35635603f9
9 files changed
+539
-229
No files matched your search
+432
-172
@@ -64,15 +64,15 @@ fn dependencies(a: std.mem.Allocator, reverse: []std.ArrayList(u32), parent: u32
|
||||
|
||||
fn accumulate(m: air.Module, out: *air.NominalProperties, t: Type, stats: ?*Stats) void {
|
||||
if (stats) |s| s.fields += 1;
|
||||
out.linear = out.linear or m.isLinearType(t);
|
||||
out.copy = out.copy and isCopy(m, t);
|
||||
out.send = out.send and isSend(m, t);
|
||||
out.share = out.share and isShare(m, t);
|
||||
out.island_safe = out.island_safe and isIslandSafe(m, t);
|
||||
out.local = out.local or isLocalType(m, t);
|
||||
out.forbidden = out.forbidden or contains(m, t, .forbidden);
|
||||
out.endpoint = out.endpoint or contains(m, t, .endpoint);
|
||||
out.zeroable = out.zeroable and isZeroable(m, t);
|
||||
out.linear = out.linear or prepared.isLinearType(m, t);
|
||||
out.copy = out.copy and prepared.isCopy(m, t);
|
||||
out.send = out.send and prepared.isSend(m, t);
|
||||
out.share = out.share and prepared.isShare(m, t);
|
||||
out.island_safe = out.island_safe and prepared.isIslandSafe(m, t);
|
||||
out.local = out.local or prepared.isLocalType(m, t);
|
||||
out.forbidden = out.forbidden or prepared.contains(m, t, .forbidden);
|
||||
out.endpoint = out.endpoint or prepared.contains(m, t, .endpoint);
|
||||
out.zeroable = out.zeroable and prepared.isZeroable(m, t);
|
||||
out.height = @max(out.height, @min(65, @as(u8, height(m, t)) + 1));
|
||||
out.source_forbidden_depth = @min(out.source_forbidden_depth, @min(34, @as(u8, sourceForbiddenDepth(m, t)) + 1));
|
||||
}
|
||||
@@ -128,168 +128,361 @@ pub fn bounded(m: air.Module, t: Type, depth: usize) bool {
|
||||
return if (t.inner()) |inner| bounded(m, inner, depth + 1) else true;
|
||||
}
|
||||
|
||||
pub fn isZeroable(m: air.Module, initial: Type) bool {
|
||||
/// Structural queries accept resolved declaration indices. Cached reads and
|
||||
/// anonymous leaves allocate nothing; a cache-free nominal query prepares
|
||||
/// immutable facts in temporary host storage and propagates allocation errors.
|
||||
pub const Property = enum { linear, zeroable, copy, local, send, share, island_safe, endpoint, forbidden };
|
||||
const Named = struct { index: u32, property: Property };
|
||||
const Leaf = union(enum) { known: bool, named: Named };
|
||||
|
||||
fn classify(initial: Type, initial_property: Property) Leaf {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].zeroable else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!isZeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isCopy(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .iref => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].copy else aggregateAll(m, i, isCopy),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isLocalType(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => true,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| {
|
||||
t = inner.*;
|
||||
continue;
|
||||
},
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].local else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (isLocalType(m, f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (isLocalType(m, p)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return sendShare(m, t, .send);
|
||||
}
|
||||
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return sendShare(m, t, .share);
|
||||
}
|
||||
|
||||
/// Borrow and mutex edges can change the property being queried. Keep that
|
||||
/// state explicit so alternating ref/mutex chains do not grow the native stack.
|
||||
fn sendShare(m: air.Module, initial: Type, initial_mode: enum { send, share }) bool {
|
||||
var t = initial;
|
||||
var mode = initial_mode;
|
||||
while (true) {
|
||||
if (mode == .send) switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => return true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => return true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => return true,
|
||||
var property = initial_property;
|
||||
while (true) switch (property) {
|
||||
.linear => {
|
||||
if (t.isLinear()) return .{ .known = true };
|
||||
switch (t) {
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return .{ .named = .{ .index = i, .property = property } },
|
||||
else => return .{ .known = false },
|
||||
}
|
||||
},
|
||||
.zeroable => switch (t) {
|
||||
.int, .bool => return .{ .known = true },
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return .{ .named = .{ .index = i, .property = property } },
|
||||
else => return .{ .known = false },
|
||||
},
|
||||
.copy => switch (t) {
|
||||
.int, .bool, .builtin, .cap, .iref => return .{ .known = true },
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return .{ .named = .{ .index = i, .property = property } },
|
||||
else => return .{ .known = false },
|
||||
},
|
||||
.local => switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => return .{ .known = true },
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| t = inner.*,
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return .{ .named = .{ .index = i, .property = property } },
|
||||
else => return .{ .known = false },
|
||||
},
|
||||
.send => switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver, .island, .frozen, .arc, .iref, .atomic => return .{ .known = true },
|
||||
.own, .own_array, .buffer, .mutex, .mut, .slice_mut => |inner| t = inner.*,
|
||||
.ref, .slice => |inner| {
|
||||
t = inner.*;
|
||||
mode = .share;
|
||||
property = .share;
|
||||
},
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return if (m.nominal_properties) |p| p[i].send else aggregateAll(m, i, isSend),
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => return false,
|
||||
} else switch (t) {
|
||||
.int, .bool, .builtin, .cap => return true,
|
||||
.frozen, .arc, .iref, .atomic => return true,
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => return false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => return false,
|
||||
.named => |i| return .{ .named = .{ .index = i, .property = property } },
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => return .{ .known = false },
|
||||
},
|
||||
.share => switch (t) {
|
||||
.int, .bool, .builtin, .cap, .frozen, .arc, .iref, .atomic => return .{ .known = true },
|
||||
.own, .own_array, .buffer, .ref, .slice => |inner| t = inner.*,
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
.mutex => |inner| {
|
||||
t = inner.*;
|
||||
mode = .send;
|
||||
property = .send;
|
||||
},
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return if (m.nominal_properties) |p| p[i].share else aggregateAll(m, i, isShare),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isIslandSafe(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .iref => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
.named => |i| return .{ .named = .{ .index = i, .property = property } },
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never, .island => return .{ .known = false },
|
||||
},
|
||||
.island_safe => switch (t) {
|
||||
.int, .bool, .builtin, .iref => return .{ .known = true },
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return .{ .named = .{ .index = i, .property = property } },
|
||||
else => return .{ .known = false },
|
||||
},
|
||||
.endpoint, .forbidden => {
|
||||
const here = if (property == .endpoint) t.isEndpoint() else t.isBorrow() or t.isTaskBound() or t == .guard;
|
||||
if (here) return .{ .known = true };
|
||||
// An iref<T> holds no T (spec 03 §22).
|
||||
if (t == .iref) return .{ .known = false };
|
||||
if (t.inner()) |inner| if (t != .ref and t != .mut and t != .slice and t != .slice_mut) {
|
||||
t = inner;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].island_safe else aggregateAll(m, i, isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub 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| {
|
||||
if (!pred(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| {
|
||||
for (vs) |v| for (v.payload) |p| if (!pred(m, p)) return false;
|
||||
return true;
|
||||
};
|
||||
return if (t == .named) .{ .named = .{ .index = t.named, .property = property } } else .{ .known = false };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn contains(m: air.Module, initial: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
const here = switch (what) {
|
||||
.endpoint => t.isEndpoint(),
|
||||
.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) {
|
||||
t = inner;
|
||||
continue;
|
||||
};
|
||||
return switch (t) {
|
||||
.named => |i| if (m.nominal_properties) |p| @field(p[i], @tagName(what)) else 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,
|
||||
fn fact(m: air.Module, n: Named) bool {
|
||||
return switch (n.property) {
|
||||
inline else => |p| @field(m.nominal_properties.?[n.index], @tagName(p)),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn query(a: std.mem.Allocator, m: air.Module, t: Type, property: Property) std.mem.Allocator.Error!bool {
|
||||
return switch (classify(t, property)) {
|
||||
.known => |value| value,
|
||||
.named => |n| blk: {
|
||||
if (m.nominal_properties != null) break :blk fact(m, n);
|
||||
// An enum never has a zero value; its payloads need no query.
|
||||
if (n.property == .zeroable and m.types[n.index].kind == .@"enum") break :blk false;
|
||||
var scratch: std.heap.ArenaAllocator = .init(a);
|
||||
defer scratch.deinit();
|
||||
break :blk fact(try prepare(scratch.allocator(), m, null), n);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isLinearType(a: std.mem.Allocator, m: air.Module, t: Type) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, .linear);
|
||||
}
|
||||
pub fn isZeroable(a: std.mem.Allocator, m: air.Module, t: Type) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, .zeroable);
|
||||
}
|
||||
pub fn isCopy(a: std.mem.Allocator, m: air.Module, t: Type) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, .copy);
|
||||
}
|
||||
pub fn isLocalType(a: std.mem.Allocator, m: air.Module, t: Type) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, .local);
|
||||
}
|
||||
pub fn isSend(a: std.mem.Allocator, m: air.Module, t: Type) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, .send);
|
||||
}
|
||||
pub fn isShare(a: std.mem.Allocator, m: air.Module, t: Type) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, .share);
|
||||
}
|
||||
pub fn isIslandSafe(a: std.mem.Allocator, m: air.Module, t: Type) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, .island_safe);
|
||||
}
|
||||
pub fn contains(a: std.mem.Allocator, m: air.Module, t: Type, comptime what: enum { endpoint, forbidden }) std.mem.Allocator.Error!bool {
|
||||
return query(a, m, t, switch (what) {
|
||||
.endpoint => .endpoint,
|
||||
.forbidden => .forbidden,
|
||||
});
|
||||
}
|
||||
|
||||
/// Infallible queries for prepared modules (or anonymous terminal paths).
|
||||
/// Reaching a named declaration requires valid immutable nominal_properties.
|
||||
pub const prepared = struct {
|
||||
fn value(m: air.Module, t: Type, property: Property) bool {
|
||||
return switch (classify(t, property)) {
|
||||
.known => |v| v,
|
||||
.named => |n| fact(m, n),
|
||||
};
|
||||
}
|
||||
}
|
||||
pub fn isLinearType(m: air.Module, t: Type) bool {
|
||||
return value(m, t, .linear);
|
||||
}
|
||||
pub fn isZeroable(m: air.Module, t: Type) bool {
|
||||
return value(m, t, .zeroable);
|
||||
}
|
||||
pub fn isCopy(m: air.Module, t: Type) bool {
|
||||
return value(m, t, .copy);
|
||||
}
|
||||
pub fn isLocalType(m: air.Module, t: Type) bool {
|
||||
return value(m, t, .local);
|
||||
}
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return value(m, t, .send);
|
||||
}
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return value(m, t, .share);
|
||||
}
|
||||
pub fn isIslandSafe(m: air.Module, t: Type) bool {
|
||||
return value(m, t, .island_safe);
|
||||
}
|
||||
pub fn contains(m: air.Module, t: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
return value(m, t, switch (what) {
|
||||
.endpoint => .endpoint,
|
||||
.forbidden => .forbidden,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Independent pre-repair derivation for shallow, acyclic test graphs.
|
||||
const Reference = struct {
|
||||
/// Whole-value linearity; well-formed nominal ownership graphs are acyclic.
|
||||
pub fn isLinearType(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
if (t.isLinear()) return true;
|
||||
return switch (t) {
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].linear else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (Reference.isLinearType(m, f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (Reference.isLinearType(m, p)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isZeroable(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].zeroable else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!Reference.isZeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isCopy(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .iref => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].copy else Reference.aggregateAll(m, i, Reference.isCopy),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isLocalType(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => true,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| {
|
||||
t = inner.*;
|
||||
continue;
|
||||
},
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].local else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (Reference.isLocalType(m, f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (Reference.isLocalType(m, p)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return Reference.sendShare(m, t, .send);
|
||||
}
|
||||
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return Reference.sendShare(m, t, .share);
|
||||
}
|
||||
|
||||
/// Borrow and mutex edges can change the property being queried. Keep that
|
||||
/// state explicit so alternating ref/mutex chains do not grow the native stack.
|
||||
fn sendShare(m: air.Module, initial: Type, initial_mode: enum { send, share }) bool {
|
||||
var t = initial;
|
||||
var mode = initial_mode;
|
||||
while (true) {
|
||||
if (mode == .send) switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => return true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => return true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => return true,
|
||||
.own, .own_array, .buffer, .mutex, .mut, .slice_mut => |inner| t = inner.*,
|
||||
.ref, .slice => |inner| {
|
||||
t = inner.*;
|
||||
mode = .share;
|
||||
},
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return if (m.nominal_properties) |p| p[i].send else Reference.aggregateAll(m, i, Reference.isSend),
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => return false,
|
||||
} else switch (t) {
|
||||
.int, .bool, .builtin, .cap => return true,
|
||||
.frozen, .arc, .iref, .atomic => return true,
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => return false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => return false,
|
||||
.own, .own_array, .buffer, .ref, .slice => |inner| t = inner.*,
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
.mutex => |inner| {
|
||||
t = inner.*;
|
||||
mode = .send;
|
||||
},
|
||||
.array => |a| t = a.elem,
|
||||
.named => |i| return if (m.nominal_properties) |p| p[i].share else Reference.aggregateAll(m, i, Reference.isShare),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isIslandSafe(m: air.Module, initial: Type) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .iref => true,
|
||||
.array => |a| {
|
||||
t = a.elem;
|
||||
continue;
|
||||
},
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].island_safe else Reference.aggregateAll(m, i, Reference.isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub 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| {
|
||||
if (!pred(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| {
|
||||
for (vs) |v| for (v.payload) |p| if (!pred(m, p)) return false;
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn contains(m: air.Module, initial: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
const here = switch (what) {
|
||||
.endpoint => t.isEndpoint(),
|
||||
.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) {
|
||||
t = inner;
|
||||
continue;
|
||||
};
|
||||
return switch (t) {
|
||||
.named => |i| if (m.nominal_properties) |p| @field(p[i], @tagName(what)) else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (Reference.contains(m, f.ty, what)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| for (vs) |v| {
|
||||
for (v.payload) |p| if (Reference.contains(m, p, what)) return true;
|
||||
} else false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
test "shared nominal diamonds have counted bounded property queries" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
@@ -308,15 +501,80 @@ test "shared nominal diamonds have counted bounded property queries" {
|
||||
try std.testing.expectEqual(@as(usize, 129), stats.fields);
|
||||
for (0..65) |i| {
|
||||
const t: Type = .{ .named = @intCast(i) };
|
||||
try std.testing.expect(!cached.isLinearType(t));
|
||||
try std.testing.expect(isCopy(cached, t) and isSend(cached, t) and isShare(cached, t) and isIslandSafe(cached, t) and isZeroable(cached, t));
|
||||
try std.testing.expect(!isLocalType(cached, t) and !contains(cached, t, .endpoint) and !contains(cached, t, .forbidden));
|
||||
try std.testing.expect(!try cached.isLinearType(a, t));
|
||||
try std.testing.expect(try isCopy(a, cached, t) and try isSend(a, cached, t) and try isShare(a, cached, t) and try isIslandSafe(a, cached, t) and try isZeroable(a, cached, t));
|
||||
try std.testing.expect(!try isLocalType(a, cached, t) and !try contains(a, cached, t, .endpoint) and !try contains(a, cached, t, .forbidden));
|
||||
}
|
||||
try std.testing.expect(!bounded(cached, .{ .named = 0 }, 0));
|
||||
try std.testing.expect(!bounded(cached, .{ .named = 1 }, 0));
|
||||
try std.testing.expect(bounded(cached, .{ .named = 2 }, 0));
|
||||
}
|
||||
|
||||
test "cache-free nominal property queries handle deep records and independent terminal facts" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const byte: Type = .{ .int = .u8 };
|
||||
const leaves = [_]Type{ byte, .scope, .{ .sender = &byte } };
|
||||
// Property order: linear, zeroable, Copy, Local, Send, Share,
|
||||
// IslandSafe, endpoint and forbidden content.
|
||||
const expected = [_][9]bool{
|
||||
.{ false, true, true, false, true, true, true, false, false },
|
||||
.{ true, false, false, true, false, false, false, false, true },
|
||||
.{ true, false, false, false, true, false, false, true, false },
|
||||
};
|
||||
const ds = try a.alloc(air.TypeDecl, 32768);
|
||||
const fields = try a.alloc(air.Field, ds.len * 2);
|
||||
for (ds, 0..) |*d, i| {
|
||||
fields[2 * i] = .{ .name = "child", .ty = if (i + 1 == ds.len) byte else .{ .named = @intCast(i + 1) } };
|
||||
fields[2 * i + 1] = .{ .name = "flag", .ty = .bool };
|
||||
d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = fields[2 * i ..][0..2] } };
|
||||
}
|
||||
const m: air.Module = .{ .name = "deep", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
|
||||
const ty: Type = .{ .named = 0 };
|
||||
for (leaves, expected) |leaf, row| {
|
||||
fields[fields.len - 2].ty = leaf;
|
||||
inline for (std.meta.tags(Property), 0..) |property, column|
|
||||
try std.testing.expectEqual(row[column], try query(std.testing.allocator, m, ty, property));
|
||||
try std.testing.expect(m.nominal_properties == null);
|
||||
}
|
||||
}
|
||||
|
||||
test "nominal query metadata failures are host errors and prepared or terminal reads allocate nothing" {
|
||||
const byte: Type = .{ .int = .u8 };
|
||||
const ds = [_]air.TypeDecl{
|
||||
.{ .name = "Outer", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "inner", .ty = .{ .named = 1 } }} } },
|
||||
.{ .name = "Inner", .span = .{ .line = 2, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "owner", .ty = .{ .own = &byte } }} } },
|
||||
.{ .name = "Choice", .span = .{ .line = 3, .col = 1 }, .kind = .{ .@"enum" = &.{.{ .name = "empty", .payload = &.{} }} } },
|
||||
};
|
||||
const m: air.Module = .{ .name = "fail", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &ds, .functions = &.{} };
|
||||
const ty: Type = .{ .named = 0 };
|
||||
inline for (std.meta.tags(Property)) |property| {
|
||||
var fail_index: usize = 0;
|
||||
while (true) : (fail_index += 1) {
|
||||
try std.testing.expect(fail_index < 100);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
|
||||
const result = query(failing.allocator(), m, ty, property);
|
||||
if (failing.has_induced_failure) {
|
||||
try std.testing.expectError(error.OutOfMemory, result);
|
||||
} else _ = try result;
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
if (!failing.has_induced_failure) break;
|
||||
}
|
||||
}
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const cached = try prepare(scratch.allocator(), m, null);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
inline for (std.meta.tags(Property)) |property| {
|
||||
try std.testing.expectEqual(try query(std.testing.allocator, m, ty, property), try query(failing.allocator(), cached, ty, property));
|
||||
try std.testing.expectEqual(try query(std.testing.allocator, m, byte, property), try query(failing.allocator(), m, byte, property));
|
||||
}
|
||||
// Zeroable rejects an enum without inspecting its payload graph.
|
||||
try std.testing.expect(!try isZeroable(failing.allocator(), m, .{ .named = 2 }));
|
||||
try std.testing.expect(!failing.has_induced_failure);
|
||||
}
|
||||
|
||||
test "cyclic property work terminates and phantom handles stay Copy" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
@@ -327,8 +585,8 @@ test "cyclic property work terminates and phantom handles stay Copy" {
|
||||
var stats: Stats = .{};
|
||||
const cached = try prepare(a, m, &stats);
|
||||
try std.testing.expect(stats.evaluations <= 65);
|
||||
try std.testing.expect(isCopy(cached, self) and isIslandSafe(cached, self));
|
||||
try std.testing.expect(!cached.isLinearType(self) and !contains(cached, self, .forbidden));
|
||||
try std.testing.expect(try isCopy(a, cached, self) and try isIslandSafe(a, cached, self));
|
||||
try std.testing.expect(!try cached.isLinearType(a, self) and !try contains(a, cached, self, .forbidden));
|
||||
try std.testing.expect(!bounded(cached, self, 0));
|
||||
}
|
||||
|
||||
@@ -356,9 +614,11 @@ test "cached predicates match uncached derivations on mixed acyclic graphs" {
|
||||
const cached = try prepare(a, m, null);
|
||||
for (0..4) |i| {
|
||||
const t: Type = .{ .named = @intCast(i) };
|
||||
try std.testing.expectEqual(m.isLinearType(t), cached.isLinearType(t));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isLocalType, isZeroable }) |pred| try std.testing.expectEqual(pred(m, t), pred(cached, t));
|
||||
inline for (.{ .endpoint, .forbidden }) |what| try std.testing.expectEqual(contains(m, t, what), contains(cached, t, what));
|
||||
for ([_]air.Module{ m, cached }) |module| {
|
||||
try std.testing.expectEqual(Reference.isLinearType(m, t), try module.isLinearType(a, t));
|
||||
inline for (.{ .{ Reference.isCopy, isCopy }, .{ Reference.isSend, isSend }, .{ Reference.isShare, isShare }, .{ Reference.isIslandSafe, isIslandSafe }, .{ Reference.isLocalType, isLocalType }, .{ Reference.isZeroable, isZeroable } }) |pred| try std.testing.expectEqual(pred[0](m, t), try pred[1](a, module, t));
|
||||
inline for (.{ .endpoint, .forbidden }) |what| try std.testing.expectEqual(Reference.contains(m, t, what), try contains(a, module, t, what));
|
||||
}
|
||||
for ([_]usize{ 0, 31, 63, 64 }) |depth| try std.testing.expectEqual(bounded(m, t, depth), bounded(cached, t, depth));
|
||||
for ([_]u32{ 0, 30, 31, 32, 33 }) |depth| {
|
||||
const before = witness(m, t, depth, false);
|
||||
@@ -406,8 +666,8 @@ test "cyclic existential facts reach all predecessors without a false cache" {
|
||||
try std.testing.expect(stats.evaluations < 150);
|
||||
for (0..2) |i| {
|
||||
const t: Type = .{ .named = @intCast(i) };
|
||||
try std.testing.expect(cached.isLinearType(t) and contains(cached, t, .forbidden));
|
||||
try std.testing.expect(!isCopy(cached, t) and !isShare(cached, t));
|
||||
try std.testing.expect(try cached.isLinearType(a, t) and try contains(a, cached, t, .forbidden));
|
||||
try std.testing.expect(!try isCopy(a, cached, t) and !try isShare(a, cached, t));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -436,13 +696,13 @@ test "deep anonymous property queries and nominal preparation avoid recursion" {
|
||||
}, .functions = &.{} };
|
||||
const cached = try prepare(a, m, null);
|
||||
for ([_]air.Module{ m, cached }) |module| {
|
||||
try std.testing.expect(!module.isLinearType(plain));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isZeroable }) |pred| try std.testing.expect(pred(module, plain));
|
||||
try std.testing.expect(!isLocalType(module, plain) and !contains(module, plain, .forbidden) and !contains(module, plain, .endpoint));
|
||||
try std.testing.expect(module.isLinearType(task) and isLocalType(module, task) and contains(module, task, .forbidden));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isZeroable }) |pred| try std.testing.expect(!pred(module, task));
|
||||
try std.testing.expect(module.isLinearType(endpoint) and isSend(module, endpoint) and contains(module, endpoint, .endpoint));
|
||||
try std.testing.expect(!isLocalType(module, endpoint) and !isShare(module, endpoint) and !contains(module, endpoint, .forbidden));
|
||||
try std.testing.expect(!try module.isLinearType(a, plain));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isZeroable }) |pred| try std.testing.expect(try pred(a, module, plain));
|
||||
try std.testing.expect(!try isLocalType(a, module, plain) and !try contains(a, module, plain, .forbidden) and !try contains(a, module, plain, .endpoint));
|
||||
try std.testing.expect(try module.isLinearType(a, task) and try isLocalType(a, module, task) and try contains(a, module, task, .forbidden));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isZeroable }) |pred| try std.testing.expect(!try pred(a, module, task));
|
||||
try std.testing.expect(try module.isLinearType(a, endpoint) and try isSend(a, module, endpoint) and try contains(a, module, endpoint, .endpoint));
|
||||
try std.testing.expect(!try isLocalType(a, module, endpoint) and !try isShare(a, module, endpoint) and !try contains(a, module, endpoint, .forbidden));
|
||||
}
|
||||
for ([_]Type{ plain, task, endpoint }) |t| {
|
||||
try std.testing.expectEqual(@as(u7, 65), height(cached, t));
|
||||
@@ -483,8 +743,8 @@ test "deep Send Share transitions preserve ref and mutex modes" {
|
||||
node.* = t;
|
||||
t = if ((i % 2 == 1) == outer_ref) .{ .ref = node } else .{ .mutex = node };
|
||||
}
|
||||
try std.testing.expectEqual(outer_ref, isSend(m, t));
|
||||
try std.testing.expectEqual(outer_ref, isShare(m, t));
|
||||
try std.testing.expectEqual(outer_ref, try isSend(a, m, t));
|
||||
try std.testing.expectEqual(outer_ref, try isShare(a, m, t));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user