[前][次][番号順一覧][スレッド一覧]

ruby-changes:60161

From: Nobuyoshi <ko1@a...>
Date: Sat, 22 Feb 2020 10:53:01 +0900 (JST)
Subject: [ruby-changes:60161] 5b29ea0845 (master): Proc from Symbol needs a receiver

https://git.ruby-lang.org/ruby.git/commit/?id=5b29ea0845

From 5b29ea0845c14092abd866ce0183c52635bade4c Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 22 Feb 2020 10:40:25 +0900
Subject: Proc from Symbol needs a receiver

So its arity should be -2 instead of -1.

[Bug #16640]
https://bugs.ruby-lang.org/issues/16640#change-84337

diff --git a/proc.c b/proc.c
index 7fee55b..390b1bf 100644
--- a/proc.c
+++ b/proc.c
@@ -1091,7 +1091,8 @@ rb_vm_block_min_max_arity(const struct rb_block *block, int *max) https://github.com/ruby/ruby/blob/trunk/proc.c#L1091
 	    return ifunc->argc.min;
 	}
       case block_type_symbol:
-	break;
+        *max = UNLIMITED_ARGUMENTS;
+        return 1;
     }
     *max = UNLIMITED_ARGUMENTS;
     return 0;
diff --git a/spec/ruby/core/symbol/to_proc_spec.rb b/spec/ruby/core/symbol/to_proc_spec.rb
index a58187d..27eb697 100644
--- a/spec/ruby/core/symbol/to_proc_spec.rb
+++ b/spec/ruby/core/symbol/to_proc_spec.rb
@@ -12,18 +12,20 @@ describe "Symbol#to_proc" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/symbol/to_proc_spec.rb#L12
     :to_s.to_proc.call(obj).should == "Received #to_s"
   end
 
-  it "produces a proc with arity -1" do
+  expected_arity = ruby_version_is("2.8") {-2} || -1
+  it "produces a proc with arity #{expected_arity}" do
     pr = :to_s.to_proc
-    pr.arity.should == -1
+    pr.arity.should == expected_arity
   end
 
   it "raises an ArgumentError when calling #call on the Proc without receiver" do
     -> { :object_id.to_proc.call }.should raise_error(ArgumentError, "no receiver given")
   end
 
-  it "produces a proc that always returns [[:rest]] for #parameters" do
+  expected_parameters = ruby_version_is("2.8") {[[:req], [:rest]]} || [[:rest]]
+  it "produces a proc that always returns #{expected_parameters} for #parameters" do
     pr = :to_s.to_proc
-    pr.parameters.should == [[:rest]]
+    pr.parameters.should == expected_parameters
   end
 
   it "passes along the block passed to Proc#call" do
diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb
index 632acb3..569b888 100644
--- a/test/ruby/test_symbol.rb
+++ b/test/ruby/test_symbol.rb
@@ -157,6 +157,10 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L157
     assert_predicate(:itself.to_proc, :lambda?)
   end
 
+  def test_to_proc_arity
+    assert_equal(-2, :itself.to_proc.arity)
+  end
+
   def test_to_proc_call_with_symbol_proc
     first = 1
     bug11594 = "[ruby-core:71088] [Bug #11594] corrupted the first local variable"
@@ -187,6 +191,10 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L191
     assert_predicate(_test_to_proc_with_refinements_call(&:hoge), :lambda?)
   end
 
+  def test_to_proc_arity_with_refinements
+    assert_equal(-2, _test_to_proc_with_refinements_call(&:hoge).arity)
+  end
+
   def self._test_to_proc_arg_with_refinements_call(&block)
     block.call TestToPRocArgWithRefinements.new
   end
@@ -230,11 +238,11 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L238
     begin;
       bug11845 = '[ruby-core:72381] [Bug #11845]'
       assert_nil(:class.to_proc.source_location, bug11845)
-      assert_equal([[:rest]], :class.to_proc.parameters, bug11845)
+      assert_equal([[:req], [:rest]], :class.to_proc.parameters, bug11845)
       c = Class.new {define_method(:klass, :class.to_proc)}
       m = c.instance_method(:klass)
       assert_nil(m.source_location, bug11845)
-      assert_equal([[:rest]], m.parameters, bug11845)
+      assert_equal([[:req], [:rest]], m.parameters, bug11845)
     end;
   end
 
diff --git a/vm_args.c b/vm_args.c
index 9357e97..e6d4981 100644
--- a/vm_args.c
+++ b/vm_args.c
@@ -868,7 +868,7 @@ vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t * https://github.com/ruby/ruby/blob/trunk/vm_args.c#L868
                     rb_ary_push(callback_arg, block_code);
                     rb_ary_push(callback_arg, ref);
                     OBJ_FREEZE_RAW(callback_arg);
-                    func = rb_func_lambda_new(refine_sym_proc_call, callback_arg, 0, UNLIMITED_ARGUMENTS);
+                    func = rb_func_lambda_new(refine_sym_proc_call, callback_arg, 1, UNLIMITED_ARGUMENTS);
 		    rb_hash_aset(ref, block_code, func);
 		}
 		block_code = func;
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]