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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user