From d0e6c6e682b9ba2b0309a5177933a0628e8ef316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= Date: Fri, 7 May 2021 11:07:11 +0900 Subject: [PATCH] cdhash_cmp: rational literals with fractions Nobu kindly pointed out that rational literals can have fractions. --- compile.c | 5 +++-- test/ruby/test_rational.rb | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/compile.c b/compile.c index 9001fd14ff..341361dc78 100644 --- a/compile.c +++ b/compile.c @@ -2006,8 +2006,9 @@ cdhash_cmp(VALUE val, VALUE lit) return rb_float_cmp(lit, val); } else if (tlit == T_RATIONAL) { - /* Rational literals don't have fractions. */ - return cdhash_cmp(val, rb_rational_num(lit)); + const struct RRational *dat1 = RRATIONAL(val); + const struct RRational *dat2 = RRATIONAL(lit); + return (dat1->num == dat2->num) && (dat1->den == dat2->den); } else { UNREACHABLE_RETURN(-1); diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index d7f4961214..5262b6edad 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -835,6 +835,10 @@ class Rational_Test < Test::Unit::TestCase n = case 1 when 2r then false else true end assert_equal(n, true, '[ruby-core:103759] [Bug #17854]') RUBY + assert_separately([], <<-RUBY) + n = case 3/2r when 1.5r then true else false end + assert_equal(n, true, '[ruby-core:103759] [Bug #17854]') + RUBY end def test_Rational_with_invalid_exception