authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-14 23:12:51-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-14 23:12:51-05:00
log1c1c0691cc4d47a39f382aec29654ae94cdf524c
treecb2761f1a5c56218665aa097a97f41ebca8aa0fe
parentca597e2bfb3c39aecdf3dea2718e84deb749ed07

fix crash when doing comptime float rem comptime int

closes #776

2 files changed, 14 insertions(+), 4 deletions(-)

src/ir.cpp+6-3
......@@ -9975,15 +9975,18 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
99759975 ok = bigint_cmp(&rem_result, &mod_result) == CmpEQ;
99769976 }
99779977 } else {
9978 if (float_cmp_zero(&op2->value) == CmpEQ) {
9978 IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, resolved_type);
9979 if (casted_op2 == ira->codegen->invalid_instruction)
9980 return ira->codegen->builtin_types.entry_invalid;
9981 if (float_cmp_zero(&casted_op2->value) == CmpEQ) {
99799982 // the division by zero error will be caught later, but we don't
99809983 // have a remainder function ambiguity problem
99819984 ok = true;
99829985 } else {
99839986 ConstExprValue rem_result;
99849987 ConstExprValue mod_result;
9985 float_rem(&rem_result, &op1->value, &op2->value);
9986 float_mod(&mod_result, &op1->value, &op2->value);
9988 float_rem(&rem_result, &op1->value, &casted_op2->value);
9989 float_mod(&mod_result, &op1->value, &casted_op2->value);
99879990 ok = float_cmp(&rem_result, &mod_result) == CmpEQ;
99889991 }
99899992 }
test/cases/math.zig+8-1
......@@ -394,4 +394,11 @@ fn test_f128() void {
394394
395395fn should_not_be_zero(x: f128) void {
396396 assert(x != 0.0);
397}
\ No newline at end of file
397}
398
399test "comptime float rem int" {
400 comptime {
401 var x = f32(1) % 2;
402 assert(x == 1.0);
403 }
404}