fix: thread optional child type into ?? struct-literal default (issue 0166)

The RHS of a null-coalesce was lowered with no target type, so a bare
struct literal default (x ?? .{ ... }) produced a struct_init with
.ty == .unresolved that panicked in emitStructInit. lowerNullCoalesce
now saves self.target_type, sets it to the optional's resolved child
before lowering nc.rhs, and restores it (leak-free). Verified across
struct/slice/enum/tuple/protocol/nested-optional/generic child types by
3 adversarial reviews.

Regression: examples/optionals/0912-null-coalesce-struct-literal.sx.
Filed adjacent pre-existing bug 0172 (?? on a non-optional lhs panics).
This commit is contained in:
agra
2026-06-22 22:17:01 +03:00
parent 0bc8005b99
commit 2ea25e84ec
7 changed files with 98 additions and 0 deletions

View File

@@ -1,5 +1,16 @@
# 0166 — `?? .{ ... }` struct-literal default panics with "unresolved type reached LLVM emission"
> **RESOLVED.** The `??` RHS struct literal was lowered with no target type, so
> its `struct_init.ty` stayed `.unresolved` and reached `emitStructInit`. Fix:
> in `src/ir/lower/expr.zig` `lowerNullCoalesce`, save `self.target_type`, set it
> to `inner_ty` (the optional's resolved child) before lowering `nc.rhs`, and
> restore afterward (unconditional, leak-free — `lowerExpr` returns a plain
> `Ref`). Verified across struct/slice/enum/tuple/protocol/nested-optional/
> generic child types and present/absent branches by 3 adversarial reviews.
> Regression: `examples/optionals/0912-null-coalesce-struct-literal.sx`.
> (Adjacent pre-existing bug found + filed: 0172 — `??` on a NON-optional lhs
> panics; `lowerNullCoalesce` must diagnose it.)
## Symptom
Using a struct literal as the default of a `??` (null-coalesce) operator panics: