verifier: traverse type validity and layouts iteratively
This commit is contained in:
1 parent
3906ce5a9b
commit
b0cd7d74ff
4 files changed
+228
-51
No files matched your search
+214
-51
@@ -60,35 +60,82 @@ fn checkDeclarations(c: *Context) !void {
|
||||
|
||||
/// E1003 applies through arrays as well as direct nominal members.
|
||||
pub fn checkFiniteLayouts(c: *Context) !void {
|
||||
const m = c.module;
|
||||
const state = try c.arena.alloc(enum { white, grey, black }, m.types.len);
|
||||
const state = try c.arena.alloc(enum { white, grey, black }, c.module.types.len);
|
||||
@memset(state, .white);
|
||||
for (0..m.types.len) |i| try visitDecl(c, state, @intCast(i));
|
||||
var pending: std.ArrayList(LayoutFrame) = .empty;
|
||||
for (0..c.module.types.len) |i| {
|
||||
try pending.append(c.arena, .{ .decl = @intCast(i) });
|
||||
while (pending.items.len != 0) {
|
||||
const frame = &pending.items[pending.items.len - 1];
|
||||
const d = c.module.types[frame.decl];
|
||||
if (!frame.entered) {
|
||||
if (state[frame.decl] == .black) {
|
||||
_ = pending.pop();
|
||||
continue;
|
||||
}
|
||||
if (state[frame.decl] == .grey) {
|
||||
try c.report("E1003", d.span, "aggregate type contains itself by value", &.{.{ .name = "type", .value = .{ .str = d.name } }});
|
||||
state[frame.decl] = .black;
|
||||
_ = pending.pop();
|
||||
continue;
|
||||
}
|
||||
state[frame.decl] = .grey;
|
||||
frame.entered = true;
|
||||
}
|
||||
if (frame.nextType(d)) |t| {
|
||||
if (layoutTarget(c.module, t)) |child| try pending.append(c.arena, .{ .decl = child });
|
||||
} else {
|
||||
state[frame.decl] = .black;
|
||||
_ = pending.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visitDecl(c: *Context, state: anytype, i: u32) std.mem.Allocator.Error!void {
|
||||
if (state[i] == .black) return;
|
||||
if (state[i] == .grey) {
|
||||
const d = c.module.types[i];
|
||||
try c.report("E1003", d.span, "aggregate type contains itself by value", &.{.{ .name = "type", .value = .{ .str = d.name } }});
|
||||
state[i] = .black;
|
||||
return;
|
||||
}
|
||||
state[i] = .grey;
|
||||
switch (c.module.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| try visitValue(c, state, f.ty),
|
||||
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| try visitValue(c, state, t),
|
||||
}
|
||||
state[i] = .black;
|
||||
}
|
||||
/// Suspended DFS calls retain field/payload order, including after a back edge
|
||||
/// marks an active declaration black. That active call still visits its siblings.
|
||||
const LayoutFrame = struct {
|
||||
decl: u32,
|
||||
entered: bool = false,
|
||||
next: usize = 0,
|
||||
variant: usize = 0,
|
||||
|
||||
fn visitValue(c: *Context, state: anytype, t: Type) std.mem.Allocator.Error!void {
|
||||
switch (t) {
|
||||
.named => |i| try visitDecl(c, state, i),
|
||||
.array => |a| try visitValue(c, state, a.elem),
|
||||
.own, .own_array, .buffer, .sender, .receiver, .mutex, .ref, .mut, .slice, .slice_mut, .guard, .task, .local => |inner| if (c.module.declares("linear_aggregates")) try visitValue(c, state, inner.*),
|
||||
else => {},
|
||||
fn nextType(f: *LayoutFrame, d: air.TypeDecl) ?Type {
|
||||
switch (d.kind) {
|
||||
.@"struct" => |fs| {
|
||||
if (f.next == fs.len) return null;
|
||||
const t = fs[f.next].ty;
|
||||
f.next += 1;
|
||||
return t;
|
||||
},
|
||||
.@"enum" => |vs| {
|
||||
while (f.variant < vs.len) {
|
||||
const payload = vs[f.variant].payload;
|
||||
if (f.next < payload.len) {
|
||||
const t = payload[f.next];
|
||||
f.next += 1;
|
||||
return t;
|
||||
}
|
||||
f.variant += 1;
|
||||
f.next = 0;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn layoutTarget(m: air.Module, initial: Type) ?u32 {
|
||||
var t = initial;
|
||||
while (true) switch (t) {
|
||||
.named => |i| return i,
|
||||
.array => |a| t = a.elem,
|
||||
.own, .own_array, .buffer, .sender, .receiver, .mutex, .ref, .mut, .slice, .slice_mut, .guard, .task, .local => |inner| {
|
||||
if (!m.declares("linear_aggregates")) return null;
|
||||
t = inner.*;
|
||||
},
|
||||
else => return null,
|
||||
};
|
||||
}
|
||||
|
||||
pub const Position = enum { member, param, ret, block_param, borrowed };
|
||||
@@ -118,33 +165,39 @@ pub fn innerPosition(tag: std.meta.Tag(Type)) Position {
|
||||
/// - borrows only as parameters, returns and (gated) block parameters,
|
||||
/// never as members (fields, payloads, elements, locals);
|
||||
/// - the inner type of every compound type is a value type.
|
||||
fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []const u8) !void {
|
||||
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", &.{});
|
||||
// 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 } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t.atomic.*) } },
|
||||
});
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
.{ .name = "position", .value = .{ .str = @tagName(pos) } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t) } },
|
||||
});
|
||||
// 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", &.{
|
||||
fn wellFormed(c: *Context, span: air.Span, initial: Type, initial_pos: Position, owner: []const u8) !void {
|
||||
var t = initial;
|
||||
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", &.{});
|
||||
// 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 } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t.inner().?) } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t.atomic.*) } },
|
||||
});
|
||||
return;
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
.{ .name = "position", .value = .{ .str = @tagName(pos) } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t) } },
|
||||
});
|
||||
// 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", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t.inner().?) } },
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 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.
|
||||
// Channel elements and mutex contents may be linear (spec 04 §38.1);
|
||||
// their remaining rules are the element rules of `checkElement`.
|
||||
const inner = t.inner() orelse return;
|
||||
pos = innerPosition(std.meta.activeTag(t));
|
||||
t = inner;
|
||||
}
|
||||
// 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.
|
||||
// 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, innerPosition(std.meta.activeTag(t)), owner);
|
||||
}
|
||||
|
||||
/// Validate a phantom or used source generic argument by ordinary type rules.
|
||||
@@ -152,9 +205,12 @@ pub fn checkTypeArgument(c: *Context, span: air.Span, t: Type) !void {
|
||||
try wellFormed(c, span, t, .param, "generic argument");
|
||||
try argumentLengths(c, span, t);
|
||||
}
|
||||
fn argumentLengths(c: *Context, span: air.Span, t: Type) !void {
|
||||
if (t == .array and (t.array.len == 0 or t.array.len > (1 << 32))) try c.report("E1001", span, "array length outside format-0 range", &.{});
|
||||
if (t.inner()) |inner| try argumentLengths(c, span, inner);
|
||||
fn argumentLengths(c: *Context, span: air.Span, initial: Type) !void {
|
||||
var t = initial;
|
||||
while (true) {
|
||||
if (t == .array and (t.array.len == 0 or t.array.len > (1 << 32))) try c.report("E1001", span, "array length outside format-0 range", &.{});
|
||||
t = t.inner() orelse return;
|
||||
}
|
||||
}
|
||||
|
||||
/// `local.alloc` results are checked separately: their own type is storage.
|
||||
@@ -1181,3 +1237,110 @@ test "aggregates and declarations" {
|
||||
try verifier.expectCodes(h ++ "type @A = struct { b: @B }\ntype @B = enum { X(@A) }\nfn @f() -> void\neffects []\nentry:\n ret\n", &.{"E1003"});
|
||||
try verifier.expectCodes(h ++ "fn @f(%v: void) -> void\neffects []\nentry:\n ret\n", &.{"E1001"});
|
||||
}
|
||||
|
||||
fn nestedArrays(a: std.mem.Allocator, depth: usize, leaf: Type) !Type {
|
||||
const rows = try a.alloc(Type.Array, depth);
|
||||
var t = leaf;
|
||||
for (rows) |*row| {
|
||||
row.* = .{ .elem = t, .len = 1 };
|
||||
t = .{ .array = row };
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
test "deep type positions and argument lengths avoid native recursion" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
var c: Context = .{ .arena = a, .file = "deep.air", .module = .{ .name = "deep", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{"arrays"}, .types = &.{}, .functions = &.{} } };
|
||||
const t = try nestedArrays(a, 32_768, .{ .int = .u8 });
|
||||
try checkTypeArgument(&c, .{ .line = 5, .col = 4 }, t);
|
||||
try std.testing.expectEqual(@as(usize, 0), c.diags.items.len);
|
||||
const bad = try nestedArrays(a, 32_768, .void);
|
||||
try wellFormed(&c, .{ .line = 7, .col = 3 }, bad, .param, "f");
|
||||
try std.testing.expectEqual(@as(usize, 1), c.diags.items.len);
|
||||
try std.testing.expectEqualStrings("E1001", c.diags.items[0].code.id);
|
||||
try std.testing.expectEqual(@as(u32, 7), c.diags.items[0].span.?.line);
|
||||
try std.testing.expectEqual(@as(u32, 3), c.diags.items[0].span.?.col);
|
||||
}
|
||||
|
||||
test "graph type validation retains its stop before ordinary inner positions" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
var c: Context = .{ .arena = a, .file = "graph.air", .module = .{ .name = "graph", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{"linear_aggregates"}, .types = &.{}, .functions = &.{} } };
|
||||
const scalar: Type = .{ .int = .u8 };
|
||||
const borrowed: Type = .{ .ref = &scalar };
|
||||
const graph: Type = .{ .island = &borrowed };
|
||||
try wellFormed(&c, .{ .line = 1, .col = 1 }, graph, .member, "R");
|
||||
try std.testing.expectEqual(@as(usize, 1), c.diags.items.len);
|
||||
try std.testing.expectEqualStrings("E2111", c.diags.items[0].code.id);
|
||||
}
|
||||
|
||||
test "deep nominal layouts and anonymous cycle edges use explicit traversal" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const count = 32_768;
|
||||
const ds = try a.alloc(air.TypeDecl, count);
|
||||
for (ds, 0..) |*d, i| d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = try a.dupe(@import("../air/module.zig").Field, &.{.{ .name = "x", .ty = if (i + 1 == count) .{ .int = .u8 } else .{ .named = @intCast(i + 1) } }}) } };
|
||||
var c: Context = .{ .arena = a, .file = "layout.air", .module = .{ .name = "layout", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} } };
|
||||
try checkFiniteLayouts(&c);
|
||||
try std.testing.expectEqual(@as(usize, 0), c.diags.items.len);
|
||||
const loop = try nestedArrays(a, 32_768, .{ .named = 0 });
|
||||
ds[0] = .{ .name = "Loop", .span = .{ .line = 8, .col = 6 }, .kind = .{ .@"struct" = try a.dupe(@import("../air/module.zig").Field, &.{.{ .name = "x", .ty = loop }}) } };
|
||||
c.module.types = ds[0..1];
|
||||
try checkFiniteLayouts(&c);
|
||||
try std.testing.expectEqual(@as(usize, 1), c.diags.items.len);
|
||||
try std.testing.expectEqualStrings("E1003", c.diags.items[0].code.id);
|
||||
try std.testing.expectEqual(@as(u32, 8), c.diags.items[0].span.?.line);
|
||||
try std.testing.expectEqual(@as(u32, 6), c.diags.items[0].span.?.col);
|
||||
}
|
||||
|
||||
test "finite-layout wrapper edges retain their feature gate and graph boundary" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const self: Type = .{ .named = 0 };
|
||||
inline for (.{ .array, .own, .ref, .island, .atomic }) |tag| {
|
||||
const row: Type.Array = .{ .elem = self, .len = 1 };
|
||||
const t: Type = if (tag == .array) .{ .array = &row } else @unionInit(Type, @tagName(tag), &self);
|
||||
for ([_]bool{ false, true }) |enabled| {
|
||||
const ds = [_]air.TypeDecl{.{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "x", .ty = t }} } }};
|
||||
var c: Context = .{ .arena = a, .file = "edge.air", .module = .{ .name = "edge", .format_version = 0, .lang = .{ 0, 1 }, .features = if (enabled) &.{"linear_aggregates"} else &.{}, .types = &ds, .functions = &.{} } };
|
||||
try checkFiniteLayouts(&c);
|
||||
const cycle = tag == .array or ((tag == .own or tag == .ref) and enabled);
|
||||
try std.testing.expectEqual(@as(usize, if (cycle) 1 else 0), c.diags.items.len);
|
||||
if (cycle) try std.testing.expectEqualStrings("E1003", c.diags.items[0].code.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "finite-layout diagnostics retain DFS siblings after back edges" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const ds = [_]air.TypeDecl{
|
||||
.{ .name = "A", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{
|
||||
.{ .name = "self", .ty = .{ .named = 0 } },
|
||||
.{ .name = "next", .ty = .{ .named = 1 } },
|
||||
} } },
|
||||
.{ .name = "B", .span = .{ .line = 2, .col = 1 }, .kind = .{ .@"enum" = &.{
|
||||
.{ .name = "Empty", .payload = &.{} },
|
||||
.{ .name = "Pair", .payload = &.{ .{ .named = 1 }, .{ .named = 2 } } },
|
||||
.{ .name = "EmptyAgain", .payload = &.{} },
|
||||
.{ .name = "Last", .payload = &.{.{ .named = 3 }} },
|
||||
} } },
|
||||
.{ .name = "C", .span = .{ .line = 3, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "self", .ty = .{ .named = 2 } }} } },
|
||||
.{ .name = "D", .span = .{ .line = 4, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "self", .ty = .{ .named = 3 } }} } },
|
||||
};
|
||||
var c: Context = .{ .arena = a, .file = "order.air", .module = .{ .name = "order", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &ds, .functions = &.{} } };
|
||||
try checkFiniteLayouts(&c);
|
||||
try std.testing.expectEqual(@as(usize, 4), c.diags.items.len);
|
||||
for (c.diags.items, ds) |d, decl| {
|
||||
try std.testing.expectEqualStrings("E1003", d.code.id);
|
||||
try std.testing.expectEqual(decl.span.line, d.span.?.line);
|
||||
try std.testing.expectEqual(decl.span.col, d.span.?.col);
|
||||
try std.testing.expectEqualStrings(decl.name, d.fields[0].value.str);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user