Completes the issue-0094 fix. attempt-1 made single-assign and address-of diagnose a missing struct field; the stress-review found two remaining defects in that change: 1. lowerMultiAssign's `.field_access` target kept the pre-fix shape — a struct-only loop that defaulted `field_idx 0` / `field_ty .unresolved` on a miss, then built the GEP and stored unconditionally. A missing field (`p.q, y = 2, 3`) silently wrote field 0 (printed `x=2 y=3`, no diagnostic), and a valid promoted-union / tuple member at a non-zero offset corrupted field 0 instead of its own slot. 2. attempt-1's new union branch in lowerExprAsPtr resolved only DIRECT union field names, so `@v.x` on a promoted anonymous-struct member reported "field 'x' not found on type 'Vec2'" even though `v.x = 41` worked. Both lvalue-pointer sites and the multi-assign store now route through one shared resolver, `fieldLvaluePtr`, that handles struct fields, union direct fields, promoted anonymous-struct union members, and tuple elements, and returns null (no field-0 / `.unresolved` default) on a genuine miss. Each caller emits the read path's `emitFieldError` on null. This collapses the three previously-divergent field-lvalue walks into one, fixing the multi-assign missing-field corruption, the promoted-member over-rejection, and (as a side effect of correct resolution) non-zero-offset promoted-union and tuple multi-assign stores. The types.zig tripwire is untouched. Regression tests: - examples/1145 extended: multi-assign missing field (`p.r, y`) errors, exit 1. - examples/0166 (new): promoted union member written and address-of'd, including a non-zero-offset member (`@v.y`), compiles and runs. - src/ir/lower.test.zig: multi-assign missing-field field-not-found unit test.
33 lines
1.1 KiB
Plaintext
33 lines
1.1 KiB
Plaintext
// Taking the address of a promoted anonymous-struct union member yields a
|
|
// pointer to that member's slot, so mutating through the pointer is visible
|
|
// through the member. The write path (`v.x = 41`) and the read path already
|
|
// resolve promoted members; the lvalue-pointer path (`@v.x`) now resolves them
|
|
// too, via the shared field-lvalue resolver.
|
|
//
|
|
// Regression (issue 0094, attempt 2): lowerExprAsPtr's union branch handled
|
|
// only DIRECT union field names, so `@v.x` on a promoted member reported
|
|
// "field 'x' not found on type 'Vec2'" even though `v.x = 41` worked. The
|
|
// over-rejection is gone, and a member that is NOT at offset 0 (`v.y`) resolves
|
|
// to its own slot — not a default field 0.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
Vec2 :: union {
|
|
data: [2]s64;
|
|
struct { x: s64; y: s64; };
|
|
}
|
|
|
|
bump :: (p: *s64) {
|
|
p.* = p.* + 1;
|
|
}
|
|
|
|
main :: () {
|
|
v : Vec2 = ---;
|
|
v.x = 41;
|
|
v.y = 100;
|
|
bump(@v.x); // promoted member at offset 0 → 42
|
|
bump(@v.y); // promoted member at offset 8 → 101 (its own slot)
|
|
print("x={}\n", v.x);
|
|
print("y={}\n", v.y);
|
|
}
|