fix(ir): missing-field multi-assign + promoted-union-member lvalue [F0.10]
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.
This commit is contained in:
32
examples/0166-types-union-promoted-member-lvalue.sx
Normal file
32
examples/0166-types-union-promoted-member-lvalue.sx
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
// Assigning to a field that does not exist on a struct produces the same
|
||||
// `field 'X' not found on type 'Y'` diagnostic as the read path (1100), and
|
||||
// exits 1 — never the `.unresolved` LLVM-emission panic.
|
||||
// exits 1 — never the `.unresolved` LLVM-emission panic, never a silent store
|
||||
// into a neighbouring field.
|
||||
//
|
||||
// Regression (issue 0094): the lvalue field lookup left `field_ty = .unresolved`
|
||||
// (lowerAssignment's assignment-target path) or silently GEP'd field 0 as `.s64`
|
||||
// (lowerExprAsPtr's fallback), so a missing-field store built a
|
||||
// pointer-to-`.unresolved` that panicked at LLVM emission. Both the
|
||||
// assignment-target path (`p.q`) and the nested lvalue-pointer path
|
||||
// (`o.missing.a`) now emit the field-not-found diagnostic.
|
||||
// (lowerExprAsPtr's fallback / lowerMultiAssign's struct loop), so a missing-field
|
||||
// store either built a pointer-to-`.unresolved` that panicked at LLVM emission or
|
||||
// silently wrote field 0. All three lvalue sites now emit the field-not-found
|
||||
// diagnostic: the assignment-target path (`p.q`), the nested lvalue-pointer path
|
||||
// (`o.missing.a`), and the multi-target store path (`p.r, y`).
|
||||
|
||||
Point :: struct { x: s64; }
|
||||
Inner :: struct { a: s64; }
|
||||
@@ -20,5 +22,8 @@ main :: () -> s32 {
|
||||
o := Outer.{ inner = Inner.{ a = 1 } };
|
||||
o.missing.a = 5; // site 2: lowerExprAsPtr fallback
|
||||
|
||||
y : s64 = 0;
|
||||
p.r, y = 3, 4; // site 3: lowerMultiAssign field path
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
x=42
|
||||
y=101
|
||||
@@ -1,11 +1,17 @@
|
||||
error: field 'q' not found on type 'Point'
|
||||
--> examples/1145-diagnostics-missing-struct-field-assign.sx:18:5
|
||||
--> examples/1145-diagnostics-missing-struct-field-assign.sx:20:5
|
||||
|
|
||||
18 | p.q = 2; // site 1: lowerAssignment target path
|
||||
20 | p.q = 2; // site 1: lowerAssignment target path
|
||||
| ^^^
|
||||
|
||||
error: field 'missing' not found on type 'Outer'
|
||||
--> examples/1145-diagnostics-missing-struct-field-assign.sx:21:5
|
||||
--> examples/1145-diagnostics-missing-struct-field-assign.sx:23:5
|
||||
|
|
||||
21 | o.missing.a = 5; // site 2: lowerExprAsPtr fallback
|
||||
23 | o.missing.a = 5; // site 2: lowerExprAsPtr fallback
|
||||
| ^^^^^^^^^
|
||||
|
||||
error: field 'r' not found on type 'Point'
|
||||
--> examples/1145-diagnostics-missing-struct-field-assign.sx:26:5
|
||||
|
|
||||
26 | p.r, y = 3, 4; // site 3: lowerMultiAssign field path
|
||||
| ^^^
|
||||
|
||||
Reference in New Issue
Block a user