commit 1ea1228036b6d3424c46a3ce66f4b7cbf461fc21 (tree)
parent 61f5ea4c9adc96dbdabca533f77d475233089b1c
Author: Veikka Tuominen <git@vexu.eu>
Date: Fri, 28 Oct 2022 15:59:04 +0300
Sema: fix floatToInt to zero bit ints
Closes #9415
Diffstat:
2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/src/Sema.zig b/src/Sema.zig
@@ -18668,6 +18668,13 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
}
try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);
+ if (dest_ty.intInfo(sema.mod.getTarget()).bits == 0) {
+ if (block.wantSafety()) {
+ const ok = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_eq_optimized else .cmp_eq, operand, try sema.addConstant(operand_ty, Value.zero));
+ try sema.addSafetyCheck(block, ok, .integer_part_out_of_bounds);
+ }
+ return sema.addConstant(dest_ty, Value.zero);
+ }
const result = try block.addTyOp(if (block.float_mode == .Optimized) .float_to_int_optimized else .float_to_int, dest_ty, operand);
if (block.wantSafety()) {
const back = try block.addTyOp(.int_to_float, operand_ty, result);
diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig
@@ -1451,3 +1451,11 @@ test "peer type resolution of const and non-const pointer to array" {
try std.testing.expect(@TypeOf(a, b) == *const [1024]u8);
try std.testing.expect(a == b);
}
+
+test "floatToInt to zero-bit int" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+
+ var a: f32 = 0.0;
+ comptime try std.testing.expect(@floatToInt(u0, a) == 0);
+}