Commit 1ea1228036

Veikka Tuominen <git@vexu.eu>
2022-10-28 14:59:04
Sema: fix floatToInt to zero bit ints
Closes #9415
1 parent 61f5ea4
Changed files (2)
src
test
behavior
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);
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);
+}