Files
sx/examples/optionals/0906-optionals-struct-literal-into-optional.sx
agra 1b0c857b91 fix: struct-literal → optional coercion + #get through optional chain (issue 0160)
Two fixes for optional interactions surfaced by the #set/#get review. The
original issue 0160 mis-diagnosed (A) as an optional-chain bug; the chain works
fine for real fields. The actual bugs:

(A) A bare struct literal `.{ ... }` against an optional target `?T` was built
into the optional's {payload, has_value} layout instead of the inner T, then
re-wrapped — corrupting the value (a multi-field payload's first field clobbered
by the has_value flag, or a `?T` arg silently null) or failing LLVM
verification. lowerStructLiteral now builds the inner T, materializes it, and
wraps via coerceToType; lowerVarDecl's previously-UNCONDITIONAL optional wrap is
guarded so an already-`?T` value isn't double-wrapped. Fixed across var-decl,
arg, return, nested field, reassignment, and array-element contexts.

(B) `#get` accessors are now reachable through an optional chain (`obj?.getter`):
lowerOptionalChain dispatches the getter via a synthetic receiver, and
expr_typer types `obj?.getter` through a shared getterReturnTypeOnDeref helper
(handles `?T` and `?*T`, value and pointer optionals, and generic-instance
getters like List.len). The `#set` write side through `?.` is intentionally left
matching real-field behavior (optional-chain assignment unsupported).

Regression tests: examples/optionals/0906 (struct-literal → optional) and 0907
(accessor through chain). issues/0160 marked RESOLVED with the corrected root
cause.
2026-06-22 18:28:57 +03:00

42 lines
1.6 KiB
Plaintext

// A bare struct literal `.{ ... }` against an optional target `?T` builds the
// inner `T` and wraps it once — in every caller context: var-decl, function
// argument, return value, a nested `?T` struct field, reassignment, and an
// array element. Previously this filled the optional's {payload, has_value}
// layout directly, corrupting the value (a multi-field payload's first field
// was clobbered by the has_value flag, or a `?T` arg silently read as null) or
// failing LLVM verification on a double wrap.
// Regression (issue 0160).
#import "modules/std.sx";
T :: struct { a: i64 = 0; b: i64 = 0; }
Outer :: struct { opt: ?T = null; tag: i64 = 0; }
take :: (o: ?T) -> i64 { if o != null { return o!.a * 10 + o!.b; } return -1; }
make :: () -> ?T { return .{ a = 5, b = 6 }; }
main :: () -> i64 {
// var-decl
v : ?T = .{ a = 3, b = 9 };
if v != null { print("var: {} {}\n", v!.a, v!.b); } // 3 9
// function argument
print("arg: {}\n", take(.{ a = 4, b = 7 })); // 47
// return value
r := make();
if r != null { print("ret: {} {}\n", r!.a, r!.b); } // 5 6
// nested ?T struct field
o : Outer = .{ opt = .{ a = 1, b = 2 }, tag = 8 };
if o.opt != null { print("nested: {} {} {}\n", o.opt!.a, o.opt!.b, o.tag); } // 1 2 8
// reassignment
v = .{ a = 11, b = 12 };
if v != null { print("reassign: {} {}\n", v!.a, v!.b); } // 11 12
// array element
arr : [2]?T = .[ .{ a = 20 }, .{ a = 21 } ];
if arr[0] != null { if arr[1] != null { print("arr: {} {}\n", arr[0]!.a, arr[1]!.a); } } // 20 21
return 0;
}