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
| ^^^^^^^^