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:
agra
2026-06-04 10:38:21 +03:00
parent d2bf8f3f2d
commit cd39316f5e
11 changed files with 275 additions and 41 deletions

View File

@@ -55,6 +55,48 @@ pub fn moduleConstInt(consts: *const std.StringHashMap(ModuleConstInfo), name: [
return null;
}
/// Evaluate a constant-expression array dimension to its integer value. Folds
/// integer `+ - * / %` and unary negate over int literals and named module /
/// comptime consts — recursively, so nested and parenthesised forms
/// (`[M + N - 1]`, `[(M + 1) * 2]`) fold (a grouping `(…)` carries no AST node;
/// the parser returns the inner expression). Leaf names resolve through
/// `ctx.lookupDimName`, so the stateful body-lowering path (which also sees
/// comptime constants and generic `$N` value bindings) and the stateless
/// registration path (module consts only) share THIS expression-folding logic
/// and cannot disagree on a dimension's value — the same unify-or-die rule that
/// keeps an array laid out via a type alias identical to the direct form
/// (issue 0083). Returns null when any operand is not a compile-time integer (a
/// runtime value, a non-comptime call, an unbound name) or the arithmetic
/// overflows / divides by zero: the caller then emits the clean compile-halting
/// diagnostic, never a fabricated length.
pub fn evalConstIntExpr(node: *const Node, ctx: anytype) ?i64 {
return switch (node.data) {
.int_literal => |lit| lit.value,
.identifier => |id| ctx.lookupDimName(id.name),
.type_expr => |te| ctx.lookupDimName(te.name),
.unary_op => |u| switch (u.op) {
.negate => {
const v = evalConstIntExpr(u.operand, ctx) orelse return null;
return if (v == std.math.minInt(i64)) null else -v;
},
else => null,
},
.binary_op => |b| {
const l = evalConstIntExpr(b.lhs, ctx) orelse return null;
const r = evalConstIntExpr(b.rhs, ctx) orelse return null;
return switch (b.op) {
.add => std.math.add(i64, l, r) catch null,
.sub => std.math.sub(i64, l, r) catch null,
.mul => std.math.mul(i64, l, r) catch null,
.div => std.math.divTrunc(i64, l, r) catch null,
.mod => if (r == 0) null else @rem(l, r),
else => null,
};
},
else => null,
};
}
pub const GlobalInfo = struct { id: inst.GlobalId, ty: TypeId };
/// Single lowering access point for declaration-name / import / visibility