lang: tuple element assignment + named-tuple field names

Two fixes:
- Element assignment `t.0 = v` (the known Phase-4.2 gap): the lvalue path
  looked the element up by NAME via getStructFields, never matched a tuple
  (positional), and left field_ty .unresolved -> ptr(.unresolved) -> codegen
  panic. Added a tuple branch to the field-assignment lowering that indexes by
  position (numeric) or name (tup.names), mirroring the read path. Fixes
  `c.sources.0 = v` on a generic-instance pack field too.
- Named tuples: the parser dropped captured field names for a tuple TYPE
  `(x: T, y: U)` (passed field_names=null), and resolveTupleTypeWithBindings
  also nulled them. Both now preserve names (synthesizing _<i> for any unnamed
  slot), so `t.x` reads/writes by name and `.0` by position.

examples/208. 243 examples + unit green.
This commit is contained in:
agra
2026-05-30 03:00:58 +03:00
parent a922814ba3
commit 39d77ff886
5 changed files with 89 additions and 3 deletions

View File

@@ -1920,6 +1920,38 @@ pub const Lowering = struct {
}
}
// Tuple field element: `t.0 = v` / `t.x = v` — indexed by
// position (numeric) or by name, not via `getStructFields`
// (a tuple's elements aren't named-struct fields). Mirrors
// the read path's tuple handling.
if (!obj_ty.isBuiltin()) {
const ti = self.module.types.get(obj_ty);
if (ti == .tuple) {
const tup = ti.tuple;
var elem_idx: ?usize = null;
if (std.fmt.parseInt(usize, fa.field, 10)) |n| {
if (n < tup.fields.len) elem_idx = n;
} else |_| {
if (tup.names) |names| {
for (names, 0..) |nm, i| {
if (nm == field_name_id and i < tup.fields.len) {
elem_idx = i;
break;
}
}
}
}
if (elem_idx) |idx| {
const elem_ty = tup.fields[idx];
const gep = self.builder.structGepTyped(obj_ptr, @intCast(idx), self.module.types.ptrTo(elem_ty), obj_ty);
const src_ty = self.builder.getRefType(val);
const coerced = self.coerceToType(val, src_ty, elem_ty);
self.storeOrCompound(gep, coerced, asgn.op, elem_ty);
return;
}
}
}
const struct_fields = self.getStructFields(obj_ty);
var field_idx: u32 = 0;
var field_ty: TypeId = .unresolved;
@@ -11199,19 +11231,34 @@ pub const Lowering = struct {
fn resolveTupleTypeWithBindings(self: *Lowering, tt: *const ast.TupleTypeExpr) TypeId {
var field_ids = std.ArrayList(TypeId).empty;
defer field_ids.deinit(self.alloc);
var had_spread = false;
for (tt.field_types) |ft| {
if (ft.data == .spread_expr) {
if (self.packTypeElems(ft.data.spread_expr.operand)) |elems| {
defer self.alloc.free(elems);
for (elems) |e| field_ids.append(self.alloc, e) catch return .void;
had_spread = true;
continue;
}
}
field_ids.append(self.alloc, self.resolveTypeWithBindings(ft)) catch return .void;
}
// Preserve field names for a named tuple `(x: T, y: U)` so `t.x` resolves
// (matches type_bridge.resolveTupleType). A spread expands to unnamed
// pack elements, so names only apply when there was no spread.
var name_ids: ?[]const types.StringId = null;
if (!had_spread) {
if (tt.field_names) |names| {
if (names.len == field_ids.items.len) {
var ids = std.ArrayList(types.StringId).empty;
for (names) |n| ids.append(self.alloc, self.module.types.internString(n)) catch return .void;
name_ids = ids.toOwnedSlice(self.alloc) catch null;
}
}
}
return self.module.types.intern(.{ .tuple = .{
.fields = self.alloc.dupe(TypeId, field_ids.items) catch return .void,
.names = null,
.names = name_ids,
} });
}