fix(ir): complete const-float evaluator — resolve float-const leaves too [F0.11]
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.
This commit is contained in:
@@ -2,54 +2,64 @@
|
||||
// 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`) or a
|
||||
// const-EXPRESSION (`M + 2.0`). The escape hatch (`xx` / `cast`) still TRUNCATES
|
||||
// any float, integral or not — including a non-integral const expression.
|
||||
// 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, and a non-integral const
|
||||
// EXPRESSION (`M + 0.5`) truncated even when written through an int binding; the
|
||||
// rule now folds an integral float (literal or expression) and rejects a
|
||||
// non-integral one.
|
||||
// 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; // module const, for the const-EXPRESSION cases
|
||||
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 float EXPRESSION 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
|
||||
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
|
||||
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 + expression).
|
||||
// Typed local: integral float folds (literal + int-const expr + float-const leaf).
|
||||
z : s64 = 4.0;
|
||||
ze : s64 = M + 2.0;
|
||||
print("local={} localExpr={}\n", z, ze);
|
||||
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 + expression).
|
||||
// Struct field defaults fold (literal + int-const expr + float-const leaf).
|
||||
b := Box.{};
|
||||
print("field={} fieldExpr={}\n", b.n, b.ne);
|
||||
print("field={} fieldExpr={} fieldFlt={}\n", b.n, b.ne, b.nf);
|
||||
|
||||
// Param default folds.
|
||||
print("param={}\n", withDefault());
|
||||
// Param defaults fold.
|
||||
print("param={} paramFlt={}\n", withDefault(), withFlt());
|
||||
|
||||
// Module const folds (and can drive an array dimension: len 8).
|
||||
// Module consts fold (and an integral float const can drive an array dim: len 8).
|
||||
a : [K]s64 = ---;
|
||||
print("const={} len={}\n", K, a.len);
|
||||
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).
|
||||
// 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);
|
||||
print("xx={} cast={} xxExpr={}\n", e, c, xc);
|
||||
xf : s64 = xx (F + 0.25);
|
||||
print("xx={} cast={} xxExpr={} xxFlt={}\n", e, c, xc, xf);
|
||||
}
|
||||
|
||||
@@ -3,33 +3,39 @@
|
||||
// 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`) or a compile-time const EXPRESSION (`M + 0.5`) — the latter is
|
||||
// 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.
|
||||
// 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).
|
||||
//
|
||||
// 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).
|
||||
//
|
||||
// Regression (issue 0095): `y : s64 = 1.5` silently truncated to 1, and
|
||||
// `y : s64 = M + 0.5` silently truncated to 2.
|
||||
// Regression (issue 0095): `y : s64 = 1.5` silently truncated to 1,
|
||||
// `y : s64 = M + 0.5` to 2, and `y : s64 = F + 0.25` (float-const leaf) to 2.
|
||||
#import "modules/std.sx";
|
||||
|
||||
M :: 2; // module const, for the const-EXPRESSION cases
|
||||
M :: 2; // int module const, for the INT-const-EXPRESSION cases
|
||||
F : f64 : 2.5; // float module const, for the FLOAT-const-LEAF cases
|
||||
|
||||
Bad :: struct {
|
||||
f : s64 = 3.5; // non-integral float LITERAL field default → error
|
||||
fe : s64 = M + 0.5; // non-integral const-EXPRESSION field default → error
|
||||
fe : s64 = M + 0.5; // non-integral int-const-EXPR field default → error
|
||||
ff : s64 = F + 0.25; // non-integral float-const-LEAF field default → error
|
||||
}
|
||||
|
||||
badLit :: (x : s64 = 2.5) -> s64 { return x; } // non-integral LITERAL param default → error
|
||||
badExpr :: (x : s64 = M + 0.5) -> s64 { return x; } // non-integral const-EXPR param default → error
|
||||
badLit :: (x : s64 = 2.5) -> s64 { return x; } // non-integral LITERAL param default → error
|
||||
badExpr :: (x : s64 = M + 0.5) -> s64 { return x; } // non-integral int-const-EXPR param default → error
|
||||
badFlt :: (x : s64 = F + 0.25) -> s64 { return x; } // non-integral float-const-LEAF param default → error
|
||||
|
||||
main :: () {
|
||||
y : s64 = 1.5; // non-integral float LITERAL local → error
|
||||
ye : s64 = M + 0.5; // non-integral const-EXPRESSION 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
|
||||
b := Bad.{};
|
||||
print("{} {}\n", b.f, b.fe);
|
||||
print("{} {}\n", badLit(), badExpr());
|
||||
print("{} {}\n", y, ye);
|
||||
print("{} {} {}\n", b.f, b.fe, b.ff);
|
||||
print("{} {} {}\n", badLit(), badExpr(), badFlt());
|
||||
print("{} {} {}\n", y, ye, yf);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
local=4 localExpr=4
|
||||
local=4 localExpr=4 localFlt=4
|
||||
neg=-2
|
||||
field=4 fieldExpr=4
|
||||
param=6
|
||||
const=8 len=8
|
||||
xx=4 cast=1 xxExpr=2
|
||||
field=4 fieldExpr=4 fieldFlt=4
|
||||
param=6 paramFlt=4
|
||||
const=8 constFlt=4 len=8
|
||||
xx=4 cast=1 xxExpr=2 xxFlt=2
|
||||
|
||||
@@ -1,35 +1,53 @@
|
||||
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:29:16
|
||||
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:34:16
|
||||
|
|
||||
29 | y : s64 = 1.5; // non-integral float LITERAL local → error
|
||||
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:30:16
|
||||
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:35:16
|
||||
|
|
||||
30 | ye : s64 = M + 0.5; // non-integral const-EXPRESSION local → error
|
||||
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:21:16
|
||||
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:24:16
|
||||
|
|
||||
21 | f : s64 = 3.5; // non-integral float LITERAL field default → error
|
||||
24 | 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:22:16
|
||||
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:25:16
|
||||
|
|
||||
22 | fe : s64 = M + 0.5; // non-integral const-EXPRESSION field default → error
|
||||
25 | fe : s64 = M + 0.5; // non-integral int-const-EXPR 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:25:23
|
||||
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:26:16
|
||||
|
|
||||
25 | badLit :: (x : s64 = 2.5) -> s64 { return x; } // non-integral LITERAL param default → error
|
||||
26 | 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
|
||||
|
|
||||
29 | 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:26:23
|
||||
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:30:23
|
||||
|
|
||||
26 | badExpr :: (x : s64 = M + 0.5) -> s64 { return x; } // non-integral const-EXPR param default → error
|
||||
30 | 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
|
||||
|
|
||||
31 | badFlt :: (x : s64 = F + 0.25) -> s64 { return x; } // non-integral float-const-LEAF param default → error
|
||||
| ^^^^^^^^
|
||||
|
||||
Reference in New Issue
Block a user