cli: compare source types iteratively

This commit is contained in:
dev committed 2026-10-10 14:51:29 +03:00
1 parent 936d65359c
commit 232f93f188
4 files changed
+121 -12

No files matched your search

+109 -12
View File
@@ -90,18 +90,29 @@ pub const Ty = union(enum) {
};
}
fn eql(a: Ty, b: Ty) bool {
if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false;
return switch (a) {
.int => |x| std.mem.eql(u8, x, b.int),
.named => |i| i == b.named,
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(inner(b).?),
.cap => |k| std.mem.eql(u8, k, b.cap),
.builtin => |e| e == b.builtin,
.resource => |r| r == b.resource,
else => true,
};
fn eql(initial_a: Ty, initial_b: Ty) bool {
var a = initial_a;
var b = initial_b;
while (true) {
if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false;
switch (a) {
.int => |x| return std.mem.eql(u8, x, b.int),
.named => |i| return i == b.named,
.array => |x| {
if (x.len != b.array.len) return false;
a = x.elem;
b = b.array.elem;
},
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| {
a = x.*;
b = inner(b).?;
},
.cap => |k| return std.mem.eql(u8, k, b.cap),
.builtin => |e| return e == b.builtin,
.resource => |r| return r == b.resource,
.bool, .void, .never, .region, .scope, .shield => return true,
}
}
}
fn inner(t: Ty) ?Ty {
@@ -4852,3 +4863,89 @@ const AirConversionAllocations = struct {
self.count = 0;
}
};
// Independent pre-repair comparison, restricted to shallow test trees.
fn referenceSourceEquality(a: Ty, b: Ty) bool {
if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false;
return switch (a) {
.int => |x| std.mem.eql(u8, x, b.int),
.named => |i| i == b.named,
.array => |x| x.len == b.array.len and referenceSourceEquality(x.elem, b.array.elem),
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| referenceSourceEquality(x.*, Ty.inner(b).?),
.cap => |k| std.mem.eql(u8, k, b.cap),
.builtin => |e| e == b.builtin,
.resource => |r| r == b.resource,
else => true,
};
}
test "source equality matches the independent shallow comparison" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const m: ast.Module = .{ .name = "equality", .types = &.{}, .functions = &.{} };
var builder: Lowerer = .{ .arena = a, .m = m };
var leaves: std.ArrayList(Ty) = .empty;
for (int_types) |name| {
try leaves.append(a, .{ .int = name });
// Equal contents in different storage must still compare equal.
try leaves.append(a, .{ .int = try a.dupe(u8, name) });
}
try leaves.appendSlice(a, &.{ .bool, .void, .never, .region, .scope, .shield, .{ .named = 0 }, .{ .named = 123 } });
for (authority.kinds) |kind| {
try leaves.append(a, .{ .cap = kind.name });
try leaves.append(a, .{ .cap = try a.dupe(u8, 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) });
var shapes: std.ArrayList(Ty) = .empty;
for (leaves.items) |leaf| {
try shapes.append(a, leaf);
for ([_]u64{ 0, 1, @as(u64, 1) << 32, std.math.maxInt(u64) }) |length| try shapes.append(a, try builder.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 shapes.append(a, try builder.wrap(tag, leaf));
try shapes.append(a, try builder.arrayOf(try builder.wrap(tag, leaf), 123));
try shapes.append(a, try builder.wrap(tag, try builder.arrayOf(leaf, 456)));
};
}
for (shapes.items) |left| for (shapes.items) |right| {
try std.testing.expectEqual(referenceSourceEquality(left, right), left.eql(right));
};
}
test "source equality traverses deep trees and detects leaf, interior and outer mismatches" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const m: ast.Module = .{ .name = "equality", .types = &.{}, .functions = &.{} };
var builder: Lowerer = .{ .arena = a, .m = m };
const count = 65_536;
var left: Ty = .{ .int = "u8" };
var right: Ty = .{ .int = try a.dupe(u8, "u8") };
var wrong_leaf: Ty = .{ .int = "u16" };
var wrong_length: Ty = left;
var wrong_tag: Ty = left;
for (0..count) |i| {
if (i % 2 == 0) {
left = try builder.arrayOf(left, i + 1);
right = try builder.arrayOf(right, i + 1);
wrong_leaf = try builder.arrayOf(wrong_leaf, i + 1);
wrong_length = try builder.arrayOf(wrong_length, if (i == count / 2) i + 2 else i + 1);
wrong_tag = try builder.arrayOf(wrong_tag, i + 1);
} else {
left = try builder.wrap(.ref, left);
right = try builder.wrap(.ref, right);
wrong_leaf = try builder.wrap(.ref, wrong_leaf);
wrong_length = try builder.wrap(.ref, wrong_length);
wrong_tag = if (i == count / 2 + 1) try builder.wrap(.mut, wrong_tag) else try builder.wrap(.ref, wrong_tag);
}
}
try std.testing.expect(left.ref != right.ref);
try std.testing.expect(left.eql(right));
try std.testing.expect(right.eql(left));
for ([_]Ty{ wrong_leaf, wrong_length, wrong_tag, .bool, try builder.wrap(.mut, right.ref.*) }) |different| {
try std.testing.expect(!left.eql(different));
try std.testing.expect(!different.eql(left));
}
}