fix(ir): converge the comptime-int count surface (0083)

Three adjacent cells of the shared count surface still diverged from the
rest; all now route through the same leaf+fold+narrow+diagnose path.

1. Aliased integer constraint bypassed the value-param range gate — only
   builtin constraint names matched intTypeRange, so Box(5_000_000_000)
   with `$K: Count` (Count :: u32) compiled and bound a truncated value.
   resolveValueParamArg (shared by both the struct AND type-fn binder) now
   resolves the constraint to its underlying builtin via
   canonicalIntConstraintName (Count -> u32, Small -> s8) before
   range-checking, so an aliased integer constraint behaves exactly like
   the builtin it names.

2. A named const with an expression RHS (M :: 2; N :: M + 1) did not fold
   as a count — moduleConstInt read only a literal RHS node. It now folds
   every const's RHS through the shared evalConstIntExpr, cycle-guarded
   (mutual / self cycles fold to null, not a stack overflow), and pass-0
   pre-registers expression-RHS consts. N :: M + 1 == 3 at every consumer:
   dim (direct + alias), Vector lane, value-param (struct + type-fn),
   inline for.

3. Stateful resolveArrayLen still fabricated length 0 after a failed fold;
   it now returns null -> the .unresolved sentinel (no fabrication). The
   binding's lowering never reaches sizeOf (alloca defers it; hasErrors
   aborts first) and a field access on an already-diagnosed .unresolved
   value is poison-suppressed (emitFieldError), so a failed-fold dim emits
   ONE clean diagnostic with no panic.

Regressions: examples/0146 (full positive matrix — every consumer x leaf
form), 1135 (aliased u32 + s8 overflow), 1136 (direct non-const dim halts
cleanly). The cascade cleanup also tightened 1502/1503 to one diagnostic.
Unit test added for moduleConstInt expression-folding + cycle detection.
This commit is contained in:
agra
2026-06-04 14:09:46 +03:00
parent e03c087e5a
commit a821323c3c
18 changed files with 328 additions and 33 deletions

View File

@@ -214,6 +214,53 @@ test "floatToIntExact accepts integral floats, rejects the rest" {
try std.testing.expect(f(1.0e30) == null);
}
test "moduleConstInt folds expression-RHS consts and rejects cycles" {
var map = std.StringHashMap(pi.ModuleConstInfo).init(std.testing.allocator);
defer map.deinit();
// M :: 2 (literal), N :: M + 1 (expression), P :: N * 2 (expression over an
// expression const), F :: 4.0 (integral float), G :: 4.5 (fractional).
var m_val = nLit(2);
var m_id = nIdent("M");
var one = nLit(1);
var n_val = nBin(.add, &m_id, &one);
var n_id = nIdent("N");
var two = nLit(2);
var p_val = nBin(.mul, &n_id, &two);
var f_val = nFloat(4.0);
var g_val = nFloat(4.5);
try map.put("M", .{ .value = &m_val, .ty = .s64 });
try map.put("N", .{ .value = &n_val, .ty = .s64 });
try map.put("P", .{ .value = &p_val, .ty = .s64 });
try map.put("F", .{ .value = &f_val, .ty = .f64 });
try map.put("G", .{ .value = &g_val, .ty = .f64 });
try std.testing.expectEqual(@as(?i64, 2), pi.moduleConstInt(&map, "M"));
try std.testing.expectEqual(@as(?i64, 3), pi.moduleConstInt(&map, "N"));
try std.testing.expectEqual(@as(?i64, 6), pi.moduleConstInt(&map, "P"));
try std.testing.expectEqual(@as(?i64, 4), pi.moduleConstInt(&map, "F"));
try std.testing.expect(pi.moduleConstInt(&map, "G") == null);
try std.testing.expect(pi.moduleConstInt(&map, "absent") == null);
// A cyclic const has no compile-time integer value, and folding it must not
// recurse forever: mutual `A :: B + 0; B :: A + 0` and self `C :: C + 0` all
// fold to null via the frame-based cycle guard.
var a_id = nIdent("A");
var b_id = nIdent("B");
var c_id = nIdent("C");
var zero = nLit(0);
var a_val = nBin(.add, &b_id, &zero);
var b_val = nBin(.add, &a_id, &zero);
var c_val = nBin(.add, &c_id, &zero);
try map.put("A", .{ .value = &a_val, .ty = .s64 });
try map.put("B", .{ .value = &b_val, .ty = .s64 });
try map.put("C", .{ .value = &c_val, .ty = .s64 });
try std.testing.expect(pi.moduleConstInt(&map, "A") == null);
try std.testing.expect(pi.moduleConstInt(&map, "B") == null);
try std.testing.expect(pi.moduleConstInt(&map, "C") == null);
}
test "evalConstIntExpr folds an integral float literal, halts on a fractional one" {
const eval = pi.evalConstIntExpr;
const ctx = DimCtx{};