fix(ir): evaluate constant-expression array dimensions (0083)
A constant-FOLDABLE expression array dimension (`[M + 1]`, `[M * N]`, `[N - M]`, nested `[M + N - 1]`, parenthesised `[(M + 1) * 2]`, mixing untyped and typed module consts) was wrongly rejected as "not a compile-time integer constant" even though every operand is compile-time-known. Attempts 1-3 resolved only a bare named-const dim or a literal; an expression dim must be EVALUATED, not rejected. Fix: the shared dim resolver now routes the dimension through a single constant integer-expression evaluator (`program_index.evalConstIntExpr`) that folds integer `+ - * / %` and unary negate over literals and named/typed module consts, recursively (parentheses carry no AST node). The leaf-name lookup is delegated via `ctx.lookupDimName`, so the stateful body-lowering path (`Lowering`, which also sees comptime constants and generic `$N` values) and the stateless registration path (`type_bridge.StatelessInner`, module consts only) share the EXACT SAME folding logic and cannot diverge — an expression dim via a type alias resolves identically to the direct form. No-fabrication discipline unchanged: a genuinely non-comptime dimension (runtime local, non-comptime call, unbound name) or arithmetic that overflows / divides by zero still yields null -> `.unresolved` -> the same clean compile-halting diagnostic, never a fabricated length. - examples/0144-types-const-expr-array-dim.sx: every expression form, direct vs alias, scalar / string / struct element types (fails on the pre-fix compiler, passes after). - examples/1129 re-pointed at a genuinely non-const dimension (`[get()]s64`, a runtime call) so it still proves the stateless clean-halt (a foldable expression is no longer an error). - program_index.test.zig: unit test for evalConstIntExpr folding and clean-halt-on-non-const.
This commit is contained in:
77
examples/0144-types-const-expr-array-dim.sx
Normal file
77
examples/0144-types-const-expr-array-dim.sx
Normal file
@@ -0,0 +1,77 @@
|
||||
// A constant-FOLDABLE expression array dimension (`[M + 1]`, `[M * N]`,
|
||||
// `[N - M]`, nested `[M + N - 1]`, parenthesised `[(M + 1) * 2]`, and an
|
||||
// expression mixing an untyped and a typed module const) resolves to its
|
||||
// evaluated length — IDENTICALLY whether used DIRECTLY (`a : [M + 1]T`) or
|
||||
// through a type alias (`A :: [M + 1]T`), and for scalar, string (slice/pointer
|
||||
// class), and struct element types.
|
||||
//
|
||||
// Regression (issue 0083): the shared array-dimension resolver only looked up a
|
||||
// bare named const or a literal; any const-foldable EXPRESSION dimension was
|
||||
// rejected as "not a compile-time integer constant". It now routes the
|
||||
// dimension through the shared comptime integer-expression evaluator
|
||||
// (`program_index.evalConstIntExpr`), so integer `+ - * /` and parenthesisation
|
||||
// over literals and module consts fold on BOTH the stateful (direct) and
|
||||
// stateless (alias) paths — they share the one evaluator and cannot diverge.
|
||||
#import "modules/std.sx";
|
||||
|
||||
M :: 4;
|
||||
N :: 6;
|
||||
TK : s64 : 2; // typed const, used inside an expression dimension
|
||||
|
||||
P :: struct { x: s64; y: s64; }
|
||||
|
||||
AddAlias :: [M + 1]s64; // 5
|
||||
MulAlias :: [M * N]s64; // 24
|
||||
SubAlias :: [N - M]s64; // 2
|
||||
NestAlias :: [M + N - 1]s64; // 9
|
||||
ParenAlias :: [(M + 1) * 2]s64; // 10
|
||||
TypedAlias :: [M + TK]s64; // 6
|
||||
StrAlias :: [M + 1]string; // 5, slice/pointer elements
|
||||
StructAlias :: [M + 1]P; // 5, struct elements
|
||||
|
||||
main :: () {
|
||||
// const + literal: direct and via alias resolve to the same length.
|
||||
add_d : [M + 1]s64 = ---;
|
||||
add_a : AddAlias = ---;
|
||||
add_d[4] = 7;
|
||||
add_a[4] = 7;
|
||||
print("add direct.len={} alias.len={} d4={} a4={}\n", add_d.len, add_a.len, add_d[4], add_a[4]);
|
||||
|
||||
// const * const.
|
||||
mul_d : [M * N]s64 = ---;
|
||||
mul_a : MulAlias = ---;
|
||||
mul_d[23] = 230;
|
||||
mul_a[23] = 230;
|
||||
print("mul direct.len={} alias.len={} d23={} a23={}\n", mul_d.len, mul_a.len, mul_d[23], mul_a[23]);
|
||||
|
||||
// const - const.
|
||||
sub_d : [N - M]s64 = ---;
|
||||
sub_a : SubAlias = ---;
|
||||
sub_d[1] = 9;
|
||||
sub_a[1] = 9;
|
||||
print("sub direct.len={} alias.len={} d1={} a1={}\n", sub_d.len, sub_a.len, sub_d[1], sub_a[1]);
|
||||
|
||||
// nested and parenthesised forms (direct vs alias).
|
||||
nest_d : [M + N - 1]s64 = ---;
|
||||
nest_a : NestAlias = ---;
|
||||
paren_d : [(M + 1) * 2]s64 = ---;
|
||||
paren_a : ParenAlias = ---;
|
||||
print("nest direct.len={} alias.len={} paren direct.len={} alias.len={}\n", nest_d.len, nest_a.len, paren_d.len, paren_a.len);
|
||||
|
||||
// typed const inside the expression dimension.
|
||||
typ_d : [M + TK]s64 = ---;
|
||||
typ_a : TypedAlias = ---;
|
||||
print("typed direct.len={} alias.len={}\n", typ_d.len, typ_a.len);
|
||||
|
||||
// string elements (slice/pointer class) — no bus error, correct reads.
|
||||
str_a : StrAlias = ---;
|
||||
str_a[0] = "hi";
|
||||
str_a[4] = "yo";
|
||||
print("str alias.len={} s0={} s4={}\n", str_a.len, str_a[0], str_a[4]);
|
||||
|
||||
// struct elements.
|
||||
ps : StructAlias = ---;
|
||||
ps[0] = P.{ x = 1, y = 2 };
|
||||
ps[4] = P.{ x = 5, y = 6 };
|
||||
print("struct alias.len={} p0x={} p4y={}\n", ps.len, ps[0].x, ps[4].y);
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
// 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.
|
||||
// dimension is a runtime function call (`get()`), which is genuinely not
|
||||
// compile-time-known — the registration-time resolver cannot evaluate it.
|
||||
//
|
||||
// (A const-FOLDABLE expression dimension such as `[M + 1]` is NOT an error — it
|
||||
// folds; see examples/0144-types-const-expr-array-dim.sx. Only a dimension with
|
||||
// a genuinely runtime operand halts here.)
|
||||
//
|
||||
// Regression (issue 0083): the stateless resolver printed a non-fatal warning
|
||||
// and fabricated length 0, then let compilation continue — producing a 0-byte
|
||||
@@ -10,8 +14,8 @@
|
||||
// with a non-zero exit.
|
||||
#import "modules/std.sx";
|
||||
|
||||
M :: 4;
|
||||
BadArr :: [M + 1]s64;
|
||||
get :: () -> s64 { return 5; }
|
||||
BadArr :: [get()]s64;
|
||||
|
||||
main :: () {
|
||||
a : BadArr = ---;
|
||||
|
||||
1
examples/expected/0144-types-const-expr-array-dim.exit
Normal file
1
examples/expected/0144-types-const-expr-array-dim.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
1
examples/expected/0144-types-const-expr-array-dim.stderr
Normal file
1
examples/expected/0144-types-const-expr-array-dim.stderr
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
7
examples/expected/0144-types-const-expr-array-dim.stdout
Normal file
7
examples/expected/0144-types-const-expr-array-dim.stdout
Normal file
@@ -0,0 +1,7 @@
|
||||
add direct.len=5 alias.len=5 d4=7 a4=7
|
||||
mul direct.len=24 alias.len=24 d23=230 a23=230
|
||||
sub direct.len=2 alias.len=2 d1=9 a1=9
|
||||
nest direct.len=9 alias.len=9 paren direct.len=10 alias.len=10
|
||||
typed direct.len=6 alias.len=6
|
||||
str alias.len=5 s0=hi s4=yo
|
||||
struct alias.len=5 p0x=1 p4y=6
|
||||
@@ -1,5 +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
|
||||
--> examples/1129-diagnostics-array-dim-not-const.sx:18:11
|
||||
|
|
||||
14 | BadArr :: [M + 1]s64;
|
||||
18 | BadArr :: [get()]s64;
|
||||
| ^^^^^^^^^^
|
||||
|
||||
Reference in New Issue
Block a user