cli: inspect anonymous source properties iteratively
This commit is contained in:
1 parent
b23f3e678e
commit
53907ecf25
4 files changed
+194
-7
No files matched your search
@@ -15,8 +15,9 @@ const Memo = struct {
|
||||
generation: usize = 0,
|
||||
};
|
||||
|
||||
pub fn firstInvalid(t: ast.Type, context: anytype, lookup: anytype, params: []const ast.TypeParam) std.mem.Allocator.Error!?ast.Type {
|
||||
switch (t.kind) {
|
||||
pub fn firstInvalid(annotation: ast.Type, context: anytype, lookup: anytype, params: []const ast.TypeParam) std.mem.Allocator.Error!?ast.Type {
|
||||
var t = annotation;
|
||||
while (true) switch (t.kind) {
|
||||
.wrapped => |w| {
|
||||
if (std.mem.eql(u8, w.wrapper, "own_array")) {
|
||||
var memo: Memo = .{};
|
||||
@@ -28,21 +29,25 @@ pub fn firstInvalid(t: ast.Type, context: anytype, lookup: anytype, params: []co
|
||||
}
|
||||
// Graph well-formedness keeps its independently specified E2111.
|
||||
for ([_][]const u8{ "island", "frozen", "arc" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return null;
|
||||
return firstInvalid(w.inner.*, context, lookup, params);
|
||||
t = w.inner.*;
|
||||
},
|
||||
.array => |a| return firstInvalid(a.elem.*, context, lookup, params),
|
||||
.array => |a| t = a.elem.*,
|
||||
else => return null,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn linear(t: ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool {
|
||||
fn linear(annotation: ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool {
|
||||
var t = annotation;
|
||||
// Arrays add no environment or nominal path state. Peel only this
|
||||
// anonymous chain; declaration fixed points retain their original rules.
|
||||
while (t.kind == .array) t = t.kind.array.elem.*;
|
||||
switch (t.kind) {
|
||||
.file => return true,
|
||||
.wrapped => |w| {
|
||||
for ([_][]const u8{ "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "atomic" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return true;
|
||||
return false;
|
||||
},
|
||||
.array => |a| return linear(a.elem.*, context, lookup, env, path, memo),
|
||||
.array => unreachable,
|
||||
.name => |name| {
|
||||
for (env.params, 0..) |p, i| if (std.mem.eql(u8, p.name, name)) {
|
||||
return i < env.known_linear.len and env.known_linear[i];
|
||||
@@ -148,3 +153,169 @@ test "Copy-content query shares cyclic diamond branches" {
|
||||
// An unshared walk repeats every path to the same cycle. Count queries, not elapsed time.
|
||||
try std.testing.expect(probe.lookups <= 22);
|
||||
}
|
||||
|
||||
// Independent pre-repair anonymous traversal, used only on shallow trees.
|
||||
const ReferenceAnonymousProperties = struct {
|
||||
fn firstInvalid(t: ast.Type, context: anytype, lookup: anytype, params: []const ast.TypeParam) std.mem.Allocator.Error!?ast.Type {
|
||||
switch (t.kind) {
|
||||
.wrapped => |w| {
|
||||
if (std.mem.eql(u8, w.wrapper, "own_array")) {
|
||||
var memo: Memo = .{};
|
||||
while (true) {
|
||||
const generation = memo.generation;
|
||||
if (try ReferenceAnonymousProperties.linear(w.inner.*, context, lookup, &.{ .params = params }, null, &memo)) return w.inner.*;
|
||||
if (generation == memo.generation) break;
|
||||
}
|
||||
}
|
||||
// Graph well-formedness keeps its independently specified E2111.
|
||||
for ([_][]const u8{ "island", "frozen", "arc" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return null;
|
||||
return ReferenceAnonymousProperties.firstInvalid(w.inner.*, context, lookup, params);
|
||||
},
|
||||
.array => |a| return ReferenceAnonymousProperties.firstInvalid(a.elem.*, context, lookup, params),
|
||||
else => return null,
|
||||
}
|
||||
}
|
||||
fn linear(t: ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool {
|
||||
switch (t.kind) {
|
||||
.file => return true,
|
||||
.wrapped => |w| {
|
||||
for ([_][]const u8{ "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "atomic" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return true;
|
||||
return false;
|
||||
},
|
||||
.array => |a| return ReferenceAnonymousProperties.linear(a.elem.*, context, lookup, env, path, memo),
|
||||
.name => |name| {
|
||||
for (env.params, 0..) |p, i| if (std.mem.eql(u8, p.name, name)) {
|
||||
return i < env.known_linear.len and env.known_linear[i];
|
||||
};
|
||||
if (lookup(context, name, t.span)) |d| return declaration(d, &.{}, context, lookup, env, path, memo);
|
||||
for ([_][]const u8{ "region", "stream", "listener" }) |tag| if (std.mem.eql(u8, name, tag)) return true;
|
||||
return false;
|
||||
},
|
||||
.applied => |a| {
|
||||
const d = lookup(context, a.name, t.span) orelse return false;
|
||||
return declaration(d, a.args, context, lookup, env, path, memo);
|
||||
},
|
||||
else => return false,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const PropertyProbe = struct {
|
||||
arena: std.mem.Allocator,
|
||||
module: ast.Module = .{ .name = "properties", .types = &.{}, .functions = &.{} },
|
||||
lookups: usize = 0,
|
||||
|
||||
fn lookup(p: *@This(), name: []const u8, _: ast.Span) ?ast.TypeDecl {
|
||||
p.lookups += 1;
|
||||
for (p.module.types) |d| if (std.mem.eql(u8, d.name, name)) return d;
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
fn expectAnonymousPropertyMatch(arena: std.mem.Allocator, m: ast.Module, t: ast.Type, params: []const ast.TypeParam) !void {
|
||||
var original: PropertyProbe = .{ .arena = arena, .module = m };
|
||||
var current: PropertyProbe = .{ .arena = arena, .module = m };
|
||||
try std.testing.expectEqualDeep(
|
||||
try ReferenceAnonymousProperties.firstInvalid(t, &original, PropertyProbe.lookup, params),
|
||||
try firstInvalid(t, ¤t, PropertyProbe.lookup, params),
|
||||
);
|
||||
try std.testing.expectEqual(original.lookups, current.lookups);
|
||||
}
|
||||
|
||||
test "anonymous source properties preserve shallow failures and graph stops" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const source =
|
||||
\\module properties
|
||||
\\struct Plain { x: u8 }
|
||||
\\struct Linear { x: own<u8> }
|
||||
\\struct Cycle { a: Cycle, b: Linear }
|
||||
\\enum Choice { empty, value(Linear) }
|
||||
\\struct Box<T> { value: T }
|
||||
\\fn main() -> void effects [] {}
|
||||
;
|
||||
const m = (try @import("parse.zig").parse(a, source)).ok;
|
||||
const inner_span: ast.Span = .{ .start = .{ .line = 9, .col = 11 }, .end = .{ .line = 9, .col = 18 }, .file = "properties.aji" };
|
||||
const outer_span: ast.Span = .{ .start = .{ .line = 8, .col = 2 }, .end = .{ .line = 10, .col = 5 }, .file = "properties.aji" };
|
||||
const scalar: ast.Type = .{ .span = inner_span, .kind = .{ .name = "u8" } };
|
||||
const owner: ast.Type = .{ .span = inner_span, .kind = .{ .wrapped = .{ .wrapper = "own", .inner = &scalar } } };
|
||||
const leaves = [_]ast.Type{
|
||||
scalar,
|
||||
owner,
|
||||
.{ .span = inner_span, .kind = .{ .name = "missing" } },
|
||||
.{ .span = inner_span, .kind = .{ .name = "region" } },
|
||||
.{ .span = inner_span, .kind = .{ .name = "stream" } },
|
||||
.{ .span = inner_span, .kind = .{ .name = "Plain" } },
|
||||
.{ .span = inner_span, .kind = .{ .name = "Linear" } },
|
||||
.{ .span = inner_span, .kind = .{ .name = "Cycle" } },
|
||||
.{ .span = inner_span, .kind = .{ .name = "Choice" } },
|
||||
.{ .span = inner_span, .kind = .{ .name = "T" } },
|
||||
.{ .span = inner_span, .kind = .{ .file = "read" } },
|
||||
.{ .span = inner_span, .kind = .{ .cap = "mem.alloc" } },
|
||||
.{ .span = inner_span, .kind = .{ .function_ref = .{ .name = "operation" } } },
|
||||
.{ .span = inner_span, .kind = .{ .applied = .{ .name = "Box", .args = &.{scalar} } } },
|
||||
.{ .span = inner_span, .kind = .{ .applied = .{ .name = "Box", .args = &.{owner} } } },
|
||||
};
|
||||
const wrappers = [_][]const u8{ "ref", "mut", "slice", "slice_mut", "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "iref", "atomic", "unknown" };
|
||||
const param: ast.TypeParam = .{ .name = "Linear", .span = inner_span, .constraints = &.{} };
|
||||
var comparisons: usize = 0;
|
||||
for ([_][]const ast.TypeParam{ &.{}, &.{param} }) |params| for (&leaves) |*leaf| {
|
||||
const array: ast.Type = .{ .span = outer_span, .kind = .{ .array = .{ .elem = leaf, .len = 3 } } };
|
||||
for ([_]ast.Type{ leaf.*, array }) |base| {
|
||||
try expectAnonymousPropertyMatch(a, m, base, params);
|
||||
comparisons += 1;
|
||||
const owned: ast.Type = .{ .span = outer_span, .kind = .{ .wrapped = .{ .wrapper = "own_array", .inner = &base } } };
|
||||
try expectAnonymousPropertyMatch(a, m, owned, params);
|
||||
comparisons += 1;
|
||||
for (wrappers) |wrapper| {
|
||||
const wrapped: ast.Type = .{ .span = inner_span, .kind = .{ .wrapped = .{ .wrapper = wrapper, .inner = &owned } } };
|
||||
try expectAnonymousPropertyMatch(a, m, wrapped, params);
|
||||
comparisons += 1;
|
||||
const outer_owned: ast.Type = .{ .span = outer_span, .kind = .{ .wrapped = .{ .wrapper = "own_array", .inner = &wrapped } } };
|
||||
try expectAnonymousPropertyMatch(a, m, outer_owned, params);
|
||||
comparisons += 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
try std.testing.expectEqual(@as(usize, 2160), comparisons);
|
||||
}
|
||||
|
||||
test "anonymous source properties traverse deep types without work allocations" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const span: ast.Span = .{ .start = .{ .line = 2, .col = 3 }, .end = .{ .line = 2, .col = 9 } };
|
||||
const content_span: ast.Span = .{ .start = .{ .line = 7, .col = 12 }, .end = .{ .line = 7, .col = 18 } };
|
||||
const scalar: ast.Type = .{ .span = span, .kind = .{ .name = "u8" } };
|
||||
var leaf = scalar;
|
||||
const nodes = try a.alloc(ast.Type, 32_768);
|
||||
var child: *const ast.Type = &leaf;
|
||||
for (nodes) |*node| {
|
||||
node.* = .{ .span = content_span, .kind = .{ .array = .{ .elem = child, .len = 1 } } };
|
||||
child = node;
|
||||
}
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
var probe: PropertyProbe = .{ .arena = failing.allocator() };
|
||||
try std.testing.expect(try firstInvalid(child.*, &probe, PropertyProbe.lookup, &.{}) == null);
|
||||
const owned: ast.Type = .{ .span = span, .kind = .{ .wrapped = .{ .wrapper = "own_array", .inner = child } } };
|
||||
try std.testing.expect(try firstInvalid(owned, &probe, PropertyProbe.lookup, &.{}) == null);
|
||||
leaf.kind = .{ .wrapped = .{ .wrapper = "own", .inner = &scalar } };
|
||||
const bad = (try firstInvalid(owned, &probe, PropertyProbe.lookup, &.{})).?;
|
||||
try std.testing.expect(bad.kind == .array);
|
||||
try std.testing.expectEqualDeep(content_span, bad.span);
|
||||
for ([_][]const u8{ "island", "frozen", "arc" }) |wrapper| {
|
||||
const graph: ast.Type = .{ .span = span, .kind = .{ .wrapped = .{ .wrapper = wrapper, .inner = &owned } } };
|
||||
try std.testing.expect(try firstInvalid(graph, &probe, PropertyProbe.lookup, &.{}) == null);
|
||||
}
|
||||
// Mixed wrappers are followed by firstInvalid, while the linearity
|
||||
// query still stops at non-owning wrappers instead of following them.
|
||||
leaf = scalar;
|
||||
for (nodes, 0..) |*node, i| if (i % 2 == 0) {
|
||||
node.kind = .{ .wrapped = .{ .wrapper = "ref", .inner = if (i == 0) &leaf else &nodes[i - 1] } };
|
||||
};
|
||||
try std.testing.expect(try firstInvalid(child.*, &probe, PropertyProbe.lookup, &.{}) == null);
|
||||
try std.testing.expect(try firstInvalid(owned, &probe, PropertyProbe.lookup, &.{}) == null);
|
||||
try std.testing.expect(!failing.has_induced_failure);
|
||||
try std.testing.expectEqual(@as(usize, 0), failing.allocations);
|
||||
}
|
||||
Reference in new issue
Block a user