cli: inspect source linear queries iteratively

This commit is contained in:
dev committed 2026-10-10 15:01:42 +03:00
1 parent 232f93f188
commit c1bf99af83
4 files changed
+126 -13

No files matched your search

+112 -13
View File
@@ -346,24 +346,30 @@ const Lowerer = struct {
/// Count witness visits in unit tests, never used to accept/reject source.
property_visits: usize = 0,
fn needsLinearType(l: *Lowerer, t: Ty, depth: usize) bool {
if (depth > l.m.types.len + 32) return false;
if (t == .named) return l.isLinear(t);
if (t.inner()) |inner| {
fn needsLinearType(l: *Lowerer, initial: Ty, initial_depth: usize) bool {
var t = initial;
var depth = initial_depth;
while (true) {
if (depth > l.m.types.len + 32) return false;
if (t == .named) return l.isLinear(t);
const inner = t.inner() orelse return false;
if ((t == .array or t == .own or t == .own_array or t == .slice or t == .slice_mut) and l.isLinear(inner)) return true;
return l.needsLinearType(inner, depth + 1);
t = inner;
depth += 1;
}
return false;
}
fn isLinear(l: *Lowerer, t: Ty) bool {
fn isLinear(l: *Lowerer, initial: Ty) bool {
// Signatures/declarations resolve before checkRequirements; all
// structural ownership queries run after its immutable snapshot.
if (t.isLinear()) return true;
return switch (t) {
.array => |a| l.isLinear(a.elem),
.named => |i| l.property_module.?.nominal_properties.?[i].linear,
else => false,
};
var t = initial;
while (true) {
if (t.isLinear()) return true;
switch (t) {
.array => |a| t = a.elem,
.named => |i| return l.property_module.?.nominal_properties.?[i].linear,
else => return false,
}
}
}
fn fail(l: *Lowerer, comptime code: []const u8, span: Span, message: []const u8, fields: []const Problem.Field) Error {
@@ -4949,3 +4955,96 @@ test "source equality traverses deep trees and detects leaf, interior and outer
try std.testing.expect(!different.eql(left));
}
}
const ReferenceSourceLinear = struct {
fn isLinear(l: *Lowerer, t: Ty) bool {
if (t.isLinear()) return true;
return switch (t) {
.array => |a| isLinear(l, a.elem),
.named => |i| l.property_module.?.nominal_properties.?[i].linear,
else => false,
};
}
fn needsLinearType(l: *Lowerer, t: Ty, depth: usize) bool {
if (depth > l.m.types.len + 32) return false;
if (t == .named) return isLinear(l, t);
if (t.inner()) |inner| {
if ((t == .array or t == .own or t == .own_array or t == .slice or t == .slice_mut) and isLinear(l, inner)) return true;
return needsLinearType(l, inner, depth + 1);
}
return false;
}
};
fn expectSourceLinearMatches(l: *Lowerer, t: Ty) !void {
try std.testing.expectEqual(ReferenceSourceLinear.isLinear(l, t), l.isLinear(t));
for ([_]usize{ 0, 31, 32, 33, 34, 35, 64 }) |depth| try std.testing.expectEqual(ReferenceSourceLinear.needsLinearType(l, t, depth), l.needsLinearType(t, depth));
}
test "source linear queries preserve shallow classification and guard order" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
var l: Lowerer = .{ .arena = a, .m = (try parse_mod.parse(a, "module t\nstruct Plain { x: u8 }\nstruct Linear { x: own<u8> }\nfn main() -> void effects [] {}\n")).ok };
try l.checkRequirements();
var leaves: std.ArrayList(Ty) = .empty;
for (int_types) |name| try leaves.append(a, .{ .int = name });
try leaves.appendSlice(a, &.{ .bool, .void, .never, .region, .scope, .shield, .{ .named = 0 }, .{ .named = 1 } });
for (authority.kinds) |kind| try leaves.append(a, .{ .cap = kind.name });
inline for (@typeInfo(authority.Builtin).@"enum".field_names) |name| try leaves.append(a, .{ .builtin = @field(authority.Builtin, name) });
inline for (@typeInfo(authority.Resource).@"enum".field_names) |name| try leaves.append(a, .{ .resource = @field(authority.Resource, name) });
for (leaves.items) |leaf| {
try expectSourceLinearMatches(&l, leaf);
for ([_]u64{ 0, 1, @as(u64, 1) << 32, std.math.maxInt(u64) }) |length| try expectSourceLinearMatches(&l, try l.arrayOf(leaf, length));
inline for (@typeInfo(Ty).@"union".field_names, @typeInfo(Ty).@"union".field_types) |name, FieldType| if (comptime FieldType == *const Ty) {
const tag = @field(std.meta.Tag(Ty), name);
try expectSourceLinearMatches(&l, try l.wrap(tag, leaf));
try expectSourceLinearMatches(&l, try l.arrayOf(try l.wrap(tag, leaf), 123));
try expectSourceLinearMatches(&l, try l.wrap(tag, try l.arrayOf(leaf, 456)));
};
var chain = leaf;
for (0..41) |_| {
try expectSourceLinearMatches(&l, chain);
chain = try l.wrap(.ref, chain);
}
}
// Guard before a named leaf; a qualifying parent's content query still
// precedes advancing the guard, even if that child would exceed it.
try std.testing.expect(l.needsLinearType(.{ .named = 1 }, 34));
try std.testing.expect(!l.needsLinearType(.{ .named = 1 }, 35));
try std.testing.expect(l.needsLinearType(try l.arrayOf(.{ .named = 1 }, 1), 34));
try std.testing.expect(!l.needsLinearType(try l.arrayOf(.{ .named = 1 }, 1), 35));
try std.testing.expect(!l.needsLinearType(try l.wrap(.own, .{ .int = "u8" }), 0));
}
test "source linear queries walk deep arrays and module-sized feature guards" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const initial = (try parse_mod.parse(a, "module t\nstruct Plain { x: u8 }\nstruct Linear { x: own<u8> }\nfn main() -> void effects [] {}\n")).ok;
const count = 65_536;
const declarations = try a.alloc(ast.TypeDecl, count);
@memcpy(declarations[0..2], initial.types);
for (declarations[2..], 2..) |*d, i| d.* = .{ .name = try std.fmt.allocPrint(a, "Padding{d}", .{i}), .span = initial.types[0].span, .kind = .{ .@"struct" = &.{} } };
var m = initial;
m.types = declarations;
var l: Lowerer = .{ .arena = a, .m = m };
// Construct a complete immutable property snapshot, not a forged cache.
// This is a helper-stage fixture, not a complete CLI acceptance claim.
try l.checkRequirements();
var plain: Ty = .{ .named = 0 };
var linear: Ty = .{ .named = 1 };
var borrowed: Ty = .{ .named = 1 };
for (0..count) |_| {
plain = try l.arrayOf(plain, 1);
linear = try l.arrayOf(linear, 1);
borrowed = try l.wrap(.ref, borrowed);
}
try std.testing.expect(!l.isLinear(plain));
try std.testing.expect(l.isLinear(linear));
try std.testing.expect(!l.isLinear(borrowed));
try std.testing.expect(l.needsLinearType(borrowed, 0));
try std.testing.expect(l.needsLinearType(borrowed, 32));
try std.testing.expect(!l.needsLinearType(borrowed, 33));
try std.testing.expect(!l.needsLinearType(.{ .named = 1 }, count + 33));
}