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...@@ -21184,7 +21184,10 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
21184 const dest_scalar_ty = dest_ty.scalarType(zcu);21184 const dest_scalar_ty = dest_ty.scalarType(zcu);
21185 const operand_scalar_ty = operand_ty.scalarType(zcu);21185 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 }
21188 try sema.checkFloatType(block, operand_src, operand_scalar_ty);21191 try sema.checkFloatType(block, operand_src, operand_scalar_ty);
2118921192
21190 if (sema.resolveValue(operand)) |operand_val| {21193 if (sema.resolveValue(operand)) |operand_val| {
...@@ -21360,7 +21363,10 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro...@@ -21360,7 +21363,10 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
21360 const dest_scalar_ty = dest_ty.scalarType(zcu);21363 const dest_scalar_ty = dest_ty.scalarType(zcu);
21361 const operand_scalar_ty = operand_ty.scalarType(zcu);21364 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 }
21364 _ = try sema.checkIntType(block, operand_src, operand_scalar_ty);21370 _ = try sema.checkIntType(block, operand_src, operand_scalar_ty);
2136521371
21366 if (sema.resolveValue(operand)) |operand_val| {21372 if (sema.resolveValue(operand)) |operand_val| {
test/cases/compile_errors/invalid_float_casts.zig+1-1
...@@ -22,6 +22,6 @@ export fn qux() void {...@@ -22,6 +22,6 @@ export fn qux() void {
22// error22// error
23//23//
24// :4:40: error: unable to cast runtime value to 'comptime_float'24// :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'
26// :14:32: error: expected integer type, found 'f32'26// :14:32: error: expected integer type, found 'f32'
27// :19:29: error: expected float or vector type, found 'u32'27// :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 {...@@ -8,6 +8,9 @@ export fn bar() void {
8 _ = &a;8 _ = &a;
9 _ = @as(u32, @floatFromInt(a));9 _ = @as(u32, @floatFromInt(a));
10}10}
11export fn bar2() void {
12 _ = @as(comptime_int, @floatFromInt(2));
13}
11export fn baz() void {14export fn baz() void {
12 var a: u32 = 2;15 var a: u32 = 2;
13 _ = &a;16 _ = &a;
...@@ -22,6 +25,7 @@ export fn qux() void {...@@ -22,6 +25,7 @@ export fn qux() void {
22// error25// error
23//26//
24// :4:36: error: unable to cast runtime value to 'comptime_int'27// :4:36: error: unable to cast runtime value to 'comptime_int'
25// :9:18: error: expected float type, found 'u32'28// :9:18: error: expected float result type, found 'u32'
26// :14:32: error: expected float type, found 'u32'29// :12:27: error: expected float result type, found 'comptime_int'
27// :19:27: error: expected integer or vector, found 'f32'30// :17:32: error: expected float type, found 'u32'
31// :22:27: error: expected integer or vector, found 'f32'