Files
sx/examples/0168-types-integral-float-to-int.sx
agra 43d44fff75 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).
2026-06-05 16:28:12 +03:00

56 lines
2.2 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`) 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, 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
ne : s64 = M + 2.0; // integral float EXPRESSION field default → folds to 4
}
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 (literal + expression).
z : s64 = 4.0;
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 defaults fold (literal + expression).
b := Box.{};
print("field={} fieldExpr={}\n", b.n, b.ne);
// Param default folds.
print("param={}\n", withDefault());
// Module const folds (and can drive an array dimension: len 8).
a : [K]s64 = ---;
print("const={} len={}\n", K, a.len);
// 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;
xc : s64 = xx (M + 0.5);
print("xx={} cast={} xxExpr={}\n", e, c, xc);
}