fix(ir): narrow non-integral const-float EXPRESSIONS at typed local/field/param; align const message [F0.11]
Completes issue 0095 (attempt 2). The attempt-1 coerce arm only caught a direct `const_float` literal, so a non-integral const-folded float EXPRESSION still truncated silently at a typed local / field default / param default: M :: 2; local : s64 = M + 0.5; // → 2 (silent truncation — BUG; now ERRORS) fld : s64 = M + 0.5; // field default — same take(x : s64 = M + 0.5) // param default — same while the typed-CONST site already errored. The integral expression (`M + 2.0` → 4) folded but the runtime/explicit-cast paths must stay untouched. Fix: - New `program_index.evalConstFloatExpr` — the f64 counterpart to `evalConstIntExpr`, delegating every integer subtree back to it (no parallel integer logic) and adding only the float literal / unary-negate / `+ - * /` arms. Pure (no diagnostics, no resolution side effects). - `Lowering.foldComptimeFloatInit` applies the unified rule to a typed-binding initializer EXPRESSION: an integral comptime float folds to its `constInt`, a non-integral one errors, a genuine runtime float / `xx`-cast falls through to the normal path. It runs `evalConstFloatExpr` FIRST (pure) so a `$pack[i]` argument is never spuriously type-resolved outside an active binding, then gates on `isFloat(inferExprType)` so a plain comptime int is left alone. Wired into the typed-local path, the three struct field-default sites (via a shared `lowerCoercedDefault`), and the call-argument loop (covers expanded param defaults). - One `Lowering.diagNonIntegralNarrow` now emits the narrowing wording at all five sites (coerce arm, global init, const-expr value, the typed-binding sites, and the typed-const path). The typed-CONST non-integral diagnostic therefore reads "cannot implicitly narrow non-integral float …" instead of the stale "initializer is a float literal / floating-point expression". Tests: examples/1146 (negative) extended with non-integral const-EXPRESSION cases at local/field/param; examples/0168 (positive) extended with integral const-EXPRESSION folds and `xx (M + 0.5)` truncation; examples/1143 reconciled to the aligned const message (G/BAD/BAD2 stay errors); unit test `evalConstFloatExpr folds comptime float expressions`. Full gate green (447).
This commit is contained in:
@@ -2,17 +2,23 @@
|
||||
// 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. The escape hatch (`xx` / `cast`) still TRUNCATES any float,
|
||||
// integral or not.
|
||||
// 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.
|
||||
//
|
||||
// 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; the rule now folds an
|
||||
// integral float and rejects a non-integral one.
|
||||
// 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.
|
||||
#import "modules/std.sx";
|
||||
|
||||
M :: 2; // module const, for the const-EXPRESSION cases
|
||||
|
||||
Box :: struct {
|
||||
n : s64 = 4.0; // integral float field default → folds to 4
|
||||
n : s64 = 4.0; // integral float field default → folds to 4
|
||||
ne : s64 = M + 2.0; // integral float EXPRESSION field default → folds to 4
|
||||
}
|
||||
|
||||
withDefault :: (x : s64 = 6.0) -> s64 { return x; } // param default → 6
|
||||
@@ -20,17 +26,18 @@ withDefault :: (x : s64 = 6.0) -> s64 { return x; } // param default → 6
|
||||
K : s64 : 8.0; // integral float module const → folds to 8
|
||||
|
||||
main :: () {
|
||||
// Typed local: integral float folds.
|
||||
// Typed local: integral float folds (literal + expression).
|
||||
z : s64 = 4.0;
|
||||
print("local={}\n", z);
|
||||
ze : s64 = M + 2.0;
|
||||
print("local={} localExpr={}\n", z, ze);
|
||||
|
||||
// Negative integral float folds to its (negative) integer.
|
||||
neg : s64 = -2.0;
|
||||
print("neg={}\n", neg);
|
||||
|
||||
// Struct field default folds.
|
||||
// Struct field defaults fold (literal + expression).
|
||||
b := Box.{};
|
||||
print("field={}\n", b.n);
|
||||
print("field={} fieldExpr={}\n", b.n, b.ne);
|
||||
|
||||
// Param default folds.
|
||||
print("param={}\n", withDefault());
|
||||
@@ -39,8 +46,10 @@ main :: () {
|
||||
a : [K]s64 = ---;
|
||||
print("const={} len={}\n", K, a.len);
|
||||
|
||||
// Explicit escape: `xx` / `cast` always truncate, integral or not.
|
||||
// Explicit escape: `xx` / `cast` always truncate, integral or not —
|
||||
// including a non-integral const EXPRESSION (`xx (M + 0.5)` → 2).
|
||||
e : s64 = xx 4.9;
|
||||
c : s64 = cast(s64) 1.5;
|
||||
print("xx={} cast={}\n", e, c);
|
||||
xc : s64 = xx (M + 0.5);
|
||||
print("xx={} cast={} xxExpr={}\n", e, c, xc);
|
||||
}
|
||||
|
||||
@@ -2,25 +2,34 @@
|
||||
// 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). The fix is the integral-fold /
|
||||
// non-integral-error rule shared with the array-dimension path.
|
||||
// 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.
|
||||
//
|
||||
// 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.
|
||||
// Regression (issue 0095): `y : s64 = 1.5` silently truncated to 1, and
|
||||
// `y : s64 = M + 0.5` silently truncated to 2.
|
||||
#import "modules/std.sx";
|
||||
|
||||
M :: 2; // module const, for the const-EXPRESSION cases
|
||||
|
||||
Bad :: struct {
|
||||
f : s64 = 3.5; // non-integral field default → error
|
||||
f : s64 = 3.5; // non-integral float LITERAL field default → error
|
||||
fe : s64 = M + 0.5; // non-integral const-EXPRESSION field default → error
|
||||
}
|
||||
|
||||
badDefault :: (x : s64 = 2.5) -> s64 { return x; } // non-integral 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 const-EXPR param default → error
|
||||
|
||||
main :: () {
|
||||
y : s64 = 1.5; // non-integral local initializer → error
|
||||
y : s64 = 1.5; // non-integral float LITERAL local → error
|
||||
ye : s64 = M + 0.5; // non-integral const-EXPRESSION local → error
|
||||
b := Bad.{};
|
||||
print("{}\n", b.f);
|
||||
print("{}\n", badDefault());
|
||||
print("{}\n", y);
|
||||
print("{} {}\n", b.f, b.fe);
|
||||
print("{} {}\n", badLit(), badExpr());
|
||||
print("{} {}\n", y, ye);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
local=4
|
||||
local=4 localExpr=4
|
||||
neg=-2
|
||||
field=4
|
||||
field=4 fieldExpr=4
|
||||
param=6
|
||||
const=8 len=8
|
||||
xx=4 cast=1
|
||||
xx=4 cast=1 xxExpr=2
|
||||
|
||||
@@ -16,7 +16,7 @@ error: type mismatch: constant 'B' is declared 's64' but its initializer is a bo
|
||||
26 | B : s64 : true; // boolean literal where an integer is annotated
|
||||
| ^^^^
|
||||
|
||||
error: type mismatch: constant 'G' is declared 's64' but its initializer is a float literal
|
||||
error: cannot implicitly narrow non-integral float '1.5' to 's64'; use an explicit cast (`xx`/`cast`)
|
||||
--> examples/1143-diagnostics-typed-module-const-mismatch.sx:27:15
|
||||
|
|
||||
27 | G : s64 : 1.5; // float literal where an integer is annotated
|
||||
@@ -34,13 +34,13 @@ error: type mismatch: constant 'V' is declared 'string' but its initializer is a
|
||||
29 | V : string : -M; // integer (unary) expression where a string is annotated
|
||||
| ^^
|
||||
|
||||
error: type mismatch: constant 'BAD' is declared 's64' but its initializer is a floating-point expression
|
||||
error: cannot implicitly narrow non-integral float '2.5' to 's64'; use an explicit cast (`xx`/`cast`)
|
||||
--> examples/1143-diagnostics-typed-module-const-mismatch.sx:30:15
|
||||
|
|
||||
30 | BAD : s64 : M + 0.5; // mixed int+float (int LHS) → f64, rejected vs s64
|
||||
| ^^^^^^^
|
||||
|
||||
error: type mismatch: constant 'BAD2' is declared 's64' but its initializer is a floating-point expression
|
||||
error: cannot implicitly narrow non-integral float '2.5' to 's64'; use an explicit cast (`xx`/`cast`)
|
||||
--> examples/1143-diagnostics-typed-module-const-mismatch.sx:31:15
|
||||
|
|
||||
31 | BAD2 : s64 : 0.5 + M; // mixed float+int (float LHS) → f64, rejected vs s64 — order-independent
|
||||
|
||||
@@ -1,17 +1,35 @@
|
||||
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:21:15
|
||||
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:29:16
|
||||
|
|
||||
21 | y : s64 = 1.5; // non-integral local initializer → 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:15:15
|
||||
|
|
||||
15 | f : s64 = 3.5; // non-integral field default → error
|
||||
| ^^^
|
||||
29 | 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:18:26
|
||||
--> examples/1146-diagnostics-nonintegral-float-to-int.sx:30:16
|
||||
|
|
||||
18 | badDefault :: (x : s64 = 2.5) -> s64 { return x; } // non-integral param default → error
|
||||
| ^^^
|
||||
30 | ye : s64 = M + 0.5; // non-integral const-EXPRESSION 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
|
||||
|
|
||||
21 | 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
|
||||
|
|
||||
22 | fe : s64 = M + 0.5; // non-integral const-EXPRESSION 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
|
||||
|
|
||||
25 | 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
|
||||
|
|
||||
26 | badExpr :: (x : s64 = M + 0.5) -> s64 { return x; } // non-integral const-EXPR param default → error
|
||||
| ^^^^^^^
|
||||
|
||||
Reference in New Issue
Block a user