Files
sx/examples/0140-types-named-const-array-dim.sx
agra 1f9f944ca1 fix(ir): exhaustive named-const array dims (0083) + nested slice-literal coercion (0085)
Makes the F0.4 fixes exhaustive across every resolution / nesting path.

0083 — named-const array dimension, stateless paths. Attempt 1 fixed the
stateful resolver (direct local decls, struct fields, params, returns) but the
binding-free registration-time resolver (`type_bridge`, used for type aliases
`Arr :: [N]T` and inline union/enum field types) still resolved a named dim with
a silent `else 0`, so `Arr :: [N]s64; a : Arr` and `union { a: [N]s64 }` were
still miscompiled (garbage / bus error). Thread the module-global const table
(`ProgramIndex.module_const_map`) into `type_bridge` alongside the alias map, so
`StatelessInner.resolveArrayLen` resolves a named module-const dim to the same
length everywhere. The remaining unresolvable case (a computed/comptime dim on
the binding-free path, which the stateful path hard-errors) now bails LOUDLY
instead of fabricating a 0 length.

0085 — nested slice-literal elements. `lowerArrayLiteral` lowered each element
with the element type as target but appended the raw value. A nested `.[...]`
element at a slice element type (`[][]s64`) still lowers to an aggregate array
`[N]T`, so the outer aggregate held raw arrays where slice {ptr,len} headers
were expected — indexing the inner slice read a garbage pointer and segfaulted.
After lowering each element, coerce a same-element array to the slice element
type via the existing `array_to_slice` op. The coercion recurses with the
nesting, so `[][]T` and deeper materialize at every level — local-bound AND
direct-call-argument forms.

Regressions (fail-before/pass-after demonstrated on the pre-fix compiler):
  examples/0140-types-named-const-array-dim.sx — extended with type-alias,
    nested [N][M]T, and union-field named dims (s64 / string / struct elems)
  examples/0142-types-nested-slice-literal-elements.sx — [][]s64 + [][]string,
    local-bound vs direct-arg
  src/ir/type_bridge.test.zig — named-const dim resolves to literal length

Gate: zig build, zig build test, bash tests/run_examples.sh (388 passed).
Issues 0083 and 0085 marked RESOLVED.
2026-06-04 09:06:08 +03:00

70 lines
2.4 KiB
Plaintext

// A fixed array whose dimension is a module-global named constant
// (`N :: 16; [N]T`) has the same layout as a literal-dimension array
// (`[16]T`): correct length and element stride for scalar, slice/pointer
// (string), and struct element types — on EVERY type-resolution path:
// direct local decls, type aliases (`Arr :: [N]T`), nested fixed arrays
// (`[N][M]T`), and inline union fields. The named dim must resolve to the
// same length whether it flows through the stateful body-lowering resolver
// or the stateless registration-time resolver (type_bridge).
// Regression (issue 0083): a named-const dim resolved to length 0, giving a
// 0-byte alloca — scalar reads returned garbage and string/struct elements
// bus-errored. The alias and union-field paths went through the stateless
// resolver, which had no const table and silently fabricated a 0 length.
#import "modules/std.sx";
N :: 4;
M :: 3;
P :: struct { x: s64; y: s64; }
// Type aliases whose dimension is the named const N (stateless registration).
Arr :: [N]s64;
SArr :: [N]string;
// Inline union field with a named-const dimension (stateless registration).
U :: union { a: [N]s64; tag: s64; }
main :: () {
// Scalar elements (direct local): store then read back.
a : [N]s64 = ---;
a[0] = 7;
a[3] = 42;
print("scalar a0={} a3={}\n", a[0], a[3]);
// Slice/pointer elements (string, direct local): used to bus-error.
s : [N]string = ---;
s[0] = "hi";
s[1] = "yo";
print("string s0={} s1={}\n", s[0], s[1]);
// Struct elements (direct local).
ps : [N]P = ---;
ps[0] = P.{ x = 1, y = 2 };
ps[2] = P.{ x = 5, y = 6 };
print("struct p0x={} p0y={} p2x={}\n", ps[0].x, ps[0].y, ps[2].x);
// Type-alias dimension (scalar): same layout as the direct `[N]s64`.
aa : Arr = ---;
aa[0] = 11;
aa[3] = 99;
print("alias a0={} a3={}\n", aa[0], aa[3]);
// Type-alias dimension (string): no bus error, correct reads.
sa : SArr = ---;
sa[0] = "al";
sa[2] = "ok";
print("alias s0={} s2={}\n", sa[0], sa[2]);
// Nested fixed array `[N][M]s64`: both dimensions are named consts.
grid : [N][M]s64 = ---;
grid[0][0] = 1;
grid[3][2] = 8;
print("nested g00={} g32={}\n", grid[0][0], grid[3][2]);
// Inline union field with a named-const dimension.
u : U = ---;
u.a[0] = 70;
u.a[3] = 7;
print("union u0={} u3={}\n", u.a[0], u.a[3]);
}