From 232f93f18832cb18a2416fda586aba270fff6962 Mon Sep 17 00:00:00 2001 From: "A.Shakhmatov" Date: Sat, 10 Oct 2026 14:51:29 +0300 Subject: [PATCH] cli: compare source types iteratively --- docs/design/air-type-depth-audit.md | 6 ++ docs/design/open-questions.md | 2 + docs/plan/phase-6-source-language.md | 4 + src/frontend/lower.zig | 121 ++++++++++++++++++++++++--- 4 files changed, 121 insertions(+), 12 deletions(-) diff --git a/docs/design/air-type-depth-audit.md b/docs/design/air-type-depth-audit.md index a9c8505..974d2f1 100644 --- a/docs/design/air-type-depth-audit.md +++ b/docs/design/air-type-depth-audit.md @@ -172,3 +172,9 @@ The confirmed 16,384-array struct-member airType failure is repaired by an itera Independent shallow comparison covers 2,352 leaf/array/wrapper shapes, every registered capability and internal wrapper, plus allocation-free leaves. A 65,536-level mixed conversion is inspected and freed iteratively; exactly one output allocation per constructor proves no additional work allocation or borrowed source node. Per-node request lengths/alignments, OOM points, byte/allocation counts and cleanup match the old recursive derivation on a 64-level mixed type. Arena backing growth is deliberately not an equivalence oracle: in-place resize availability can differ between two arenas. Units/format checks, nominal/Copy-content/static-function integration (110/119/25), all 1,791 conformance cases with exact corpus coverage and old/new checks/lowerings (1,421/506) pass. Complete checks of structs with 16,384 and 65,536 array levels now accept without output. A separate 65,536-level source return-equality input confirms the next native failure in Ty.eql (10,001 frames, exit -6, no JSON). Core capture alone is disabled; no timeout ends a probe. Source equality and closing query paths keep Q-059 open; Q-060 validation and Phase 6 gates remain separate. No normative/bundle/feature/code, runtime/effect/ownership or accepted-depth limit changes. No fresh Claude review is claimed. + +## Source type equality + +The confirmed Ty.eql failure is repaired with two type cursors and no allocation or mutation. Tag checks still precede descent, each array length is checked before its element, integer/capability names compare string contents, and nominal types compare declaration indices without expanding their graph. All wrapper and leaf identities are unchanged. An independent shallow copy of the old comparator covers every pair of leaf/array/wrapper shapes, including equal names in different storage and zero/max-u64 lengths. Separate 65,536-level mixed trees test equality in both directions and differences at the leaf, interior array length, interior wrapper and outer tag. + +Units/format checks and all 1,791 conformance cases pass with exact corpus coverage; all 1,421 public source checks and 506 accepted lowerings preserve exact old/new exit/stdout/stderr. Complete return-equality checks now accept at 16,384 and 65,536 array levels without output. Changing the deep return leaf to u16 yields exactly one E7005 at 2:655407, with independently verified full found/expected types (655,362/655,363 bytes) and no stderr. A separate deep Copy-array move probe yields E7006/move_copy at 65,536 levels with its complete type; no native failure is observed in that query path. Only child core capture is disabled; no timeout ends a probe. Closing source-query inventory and Q-060 validation remain open, as do the independent Phase 6 gates. No normative/bundle/feature/code, runtime/effect/ownership or accepted-depth limit changes; no fresh Claude review is claimed. diff --git a/docs/design/open-questions.md b/docs/design/open-questions.md index 446f1bb..e21d74c 100644 --- a/docs/design/open-questions.md +++ b/docs/design/open-questions.md @@ -489,6 +489,8 @@ The [source type text increment](air-type-depth-audit.md#source-type-text-repair The [source-to-AIR conversion increment](air-type-depth-audit.md#source-to-air-type-conversion) repairs airType using a constant-state target cursor with the same constructor allocations and host-error behavior. Deep struct-member checks now accept; an independent return-equality probe confirms Ty.eql native-stack exhaustion at 65,536 array levels. Source equality and closing query work keep Q-059 open; no accepted-depth limit or semantic rule changes. +The [source equality increment](air-type-depth-audit.md#source-type-equality) repairs Ty.eql with allocation-free cursors preserving structural identity. Deep equal returns accept and unequal leaves yield one factual E7005 with complete types. A deep move-on-Copy query produces E7006/move_copy without a native failure. Closing source-query inventory keeps Q-059 open; Q-060 and Phase 6 gates remain separate. No semantic rule or accepted-depth limit changes. + ## Q-060 — Native recursion while materializing runtime values The [executed runtime audit](runtime-value-depth-audit.md) confirms that a verified one-byte value with 32,768 length-one nested arrays aborts inside zeroValue under `aji run --fuel 8`, rather than producing a run record. The instruction has not exhausted fuel. Iterative runtime traversal must preserve initialization, value shape, allocator/root lifetimes, collection safety and instruction accounting; adding a depth trap/acceptance limit needs a normative decision. At that baseline, recursive copying, value sizing, collection and serialization were additional audit candidates with unverified failure paths; the increment below records subsequent executed failures. Compiler type repairs in Q-059 do not resolve Q-060. diff --git a/docs/plan/phase-6-source-language.md b/docs/plan/phase-6-source-language.md index a38becc..1c4eed0 100644 --- a/docs/plan/phase-6-source-language.md +++ b/docs/plan/phase-6-source-language.md @@ -490,3 +490,7 @@ Units/format checks, 3,696 independent shallow comparisons, deep mixed output an The [Q-059 conversion increment](../design/air-type-depth-audit.md#source-to-air-type-conversion) replaces native recursion with an output-target cursor, preserving source/AIR type shape, constructor allocation order and error publication. Structs with 16,384/65,536 array levels now pass complete checking. Tests cover 2,352 independent shallow comparisons, 65,536-level construction without work allocation, and per-node OOM/order/cleanup equivalence. Units/format checks, nominal/Copy-content/static-function integration (110/119/25), all 1,791 conformance cases with exact coverage and old/new checks/lowerings (1,421/506) pass. A separate return-equality input confirms the next failure in Ty.eql at 65,536 array levels. Equality/closing query checks, Q-060 validation and Phase 6 gates remain open. No normative/bundle/feature/code change or accepted-depth limit is introduced; no fresh Claude review is claimed. + +## Source type equality (2026-10-10) + +The [Q-059 equality increment](../design/air-type-depth-audit.md#source-type-equality) removes native recursion from Ty.eql without allocation or changed type identity. Independent shallow pair comparisons and separate deep trees cover equal contents and outer/interior/leaf differences. Complete 65,536-level equal returns accept; unequal leaves return one E7005 with exact full types and source position. Units/format checks and all 1,791 conformance cases pass with exact coverage; all 1,421 source checks and 506 accepted lowerings retain exact old/new exit/stdout/stderr. Closing source-query inventory, Q-060 validation and the independent Phase 6 gates remain open. No normative/bundle/feature/code or accepted-depth limit changes; no fresh Claude review is claimed. diff --git a/src/frontend/lower.zig b/src/frontend/lower.zig index c1be946..9764fb7 100644 --- a/src/frontend/lower.zig +++ b/src/frontend/lower.zig @@ -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)); + } +}