ruby-changes:73374
From: Nobuyoshi <ko1@a...>
Date: Fri, 2 Sep 2022 14:33:47 +0900 (JST)
Subject: [ruby-changes:73374] 9212d96307 (master): [Bug #18937] Coerce non-real non-Numeric into Complex at comparisons
https://git.ruby-lang.org/ruby.git/commit/?id=9212d96307 From 9212d963070612e669c40e5fde7954f19d648002 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Wed, 31 Aug 2022 11:01:59 +0900 Subject: [Bug #18937] Coerce non-real non-Numeric into Complex at comparisons --- complex.c | 16 ++++++++++++---- test/ruby/test_complex.rb | 10 +++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/complex.c b/complex.c index 1feafa370b..7d45b445db 100644 --- a/complex.c +++ b/complex.c @@ -1124,15 +1124,23 @@ nucomp_cmp(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/complex.c#L1124 if (!k_numeric_p(other)) { return rb_num_coerce_cmp(self, other, idCmp); } - if (nucomp_real_p(self)) { - if (RB_TYPE_P(other, T_COMPLEX) && nucomp_real_p(other)) { + if (!nucomp_real_p(self)) { + return Qnil; + } + if (RB_TYPE_P(other, T_COMPLEX)) { + if (nucomp_real_p(other)) { get_dat2(self, other); return rb_funcall(adat->real, idCmp, 1, bdat->real); } - else if (f_real_p(other)) { - get_dat1(self); + } + else { + get_dat1(self); + if (f_real_p(other)) { return rb_funcall(dat->real, idCmp, 1, other); } + else { + return rb_num_coerce_cmp(dat->real, other, idCmp); + } } return Qnil; } diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index f85bf101e0..13511fd4cf 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -568,19 +568,23 @@ class Complex_Test < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_complex.rb#L568 end class ObjectX - def +(x) Rational(1) end + def initialize(real = true, n = 1) @n = n; @real = real; end + def +(x) Rational(@n) end alias - + alias * + alias / + alias quo + alias ** + - def coerce(x) [x, Complex(1)] end + def coerce(x) [x, Complex(@n)] end + def real?; @real; end end def test_coerce2 x = ObjectX.new + y = ObjectX.new(false) %w(+ - * / quo ** <=>).each do |op| - assert_kind_of(Numeric, Complex(1).__send__(op, x)) + assert_kind_of(Numeric, Complex(1).__send__(op, x), op) + assert_kind_of(Numeric, Complex(1).__send__(op, y), op) end end -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/