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

@@ -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.

View File

@@ -96,3 +96,77 @@ test "ProgramIndex declaration maps round-trip (A1.1b)" {
try idx.ufcs_alias_map.put("len", "list_len");
try std.testing.expectEqualStrings("list_len", idx.ufcs_alias_map.get("len").?);
}
/// Stand-in for the leaf-name lookup both array-dimension resolvers pass to the
/// shared `evalConstIntExpr`: `M`/`N` resolve to integers, everything else is
/// genuinely non-comptime.
const DimCtx = struct {
pub fn lookupDimName(_: DimCtx, name: []const u8) ?i64 {
if (std.mem.eql(u8, name, "M")) return 4;
if (std.mem.eql(u8, name, "N")) return 6;
return null;
}
};
fn nLit(v: i64) ast.Node {
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .int_literal = .{ .value = v } } };
}
fn nIdent(name: []const u8) ast.Node {
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .identifier = .{ .name = name } } };
}
fn nBin(op: ast.BinaryOp.Op, l: *ast.Node, r: *ast.Node) ast.Node {
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .binary_op = .{ .op = op, .lhs = l, .rhs = r } } };
}
fn nNeg(operand: *ast.Node) ast.Node {
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .unary_op = .{ .op = .negate, .operand = operand } } };
}
test "evalConstIntExpr folds constant-expression array dimensions, halts on non-const" {
const eval = pi.evalConstIntExpr;
const ctx = DimCtx{};
var l5 = nLit(5);
var one = nLit(1);
var two = nLit(2);
var zero = nLit(0);
var m = nIdent("M");
var n = nIdent("N");
var z = nIdent("Z"); // unbound — genuinely non-comptime
// Leaves: literal, named const, unbound name.
try std.testing.expectEqual(@as(?i64, 5), eval(&l5, ctx));
try std.testing.expectEqual(@as(?i64, 4), eval(&m, ctx));
try std.testing.expect(eval(&z, ctx) == null);
// `M + 1`, `M * N`, `N - M`.
var add = nBin(.add, &m, &one);
var mul = nBin(.mul, &m, &n);
var sub = nBin(.sub, &n, &m);
try std.testing.expectEqual(@as(?i64, 5), eval(&add, ctx));
try std.testing.expectEqual(@as(?i64, 24), eval(&mul, ctx));
try std.testing.expectEqual(@as(?i64, 2), eval(&sub, ctx));
// Nested `(M + N) - 1` and parenthesised `(M + 1) * 2` (parens carry no node).
var addmn = nBin(.add, &m, &n);
var nested = nBin(.sub, &addmn, &one);
var paren = nBin(.mul, &add, &two);
try std.testing.expectEqual(@as(?i64, 9), eval(&nested, ctx));
try std.testing.expectEqual(@as(?i64, 10), eval(&paren, ctx));
// Unary negate.
var neg = nNeg(&m);
try std.testing.expectEqual(@as(?i64, -4), eval(&neg, ctx));
// Genuinely non-const operand, division by zero, a non-arithmetic operator,
// and overflow all yield null → the caller's clean compile-halt (no panic,
// no fabricated length).
var addz = nBin(.add, &m, &z);
var divz = nBin(.div, &m, &zero);
var cmp = nBin(.lt, &m, &n);
var big = nLit(std.math.maxInt(i64));
var ovf = nBin(.mul, &big, &two);
try std.testing.expect(eval(&addz, ctx) == null);
try std.testing.expect(eval(&divz, ctx) == null);
try std.testing.expect(eval(&cmp, ctx) == null);
try std.testing.expect(eval(&ovf, ctx) == null);
}

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

View File

@@ -40,34 +40,34 @@ const StatelessInner = struct {
pub fn resolveInner(self: StatelessInner, node: *const Node) TypeId {
return resolveAstType(node, self.table, self.alias_map, self.consts);
}
/// Fixed-array dimension at registration time: a literal `[16]T`, or a
/// named module-global const `N :: 16; [N]T` (typed `N : s64 : 16` too)
/// looked up in the const table. Both yield the SAME length — registration-
/// time paths (aliases, inline union/enum fields) must lay out a named-const
/// dim identically to a literal (issue 0083). Returns null when the dimension
/// is neither (a computed/comptime expression, or a name not bound to an
/// integer const). Null propagates to `resolveCompound`, which yields the
/// `.unresolved` sentinel rather than fabricating a 0 length that silently
/// gives a 0-byte array and out-of-bounds element access; the registration
/// caller surfaces the unresolved alias/type as a clean diagnostic.
/// Fixed-array dimension at registration time: a literal `[16]T`, a named
/// module-global const `N :: 16; [N]T` (typed `N : s64 : 16` too), or a
/// constant-foldable expression over those (`[M + 1]`, `[(M + 1) * 2]`).
/// Folds through the shared `program_index.evalConstIntExpr` — the SAME
/// evaluator the stateful body-lowering path uses — so a dimension resolves
/// to one length on every registration-time path (aliases, inline union/enum
/// fields) and matches the direct form (issue 0083). Returns null when the
/// dimension isn't a compile-time integer (a runtime value / non-comptime
/// call, or a name not bound to an integer const). Null propagates to
/// `resolveCompound`, which yields the `.unresolved` sentinel rather than
/// fabricating a 0 length that silently gives a 0-byte array and
/// out-of-bounds element access; the registration caller surfaces the
/// unresolved alias/type as a clean diagnostic.
pub fn resolveArrayLen(self: StatelessInner, len_node: *const Node) ?u32 {
switch (len_node.data) {
.int_literal => |lit| return if (lit.value >= 0) @intCast(lit.value) else null,
.identifier => |id| if (self.namedConstLen(id.name)) |n| return n,
.type_expr => |te| if (self.namedConstLen(te.name)) |n| return n,
else => {},
}
return null;
}
/// A name that resolves to a non-negative module-global integer constant →
/// its value. Shares `program_index.moduleConstInt` with the stateful
/// body-lowering resolver so the two paths cannot disagree on which named
/// consts a dimension resolves to (issue 0083).
fn namedConstLen(self: StatelessInner, name: []const u8) ?u32 {
const consts = self.consts orelse return null;
const v = program_index_mod.moduleConstInt(consts, name) orelse return null;
const v = program_index_mod.evalConstIntExpr(len_node, self) orelse return null;
return if (v >= 0) @intCast(v) else null;
}
/// Leaf-name lookup for the shared dimension evaluator: a name that resolves
/// to a module-global integer constant → its value. Shares
/// `program_index.moduleConstInt` with the stateful body-lowering resolver so
/// the two paths cannot disagree on which named consts a dimension resolves
/// to (issue 0083). The non-negative check is applied once, on the final
/// dimension value in `resolveArrayLen` — not here, so an intermediate
/// operand may legitimately be negative.
pub fn lookupDimName(self: StatelessInner, name: []const u8) ?i64 {
const consts = self.consts orelse return null;
return program_index_mod.moduleConstInt(consts, name);
}
};
// ── AST Node → TypeId ───────────────────────────────────────────────────