authorgravatar for rue.nolastname@protonmail.comRue04 <rue.nolastname@protonmail.com> 2026-08-08 09:54:04+02:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-08-08 09:54:04+02:00
log02c49f2911674a93be7591f3d9b47bd087c0586d
tree16aef28ebecc94db79b2a2216916cd0666c67368
parent94bdde8327608af23771fbca8486c2e5c189fa1e

Sema: fix crash on `@as(comptime_int, @floatFromInt(1))`

Fixes: https://codeberg.org/ziglang/zig/issues/32147 Co-authored-by: rue04 <rue04@rue04.de> Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35663 Reviewed-by: mlugg <mlugg@mlugg.co.uk>

3 files changed, 16 insertions(+), 6 deletions(-)

src/Sema.zig+8-2
......@@ -21184,7 +21184,10 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2118421184 const dest_scalar_ty = dest_ty.scalarType(zcu);
2118521185 const operand_scalar_ty = operand_ty.scalarType(zcu);
2118621186
21187 _ = try sema.checkIntType(block, src, dest_scalar_ty);
21187 switch (dest_scalar_ty.zigTypeTag(zcu)) {
21188 .comptime_int, .int => {},
21189 else => return sema.fail(block, src, "expected integer result type, found '{f}'", .{dest_scalar_ty.fmt(pt)}),
21190 }
2118821191 try sema.checkFloatType(block, operand_src, operand_scalar_ty);
2118921192
2119021193 if (sema.resolveValue(operand)) |operand_val| {
......@@ -21360,7 +21363,10 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2136021363 const dest_scalar_ty = dest_ty.scalarType(zcu);
2136121364 const operand_scalar_ty = operand_ty.scalarType(zcu);
2136221365
21363 try sema.checkFloatType(block, src, dest_scalar_ty);
21366 switch (dest_scalar_ty.zigTypeTag(zcu)) {
21367 .comptime_float, .float => {},
21368 else => return sema.fail(block, src, "expected float result type, found '{f}'", .{dest_scalar_ty.fmt(pt)}),
21369 }
2136421370 _ = try sema.checkIntType(block, operand_src, operand_scalar_ty);
2136521371
2136621372 if (sema.resolveValue(operand)) |operand_val| {
test/cases/compile_errors/invalid_float_casts.zig+1-1
......@@ -22,6 +22,6 @@ export fn qux() void {
2222// error
2323//
2424// :4:40: error: unable to cast runtime value to 'comptime_float'
25// :9:18: error: expected integer type, found 'f32'
25// :9:18: error: expected integer result type, found 'f32'
2626// :14:32: error: expected integer type, found 'f32'
2727// :19:29: error: expected float or vector type, found 'u32'
test/cases/compile_errors/invalid_int_casts.zig+7-3
......@@ -8,6 +8,9 @@ export fn bar() void {
88 _ = &a;
99 _ = @as(u32, @floatFromInt(a));
1010}
11export fn bar2() void {
12 _ = @as(comptime_int, @floatFromInt(2));
13}
1114export fn baz() void {
1215 var a: u32 = 2;
1316 _ = &a;
......@@ -22,6 +25,7 @@ export fn qux() void {
2225// error
2326//
2427// :4:36: error: unable to cast runtime value to 'comptime_int'
25// :9:18: error: expected float type, found 'u32'
26// :14:32: error: expected float type, found 'u32'
27// :19:27: error: expected integer or vector, found 'f32'
28// :9:18: error: expected float result type, found 'u32'
29// :12:27: error: expected float result type, found 'comptime_int'
30// :17:32: error: expected float type, found 'u32'
31// :22:27: error: expected integer or vector, found 'f32'