Completes issue 0095: a non-integral float→int narrowing via a FLOAT-const leaf (`F : f64 : 2.5; y : s64 = F + 0.25` = 2.75) silently truncated to 2. `evalConstFloatExpr` delegated only INTEGER leaves to `evalConstIntExpr` and had no float-const leaf arm, so the unified rule never saw the value. - program_index.zig: add `moduleConstFloat`/`moduleConstFloatFramed` — the f64 twin of `moduleConstInt` (same `isCountableConstType` gate, same cyclic- definition frame), recovering a numeric module const's value via `evalConstFloatExpr`. Add `lookupFloatName` to `ModuleConstCtx` and the `.identifier`/`.type_expr` leaf arms to `evalConstFloatExpr` that call it. Integer / integral-float leaves keep resolving through the existing `evalConstIntExpr` delegation, so the unified rule now applies to ANY compile-time-constant float expression — literal, int-const leaf, float-const leaf, and combinations — at every binding site. - lower.zig: add `Lowering.lookupFloatName` delegating to `moduleConstFloat`. Route `typedConstInitFits`' integral-fold check through `evalConstFloatExpr` + `floatToIntExact` (the SAME facility `foldComptimeFloatInit` uses) instead of the int-only `evalComptimeInt`, which folded leaf-by-leaf in i64 and so rejected an integral SUM built from a non-integral float leaf (`K : s64 : F + 1.5` = 4.0 now folds; `K : s64 : F + 0.25` errors). A LOCAL `::` const leaf is a scope ref (not in the const tables) so neither the int nor float evaluator folds it — float now matches int exactly there. Regression: examples/1146 (negative) + 0168 (positive) extended with float-const-leaf cases at local/field/param/const; unit test in program_index.test.zig covers the leaf resolution (F→2.5, F+0.25→2.75, F+1.5→4.0). specs.md + readme.md state the rule covers any compile-time-const float expression incl. float-typed const leaves. issues/0095 banner updated. Gate: zig build + zig build test green; 447 examples pass, 0 failed.
66 lines
3.1 KiB
Plaintext
66 lines
3.1 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
|
|
// 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).
|
|
// 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
|
|
|
|
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);
|
|
|
|
// 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);
|
|
}
|