cli: resolve struct len fields by receiver type

Use ordinary field lowering and declared field types for nominal receivers while preserving array and slice lengths. Make array field type hints available to chained access. Missing struct len now reports E7002 instead of the builtin-length E7005.

Add 15 source regressions and imported generic CLI checks. Unit tests, all 1201 conformance cases, per-instruction collection for new cases, formatter corpus and original public check/lower compatibility pass.
This commit is contained in:
dev committed 2026-10-07 20:50:48 +03:00
1 parent 184cb7b321
commit 1495650587
18 files changed
+244 -4

No files matched your search

+7 -1
View File
@@ -197,4 +197,10 @@ No language/spec, AIR, diagnostic, verifier or interpreter rule changes. Existin
Validation: unit tests and full conformance pass, 1,186/1,186. All five reference pairs pass with 200 schedule seeds and collection before every instruction. New CI checks pass 3,662 CLI calls: every example/package, independent journal encoding/CRC/offset checks, overflow without writes, partial hosted writes, and concurrent source/AIR record equality at 200 seeds across three quanta. Linux raises the failed file's quota before retry to verify poisoning; the branch without `prlimit` also passes, retaining the original quota. Independent Claude review found the old-file offset error and two integration issues; these are corrected. A separate formatter correction preserves spaces in unpack/grouped range forms, with a failing-before/passing-after unit test and CLI AIR-fidelity test. Source Bundle hashes are unchanged for all 16 files; Quick Reference, AIR and token budgets do not grow. Frozen evaluation inputs remain unchanged.
Separate P6-5 implementation follow-up: a struct field named `len` currently reaches the array/slice `.len` path and rejects E7005. Spec 15 §5 already permits ordinary struct field access; fixing that dispatch requires no new language design. The executable header uses the explicit field name `length`.
## P6-5 fields named `len` (2026-10-07)
The implementation follow-up from P6-9 is resolved: nominal values and `ref`/`mut` receivers dispatch `.len` through ordinary field access, using the declared field type. Array field types are also available to inference for chained access such as `header.len.len`. Arrays and slices, including slices of structs, retain their built-in length behavior. Spec 15 §5 already defines these rules; no spec, bundle, AIR, verifier or interpreter change is required.
Existing borrow/load/end and whole-value ownership rules remain authoritative: reading a Copy field succeeds, conflicting borrows reject E2101, and projecting a linear field rejects E1001. Missing struct fields now produce E7002 with `type` and `field`, replacing the misleading array/slice E7005. Enum field access and type mismatches retain E7005. A struct length field does not prove slice bounds: the out-of-bounds regression still traps T0008. This restores specified source behavior without changing accepted AIR semantics or runtime state.
Validation: full conformance passes 1,201/1,201, including all 15 new cases; the new cases also pass with collection before every instruction. Unit tests, generic CLI integration (32 checks), formatting checks and coverage (120/120) pass. The formatter passes 3,437 checks over 916 public files, with 582 identical successful AIR outputs. Imported generic fields preserve declared types, source locations and source/AIR run-record parity. Across 1,233 original public files, all 2,134 check/lower results agree after normalizing absolute versus relative diagnostic file paths; all 570 successful source lowerings are byte-identical. Independent read-only Claude review found no remaining issues. P6-5 remains open for its other source-layer work.
+17 -3
View File
@@ -1386,8 +1386,9 @@ const Lowerer = struct {
.variant => |v| l.enumType(v.@"enum", e.span),
.struct_lit => |x| l.enumOrStruct(x.name, e.span),
.field => |x| blk: {
if (std.mem.eql(u8, x.name, "len")) break :blk Ty{ .int = "usize" };
var base = l.peekType(x.e.*) orelse break :blk null;
const known = l.peekType(x.e.*);
if (std.mem.eql(u8, x.name, "len") and (known == null or !isNominalFieldBase(known.?))) break :blk Ty{ .int = "usize" };
var base = known orelse break :blk null;
if (base.isBorrow()) base = base.inner().?;
const d = l.decl(base) orelse break :blk null;
if (d.kind != .@"struct") break :blk null;
@@ -1477,6 +1478,12 @@ const Lowerer = struct {
fn typeOfNoFail(l: *Lowerer, t: ast.Type) ?Ty {
return switch (t.kind) {
.name => |n| if (isIntName(n)) Ty{ .int = n } else if (std.mem.eql(u8, n, "bool")) .bool else if (std.meta.stringToEnum(authority.Builtin, n)) |b| Ty{ .builtin = b } else l.enumOrStruct(n, t.span),
.array => |a| blk: {
if (a.len == 0 or a.len > (@as(u64, 1) << 32)) break :blk null;
const elem = l.typeOfNoFail(a.elem.*) orelse break :blk null;
if (elem == .void or elem == .never) break :blk null;
break :blk l.arrayOf(elem, a.len) catch null;
},
else => null,
};
}
@@ -2376,9 +2383,16 @@ const Lowerer = struct {
};
}
/// `x.len`: `slice.len` of a slice, the static length of an array.
fn isNominalFieldBase(t: Ty) bool {
// Slices of structs retain their builtin length; only ref/mut
// receivers expose fields of their nominal inner type.
return t == .named or ((t == .ref or t == .mut) and t.inner().? == .named);
}
/// `x.len`: a nominal field, `slice.len`, or the static array length.
fn lenOf(l: *Lowerer, e: ast.Expr, base: ast.Expr) Error!Value {
const bt = l.peekType(base) orelse return l.fail("E7007", base.span, "the type of this expression cannot be determined", &.{});
if (isNominalFieldBase(bt)) return l.fieldValue(e, base, "len");
const arr: ?u64 = switch (bt) {
.array => |a| a.len,
.ref, .mut => |x| if (x.* == .array) x.array.len else null,
@@ -0,0 +1,12 @@
// expect: reject E2101
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: u64 }
fn main() -> u64 effects [] {
var h: Header = Header { len: 42u64 };
mut child = h.len;
let bad = h.len;
*child = bad;
return bad;
}
@@ -0,0 +1,11 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Buffer { len: [u64; 2] }
fn main() -> u64 effects [] {
let b = Buffer { len: [40u64, 2u64] };
assert(b.len.len == 2usize);
return b.len[0] + b.len[1];
}
@@ -0,0 +1,11 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Flag { len: bool }
fn main() -> u64 effects [] {
let f = Flag { len: true };
if f.len and not false { return 42; }
return 0;
}
@@ -0,0 +1,13 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: u32 }
fn read(h: ref<Header>) -> u32 effects [] { return h.len; }
fn update(h: mut<Header>) -> u32 effects [] { h.len = h.len + 1; return h.len; }
fn main() -> u32 effects [] {
var h: Header = Header { len: 40u32 };
assert(update(mut h) == 41u32);
return read(ref h) + 1;
}
@@ -0,0 +1,13 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: u32 }
fn make(count: mut<u32>) -> Header effects [] { *count = *count + 1; return Header { len: 42u32 }; }
fn main() -> u32 effects [] {
var count: u32 = 0;
let answer = make(mut count).len;
assert(count == 1u32);
return answer;
}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
enum Status { ok, bad }
fn main() -> usize effects [] { let s = Status::ok; return s.len; }
@@ -0,0 +1,13 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Field<T: Copy> { len: T }
fn read<T: Copy>(f: ref<Field<T>>) -> T effects [] { return f.len; }
fn main() -> u32 effects [] {
var f: Field<u32> = Field::<u32> { len: 42u32 };
var flag: Field<bool> = Field::<bool> { len: true };
assert(read::<bool>(ref flag));
return read::<u32>(ref f);
}
@@ -0,0 +1,15 @@
// expect: run
// result: 42
// grant: heap=mem.alloc:64
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Record { len: u64, owner: own<u64> }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let owner = own.new(40u64, heap) or trap;
var r: Record = Record { len: 2u64, owner: move owner };
{ ref view = r; assert(view.len == 2); }
{ mut view = r; view.len = view.len + 1; }
let (length, part) = struct.unpack(move r);
return own.take(move part) + length - 1;
}
@@ -0,0 +1,13 @@
// expect: reject E1001
// grant: heap=mem.alloc:64
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Record { len: own<u64>, tag: u64 }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let owner = own.new(42u64, heap) or trap;
let r = Record { len: move owner, tag: 1u64 };
let bad = r.len;
let (part, tag) = struct.unpack(move r);
return own.take(move part) + tag - 1;
}
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { count: u32 }
fn main() -> u32 effects [] { let h = Header { count: 42u32 }; return h.len; }
@@ -0,0 +1,13 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: u32 }
struct Wrapper { len: Header }
fn main() -> u32 effects [] {
var w: Wrapper = Wrapper { len: Header { len: 40u32 } };
w.len.len = w.len.len + 1;
ref r = w;
return r.len.len + 1;
}
@@ -0,0 +1,18 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: u32 }
fn read(s: slice<Header>, i: usize) -> u32 effects [] {
assert(s.len == 2usize);
if i < s.len { return s[i].len; }
return 0u32;
}
fn main() -> u32 effects [] {
var data: [Header; 2] = [Header { len: 40u32 }, Header { len: 42u32 }];
assert(data.len == 2usize);
{ ref r = data; assert(r.len == 2usize); }
{ mut s = data[0..2]; assert(s.len == 2usize); s[1].len = s[0].len + 2; }
return read(ref data[0..2], 1usize);
}
@@ -0,0 +1,14 @@
// expect: run
// trap: T0008
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: usize }
fn read(s: slice<u64>, h: Header, i: usize) -> u64 effects [] {
if i < h.len { return s[i]; }
return 0;
}
fn main() -> u64 effects [] {
var data: [u64; 1] = [42u64];
return read(ref data[0..1], Header { len: 2usize }, 1usize);
}
@@ -0,0 +1,11 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: u32 }
fn main() -> u32 effects [] {
let h = Header { len: 41u32 };
let one = 1;
return one + h.len;
}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.field_len
struct Header { len: bool }
fn main() -> u32 effects [] { let h = Header { len: true }; return h.len; }
+45
View File
@@ -41,7 +41,52 @@ with tempfile.TemporaryDirectory(prefix="aji-generics-") as directory:
cli("report", "--manifest", manifest)
cli("explore", "--max-runs", "8", "--manifest", manifest)
assert "__aji_g_" in cli("lower", "--manifest", manifest)
# Nominal `len` fields keep their declared types across module and
# generic boundaries, including diagnostic source positions.
lib.write_text("""module app.lib
struct Field<T: Copy> { len: T }
fn read<T: Copy>(value: ref<Field<T>>) -> T effects [] { return value.len; }
fn make() -> Field<u32> effects [] { return Field::<u32> { len: 42u32 }; }
""")
entry.write_text("""module app.entry
import app.lib;
fn main() -> u32 effects [] {
var value: app.lib.Field<u32> = app.lib.Field::<u32> { len: 41u32 };
var flag: app.lib.Field<bool> = app.lib.Field::<bool> { len: true };
assert(app.lib.read::<bool>(ref flag));
assert(app.lib.make().len == 42u32);
let one = 1;
return one + app.lib.read::<u32>(ref value);
}
""")
cli("check", "--manifest", manifest)
canonical = cli("lower", "--manifest", manifest)
assert canonical == cli("lower", "--manifest", manifest)
assert "struct.get" in canonical and "borrow.field" in canonical
assert "slice.len" not in canonical
air.write_text(canonical)
source_record = json.loads(cli("run", "--fuel", "100000", "--manifest", manifest))
assert source_record["value"] == 42
assert json.loads(cli("run", "--fuel", "100000", air)) == source_record
cli("report", "--manifest", manifest)
cli("explore", "--max-runs", "8", "--manifest", manifest)
lib.write_text("""module app.lib
struct Field<T: Copy> { count: T }
fn read<T: Copy>(value: ref<Field<T>>) -> T effects [] { return value.len; }
""")
entry.write_text("""module app.entry
import app.lib;
fn main() -> u32 effects [] {
var value: app.lib.Field<u32> = app.lib.Field::<u32> { count: 42u32 };
return app.lib.read::<u32>(ref value);
}
""")
for command in ("check", "lower", "report", "run", "explore"):
error = json.loads(cli(command, "--manifest", manifest, code=1))
assert error["code"] == "E7002" and error["field"] == "len"
assert error["file"] == "lib.aji" and error["line"] == 3 and error["col"] == 65, error
lib.write_text("module app.lib\nfn id<T: Copy>(value: T) -> T effects [] { assert(false); return value; }\n")
entry.write_text("module app.entry\nimport app.lib;\nstruct Pair { value: u64 }\nfn main() -> u64 effects [] { return app.lib.id::<Pair>(Pair { value: 42 }).value; }\n")
record = json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, code=4))
assert record["source_location"]["file"] == "lib.aji"
lib.write_text("module app.lib\nfn id<T: Copy>(value: T) -> T effects [] { return missing(value); }\n")