fix(ir): value-param type functions + range-checked dim/lane fold (0083, 0087)

Two remaining siblings in F0.4's comptime-int path.

1. Type-returning function with a value param used as a TYPE annotation
   (`b : Make(N, s64)` where `Make :: ($K: u32, $T: Type) -> Type`):
   - `isValueParamPosition` (semantic_diagnostics) now also skips a value
     param of a `fn_ast_map` type-returning function, so `N` is not walked
     as the type name "N" ("unknown type 'N'").
   - `resolveParameterizedWithBindings` routes a type-returning-function
     name to `instantiateTypeFunction` (the `.call` path already did).
   - `instantiateTypeFunction` resolves a general return-type expression
     (`return [K]T`) with bindings active — not just struct/union returns.
   `Make(N, s64)`, `Make(M + 1, s64)`, `Make(3, s64)` all resolve to one
   `[3]s64`.

2. Oversized dim/lane fold panicked the compiler (0087): an array dim /
   Vector lane folded to a valid i64 (5e9) then narrowed to u32 with an
   unchecked `@intCast`. New single gate `program_index.foldDimU32` folds
   via `evalConstIntExpr` then range-checks `[min, maxInt(u32)]`; the three
   narrowing sites (resolveArrayLen stateful + stateless, resolveVectorLane)
   all route through it and emit a clean diagnostic + halt instead of
   panicking. Value-param args stay i64 until used as a dim/lane, where the
   same gate checks them.

Regressions: examples/0208 (value-param type function), examples/1130
(oversized array dim clean halt), examples/1503 (oversized Vector lane
clean halt). Marks issue 0087 RESOLVED.

Gate: zig build, zig build test, bash tests/run_examples.sh — 398 passed,
0 failed, 0 timed out.
This commit is contained in:
agra
2026-06-04 12:13:45 +03:00
parent 7238eea084
commit efc09699e8
18 changed files with 321 additions and 52 deletions

View File

@@ -0,0 +1,32 @@
// A type-RETURNING function with a value parameter (`$K: u32`) used as a TYPE
// annotation: `b : Make(N, s64)` where `Make :: ($K, $T) -> Type { return [K]T; }`.
// A named-const value arg (`Make(N, s64)`), a const-expression value arg
// (`Make(M + 1, s64)`), and the literal form (`Make(3, s64)`) all instantiate to
// the SAME type — the array copy `b : Make(3, s64) = a` type-checks only because
// the three spellings name one `[3]s64`.
//
// Regression (issue 0083 / F0.4 attempt 6): the unknown-type checker walked the
// value-param position as a type name ("unknown type 'N'"), and the
// parameterized-type-annotation path never routed to `instantiateTypeFunction`,
// nor did that binder resolve a non-struct/union return shape (`return [K]T`).
// The value arg now folds through the shared const-int evaluator and the type
// function resolves its general return-type expression with bindings active.
#import "modules/std.sx";
N :: 3;
M :: 2;
Make :: ($K: u32, $T: Type) -> Type { return [K]T; }
main :: () {
a : Make(N, s64) = ---; // named-const value param
a[0] = 10; a[1] = 20; a[2] = 30;
print("named: len={} a0={} a2={}\n", a.len, a[0], a[2]);
e : Make(M + 1, s64) = ---; // const-expr value param (M + 1 == 3)
e[0] = 1; e[2] = 9;
print("expr: len={} e2={}\n", e.len, e[2]);
b : Make(3, s64) = a; // same instantiation → array copy type-checks
print("copy: len={} b2={}\n", b.len, b[2]);
}

View File

@@ -0,0 +1,15 @@
// An array dimension that folds to a valid compile-time integer but exceeds a
// `u32` (`[5_000_000_000]s64`) is a hard error — a clean sx diagnostic with a
// non-zero exit, NOT a compiler panic.
//
// Regression (issue 0087 / F0.4 attempt 6): the dimension folded to a valid i64
// (5e9) and was then narrowed with an unchecked `@intCast` to u32, aborting the
// COMPILER with "integer does not fit in destination type". Every dim/lane fold
// now narrows through the single range-checked `program_index.foldDimU32`, which
// reports an out-of-u32-range dimension as this diagnostic and halts the build.
#import "modules/std.sx";
main :: () {
a : [5000000000]s64 = ---;
print("unreachable: {}\n", a.len);
}

View File

@@ -0,0 +1,15 @@
// A `Vector` lane count that folds to a valid compile-time integer but exceeds a
// `u32` (`Vector(5_000_000_000, f32)`) is a hard error — a clean sx diagnostic
// with a non-zero exit, NOT a compiler panic.
//
// Regression (issue 0087 / F0.4 attempt 6): the lane folded to a valid i64 (5e9)
// and was then narrowed with an unchecked `@intCast` to u32, aborting the
// COMPILER with "integer does not fit in destination type". The lane now narrows
// through the single range-checked `program_index.foldDimU32`, which reports an
// out-of-u32-range lane as this diagnostic and halts the build.
#import "modules/std.sx";
main :: () {
v : Vector(5000000000, f32) = ---;
print("unreachable: {}\n", v.x);
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,3 @@
named: len=3 a0=10 a2=30
expr: len=3 e2=9
copy: len=3 b2=30

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,5 @@
error: array dimension 5000000000 does not fit in u32
--> examples/1130-diagnostics-array-dim-oversized-u32.sx:13:10
|
13 | a : [5000000000]s64 = ---;
| ^^^^^^^^^^

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,11 @@
error: Vector lane count 5000000000 does not fit in u32
--> examples/1503-vectors-oversized-lane-not-u32.sx:13:16
|
13 | v : Vector(5000000000, f32) = ---;
| ^^^^^^^^^^
error: field 'x' not found on type 'unresolved'
--> examples/1503-vectors-oversized-lane-not-u32.sx:14:32
|
14 | print("unreachable: {}\n", v.x);
| ^^^

View File

@@ -0,0 +1 @@