From 94bdde8327608af23771fbca8486c2e5c189fa1e Mon Sep 17 00:00:00 2001 From: Rue04 Date: Sat, 8 Aug 2026 09:49:12 +0200 Subject: [PATCH] Sema: fix incorrect compile error for `@as(u32, @trunc(@floor(runtime_float)))` If I had to guess, `Sema.zirRoundOpType` was probably written when `@trunc` etc. could only have a float as their destination type. As a result, for the ints they can cast to now, they'd expect the expression inside them to be a `comptime_float`, which made `@trunc(@floor(runtime_float))` or `@trunc(@floatFromInt(runtime_int))` impossible. By changing the returned type in this case to be generic poison, showing we don't know the expected type as *any* float can be converted to an int, `@trunc(@floor(runtime_float))` lowers to effectively `@floor(runtime_float)` and `@trunc(@floatFromInt(runtime_int))` throws the expected error that `@floatFromInt`'s result type is unknown here. Fixes: https://codeberg.org/ziglang/zig/issues/32111 Co-authored-by: rue04 Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35664 --- src/Sema.zig | 8 ++++---- test/behavior/cast.zig | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index c7d40ffa095a3416a65806bba49d84f6bbbc8da7..35f35d8e581d87e16335725a025a636e32ddffdf 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -25542,10 +25542,10 @@ fn zirRoundOpType(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDa return .generic_poison_type; }; - const float_ty = dest_ty.optEuBaseType(zcu); - switch (float_ty.scalarType(zcu).zigTypeTag(zcu)) { - .float, .comptime_float => return .fromType(float_ty), - else => return .comptime_float_type, + const dest_base_ty = dest_ty.optEuBaseType(zcu); + switch (dest_base_ty.scalarType(zcu).zigTypeTag(zcu)) { + .float, .comptime_float => return .fromType(dest_base_ty), + else => return .generic_poison_type, } } diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 077d3faeddc080357b48304a3f46240ad83e1fdd..2a79566b74f6fad214b6324ff3e41b5ae087ae6d 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -120,6 +120,7 @@ test "@floatFromInt" { try expect(@as(i32, @floor(f)) == k); try expect(@as(i32, @ceil(f)) == k); try expect(@as(i32, @trunc(f)) == k); + try expect(@as(i32, @trunc(@floor(f))) == k); } }; try S.doTheTest(); @@ -197,6 +198,7 @@ test "@floatFromInt(f80)" { try expect(@as(Int, @floor(f)) == k); try expect(@as(Int, @ceil(f)) == k); try expect(@as(Int, @trunc(f)) == k); + try expect(@as(Int, @trunc(@floor(f))) == k); } }; try S.doTheTest(i31); -- 2.54.0