fix(ir): unify named-const array-dim resolution + kill length-0 fabrication (0083)
A type alias whose dimension is a named const (`Arr :: [N]T`) resolves its dimension eagerly during scanDecls pass 1, on the stateless registration path, which can only read `module_const_map`. Typed consts (`N : s64 : 16`) register only in pass 2 and a forward-declared untyped const had not registered yet, so the stateless resolver saw an empty table, printed a non-fatal warning, fabricated length 0, and continued — yielding a 0-byte alloca, garbage reads, and a segfault for slice/struct elements. - scanDecls pass 0 pre-registers every integer-valued module const before any type alias resolves, so typed, untyped, and forward-referenced consts all resolve identically. - Both dim resolvers now share `program_index.moduleConstInt`, so the stateful body-lowering path and the stateless registration path cannot diverge. - `resolveArrayLen` returns `?u32`; `resolveCompound` yields `.unresolved` on null instead of a 0-length array. The stateful path emits a diagnostic; the alias-registration path surfaces an unresolved alias as a clean compile error that aborts the build. The Vector lane-count `else => 0` is fixed the same way. Regressions: examples/0143 (typed-const dim direct + via alias for s64/string/ struct, forward-ref alias, nested) and examples/1129 (an unresolvable computed dim halts with a clean diagnostic + non-zero exit). Both fail on the pre-fix compiler (garbage/segfault; warning+exit0) and pass after.
This commit is contained in:
65
examples/0143-types-typed-const-array-dim.sx
Normal file
65
examples/0143-types-typed-const-array-dim.sx
Normal file
@@ -0,0 +1,65 @@
|
||||
// A named-const array dimension lays out identically whether the const is
|
||||
// TYPED (`N : s64 : 16`) or untyped (`N :: 16`), used DIRECTLY (`a : [N]T`) or
|
||||
// through a type alias (`Arr :: [N]T`), and regardless of whether the const is
|
||||
// declared before or after the alias that consumes it.
|
||||
//
|
||||
// Regression (issue 0083): the stateless registration-time resolver
|
||||
// (type_bridge) only saw module consts that were already in `module_const_map`
|
||||
// when a type alias resolved its dimension. Typed consts register in a later
|
||||
// pass, and a forward-declared untyped const had not registered yet — so the
|
||||
// alias dimension fabricated length 0 (a 0-byte alloca), and element access
|
||||
// returned garbage (scalars) or bus-errored (slice/struct elements). Module
|
||||
// consts are now pre-registered before any alias resolves, and both the
|
||||
// stateful and stateless paths share one dimension resolver.
|
||||
#import "modules/std.sx";
|
||||
|
||||
NT : s64 : 8; // typed const used as a dimension
|
||||
|
||||
P :: struct { x: s64; y: s64; }
|
||||
|
||||
// Type aliases whose dimension is the TYPED const NT (stateless registration).
|
||||
TArr :: [NT]s64;
|
||||
TSArr :: [NT]string;
|
||||
TPArr :: [NT]P;
|
||||
|
||||
// Forward reference: this alias is declared BEFORE its dimension const NF.
|
||||
FArr :: [NF]s64;
|
||||
NF :: 5;
|
||||
|
||||
main :: () {
|
||||
// Typed-const dimension, DIRECT local decl.
|
||||
d : [NT]s64 = ---;
|
||||
d[0] = 3;
|
||||
d[7] = 21;
|
||||
print("direct d0={} d7={} len={}\n", d[0], d[7], d.len);
|
||||
|
||||
// Typed-const dimension via ALIAS (scalar): same layout as the direct form.
|
||||
a : TArr = ---;
|
||||
a[0] = 7;
|
||||
a[7] = 99;
|
||||
print("alias a0={} a7={} len={}\n", a[0], a[7], a.len);
|
||||
|
||||
// Typed-const dimension via ALIAS (string elements): no bus error.
|
||||
s : TSArr = ---;
|
||||
s[0] = "hi";
|
||||
s[7] = "yo";
|
||||
print("alias s0={} s7={}\n", s[0], s[7]);
|
||||
|
||||
// Typed-const dimension via ALIAS (struct elements).
|
||||
ps : TPArr = ---;
|
||||
ps[0] = P.{ x = 1, y = 2 };
|
||||
ps[7] = P.{ x = 5, y = 6 };
|
||||
print("alias p0x={} p0y={} p7x={}\n", ps[0].x, ps[0].y, ps[7].x);
|
||||
|
||||
// Nested fixed array whose both dimensions are the typed const NT.
|
||||
grid : [NT][NT]s64 = ---;
|
||||
grid[0][0] = 1;
|
||||
grid[7][7] = 10;
|
||||
print("nested g00={} g77={}\n", grid[0][0], grid[7][7]);
|
||||
|
||||
// Forward-referenced alias dimension (untyped const declared after it).
|
||||
f : FArr = ---;
|
||||
f[0] = 4;
|
||||
f[4] = 40;
|
||||
print("fwd f0={} f4={} len={}\n", f[0], f[4], f.len);
|
||||
}
|
||||
20
examples/1129-diagnostics-array-dim-not-const.sx
Normal file
20
examples/1129-diagnostics-array-dim-not-const.sx
Normal file
@@ -0,0 +1,20 @@
|
||||
// An array dimension that is not a compile-time integer constant is a hard
|
||||
// error, not a silently-fabricated 0-length array. Here a type alias's
|
||||
// dimension is a computed expression (`M + 1`), which the registration-time
|
||||
// resolver cannot evaluate.
|
||||
//
|
||||
// Regression (issue 0083): the stateless resolver printed a non-fatal warning
|
||||
// and fabricated length 0, then let compilation continue — producing a 0-byte
|
||||
// alloca and corrupt element access. It now yields the `.unresolved` sentinel,
|
||||
// which the alias registration surfaces as this diagnostic, aborting the build
|
||||
// with a non-zero exit.
|
||||
#import "modules/std.sx";
|
||||
|
||||
M :: 4;
|
||||
BadArr :: [M + 1]s64;
|
||||
|
||||
main :: () {
|
||||
a : BadArr = ---;
|
||||
a[0] = 7;
|
||||
print("a0={}\n", a[0]);
|
||||
}
|
||||
1
examples/expected/0143-types-typed-const-array-dim.exit
Normal file
1
examples/expected/0143-types-typed-const-array-dim.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
direct d0=3 d7=21 len=8
|
||||
alias a0=7 a7=99 len=8
|
||||
alias s0=hi s7=yo
|
||||
alias p0x=1 p0y=2 p7x=5
|
||||
nested g00=1 g77=10
|
||||
fwd f0=4 f4=40 len=5
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,5 @@
|
||||
error: type alias 'BadArr' could not be resolved: an array dimension is not a compile-time integer constant
|
||||
--> examples/1129-diagnostics-array-dim-not-const.sx:14:11
|
||||
|
|
||||
14 | BadArr :: [M + 1]s64;
|
||||
| ^^^^^^^^^^
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Reference in New Issue
Block a user