fix(ir): array-dim/count path joins the unified float→int rule — all 5 sites consistent [F0.11]

The compile-time count fold (array dimension / Vector lane / value-param) was
integer-only: it folded a DIRECT integral float literal (`[4.0]`, `[N]` with
`N : f64 : 4.0`) but rejected an INTEGRAL expression built from a non-integral
float-const leaf (`[F + 1.5]` = 4.0, `F : f64 : 2.5`) — and a const folded from
one (`[K]` with `K : s64 : F + 1.5`) — as "must be a compile-time integer
constant". This was the last of issue 0095's five narrowing sites (local /
field / param / const / array-dim) still diverging.

Route the count fold through the SAME compile-time float evaluation the other
four sites use:

- New `program_index.foldCountI64` — the single int-or-integral-float count
  fold: `evalConstIntExpr` first, then (only on failure) `evalConstFloatExpr` +
  `floatToIntExact`. `foldDimU32` (dim/lane/u32 value-param), the non-u32
  value-param gate, and `emitModuleConst`'s integer-const materialization all
  delegate to it, so a const's emitted value and its use as a count come from
  one fold (no parallel integral check, no two-resolver divergence — issue 0083).
- New `DimU32.non_integral_float` variant carries a non-integral float dim to a
  distinct, accurate diagnostic ("array dimension must be an integer, but '2.75'
  is a non-integral float") — the cast-escape advice the binding sites give does
  not apply in a count position, so the dim wording omits it. `reportDimError`,
  the Vector-lane resolver, and the top-level array-alias diagnostic all handle
  the new variant, so the DIRECT and type-ALIAS forms emit the identical message.
- `type_bridge.StatelessInner.lookupFloatName` (via `moduleConstFloat`) is the
  float twin of its `lookupDimName`, so the registration-time alias path folds a
  float-const-leaf dimension to the SAME count as the stateful direct path.

`inline for` range bounds are spec endpoints, not counts (specs.md §2), so they
keep the int-only fold deliberately (no silent-truncation bug there).

Relaxes the F0.4 `examples/1132` wording: a non-integral float const dim now
reports the precise "non-integral float" message (it still errors).

Regression: 0168 (positive — `[F + 1.5]s64`, `[KF]s64`, alias `ArrFE` all fold
to len 4), 1146 (negative — `[F + 0.25]s64` errors), 1132 (precise wording), and
a `foldCountI64`/`foldDimU32` unit test. issues/0095 marked RESOLVED (attempt 4).
specs.md + readme.md state the unified rule across all five sites.
This commit is contained in:
agra
2026-06-05 17:43:45 +03:00
parent b6d66d9c56
commit b73363ca4c
13 changed files with 268 additions and 83 deletions

View File

@@ -1,10 +1,12 @@
// Unified float→int narrowing rule (F0.11), POSITIVE side: an INTEGRAL float
// flowing into an integer-typed binding FOLDS to its integer — the same
// `floatToIntExact` rule an array dimension / `$K: Count` already uses — across
// a typed LOCAL, a struct FIELD default, a typed module CONST, and a function
// PARAM default. It folds whether written as a float LITERAL (`4.0`), an
// INT-const-EXPRESSION (`M + 2.0`, with `M :: 2`), or a FLOAT-const-LEAF
// expression whose sum is integral (`F + 1.5`, with `F : f64 : 2.5`, = 4.0).
// all FIVE sites: a typed LOCAL, a struct FIELD default, a typed module CONST, a
// function PARAM default, and an array DIMENSION. It folds whether written as a
// float LITERAL (`4.0`), an INT-const-EXPRESSION (`M + 2.0`, with `M :: 2`), or a
// FLOAT-const-LEAF expression whose sum is integral (`F + 1.5`, with
// `F : f64 : 2.5`, = 4.0) — including such a float-const-leaf expression driving
// an array dimension directly, through a const, or via a type alias.
// The escape hatch (`xx` / `cast`) still TRUNCATES any float, integral or not —
// including a non-integral const expression (`xx (M + 0.5)` / `xx (F + 0.25)`).
//
@@ -32,6 +34,10 @@ withFlt :: (x : s64 = F + 1.5) -> s64 { return x; } // float-const-leaf pa
K : s64 : 8.0; // integral float module const → folds to 8
KF : s64 : F + 1.5; // integral float-const-LEAF module const → folds to 4
ArrFE :: [F + 1.5]s64; // array-dim type ALIAS over a float-const-leaf expr → [4]s64
// (the stateless registration path must agree with the
// direct form `a : [F + 1.5]s64` below — issue 0083).
main :: () {
// Typed local: integral float folds (literal + int-const expr + float-const leaf).
z : s64 = 4.0;
@@ -54,6 +60,15 @@ main :: () {
a : [K]s64 = ---;
print("const={} constFlt={} len={}\n", K, KF, a.len);
// Array DIMENSION — the fifth site joins the unified rule: an integral
// float-const-leaf expression folds to a count whether written DIRECTLY
// (`[F + 1.5]` → 4), THROUGH a float-expr const (`[KF]`, KF = F + 1.5 = 4),
// or via a type ALIAS (`ArrFE`, the stateless path agreeing with the direct).
ad : [F + 1.5]s64 = ---;
ak : [KF]s64 = ---;
aa : ArrFE = ---;
print("dim.direct={} dim.const={} dim.alias={}\n", ad.len, ak.len, aa.len);
// Explicit escape: `xx` / `cast` always truncate, integral or not —
// including a non-integral const EXPRESSION (`xx (M + 0.5)` → 2) and a
// non-integral float-const-LEAF expression (`xx (F + 0.25)` → 2).

View File

@@ -2,6 +2,11 @@
// error — only an integral float (`4.0`) folds to a count. Clean diagnostic +
// non-zero exit, NOT a fabricated length.
//
// The dimension follows the unified float→int narrowing rule (F0.11 / issue
// 0095): an integral float folds, a non-integral one is rejected as "not an
// integer" — the SAME `floatToIntExact` judgement the typed local/field/param/
// const sites use (the count path is the last of the five sites to unify).
//
// Regression (F0.4 attempt 8, Agra ruling): the integral-float rule accepts
// `4.0` as a dimension but must keep rejecting `4.5` (it is not an integer).
#import "modules/std.sx";

View File

@@ -1,14 +1,16 @@
// Unified float→int narrowing rule (F0.11), NEGATIVE side: a NON-INTEGRAL float
// implicitly narrowing to an integer-typed binding is a COMPILE ERROR — not a
// silent truncation. The rule fires at a typed LOCAL initializer, a function
// PARAM default, and a struct FIELD default; each emits a narrowing diagnostic
// at the offending float and aborts (exit 1). It fires whether the float is a
// LITERAL (`1.5`), an INT-const-expression (`M + 0.5`, with `M :: 2`), or a
// FLOAT-const-leaf expression (`F + 0.25`, with `F : f64 : 2.5`, = 2.75) — all
// three are the core of issue 0095, which previously slipped through and
// truncated to 2. The fix is the integral-fold / non-integral-error rule shared
// with the array-dimension path, applied to ANY compile-time-constant float
// expression (literal, int-const leaf, float-const leaf, and combinations).
// PARAM default, a struct FIELD default, AND an array DIMENSION; each emits a
// narrowing diagnostic at the offending float and aborts (exit 1). It fires
// whether the float is a LITERAL (`1.5`), an INT-const-expression (`M + 0.5`,
// with `M :: 2`), or a FLOAT-const-leaf expression (`F + 0.25`, with
// `F : f64 : 2.5`, = 2.75) — all three are the core of issue 0095, which
// previously slipped through and truncated to 2. The fix is the integral-fold /
// non-integral-error rule shared across all five sites (local, field, param,
// const, and array dimension), applied to ANY compile-time-constant float
// expression (literal, int-const leaf, float-const leaf, and combinations). The
// array-dimension site phrases the same rejection as "must be an integer".
//
// The escape hatch stays open: `y : s64 = xx 1.5` (or `cast(s64) 1.5`)
// truncates with no error — exercised on the POSITIVE side (example 0168).
@@ -34,8 +36,9 @@ main :: () {
y : s64 = 1.5; // non-integral float LITERAL local → error
ye : s64 = M + 0.5; // non-integral int-const-EXPRESSION local → error
yf : s64 = F + 0.25; // non-integral float-const-LEAF local → error
ad : [F + 0.25]s64 = ---; // non-integral float-const-LEAF array DIMENSION → error
b := Bad.{};
print("{} {} {}\n", b.f, b.fe, b.ff);
print("{} {} {}\n", badLit(), badExpr(), badFlt());
print("{} {} {}\n", y, ye, yf);
print("{} {} {} {}\n", y, ye, yf, ad.len);
}

View File

@@ -3,4 +3,5 @@ neg=-2
field=4 fieldExpr=4 fieldFlt=4
param=6 paramFlt=4
const=8 constFlt=4 len=8
dim.direct=4 dim.const=4 dim.alias=4
xx=4 cast=1 xxExpr=2 xxFlt=2

View File

@@ -1,5 +1,5 @@
error: array dimension must be a compile-time integer constant
--> examples/1132-diagnostics-array-dim-non-integral-float.sx:12:10
error: array dimension must be an integer, but '4.5' is a non-integral float
--> examples/1132-diagnostics-array-dim-non-integral-float.sx:17:10
|
12 | a : [N]s64 = ---;
17 | a : [N]s64 = ---;
| ^

View File

@@ -1,53 +1,59 @@
error: cannot implicitly narrow non-integral float '1.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:34:16
|
34 | y : s64 = 1.5; // non-integral float LITERAL local → error
| ^^^
error: cannot implicitly narrow non-integral float '2.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:35:16
|
35 | ye : s64 = M + 0.5; // non-integral int-const-EXPRESSION local → error
| ^^^^^^^
error: cannot implicitly narrow non-integral float '2.75' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:36:16
|
36 | yf : s64 = F + 0.25; // non-integral float-const-LEAF local → error
| ^^^^^^^^
error: cannot implicitly narrow non-integral float '3.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:24:16
|
24 | f : s64 = 3.5; // non-integral float LITERAL field default → error
36 | y : s64 = 1.5; // non-integral float LITERAL local → error
| ^^^
error: cannot implicitly narrow non-integral float '2.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:25:16
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:37:16
|
25 | fe : s64 = M + 0.5; // non-integral int-const-EXPR field default → error
37 | ye : s64 = M + 0.5; // non-integral int-const-EXPRESSION local → error
| ^^^^^^^
error: cannot implicitly narrow non-integral float '2.75' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:38:16
|
38 | yf : s64 = F + 0.25; // non-integral float-const-LEAF local → error
| ^^^^^^^^
error: array dimension must be an integer, but '2.75' is a non-integral float
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:39:11
|
39 | ad : [F + 0.25]s64 = ---; // non-integral float-const-LEAF array DIMENSION → error
| ^^^^^^^^
error: cannot implicitly narrow non-integral float '3.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:26:16
|
26 | ff : s64 = F + 0.25; // non-integral float-const-LEAF field default → error
26 | f : s64 = 3.5; // non-integral float LITERAL field default → error
| ^^^
error: cannot implicitly narrow non-integral float '2.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:27:16
|
27 | fe : s64 = M + 0.5; // non-integral int-const-EXPR field default → error
| ^^^^^^^
error: cannot implicitly narrow non-integral float '2.75' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:28:16
|
28 | ff : s64 = F + 0.25; // non-integral float-const-LEAF field default → error
| ^^^^^^^^
error: cannot implicitly narrow non-integral float '2.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:29:23
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:31:23
|
29 | badLit :: (x : s64 = 2.5) -> s64 { return x; } // non-integral LITERAL param default → error
31 | badLit :: (x : s64 = 2.5) -> s64 { return x; } // non-integral LITERAL param default → error
| ^^^
error: cannot implicitly narrow non-integral float '2.5' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:30:23
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:32:23
|
30 | badExpr :: (x : s64 = M + 0.5) -> s64 { return x; } // non-integral int-const-EXPR param default → error
32 | badExpr :: (x : s64 = M + 0.5) -> s64 { return x; } // non-integral int-const-EXPR param default → error
| ^^^^^^^
error: cannot implicitly narrow non-integral float '2.75' to 's64'; use an explicit cast (`xx`/`cast`)
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:31:23
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:33:23
|
31 | badFlt :: (x : s64 = F + 0.25) -> s64 { return x; } // non-integral float-const-LEAF param default → error
33 | badFlt :: (x : s64 = F + 0.25) -> s64 { return x; } // non-integral float-const-LEAF param default → error
| ^^^^^^^^

View File

@@ -68,6 +68,33 @@
> in `M + 0.5` and a local `F : f64 : 2.5` in `F + 0.25` both still truncate
> identically. Float now matches int exactly at that boundary.
>
> **Completion (F0.11 attempt 4)** — attempts 13 unified the four binding sites
> (local / field / param / const) for compile-time float exprs, but the ARRAY-
> DIMENSION / count path still diverged: it folded a DIRECT integral float literal
> (`[4.0]`, `[N]` with `N : f64 : 4.0`) yet rejected an INTEGRAL expression built
> from a non-integral float-const leaf (`[F + 1.5]` = 4.0, or `[K]` with
> `K : s64 : F + 1.5`) as "must be a compile-time integer constant" — because the
> dim fold used the int-only `evalConstIntExpr`, never the float-aware path. Closed
> by routing the count fold through the SAME facility the other four sites use:
> - New `program_index.foldCountI64` — the single int-or-integral-float count fold:
> `evalConstIntExpr` first, then (only on failure) `evalConstFloatExpr` +
> `floatToIntExact`. `foldDimU32` (array dim / Vector lane / u32 value-param) and
> the non-`u32` value-param gate both delegate to it, so no count site disagrees
> on which floats fold (the issue-0083 unify-or-diverge rule extended to floats).
> - A new `DimU32.non_integral_float` variant carries a non-integral float dim to a
> distinct, accurate diagnostic ("array dimension must be an integer, but '2.75'
> is a non-integral float") rather than the generic "must be a compile-time
> integer constant" — the cast-escape advice the binding sites give does not apply
> in a dimension position, so the dim wording omits it. `reportDimError`, the
> Vector-lane resolver, and the top-level array-alias diagnostic all handle the
> new variant, so the DIRECT (`a : [F + 0.25]s64`) and type-ALIAS
> (`Arr :: [F + 0.25]s64`) forms emit the identical message.
> - `type_bridge.StatelessInner.lookupFloatName` (routed through `moduleConstFloat`)
> is the float twin of its `lookupDimName`, so the registration-time alias path
> folds a float-const-leaf dimension to the SAME count as the stateful direct
> path. This relaxes the F0.4 `examples/1132` wording (a non-integral float const
> dim now reports the precise "non-integral float" message; it still errors).
>
> Regression tests: `examples/0168-types-integral-float-to-int.sx` (positive —
> local/field/param/const fold, integral int-const-EXPRESSION (`M + 2.0`) AND
> float-const-LEAF (`F + 1.5`, `F : f64 : 2.5`) fold at local/field/param/const,
@@ -77,9 +104,15 @@
> (`F + 0.25`) error at local/param/field), the integral-float const cases in
> `examples/0162-types-typed-module-const-roundtrip.sx`, and the aligned const
> diagnostic in `examples/1143-diagnostics-typed-module-const-mismatch.sx`
> (G / BAD / BAD2 stay errors with the new wording). Unit:
> (G / BAD / BAD2 stay errors with the new wording). The array-dimension site is
> pinned in the same two examples: 0168 adds `[F + 1.5]s64`, `[KF]s64`
> (`KF : s64 : F + 1.5`), and a type-alias `ArrFE :: [F + 1.5]s64` all folding to
> len 4; 1146 adds `[F + 0.25]s64` erroring; `examples/1132` now expects the
> precise non-integral-float dim wording. Unit:
> `program_index.test.zig` "evalConstFloatExpr folds comptime float expressions"
> (covers the float-const leaf: `F` → 2.5, `F + 0.25` → 2.75, `F + 1.5` → 4.0).
> (covers the float-const leaf: `F` → 2.5, `F + 0.25` → 2.75, `F + 1.5` → 4.0) and
> "foldCountI64 / foldDimU32 fold an integral float count, reject a non-integral
> one" (the count fold + the `non_integral_float` / `below_min` distinction).
## Symptom
A typed LOCAL (and likely typed param/field) silently truncates a floating-point

View File

@@ -130,13 +130,16 @@ array dimension uses: an **integral** compile-time float folds to its integer, a
or *any* compile-time-constant float expression — including one that references a
float-typed const (`F : f64 : 2.5; y : s64 = F + 1.5` → `4`) — and is uniform
across a typed local, a parameter default, a struct field default, a call
argument, and a typed constant — `y : s64 = 4.0`, `K : s64 : 4.0`, and
`y : s64 = M + 2.0` all give `4`, while `y : s64 = 1.5`, `N : s64 : 1.5`,
`y : s64 = M + 0.5`, and `y : s64 = F + 0.25` (= `2.75`) all error (one wording
everywhere: `cannot implicitly narrow non-integral float …`). An explicit
`xx` / `cast(s64)` is the escape hatch and always truncates (`y : s64 = xx 1.5` →
`1`, `y : s64 = xx (M + 0.5)` → `2`); a genuine runtime float is likewise
unaffected.
argument, a typed constant, **and an array dimension / count** — `y : s64 = 4.0`,
`K : s64 : 4.0`, `y : s64 = M + 2.0`, and `[F + 1.5]s64` (≡ `[4]s64`, whether
written directly, through a const, or via a type alias) all give `4`, while
`y : s64 = 1.5`, `N : s64 : 1.5`, `y : s64 = M + 0.5`, `y : s64 = F + 0.25`
(= `2.75`), and `[F + 0.25]s64` all error (one wording at the binding sites:
`cannot implicitly narrow non-integral float …`; a dimension instead reports
`array dimension must be an integer, but '…' is a non-integral float`, since the
cast escape does not apply in a count position). An explicit `xx` / `cast(s64)`
is the escape hatch and always truncates (`y : s64 = xx 1.5` → `1`,
`y : s64 = xx (M + 0.5)` → `2`); a genuine runtime float is likewise unaffected.
Builtin type names (`s2`, `u8`, `bool`, `string`, …) are reserved and a *bare*
spelling can't be used as an identifier at a **value-binding or declaration-name**

View File

@@ -891,10 +891,16 @@ MyArr :: Array(5, s32); // equivalent to [5]s32
A **count** is a compile-time integer used as an array dimension, a `Vector`
lane count, or a generic value-param count. Every count must be **integral**: an
integral float (`4.0`, or a float-typed const `N : f64 : 4.0`) folds to its
integer (`[4.0]s64` ≡ `[4]s64`), while a non-integral float (`4.5`) is rejected.
This is the same integral-float rule a typed binding's float→integer initializer
follows (see "Implicit float → integer", §2 Type Conversions).
integral compile-time float folds to its integer (`[4.0]s64` ≡ `[4]s64`), while a
non-integral float is rejected (an array dimension reports "array dimension must
be an integer, but '4.5' is a non-integral float"). This holds however the float
is written — a literal (`4.0`), a float-typed const (`N : f64 : 4.0`), or a
const **expression** whose value is integral, including one built from a
non-integral float-const leaf (`F : f64 : 2.5; [F + 1.5]s64` ≡ `[4]s64`, and
likewise through a const, `K : s64 : F + 1.5; [K]s64`). A count and a typed
binding's float→integer initializer share the *same* compile-time float
evaluation, so they agree at every site — direct, through a const, or via a type
alias (see "Implicit float → integer", §2 Type Conversions).
The accepted *range* of a count is **context-dependent** — zero is legal for
some counts and not others:
@@ -1438,10 +1444,15 @@ array dimension / lane count uses (see "Array dimensions are integral", §2):
"cannot implicitly narrow non-integral float '…' to 's64'; use an explicit
cast (`xx`/`cast`)".
- This applies uniformly to a typed **local**, a function **param default**, a
struct **field default**, a call **argument**, and a typed module **constant**
struct **field default**, a call **argument**, a typed module **constant**
(`K : s64 : 4.0` → 4; `K : s64 : M + 2.0` → 4; `N : s64 : 1.5` and
`N : s64 : M + 0.5` → error). A **runtime** float (one with no compile-time
value) is unaffected — narrow it explicitly with `xx`/`cast`.
`N : s64 : M + 0.5` → error), and an array **dimension** / count (`[F + 1.5]s64`
≡ `[4]s64`; `[F + 0.25]s64` → error). All five sites fold the *same* set of
compile-time float expressions through one evaluator — only the dimension/count
site phrases its rejection as "array dimension must be an integer, but '…' is a
non-integral float", since the `xx`/`cast` escape does not apply in a count
position. A **runtime** float (one with no compile-time value) is unaffected —
narrow it explicitly with `xx`/`cast`.
**Explicit (narrowing)** — requires `xx` prefix (or `cast(T)`):
- Integer to narrower integer (`s32` → `u8`)

View File

@@ -735,7 +735,7 @@ pub const Lowering = struct {
const precise: ?program_index_mod.DimU32 = if (cd.value.data == .array_type_expr) blk: {
const dim = type_bridge.foldArrayDim(cd.value.data.array_type_expr.length, &self.module.types, &self.program_index.type_alias_map, &self.program_index.module_const_map);
break :blk switch (dim) {
.too_large, .below_min => dim,
.too_large, .below_min, .non_integral_float => dim,
else => null,
};
} else null;
@@ -4190,11 +4190,16 @@ pub const Lowering = struct {
}
/// Evaluate an `inline for` range bound to a comptime integer. Delegates to
/// the shared `program_index.evalConstIntExpr` — the SAME folder the array
/// dimension / Vector lane / value-param paths use — so a literal, a comptime
/// constant (cursor), a module/generic const (`inline for 0..M`), a
/// `<pack>.len` leaf, and any constant-foldable expression over those
/// (`inline for 0..(M + 1)`) all resolve identically. One folder, one answer.
/// the shared `program_index.evalConstIntExpr` — the SAME integer folder the
/// array dimension / Vector lane / value-param count paths build on — so a
/// literal, a comptime constant (cursor), a module/generic const
/// (`inline for 0..M`), a `<pack>.len` leaf, a DIRECT integral float
/// (`0..-2.0` → -2), and any constant-foldable expression over those
/// (`inline for 0..(M + 1)`) all resolve identically. A range bound is an
/// ENDPOINT, not a count (specs.md §2), so it deliberately does NOT take the
/// `foldCountI64` float-const-leaf fallback the count sites add: it accepts a
/// direct integral float but leaves a float-const-leaf expression to the int
/// folder (negatives are valid here, unlike a count).
fn evalComptimeInt(self: *Lowering, node: *const Node) ?i64 {
return program_index_mod.evalConstIntExpr(node, self);
}
@@ -12278,6 +12283,11 @@ pub const Lowering = struct {
d.addFmt(.err, lane_node.span, "Vector lane count {} does not fit in u32", .{v});
return null;
},
.non_integral_float => |v| {
if (self.diagnostics) |d|
d.addFmt(.err, lane_node.span, "Vector lane count must be an integer, but '{d}' is a non-integral float", .{v});
return null;
},
.not_const, .below_min => {
if (self.diagnostics) |d|
d.addFmt(.err, lane_node.span, "Vector lane count must be a positive compile-time integer constant", .{});
@@ -12314,7 +12324,7 @@ pub const Lowering = struct {
if (std.mem.eql(u8, tn, "u32")) {
switch (program_index_mod.foldDimU32(arg_node, self, 0)) {
.ok => |n| return n,
.not_const => {
.not_const, .non_integral_float => {
self.diagValueParamNotConst(arg_node, param_name);
return null;
},
@@ -12329,9 +12339,16 @@ pub const Lowering = struct {
}
}
}
const v = program_index_mod.evalConstIntExpr(arg_node, self) orelse {
self.diagValueParamNotConst(arg_node, param_name);
return null;
// Non-`u32` integer constraint: fold through the SAME unified count fold
// so an integral float arg (`Box(4.0)`, `Make(F + 1.5, ...)`) binds the
// integer it equals, exactly as the `u32` gate above does; a non-integral
// float / non-const arg is not a valid count.
const v = switch (program_index_mod.foldCountI64(arg_node, self)) {
.int => |iv| iv,
.non_integral, .not_const => {
self.diagValueParamNotConst(arg_node, param_name);
return null;
},
};
if (tn_canon) |tn| {
if (program_index_mod.intTypeRange(tn)) |r| {
@@ -14483,12 +14500,16 @@ pub const Lowering = struct {
// An integer-typed const whose initializer is a compile-time integer —
// an int literal/expression, OR an INTEGRAL float that `typedConstInitFits`
// accepted under the unified narrowing rule — materializes as its folded
// int through the SAME `evalConstIntExpr` the count / array-dim path uses.
// (`K : s64 : 4.0` → 4; `K : s64 : M + 2.0` → 4.) Non-foldable shapes
// fall through to the per-kind emitters below.
// int through the SAME `program_index.foldCountI64` the count / array-dim
// path uses, so the const's emitted VALUE and its use as a COUNT come from
// one fold (`K : s64 : 4.0` → 4; `K : s64 : M + 2.0` → 4; and a float-const-
// leaf `KF : s64 : F + 1.5` → 4, which the int-only folder could not reach).
// A non-integral float never arrives (it was rejected at registration); any
// other non-foldable shape falls through to the per-kind emitters below.
if (self.isIntEx(ci.ty)) {
if (self.evalComptimeInt(ci.value)) |iv| {
return self.builder.constInt(iv, ci.ty);
switch (program_index_mod.foldCountI64(ci.value, self)) {
.int => |iv| return self.builder.constInt(iv, ci.ty),
.non_integral, .not_const => {},
}
}
switch (ci.value.data) {

View File

@@ -378,3 +378,39 @@ test "evalConstFloatExpr folds comptime float expressions, halts on runtime leav
try std.testing.expect(eval(&cmp, ctx) == null);
try std.testing.expect(eval(&divz, ctx) == null);
}
test "foldCountI64 / foldDimU32 fold an integral float count, reject a non-integral one" {
const ctx = DimCtx{}; // M = 4, F = 2.5 (non-integral float const)
var five = nLit(5);
var f4 = nFloat(4.0);
var f45 = nFloat(4.5);
var f = nIdent("F");
var quarter = nFloat(0.25);
var three_half = nFloat(1.5);
var fh = nBin(.add, &f, &three_half); // F + 1.5 = 4.0 (integral)
var fq = nBin(.add, &f, &quarter); // F + 0.25 = 2.75 (non-integral)
var z = nIdent("Z"); // unbound — genuinely non-const
// foldCountI64: integer / integral-float (literal OR float-const-leaf SUM)
// fold to `.int`; a non-integral compile-time float surfaces as
// `.non_integral`; a runtime leaf is `.not_const`.
try std.testing.expectEqual(pi.CountFold{ .int = 5 }, pi.foldCountI64(&five, ctx));
try std.testing.expectEqual(pi.CountFold{ .int = 4 }, pi.foldCountI64(&f4, ctx));
try std.testing.expectEqual(pi.CountFold{ .int = 4 }, pi.foldCountI64(&fh, ctx));
try std.testing.expectEqual(pi.CountFold{ .non_integral = 2.75 }, pi.foldCountI64(&fq, ctx));
try std.testing.expectEqual(pi.CountFold.not_const, pi.foldCountI64(&z, ctx));
// foldDimU32 (min 0) inherits the rule: an integral float-const-leaf dim
// narrows to a `u32` count, a non-integral one reports `.non_integral_float`,
// a runtime one `.not_const`.
try std.testing.expectEqual(pi.DimU32{ .ok = 4 }, pi.foldDimU32(&fh, ctx, 0));
try std.testing.expectEqual(pi.DimU32{ .non_integral_float = 2.75 }, pi.foldDimU32(&fq, ctx, 0));
try std.testing.expectEqual(pi.DimU32{ .non_integral_float = 4.5 }, pi.foldDimU32(&f45, ctx, 0));
try std.testing.expectEqual(pi.DimU32.not_const, pi.foldDimU32(&z, ctx, 0));
// A NEGATIVE integral float folds to its integer first, then the u32 gate
// rejects it as below-minimum — NOT as a non-integral float (it IS integral).
var negf = nNeg(&f4); // -4.0 → -4
try std.testing.expectEqual(pi.DimU32{ .below_min = -4 }, pi.foldDimU32(&negf, ctx, 0));
}

View File

@@ -302,7 +302,38 @@ pub fn evalConstFloatExpr(node: *const Node, ctx: anytype) ?f64 {
};
}
/// The outcome of folding a comptime-int and narrowing it to a `u32` count
/// The outcome of folding a compile-time COUNT expression to an `i64` under the
/// unified float→int narrowing rule (F0.11 / issue 0095). THE single int-or-
/// integral-float count fold: `foldDimU32` (array dim / Vector lane / u32 value-
/// param) and the non-`u32` value-param gate both route through `foldCountI64`,
/// so no count site can disagree on which floats fold (the issue-0083 unify-or-
/// diverge rule extended to floats).
pub const CountFold = union(enum) {
/// An integer expression, or an INTEGRAL compile-time float (`[F + 1.5]` → 4).
int: i64,
/// A compile-time float that is not integral (`[F + 0.25]` → 2.75).
non_integral: f64,
/// Not a compile-time constant (runtime value, unbound name, or overflow).
not_const,
};
/// Fold `node` to an `i64` count, accepting an INTEGRAL compile-time float as the
/// integer it equals (`4.0`, `F + 1.5`, a const folding to either) and surfacing a
/// NON-integral compile-time float distinctly so the caller can reject it. Reuses
/// the SAME facility the typed local/field/param/const sites use — `evalConstIntExpr`
/// first (so int literals, named consts, `.min`/`.max`, and a DIRECT integral float
/// literal `4.0` all fold through the single int folder), then, only when that
/// yields no integer, `evalConstFloatExpr` + `floatToIntExact` (so an integral SUM
/// built from a non-integral float-const leaf, `F + 1.5` = 4.0, still folds, while
/// `F + 0.25` = 2.75 reports as non-integral). No parallel integral check.
pub fn foldCountI64(node: *const Node, ctx: anytype) CountFold {
if (evalConstIntExpr(node, ctx)) |v| return .{ .int = v };
const fv = evalConstFloatExpr(node, ctx) orelse return .not_const;
if (floatToIntExact(fv)) |iv| return .{ .int = iv };
return .{ .non_integral = fv };
}
/// The outcome of folding a comptime count and narrowing it to a `u32`
/// (array dimension / Vector lane / value-param count). `foldDimU32` is the
/// SINGLE place a folded integer becomes a `u32`, so the i64→u32 narrowing is
/// range-checked exactly once and no call site does a bare `@intCast` that could
@@ -318,16 +349,23 @@ pub const DimU32 = union(enum) {
below_min: i64,
/// Folded, but greater than `maxInt(u32)` — too large for a `u32` count.
too_large: i64,
/// A compile-time float that is not integral (`[F + 0.25]`) — under the unified
/// float→int rule it cannot serve as an integer count; reported, never truncated.
non_integral_float: f64,
};
/// Fold `node` to a `u32` count through `evalConstIntExpr`, then range-check
/// against `[min, maxInt(u32)]`. THE single fold-to-u32 for every array
/// dimension, Vector lane, and value-param count — routing all of them here
/// guarantees the narrowing is checked once and can never abort the compiler
/// (issue 0087). The fold itself stays in `i64`; only this one conversion is the
/// `u32` gate.
/// Fold `node` to a `u32` count through `foldCountI64` (the unified int-or-
/// integral-float fold), then range-check against `[min, maxInt(u32)]`. THE single
/// fold-to-u32 for every array dimension, Vector lane, and value-param count —
/// routing all of them here guarantees the narrowing is checked once and can never
/// abort the compiler (issue 0087). The fold itself stays in `i64`; only this one
/// conversion is the `u32` gate.
pub fn foldDimU32(node: *const Node, ctx: anytype, min: u32) DimU32 {
const v = evalConstIntExpr(node, ctx) orelse return .not_const;
const v = switch (foldCountI64(node, ctx)) {
.int => |iv| iv,
.non_integral => |fv| return .{ .non_integral_float = fv },
.not_const => return .not_const,
};
if (v < @as(i64, min)) return .{ .below_min = v };
if (v > std.math.maxInt(u32)) return .{ .too_large = v };
return .{ .ok = @intCast(v) };
@@ -348,6 +386,7 @@ pub fn reportDimError(diag: *errors.DiagnosticList, span: ?ast.Span, result: Dim
.below_min => |v| diag.addFmt(.err, span, "array dimension must be non-negative, got {}", .{v}),
.too_large => |v| diag.addFmt(.err, span, "array dimension {} does not fit in u32", .{v}),
.not_const => diag.addFmt(.err, span, "array dimension must be a compile-time integer constant", .{}),
.non_integral_float => |v| diag.addFmt(.err, span, "array dimension must be an integer, but '{d}' is a non-integral float", .{v}),
}
}

View File

@@ -79,6 +79,18 @@ const StatelessInner = struct {
pub fn lookupPackLen(_: StatelessInner, _: []const u8) ?i64 {
return null;
}
/// Float-valued leaf for the shared float-expression evaluator — the FLOAT
/// twin of `lookupDimName`, routed through the SAME `program_index.moduleConstFloat`
/// the stateful body-lowering path uses, so a float-const-leaf dimension
/// (`Arr :: [F + 1.5]T`, `F : f64 : 2.5` → len 4) folds to the SAME count on
/// the registration-time alias path as on the direct form `a : [F + 1.5]T`
/// (issue 0083 unify-or-diverge). Integer / integral-float leaves are already
/// resolved by the `evalConstIntExpr` delegation inside `evalConstFloatExpr`;
/// this surfaces a non-integral float const so the unified rule rejects it.
pub fn lookupFloatName(self: StatelessInner, name: []const u8) ?f64 {
const consts = self.consts orelse return null;
return program_index_mod.moduleConstFloat(consts, self.table, name);
}
};
/// Fold a registration-time array dimension to its `DimU32` outcome through the