Files
sx/examples/1145-diagnostics-missing-struct-field-assign.sx
agra e13518e8aa 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.
2026-06-05 13:24:15 +03:00

25 lines
999 B
Plaintext

// 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;
}