fix(ir): integral-float counts + range-checked value-param binds (0083)
Item 2 (Agra ruling): a compile-time INTEGRAL float (`4.0`, `N : f64 : 4.0`, `N :: 4.0`) used as an array dimension / Vector lane / generic value-param count / `inline for` bound now folds to its integer at the shared leaf — `program_index.floatToIntExact`, used by both the `.float_literal` arm of `evalConstIntExpr` and `moduleConstInt`. All four consumers route through the one evaluator, so `[4.0]s64` lays out the same `[4]s64` uniformly; a non-integral (`4.5`) or negative value stays rejected by the downstream `foldDimU32` gate. Pass-0 now pre-registers float-valued module consts for forward-alias parity with int consts. Item 1: a generic value-param bind (`Box($K: u32)`) never range-checked the folded arg, so `Box(5_000_000_000)` compiled and ran. The bind now range-checks against the param's declared type — a `u32` count through the shared `foldDimU32` gate (making program_index's "single u32 gate for value-param counts" doc true), any other integer type through the new `program_index.intTypeRange` — and emits a clean "value N does not fit in u32 parameter K" otherwise. The declared type is threaded via a new `TemplateParam.value_type`. Regressions: examples 0145 (integral-float array dim), 1504 (Vector lane), 0611 (inline-for bound), 0209 (value-param integral-float), 1132 (non-integral float dim rejected), 1133 (negative float dim rejected), 1134 (oversized u32 value-param rejected) + program_index float-fold unit tests. Gate: zig build, zig build test, 406/0 run_examples.
This commit is contained in:
@@ -116,6 +116,9 @@ const DimCtx = struct {
|
||||
fn nLit(v: i64) ast.Node {
|
||||
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .int_literal = .{ .value = v } } };
|
||||
}
|
||||
fn nFloat(v: f64) ast.Node {
|
||||
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .float_literal = .{ .value = v } } };
|
||||
}
|
||||
fn nIdent(name: []const u8) ast.Node {
|
||||
return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .identifier = .{ .name = name } } };
|
||||
}
|
||||
@@ -191,3 +194,42 @@ test "evalConstIntExpr folds constant-expression array dimensions, halts on non-
|
||||
try std.testing.expect(eval(&cmp, ctx) == null);
|
||||
try std.testing.expect(eval(&ovf, ctx) == null);
|
||||
}
|
||||
|
||||
test "floatToIntExact accepts integral floats, rejects the rest" {
|
||||
const f = pi.floatToIntExact;
|
||||
// Integral floats (positive, zero, negative) fold to their exact integer.
|
||||
try std.testing.expectEqual(@as(?i64, 4), f(4.0));
|
||||
try std.testing.expectEqual(@as(?i64, 0), f(0.0));
|
||||
try std.testing.expectEqual(@as(?i64, -2), f(-2.0));
|
||||
// Non-integral / non-finite → null (the caller's clean halt).
|
||||
try std.testing.expect(f(4.5) == null);
|
||||
try std.testing.expect(f(0.1) == null);
|
||||
try std.testing.expect(f(std.math.inf(f64)) == null);
|
||||
try std.testing.expect(f(-std.math.inf(f64)) == null);
|
||||
try std.testing.expect(f(std.math.nan(f64)) == null);
|
||||
// Out-of-i64-range integral floats → null (no @intFromFloat range panic).
|
||||
// `-2^63` is exactly the i64 minimum and IS representable.
|
||||
try std.testing.expectEqual(@as(?i64, std.math.minInt(i64)), f(-9223372036854775808.0));
|
||||
try std.testing.expect(f(9223372036854775808.0) == null); // 2^63, just past maxInt(i64)
|
||||
try std.testing.expect(f(1.0e30) == null);
|
||||
}
|
||||
|
||||
test "evalConstIntExpr folds an integral float literal, halts on a fractional one" {
|
||||
const eval = pi.evalConstIntExpr;
|
||||
const ctx = DimCtx{};
|
||||
|
||||
var f4 = nFloat(4.0);
|
||||
var f45 = nFloat(4.5);
|
||||
var one = nLit(1);
|
||||
|
||||
// A direct integral float dimension (`[4.0]T`) folds; `4.5` does not.
|
||||
try std.testing.expectEqual(@as(?i64, 4), eval(&f4, ctx));
|
||||
try std.testing.expect(eval(&f45, ctx) == null);
|
||||
|
||||
// It composes inside an expression dimension (`4.0 + 1` → 5); a fractional
|
||||
// operand poisons the whole fold to null.
|
||||
var add = nBin(.add, &f4, &one);
|
||||
var addbad = nBin(.add, &f45, &one);
|
||||
try std.testing.expectEqual(@as(?i64, 5), eval(&add, ctx));
|
||||
try std.testing.expect(eval(&addbad, ctx) == null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user