Skip to content

Commit

Permalink
Avoid improper optimization of case statements mixed integer/rational…
Browse files Browse the repository at this point in the history
…/complex

Fixes [Bug #17857]
  • Loading branch information
jeremyevans committed May 13, 2021
1 parent 11ae581 commit 9ce29c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compile.c
Expand Up @@ -4537,6 +4537,9 @@ rb_node_case_when_optimizable_literal(const NODE *const node)
modf(RFLOAT_VALUE(v), &ival) == 0.0) {
return FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival);
}
if (RB_TYPE_P(v, T_RATIONAL) || RB_TYPE_P(v, T_COMPLEX)) {
return Qundef;
}
if (SYMBOL_P(v) || rb_obj_is_kind_of(v, rb_cNumeric)) {
return v;
}
Expand Down
12 changes: 12 additions & 0 deletions test/ruby/test_case.rb
Expand Up @@ -102,6 +102,18 @@ def test_optimization
else
assert(false)
end
case 0
when 0r
assert(true)
else
assert(false)
end
case 0
when 0i
assert(true)
else
assert(false)
end
end

def test_method_missing
Expand Down

0 comments on commit 9ce29c9

Please sign in to comment.