air: query cache-free nominal properties without recursion

This commit is contained in:
dev committed 2026-10-10 12:13:49 +03:00
1 parent 897f538840
commit 35635603f9
9 files changed
+539 -229

No files matched your search

+9 -22
View File
@@ -526,28 +526,15 @@ pub const Module = struct {
return false;
}
/// Whole-value linearity; well-formed nominal ownership graphs are acyclic.
pub fn isLinearType(m: 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 (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,
};
}
/// Whole-value linearity, with explicit host-allocation failure when a
/// resolved nominal module has no prepared property cache.
pub fn isLinearType(m: Module, a: std.mem.Allocator, t: Type) std.mem.Allocator.Error!bool {
return @import("properties.zig").isLinearType(a, m, t);
}
/// Allocation-free read: named declarations require prepared properties.
pub fn isLinearTypeCached(m: Module, t: Type) bool {
return @import("properties.zig").prepared.isLinearType(m, t);
}
/// Variant names and payloads of a declared or built-in enum type.
+432 -172
View File
@@ -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));
}
}
+4 -4
View File
@@ -616,7 +616,7 @@ const Lowerer = struct {
continue;
}
const cached = l.property_module.?;
const ok = if (std.mem.eql(u8, r.property, "Copy")) verifier_types.isCopy(cached, t) else if (std.mem.eql(u8, r.property, "Send")) verifier_types.isSend(cached, t) else if (std.mem.eql(u8, r.property, "Share")) verifier_types.isShare(cached, t) else if (std.mem.eql(u8, r.property, "Freeze")) verifier_types.isIslandSafe(cached, t) else verifier_types.isLocalType(cached, t);
const ok = if (std.mem.eql(u8, r.property, "Copy")) try verifier_types.isCopy(l.arena, cached, t) else if (std.mem.eql(u8, r.property, "Send")) try verifier_types.isSend(l.arena, cached, t) else if (std.mem.eql(u8, r.property, "Share")) try verifier_types.isShare(l.arena, cached, t) else if (std.mem.eql(u8, r.property, "Freeze")) try verifier_types.isIslandSafe(l.arena, cached, t) else try verifier_types.isLocalType(l.arena, cached, t);
if (!ok) return l.fail("E7005", r.span, "generic argument does not satisfy structural constraint", &.{
.{ .name = "parameter", .value = r.parameter },
.{ .name = "constraint", .value = r.property },
@@ -811,7 +811,7 @@ const Lowerer = struct {
.{ .name = "position", .value = "member" },
.{ .name = "type_constructor", .value = @tagName(t) },
});
if (repeated and !verifier_types.isCopy(l.property_module.?, t)) return l.fail("E7005", span, "an array repetition requires Copy content", &.{
if (repeated and !try verifier_types.isCopy(l.arena, l.property_module.?, t)) return l.fail("E7005", span, "an array repetition requires Copy content", &.{
.{ .name = "constraint", .value = "Copy" },
.{ .name = "type_constructor", .value = "array" },
});
@@ -833,7 +833,7 @@ const Lowerer = struct {
}
fn copyContent(l: *Lowerer, span: Span, ty: Ty, operation: []const u8) Error!void {
if (!verifier_types.isCopy(l.property_module.?, try l.airType(ty))) return l.fail("E7005", span, "this allocation requires Copy content", &.{
if (!try verifier_types.isCopy(l.arena, l.property_module.?, try l.airType(ty))) return l.fail("E7005", span, "this allocation requires Copy content", &.{
.{ .name = "constraint", .value = "Copy" },
.{ .name = "operation", .value = operation },
});
@@ -3414,7 +3414,7 @@ const Lowerer = struct {
const n = try l.expr(args[0], usize_ty);
try l.checkOperandTy(args[0].span, n.ty, usize_ty);
const v = try l.consuming(args[1], null);
if (!verifier_types.isCopy(l.property_module.?, try l.airType(v.ty))) return l.copyArrayError(args[1].span);
if (!try verifier_types.isCopy(l.arena, l.property_module.?, try l.airType(v.ty))) return l.copyArrayError(args[1].span);
const c = try l.expectCap(args[2], "mem.alloc");
return .{ .text = try l.fmt("own.array %{s}, %{s}, %{s}", .{ n.name, v.name, c.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.own_array, v.ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) };
}
+1 -1
View File
@@ -149,7 +149,7 @@ test "verification rebuilds caller-supplied property and size caches" {
m.nominal_properties = fake;
const result = try verifyModule(a, "t.air", m);
try std.testing.expectEqual(@as(usize, 0), result.diagnostics.len);
try std.testing.expect(!result.module.isLinearType(.{ .named = 0 }));
try std.testing.expect(!result.module.isLinearTypeCached(.{ .named = 0 }));
try std.testing.expect(result.module.accounted_sizes != null);
var large = (try air.parse.parse(a, "t.air", test_header ++ "type @P = struct { x: array<u8, 1048577> }\nfn @f() -> void\neffects []\nentry:\n ret\n")).module.?;
var sizes: @import("air/module.zig").SizeCache = .{};
+7 -6
View File
@@ -4,6 +4,7 @@
//! Borrow definitions have static provenance. Live values must agree at joins.
//! Resource relations also follow block arguments; a monotone worklist solves
//! them before the diagnostic walk, including later predecessors/backedges.
//! Runs after type verification, which prepares nominal properties and value types.
const std = @import("std");
const air = @import("../air.zig");
@@ -207,7 +208,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.c.module.isLinearType(w.types[v]);
return w.c.module.isLinearTypeCached(w.types[v]);
}
/// A non-consuming use of resource `v` (a borrow of it).
@@ -319,7 +320,7 @@ fn walker(c: *Context, f: air.Function, types: []const air.Type) !Walker {
.parent = null,
};
// Linear parameters were moved in: the function owns them (spec 06 §6.3).
if (c.module.isLinearType(p.ty)) w.live[p.value] = true;
if (c.module.isLinearTypeCached(p.ty)) w.live[p.value] = true;
}
return w;
@@ -370,7 +371,7 @@ fn walkFunction(w: *Walker, dom: ssa.Dominators, facts: ?[]provenance.State, eve
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 (c.module.isLinearType(p.ty) or (p.ty.isBorrow() and w.info[p.value] != null)) {
for (blk.params) |p| if (c.module.isLinearTypeCached(p.ty) or (p.ty.isBorrow() and w.info[p.value] != null)) {
w.live[p.value] = true;
w.dead[p.value] = .none;
};
@@ -393,19 +394,19 @@ fn walkFunction(w: *Walker, dom: ssa.Dominators, facts: ?[]provenance.State, eve
@memcpy(w.live, base);
@memcpy(w.dead, base_dead);
w.facts = try base_facts.clone(c.arena);
for (e.args) |a| if (c.module.isLinearType(types[a])) try w.consume(a, .moved);
for (e.args) |a| if (c.module.isLinearTypeCached(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 (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);
if (c.module.isLinearTypeCached(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 = c.module.isLinearType(types[v]);
const is_res = c.module.isLinearTypeCached(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 } },
+66 -24
View File
@@ -170,7 +170,7 @@ fn wellFormed(c: *Context, span: air.Span, initial: Type, initial_pos: Position,
var pos = initial_pos;
while (true) {
const ok = allowedPosition(std.meta.activeTag(t), pos, c.module.declares("linear_aggregates"));
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", &.{});
if (t == .own_array and c.module.declares("linear_aggregates") and try c.module.isLinearType(c.arena, 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 } },
@@ -184,7 +184,7 @@ fn wellFormed(c: *Context, span: air.Span, initial: Type, initial_pos: Position,
// A graph holds IslandSafe values only (spec 03 §22–§24): reported once,
// as E2111, at the type.
if (t.isGraphOwner()) {
if (!isIslandSafe(c.module, t.inner().?)) try c.report("E2111", span, "a graph holds only plain data and iref handles", &.{
if (!try isIslandSafe(c.arena, c.module, t.inner().?)) try c.report("E2111", span, "a graph holds only plain data and iref handles", &.{
.{ .name = "in", .value = .{ .str = owner } },
.{ .name = "found", .value = .{ .str = try c.typeName(t.inner().?) } },
});
@@ -214,7 +214,7 @@ fn argumentLengths(c: *Context, span: air.Span, initial: Type) !void {
}
/// `local.alloc` results are checked separately: their own type is storage.
const zeroable = air.properties.isZeroable;
const zeroable = air.properties.prepared.isZeroable;
// ---------------------------------------------------------------- functions
@@ -489,7 +489,7 @@ fn checkSpawn(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "task_spawn")) !
try fc.expectArgs(span, x.args, tys);
if (x.args.len == tys.len) for (x.args, tys) |a, pt| {
const shared = pt == .ref or pt == .slice;
const ok = if (shared) isShare(m, pt.inner().?) else isSend(m, pt);
const ok = if (shared) try isShare(fc.c.arena, m, pt.inner().?) else try isSend(fc.c.arena, m, pt);
if (!ok) try fc.c.report(if (shared) "E4002" else "E4001", span, if (shared) "data lent to a task through a shared borrow must be Share" else "a value moved or exclusively lent to a task must be Send", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "task_function", .value = .{ .str = callee.name } },
@@ -506,7 +506,7 @@ fn checkSpawn(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "task_spawn")) !
});
} else if (r.isTaskBound()) {
try fc.c.report("E1001", span, "a task result must not contain a scope or task handle", &.{.{ .name = "function", .value = .{ .str = f.name } }});
} else if (r != .void and r != .never and !isSend(m, r)) {
} else if (r != .void and r != .never and !try isSend(fc.c.arena, m, r)) {
try fc.c.report("E4001", span, "a task result must be Send", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "task_function", .value = .{ .str = callee.name } },
@@ -526,7 +526,7 @@ fn checkElement(fc: *FnCtx, span: air.Span, t: Type) !void {
.{ .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", &.{
if (!try isSend(fc.c.arena, 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) } },
});
@@ -534,7 +534,7 @@ fn checkElement(fc: *FnCtx, span: air.Span, t: Type) !void {
/// Whether `t` transitively contains an endpoint, or a borrow or
/// function-bound value.
const contains = air.properties.contains;
const contains = air.properties.prepared.contains;
/// `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.
@@ -622,7 +622,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().?) },
.named, .array => if (m.isLinearType(t)) .{ .ref = try boxed(a, t) } else invalid,
.named, .array => if (try m.isLinearType(a, t)) .{ .ref = try boxed(a, t) } else invalid,
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .buffer, .island, .frozen, .arc, .atomic => .{ .ref = try boxed(a, t) },
.own => |inner| .{ .ref = inner },
.own_array => |inner| .{ .slice = inner },
@@ -631,7 +631,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,
.named, .array => if (try m.isLinearType(a, t)) .{ .mut = try boxed(a, t) } else invalid,
.resource, .region, .task, .scope, .sender, .receiver, .buffer, .island => .{ .mut = try boxed(a, t) },
.own => |inner| .{ .mut = inner },
.own_array => |inner| .{ .slice_mut = inner },
@@ -761,11 +761,11 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
switch (inst.op) {
.@"const", .trap => {},
.error_const => |x| if (x.variant >= x.which.variants().len) try fc.c.report("E1001", span, "invalid built-in error variant", &.{}),
.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", &.{
.copy => |v| if (!fc.typeOf(v).isValue() or m.isLinearTypeCached(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 => |t| if (m.isLinearType(t)) try fc.c.report("E1001", span, "frame storage requires Copy content", &.{}),
.local_alloc => |t| if (m.isLinearTypeCached(t)) try fc.c.report("E1001", span, "frame storage requires Copy content", &.{}),
.slice_len => {},
.local_store => |x| {
const t = fc.typeOf(x.local);
@@ -790,14 +790,14 @@ 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", &.{});
if (m.isLinearTypeCached(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", &.{});
if (m.isLinearTypeCached(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);
@@ -859,7 +859,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", &.{});
if (m.isLinearTypeCached(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");
@@ -877,7 +877,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 and m.isLinearTypeCached(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().?) }));
@@ -912,7 +912,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
},
.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", &.{});
if (m.isLinearTypeCached(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| {
@@ -945,7 +945,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
},
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", &.{});
if (inst.op == .enum_switch and m.isLinearTypeCached(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;
@@ -966,21 +966,21 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
.own_new => |x| {
try fc.expectAllocCap(span, x.cap);
const t = fc.typeOf(x.value);
if (!t.isValue() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy value type");
if (!t.isValue() or m.isLinearTypeCached(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() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy value type");
if (!t.isValue() or m.isLinearTypeCached(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| {
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", &.{});
if (m.isLinearTypeCached(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);
@@ -989,14 +989,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() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy value type");
if (!t.isValue() or m.isLinearTypeCached(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() or m.isLinearType(t)) return fc.kindError(span, x.value, "a Copy value type");
if (!t.isValue() or m.isLinearTypeCached(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"),
@@ -1060,7 +1060,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
try fc.expectAllocCap(span, x.cap);
const t = fc.typeOf(x.value);
// A non-IslandSafe content is reported at the owner type (E2111).
if (!isIslandSafe(m, t)) return;
if (!try isIslandSafe(fc.c.arena, m, t)) return;
const b = try boxed(fc.c.arena, t);
try fc.expectOkErr(span, x.ok, x.err, if (inst.op == .arc_new) Type{ .arc = b } else Type{ .island = b });
},
@@ -1068,7 +1068,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
const g = fc.typeOf(x.island);
if (g != .mut or g.mut.* != .island) return fc.kindError(span, x.island, "mut<island<T>>");
const t = fc.typeOf(x.value);
if (!isIslandSafe(m, t)) return graphContent(fc, span, t, "IslandSafe");
if (!try isIslandSafe(fc.c.arena, m, t)) return graphContent(fc, span, t, "IslandSafe");
try fc.expectOkErr(span, x.ok, x.err, .{ .iref = try boxed(fc.c.arena, t) });
},
.island_root => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<island<T>> or mut<island<T>>"),
@@ -1264,6 +1264,48 @@ test "deep type positions and argument lengths avoid native recursion" {
try std.testing.expectEqual(@as(u32, 3), c.diags.items[0].span.?.col);
}
test "public generic argument validation works with and without nominal caches" {
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const byte: Type = .{ .int = .u8 };
const plain: Type = .{ .named = 0 };
const owner: Type = .{ .named = 1 };
const raw: air.Module = .{
.name = "arguments",
.format_version = 0,
.lang = .{ 0, 1 },
.features = &.{"linear_aggregates"},
.types = &.{
.{ .name = "Plain", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "x", .ty = byte }} } },
.{ .name = "Owner", .span = .{ .line = 2, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "x", .ty = .{ .own = &byte } }} } },
},
.functions = &.{},
};
const cached = try air.properties.prepare(a, raw, null);
for ([_]air.Module{ raw, cached }) |m| {
var c: Context = .{ .arena = a, .file = "arguments.air", .module = m };
try checkTypeArgument(&c, .{ .line = 5, .col = 4 }, .{ .own_array = &plain });
try std.testing.expectEqual(@as(usize, 0), c.diags.items.len);
try checkTypeArgument(&c, .{ .line = 5, .col = 4 }, .{ .own_array = &owner });
try std.testing.expectEqual(@as(usize, 1), c.diags.items.len);
const d = c.diags.items[0];
try std.testing.expectEqualStrings("E1001", d.code.id);
try std.testing.expectEqualStrings("dynamic heap arrays require Copy content", d.message);
try std.testing.expectEqualStrings("arguments.air", d.span.?.file);
try std.testing.expectEqual(@as(u32, 5), d.span.?.line);
try std.testing.expectEqual(@as(u32, 4), d.span.?.col);
try std.testing.expectEqual(@as(usize, 0), d.fields.len);
try std.testing.expectEqual(@as(usize, 0), d.related_locations.len);
try std.testing.expectEqual(m.nominal_properties, c.module.nominal_properties);
}
try std.testing.expect(raw.nominal_properties == null);
var failing: std.testing.FailingAllocator = .init(a, .{ .fail_index = 0 });
var failed: Context = .{ .arena = failing.allocator(), .file = "arguments.air", .module = raw };
try std.testing.expectError(error.OutOfMemory, checkTypeArgument(&failed, .{ .line = 5, .col = 4 }, .{ .own_array = &plain }));
try std.testing.expectEqual(@as(usize, 0), failed.diags.items.len);
}
test "graph type validation retains its stop before ordinary inner positions" {
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
defer scratch.deinit();