Sema: Allow small integer types to coerce to floats

If the float can store all possible values of the integer without
rounding, coercion is allowed. The integer's precision must be less than
or equal to the float's significand precision.

Closes #18614
This commit is contained in:
Jay Petacat
2026-01-08 23:48:25 -07:00
committed by Andrew Kelley
parent 5082e85de9
commit 484cc15366
6 changed files with 151 additions and 2 deletions
@@ -0,0 +1,52 @@
// Test that integer types above a certain size will not coerce to a float.
fn testCoerce(Float: type, Int: type) void {
var i: Int = 0;
_ = &i;
_ = @as(Float, i);
}
export fn entry() void {
testCoerce(f16, u11); // Okay
testCoerce(f16, u12); // Too big
testCoerce(f16, i12);
testCoerce(f16, i13);
testCoerce(f32, u24);
testCoerce(f32, u25);
testCoerce(f32, i25);
testCoerce(f32, i26);
testCoerce(f64, u53);
testCoerce(f64, u54);
testCoerce(f64, i54);
testCoerce(f64, i55);
testCoerce(f80, u64);
testCoerce(f80, u65);
testCoerce(f80, i65);
testCoerce(f80, i66);
testCoerce(f128, u113);
testCoerce(f128, u114);
testCoerce(f128, i114);
testCoerce(f128, i115);
}
// error
//
// :6:20: error: expected type 'f16', found 'u12'
// :6:20: error: expected type 'f16', found 'i13'
// :6:20: error: expected type 'f32', found 'u25'
// :6:20: error: expected type 'f32', found 'i26'
// :6:20: error: expected type 'f64', found 'u54'
// :6:20: error: expected type 'f64', found 'i55'
// :6:20: error: expected type 'f80', found 'u65'
// :6:20: error: expected type 'f80', found 'i66'
// :6:20: error: expected type 'f128', found 'u114'
// :6:20: error: expected type 'f128', found 'i115'