summaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2021-05-22 17:09:16 +0900
committernagachika <nagachika@ruby-lang.org>2021-05-22 17:09:16 +0900
commit31816356eee6313fe968eecd4cd3ad9ac4848819 (patch)
tree58960c14744acbf941c6149b865b654acc07dda3 /compile.c
parentaf9de56c6fde7f9adb81d6ed0ef3067af81f90e5 (diff)
merge revision(s) 2bc293e899c9d32dcd794a73de8925c49ecf8f15,d0e6c6e682b9ba2b0309a5177933a0628e8ef316,cc0dc67bbbe1951ff90004bc987f78545625d772,e1eff837cf12a8e813de9d4ff2db50c9b68b86b5,0ab0b86c8491d639b9ff1335ddf35e341ecd867e,6911b4bc82889860ff7da4ecf975504cdc3e5314: [Backport #17854]
cdhash_cmp: can take rational literals Rational literals are those integers suffixed with `r`. They tend to be a part of more complex expressions like `123/456r`, but in theory they can live alone. When such "bare" rational literals are passed to case-when branch, we have to take care of them. Fixes [Bug #17854] --- common.mk | 1 + compile.c | 7 +++++++ internal/rational.h | 1 + rational.c | 13 ++++++++++--- test/ruby/test_rational.rb | 7 +++++++ 5 files changed, 26 insertions(+), 3 deletions(-) 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(-) cdhash_cmp: can also take complex There are complex literals `123i`, which can also be a case condition. --- compile.c | 15 +++++++++++---- complex.c | 12 +++++++++--- internal/complex.h | 1 + test/ruby/test_rational.rb | 4 ++++ 4 files changed, 25 insertions(+), 7 deletions(-) cdhash_cmp: recursively apply For instance a rational's numerator can be a bignum. Comparison using C's == can be insufficient. --- compile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) cdhash_cmp: should use || cf: https://github.com/ruby/ruby/pull/4469#discussion_r628386707 --- compile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) test_cdhash: refactor change class It is now strange to test Complex in a class named Rational_Test. --- test/ruby/test_rational.rb | 15 --------------- test/ruby/test_syntax.rb | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 15 deletions(-)
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index b78c7f26d9..502d12d6c7 100644
--- a/compile.c
+++ b/compile.c
@@ -28,6 +28,7 @@
#include "internal/hash.h"
#include "internal/numeric.h"
#include "internal/object.h"
+#include "internal/rational.h"
#include "internal/re.h"
#include "internal/symbol.h"
#include "internal/thread.h"
@@ -1985,6 +1986,16 @@ cdhash_cmp(VALUE val, VALUE lit)
else if (tlit == T_FLOAT) {
return rb_float_cmp(lit, val);
}
+ else if (tlit == T_RATIONAL) {
+ const struct RRational *rat1 = RRATIONAL(val);
+ const struct RRational *rat2 = RRATIONAL(lit);
+ return cdhash_cmp(rat1->num, rat2->num) || cdhash_cmp(rat1->den, rat2->den);
+ }
+ else if (tlit == T_COMPLEX) {
+ const struct RComplex *comp1 = RCOMPLEX(val);
+ const struct RComplex *comp2 = RCOMPLEX(lit);
+ return cdhash_cmp(comp1->real, comp2->real) || cdhash_cmp(comp1->imag, comp2->imag);
+ }
else {
UNREACHABLE_RETURN(-1);
}
@@ -2003,6 +2014,10 @@ cdhash_hash(VALUE a)
return FIX2LONG(rb_big_hash(a));
case T_FLOAT:
return rb_dbl_long_hash(RFLOAT_VALUE(a));
+ case T_RATIONAL:
+ return rb_rational_hash(a);
+ case T_COMPLEX:
+ return rb_complex_hash(a);
default:
UNREACHABLE_RETURN(0);
}