Skip to content

Commit

Permalink
fix crash when doing comptime float rem comptime int
Browse files Browse the repository at this point in the history
closes #776
  • Loading branch information
andrewrk committed Feb 15, 2018
1 parent ca597e2 commit 1c1c069
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/ir.cpp
Expand Up @@ -9975,15 +9975,18 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
ok = bigint_cmp(&rem_result, &mod_result) == CmpEQ;
}
} else {
if (float_cmp_zero(&op2->value) == CmpEQ) {
IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, resolved_type);
if (casted_op2 == ira->codegen->invalid_instruction)
return ira->codegen->builtin_types.entry_invalid;
if (float_cmp_zero(&casted_op2->value) == CmpEQ) {
// the division by zero error will be caught later, but we don't
// have a remainder function ambiguity problem
ok = true;
} else {
ConstExprValue rem_result;
ConstExprValue mod_result;
float_rem(&rem_result, &op1->value, &op2->value);
float_mod(&mod_result, &op1->value, &op2->value);
float_rem(&rem_result, &op1->value, &casted_op2->value);
float_mod(&mod_result, &op1->value, &casted_op2->value);
ok = float_cmp(&rem_result, &mod_result) == CmpEQ;
}
}
Expand Down
9 changes: 8 additions & 1 deletion test/cases/math.zig
Expand Up @@ -394,4 +394,11 @@ fn test_f128() void {

fn should_not_be_zero(x: f128) void {
assert(x != 0.0);
}
}

test "comptime float rem int" {
comptime {
var x = f32(1) % 2;
assert(x == 1.0);
}
}

0 comments on commit 1c1c069

Please sign in to comment.