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.
81 lines
3.9 KiB
Plaintext
81 lines
3.9 KiB
Plaintext
// 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
|
|
// 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)`).
|
|
//
|
|
// Companion to the negative example 1146 (non-integral floats error).
|
|
// Regression (issue 0095): a typed local/param/field silently truncated a float
|
|
// initializer (`y : s64 = 1.5` → 1) with no diagnostic; a non-integral const
|
|
// EXPRESSION (`M + 0.5`) and a non-integral float-const-LEAF expression
|
|
// (`F + 0.25`) truncated even when written through an int binding; the rule now
|
|
// folds an integral float (literal, int-const expr, or float-const leaf) and
|
|
// rejects a non-integral one.
|
|
#import "modules/std.sx";
|
|
|
|
M :: 2; // int module const, for the INT-const-EXPRESSION cases
|
|
F : f64 : 2.5; // float module const, for the FLOAT-const-LEAF cases
|
|
|
|
Box :: struct {
|
|
n : s64 = 4.0; // integral float field default → folds to 4
|
|
ne : s64 = M + 2.0; // integral int-const-EXPR field default → folds to 4
|
|
nf : s64 = F + 1.5; // integral float-const-LEAF field default → folds to 4
|
|
}
|
|
|
|
withDefault :: (x : s64 = 6.0) -> s64 { return x; } // param default → 6
|
|
withFlt :: (x : s64 = F + 1.5) -> s64 { return x; } // float-const-leaf param default → 4
|
|
|
|
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;
|
|
ze : s64 = M + 2.0;
|
|
zf : s64 = F + 1.5;
|
|
print("local={} localExpr={} localFlt={}\n", z, ze, zf);
|
|
|
|
// Negative integral float folds to its (negative) integer.
|
|
neg : s64 = -2.0;
|
|
print("neg={}\n", neg);
|
|
|
|
// Struct field defaults fold (literal + int-const expr + float-const leaf).
|
|
b := Box.{};
|
|
print("field={} fieldExpr={} fieldFlt={}\n", b.n, b.ne, b.nf);
|
|
|
|
// Param defaults fold.
|
|
print("param={} paramFlt={}\n", withDefault(), withFlt());
|
|
|
|
// Module consts fold (and an integral float const can drive an array dim: len 8).
|
|
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).
|
|
e : s64 = xx 4.9;
|
|
c : s64 = cast(s64) 1.5;
|
|
xc : s64 = xx (M + 0.5);
|
|
xf : s64 = xx (F + 0.25);
|
|
print("xx={} cast={} xxExpr={} xxFlt={}\n", e, c, xc, xf);
|
|
}
|