Files
sx/examples/types/0166-types-union-promoted-member-lvalue.sx
agra 66bdc70bf1 test: group examples into per-category folders
Move examples/*.sx and their expected/ snapshots into per-category
subfolders (examples/<category>/...). Folder = leading filename token,
with ffi-objc/ffi-jni kept whole; filenames are unchanged. The corpus
runner and LSP sweep now discover each category's expected/ dir, while
issues/ stays flat. Example 1058's repo-root-relative companion import
is made file-relative. Path strings embedded in 164 snapshots were
regenerated (path-only changes). Test-layout docs in CLAUDE.md updated.
2026-06-21 14:41:34 +03:00

33 lines
1.1 KiB
Plaintext

// 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]i64;
struct { x: i64; y: i64; };
}
bump :: (p: *i64) {
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);
}