fix(ir): resolve named-const array dims (0083) + materialize literal slice args (0084)
Two silent-miscompile codegen fixes:
0083 — named-const array dimension. `TypeResolver.resolveCompound`'s array
arm resolved the dimension with `if int_literal ... else 0`, so a named const
(`N :: 16; [N]T`) hit the silent `else 0`: the array became 0-length / 0-byte
and element access ran out of bounds (garbage for scalars, bus error for
slice/pointer/struct elements). The arm now delegates the dimension to
`inner.resolveArrayLen` (symmetric with `inner.resolveInner` for the element).
The stateful `Lowering.resolveArrayLen` evaluates it as a compile-time integer
across the comptime-constant / generic-value / module-global const tables and
emits a diagnostic — no fabricated length — when it isn't one.
0084 — `.[...]` literal passed directly as a call arg. `lowerArrayLiteral`
always yields an aggregate array value; the array→slice conversion is the
caller's job. The local-bound var-decl path did it, but the call-arg coercion
path had no array→slice arm, so `classify([N]T, []T)` returned `.none` and the
raw array was passed where a slice was expected (callee read its {ptr,len}
header off the wrong bytes → 0 / garbage / segfault). `classify` now returns a
new `.array_to_slice` plan for same-element `[N]T → []T`, and `coerceToType`
emits the existing `array_to_slice` op — identical to the local-bound path.
Regressions (fail-before/pass-after demonstrated on the pre-fix compiler):
examples/0140-types-named-const-array-dim.sx (s64 + string + struct elems)
examples/0141-types-slice-literal-direct-call-arg.sx (string + []s64)
Gate: zig build, zig build test, bash tests/run_examples.sh (387 passed).
Issues 0083 and 0084 marked RESOLVED.
This commit is contained in:
32
examples/0140-types-named-const-array-dim.sx
Normal file
32
examples/0140-types-named-const-array-dim.sx
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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.
|
||||
// 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.
|
||||
#import "modules/std.sx";
|
||||
|
||||
N :: 4;
|
||||
|
||||
P :: struct { x: s64; y: s64; }
|
||||
|
||||
main :: () {
|
||||
// Scalar elements: 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): used to bus-error.
|
||||
s : [N]string = ---;
|
||||
s[0] = "hi";
|
||||
s[1] = "yo";
|
||||
print("string s0={} s1={}\n", s[0], s[1]);
|
||||
|
||||
// Struct elements.
|
||||
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);
|
||||
}
|
||||
34
examples/0141-types-slice-literal-direct-call-arg.sx
Normal file
34
examples/0141-types-slice-literal-direct-call-arg.sx
Normal file
@@ -0,0 +1,34 @@
|
||||
// A `.[...]` array/slice literal passed DIRECTLY as a call argument behaves
|
||||
// identically to binding it to a typed local first: the literal is
|
||||
// materialized into addressable storage and a {ptr,len} slice header is built
|
||||
// over it, so the callee reads the element CONTENTS correctly.
|
||||
// Regression (issue 0084): a direct literal arg passed the raw array value
|
||||
// where a slice was expected, so the callee read its header off the wrong
|
||||
// bytes and returned garbage (0).
|
||||
#import "modules/std.sx";
|
||||
|
||||
count_nope :: (xs: []string) -> s64 {
|
||||
n := 0;
|
||||
i := 0;
|
||||
while i < xs.len { if xs[i] == "nope" { n += 1; } i += 1; }
|
||||
return n;
|
||||
}
|
||||
|
||||
sum :: (xs: []s64) -> s64 {
|
||||
s := 0;
|
||||
i := 0;
|
||||
while i < xs.len { s += xs[i]; i += 1; }
|
||||
return s;
|
||||
}
|
||||
|
||||
main :: () {
|
||||
// string slice: direct literal vs local-bound — both see 2 "nope"s.
|
||||
print("str direct={}\n", count_nope(.["a", "nope", "b", "nope"]));
|
||||
local : []string = .["a", "nope", "b", "nope"];
|
||||
print("str local={}\n", count_nope(local));
|
||||
|
||||
// numeric slice: direct literal vs local-bound — both sum to 100.
|
||||
print("num direct={}\n", sum(.[10, 20, 30, 40]));
|
||||
nums : []s64 = .[10, 20, 30, 40];
|
||||
print("num local={}\n", sum(nums));
|
||||
}
|
||||
1
examples/expected/0140-types-named-const-array-dim.exit
Normal file
1
examples/expected/0140-types-named-const-array-dim.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
scalar a0=7 a3=42
|
||||
string s0=hi s1=yo
|
||||
struct p0x=1 p0y=2 p2x=5
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
str direct=2
|
||||
str local=2
|
||||
num direct=100
|
||||
num local=100
|
||||
Reference in New Issue
Block a user