ruby-changes:73373
From: Nobuyoshi <ko1@a...>
Date: Fri, 2 Sep 2022 14:33:46 +0900 (JST)
Subject: [ruby-changes:73373] b5cf356447 (master): Consider Complex from Complex cases
https://git.ruby-lang.org/ruby.git/commit/?id=b5cf356447 From b5cf3564471af6e11760bf381251f918cdcd7398 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Wed, 31 Aug 2022 13:20:40 +0900 Subject: Consider Complex from Complex cases The assertions that "an argument of a Complex constructor must not be a Complex" may not hold for some Numeric objects. --- complex.c | 24 +++++++++++++----------- test/ruby/test_complex.rb | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/complex.c b/complex.c index 7d45b445db..07e5914d54 100644 --- a/complex.c +++ b/complex.c @@ -495,7 +495,11 @@ nucomp_s_new(int argc, VALUE *argv, VALUE klass) https://github.com/ruby/ruby/blob/trunk/complex.c#L495 inline static VALUE f_complex_new2(VALUE klass, VALUE x, VALUE y) { - assert(!RB_TYPE_P(x, T_COMPLEX)); + if (RB_TYPE_P(x, T_COMPLEX)) { + get_dat1(x); + x = dat->real; + y = f_add(dat->imag, y); + } return nucomp_s_canonicalize_internal(klass, x, y); } @@ -609,8 +613,14 @@ m_sin(VALUE x) https://github.com/ruby/ruby/blob/trunk/complex.c#L613 static VALUE f_complex_polar(VALUE klass, VALUE x, VALUE y) { - assert(!RB_TYPE_P(x, T_COMPLEX)); - assert(!RB_TYPE_P(y, T_COMPLEX)); + if (RB_TYPE_P(x, T_COMPLEX)) { + get_dat1(x); + x = dat->real; + } + if (RB_TYPE_P(y, T_COMPLEX)) { + get_dat1(y); + y = dat->real; + } if (f_zero_p(x) || f_zero_p(y)) { return nucomp_s_new_internal(klass, x, RFLOAT_0); } @@ -703,14 +713,6 @@ nucomp_s_polar(int argc, VALUE *argv, VALUE klass) https://github.com/ruby/ruby/blob/trunk/complex.c#L713 nucomp_real_check(arg); break; } - if (RB_TYPE_P(abs, T_COMPLEX)) { - get_dat1(abs); - abs = dat->real; - } - if (RB_TYPE_P(arg, T_COMPLEX)) { - get_dat1(arg); - arg = dat->real; - } return f_complex_polar(klass, abs, arg); } diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 13511fd4cf..17b5f64db2 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -567,7 +567,7 @@ class Complex_Test < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_complex.rb#L567 assert_raise_with_message(TypeError, /C\u{1f5ff}/) { Complex(1).coerce(obj) } end - class ObjectX + class ObjectX < Numeric def initialize(real = true, n = 1) @n = n; @real = real; end def +(x) Rational(@n) end alias - + -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/