fix(ir): missing struct field assignment errors cleanly, no LLVM panic [F0.10]

Assigning to a nonexistent struct field (`p.q = 2` where Point has no `q`)
aborted the compiler with the `.unresolved` LLVM tripwire instead of a source
diagnostic (issue 0094). The lvalue field lookup never diagnosed a miss:

- `lowerAssignment`'s `.field_access` target left `field_ty = .unresolved` when
  no struct field matched, then built `ptrTo(field_ty)` and stored — so a
  pointer-to-`.unresolved` reached LLVM emission and tripped the panic.
- `lowerExprAsPtr`'s `.field_access` fallback returned
  `structGepTyped(obj_ptr, 0, .s64, obj_ty)` on a miss — a silent field-0/`.s64`
  default that mislowered the lvalue.

Both sites now reuse the read path's `emitFieldError` (the exact facility
`lowerFieldAccessOnType` uses), so read and write reject identically with
`field 'q' not found on type 'Point'`. `lowerExprAsPtr` also resolves
union/tagged-union fields via `union_gep` (the old `.s64` fallback was silently
standing in for union field access — e.g. `u.a[0] = v`), so that path is fixed,
not just made loud. The `types.zig` tripwire is untouched: the fix is to never
produce `.unresolved` for a missing-field store.

Regression tests:
- examples/1145-diagnostics-missing-struct-field-assign.sx — negative, both
  sites error, exit 1.
- examples/0165-types-nested-struct-field-assign.sx — positive, nested struct
  field write + address-of a matched field still work.
- src/ir/lower.test.zig — lowering unit test asserting the field-not-found
  diagnostic for a missing-field assignment.
This commit is contained in:
agra
2026-06-05 13:24:15 +03:00
parent 22c2e60efc
commit e13518e8aa
11 changed files with 210 additions and 2 deletions

View File

@@ -0,0 +1,21 @@
// Writing through a nested struct field lvalue (`outer.inner.x = v`) and taking
// the address of a valid field both resolve the field pointer correctly: the
// lvalue-pointer path (lowerExprAsPtr) GEPs the matched field, never a silent
// field-0 default. Positive companion to the missing-field diagnostic (1145).
#import "modules/std.sx";
Inner :: struct { a: s64; b: s64; }
Outer :: struct { inner: Inner; tag: s64; }
bump :: (p: *s64) { p.* = p.* + 100; }
main :: () {
o := Outer.{ inner = Inner.{ a = 1, b = 2 }, tag = 9 };
o.inner.a = 11; // nested struct field store via lowerExprAsPtr
o.inner.b = 22;
o.tag = 33; // direct struct field store
print("a={} b={} tag={}\n", o.inner.a, o.inner.b, o.tag);
bump(@o.inner.a); // address-of a matched nested field
print("a2={}\n", o.inner.a);
}

View File

@@ -0,0 +1,24 @@
// 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.
//
// 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.
Point :: struct { x: s64; }
Inner :: struct { a: s64; }
Outer :: struct { inner: Inner; }
main :: () -> s32 {
p := Point.{ x = 1 };
p.q = 2; // site 1: lowerAssignment target path
o := Outer.{ inner = Inner.{ a = 1 } };
o.missing.a = 5; // site 2: lowerExprAsPtr fallback
return 0;
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,2 @@
a=11 b=22 tag=33
a2=111

View File

@@ -0,0 +1,11 @@
error: field 'q' not found on type 'Point'
--> examples/1145-diagnostics-missing-struct-field-assign.sx:18:5
|
18 | 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
|
21 | o.missing.a = 5; // site 2: lowerExprAsPtr fallback
| ^^^^^^^^^