Sema: fix floatToInt to zero bit ints

Closes #9415
This commit is contained in:
Veikka Tuominen 2022-10-28 15:59:04 +03:00
parent 61f5ea4c9a
commit 1ea1228036
2 changed files with 15 additions and 0 deletions

View File

@ -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);

View File

@ -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);
}