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:
@@ -315,3 +315,43 @@ test "evalConstIntExpr folds an integral float literal, halts on a fractional on
|
||||
try std.testing.expectEqual(@as(?i64, 5), eval(&add, ctx));
|
||||
try std.testing.expect(eval(&addbad, ctx) == null);
|
||||
}
|
||||
|
||||
test "evalConstFloatExpr folds comptime float expressions, halts on runtime leaves" {
|
||||
const eval = pi.evalConstFloatExpr;
|
||||
const ctx = DimCtx{}; // M = 4, N = 6
|
||||
|
||||
var half = nFloat(0.5);
|
||||
var two_f = nFloat(2.0);
|
||||
var m = nIdent("M");
|
||||
var z = nIdent("Z"); // unbound — genuinely runtime
|
||||
|
||||
// Leaves: a float literal is itself; an int leaf delegates to the int folder
|
||||
// and promotes (`M` → 4.0); an unbound name is not a compile-time float.
|
||||
try std.testing.expectEqual(@as(?f64, 0.5), eval(&half, ctx));
|
||||
try std.testing.expectEqual(@as(?f64, 4.0), eval(&m, ctx));
|
||||
try std.testing.expect(eval(&z, ctx) == null);
|
||||
|
||||
// Mixed int+float arithmetic promotes to f64, order-independent
|
||||
// (`M + 0.5` and `0.5 + M` → 4.5). `M + 2.0` is integral (6.0) but still a
|
||||
// float value here — `floatToIntExact` is what the narrowing rule applies.
|
||||
var mp = nBin(.add, &m, &half);
|
||||
var pm = nBin(.add, &half, &m);
|
||||
var mi = nBin(.add, &m, &two_f);
|
||||
try std.testing.expectEqual(@as(?f64, 4.5), eval(&mp, ctx));
|
||||
try std.testing.expectEqual(@as(?f64, 4.5), eval(&pm, ctx));
|
||||
try std.testing.expectEqual(@as(?f64, 6.0), eval(&mi, ctx));
|
||||
|
||||
// Unary negate of a float expression.
|
||||
var neg = nNeg(&mp);
|
||||
try std.testing.expectEqual(@as(?f64, -4.5), eval(&neg, ctx));
|
||||
|
||||
// A runtime operand poisons the whole fold; a non-arithmetic operator and a
|
||||
// float division by zero are not compile-time float leaves → null.
|
||||
var zp = nBin(.add, &z, &half);
|
||||
var cmp = nBin(.lt, &m, &half);
|
||||
var zero_f = nFloat(0.0);
|
||||
var divz = nBin(.div, &half, &zero_f);
|
||||
try std.testing.expect(eval(&zp, ctx) == null);
|
||||
try std.testing.expect(eval(&cmp, ctx) == null);
|
||||
try std.testing.expect(eval(&divz, ctx) == null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user