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

+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,