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:
@@ -11681,17 +11681,22 @@ pub const Lowering = struct {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// Evaluate a fixed-array dimension to a compile-time integer: a literal, or
|
||||
/// a name bound to an integer in the comptime-constant (`OS`/loop cursors),
|
||||
/// generic-value (`$N`), or module-global const (`N :: 16`) tables. Returns
|
||||
/// null when the dimension isn't a compile-time integer.
|
||||
/// Evaluate a fixed-array dimension to a compile-time integer: a literal, a
|
||||
/// name bound to an integer (comptime-constant `OS`/loop cursors, generic
|
||||
/// `$N` value, or module-global const `N :: 16`), or a constant-foldable
|
||||
/// expression over those (`[M + 1]`, `[(M + 1) * 2]`). Delegates the
|
||||
/// expression folding to the shared `program_index.evalConstIntExpr` so this
|
||||
/// body-lowering path and the stateless registration path cannot diverge on
|
||||
/// a dimension's value. Returns null when the dimension isn't a compile-time
|
||||
/// integer.
|
||||
fn comptimeArrayDim(self: *Lowering, node: *const Node) ?i64 {
|
||||
return switch (node.data) {
|
||||
.int_literal => |lit| lit.value,
|
||||
.identifier => |id| self.comptimeIntNamed(id.name),
|
||||
.type_expr => |te| self.comptimeIntNamed(te.name),
|
||||
else => null,
|
||||
};
|
||||
return program_index_mod.evalConstIntExpr(node, self);
|
||||
}
|
||||
|
||||
/// Leaf-name lookup for the shared dimension evaluator: a name bound to a
|
||||
/// compile-time integer across the three const tables.
|
||||
pub fn lookupDimName(self: *Lowering, name: []const u8) ?i64 {
|
||||
return self.comptimeIntNamed(name);
|
||||
}
|
||||
|
||||
/// Resolve a name to a compile-time integer across the three const tables.
|
||||
|
||||
Reference in New Issue
Block a user