From 9f1fb0a17febc59356d58cef5e98db61a3c03550 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 10 Jan 2019 08:19:14 +0000 Subject: [PATCH] proc.c: proc without block * proc.c (proc_new): promoted lambda/proc/Proc.new with no block in a method called with a block to a warning/error. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- NEWS | 5 +++ lib/matrix.rb | 3 +- lib/prime.rb | 6 +-- proc.c | 3 ++ spec/ruby/core/kernel/proc_spec.rb | 24 +++++++++--- spec/ruby/core/proc/block_pass_spec.rb | 45 +++++++++++++++------- spec/ruby/core/proc/new_spec.rb | 53 +++++++++++++++++++------- spec/ruby/language/lambda_spec.rb | 27 +++++++++---- spec/ruby/optional/capi/proc_spec.rb | 40 ++++++++++--------- test/ruby/test_proc.rb | 4 +- 10 files changed, 144 insertions(+), 66 deletions(-) diff --git a/NEWS b/NEWS index 6045717bb288f5..a08c10f5bca6bc 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,11 @@ sufficient information, see the ChangeLog file or Redmine * Method reference operator, .: is introduced as an experimental feature. [Feature #12125] [Feature #13581] +* Proc.new and proc with no block in a method called with a block is warned + now. + +* lambda with no block in a method called with a block errs. + === Core classes updates (outstanding ones only) === Stdlib updates (outstanding ones only) diff --git a/lib/matrix.rb b/lib/matrix.rb index 7f338bb07e8c55..34602f5c40310f 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -514,12 +514,11 @@ def freeze # # => prints the numbers 1 to 4 # Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3] # - def each(which = :all) # :yield: e + def each(which = :all, &block) # :yield: e return to_enum :each, which unless block_given? last = column_count - 1 case which when :all - block = Proc.new @rows.each do |row| row.each(&block) end diff --git a/lib/prime.rb b/lib/prime.rb index 9f6db448be8547..3aa20f8def95a1 100644 --- a/lib/prime.rb +++ b/lib/prime.rb @@ -283,9 +283,9 @@ def each end # see +Enumerator+#with_index. - def with_index(offset = 0) - return enum_for(:with_index, offset) { Float::INFINITY } unless block_given? - return each_with_index(&proc) if offset == 0 + def with_index(offset = 0, &block) + return enum_for(:with_index, offset) { Float::INFINITY } unless block + return each_with_index(&block) if offset == 0 each do |prime| yield prime, offset diff --git a/proc.c b/proc.c index c24c0b302345df..6241bf32ec3f78 100644 --- a/proc.c +++ b/proc.c @@ -707,6 +707,9 @@ proc_new(VALUE klass, int8_t is_lambda) if ((block_handler = rb_vm_frame_block_handler(cfp)) != VM_BLOCK_HANDLER_NONE) { if (is_lambda) { + rb_raise(rb_eArgError, proc_without_block); + } + else { rb_warn(proc_without_block); } } diff --git a/spec/ruby/core/kernel/proc_spec.rb b/spec/ruby/core/kernel/proc_spec.rb index d9c09117e71345..2c0e7988f9d817 100644 --- a/spec/ruby/core/kernel/proc_spec.rb +++ b/spec/ruby/core/kernel/proc_spec.rb @@ -36,14 +36,28 @@ def test end describe "Kernel#proc" do - it "uses the implicit block from an enclosing method" do - def some_method - proc + ruby_version_is ""..."2.7" do + it "uses the implicit block from an enclosing method" do + def some_method + proc + end + + prc = some_method { "hello" } + + prc.call.should == "hello" end + end - prc = some_method { "hello" } + ruby_version_is "2.7" do + it "can be created when called with no block" do + def some_method + proc + end - prc.call.should == "hello" + -> { + some_method { "hello" } + }.should complain(/tried to create Proc object without a block/) + end end it "needs to be reviewed for spec completeness" diff --git a/spec/ruby/core/proc/block_pass_spec.rb b/spec/ruby/core/proc/block_pass_spec.rb index 282f00b5a8b6d5..917a38efacc106 100644 --- a/spec/ruby/core/proc/block_pass_spec.rb +++ b/spec/ruby/core/proc/block_pass_spec.rb @@ -20,22 +20,39 @@ def revivify(&b) end end -describe "Proc as an implicit block pass argument" do - def revivify - Proc.new - end +ruby_version_is ""..."2.7" do + describe "Proc as an implicit block pass argument" do + def revivify + Proc.new + end - it "remains the same object if re-vivified by the target method" do - p = Proc.new {} - p2 = revivify(&p) - p.should equal p2 - p.should == p2 + it "remains the same object if re-vivified by the target method" do + p = Proc.new {} + p2 = revivify(&p) + p.should equal p2 + p.should == p2 + end + + it "remains the same object if reconstructed with Proc.new" do + p = Proc.new {} + p2 = Proc.new(&p) + p.should equal p2 + p.should == p2 + end end +end - it "remains the same object if reconstructed with Proc.new" do - p = Proc.new {} - p2 = Proc.new(&p) - p.should equal p2 - p.should == p2 +ruby_version_is "2.7" do + describe "Proc called with no block" do + def revivify + Proc.new + end + + it "raises ArgumentError when called with no block" do + p = Proc.new {} + -> { + revivify(&p) + }.should + end end end diff --git a/spec/ruby/core/proc/new_spec.rb b/spec/ruby/core/proc/new_spec.rb index abdd1872766333..1c37c64478690b 100644 --- a/spec/ruby/core/proc/new_spec.rb +++ b/spec/ruby/core/proc/new_spec.rb @@ -95,16 +95,18 @@ def some_method obj.second.should == 2 end - it "returns a new Proc instance from the block passed to the containing method" do - prc = ProcSpecs.new_proc_in_method { "hello" } - prc.should be_an_instance_of(Proc) - prc.call.should == "hello" - end + ruby_version_is ""..."2.7" do + it "returns a new Proc instance from the block passed to the containing method" do + prc = ProcSpecs.new_proc_in_method { "hello" } + prc.should be_an_instance_of(Proc) + prc.call.should == "hello" + end - it "returns a new Proc instance from the block passed to the containing method" do - prc = ProcSpecs.new_proc_subclass_in_method { "hello" } - prc.should be_an_instance_of(ProcSpecs::ProcSubclass) - prc.call.should == "hello" + it "returns a new Proc instance from the block passed to the containing method" do + prc = ProcSpecs.new_proc_subclass_in_method { "hello" } + prc.should be_an_instance_of(ProcSpecs::ProcSubclass) + prc.call.should == "hello" + end end end @@ -178,13 +180,36 @@ def some_method lambda { ProcSpecs.new_proc_subclass_in_method }.should raise_error(ArgumentError) end - it "uses the implicit block from an enclosing method" do - def some_method - Proc.new + ruby_version_is ""..."2.7" do + it "uses the implicit block from an enclosing method" do + def some_method + Proc.new + end + + prc = some_method { "hello" } + + prc.call.should == "hello" + end + end + + ruby_version_is "2.7" do + it "can be created if invoked from within a method with a block" do + lambda { ProcSpecs.new_proc_in_method { "hello" } }.should complain(/tried to create Proc object without a block/) + end + + it "can be created if invoked on a subclass from within a method with a block" do + lambda { ProcSpecs.new_proc_subclass_in_method { "hello" } }.should complain(/tried to create Proc object without a block/) end - prc = some_method { "hello" } - prc.call.should == "hello" + it "can be create when called with no block" do + def some_method + Proc.new + end + + -> { + some_method { "hello" } + }.should complain(/tried to create Proc object without a block/) + end end end diff --git a/spec/ruby/language/lambda_spec.rb b/spec/ruby/language/lambda_spec.rb index 3934a2bd917980..7114a38e5cbee3 100644 --- a/spec/ruby/language/lambda_spec.rb +++ b/spec/ruby/language/lambda_spec.rb @@ -310,14 +310,25 @@ def obj.define def meth; lambda; end end - it "can be created" do - implicit_lambda = nil - -> { - implicit_lambda = meth { 1 } - }.should complain(/tried to create Proc object without a block/) - - implicit_lambda.lambda?.should be_true - implicit_lambda.call.should == 1 + ruby_version_is ""..."2.7" do + it "can be created" do + implicit_lambda = nil + -> { + implicit_lambda = meth { 1 } + }.should complain(/tried to create Proc object without a block/) + + implicit_lambda.lambda?.should be_true + implicit_lambda.call.should == 1 + end + end + + ruby_version_is "2.7" do + it "raises ArgumentError" do + implicit_lambda = nil + -> { + meth { 1 } + }.should raise_error(ArgumentError, /tried to create Proc object without a block/) + end end end diff --git a/spec/ruby/optional/capi/proc_spec.rb b/spec/ruby/optional/capi/proc_spec.rb index 94c9276c09d2e6..bffb32484c8f0e 100644 --- a/spec/ruby/optional/capi/proc_spec.rb +++ b/spec/ruby/optional/capi/proc_spec.rb @@ -69,16 +69,18 @@ # For example: C -> Ruby <- C -> Ruby means a C function called into Ruby # code which returned to C, then C called into Ruby code again. - # Ruby -> C -> rb_funcall(Proc.new) - it "returns the Proc passed by the Ruby code calling the C function" do - prc = @p.rb_Proc_new(0) { :called } - prc.call.should == :called - end + ruby_version_is ""..."2.7" do + # Ruby -> C -> rb_funcall(Proc.new) + it "returns the Proc passed by the Ruby code calling the C function" do + prc = @p.rb_Proc_new(0) { :called } + prc.call.should == :called + end - # Ruby -> C -> Ruby <- C -> rb_funcall(Proc.new) - it "returns the Proc passed to the Ruby method when the C function calls other Ruby methods before calling Proc.new" do - prc = @p.rb_Proc_new(1) { :called } - prc.call.should == :called + # Ruby -> C -> Ruby <- C -> rb_funcall(Proc.new) + it "returns the Proc passed to the Ruby method when the C function calls other Ruby methods before calling Proc.new" do + prc = @p.rb_Proc_new(1) { :called } + prc.call.should == :called + end end # Ruby -> C -> Ruby -> Proc.new @@ -93,16 +95,18 @@ def @p.redispatch() rb_Proc_new(0) end lambda { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError) end - # Ruby -> C -> Ruby -> C (with new block) -> rb_funcall(Proc.new) - it "returns the most recent Proc passed when the Ruby method called the C function" do - prc = @p.rb_Proc_new(4) { :called } - prc.call.should == :calling_with_block - end + ruby_version_is ""..."2.7" do + # Ruby -> C -> Ruby -> C (with new block) -> rb_funcall(Proc.new) + it "returns the most recent Proc passed when the Ruby method called the C function" do + prc = @p.rb_Proc_new(4) { :called } + prc.call.should == :calling_with_block + end - # Ruby -> C -> Ruby -> C (with new block) <- Ruby <- C -> # rb_funcall(Proc.new) - it "returns the Proc passed from the original Ruby call to the C function" do - prc = @p.rb_Proc_new(5) { :called } - prc.call.should == :called + # Ruby -> C -> Ruby -> C (with new block) <- Ruby <- C -> # rb_funcall(Proc.new) + it "returns the Proc passed from the original Ruby call to the C function" do + prc = @p.rb_Proc_new(5) { :called } + prc.call.should == :called + end end # Ruby -> C -> Ruby -> block_given? diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 4abe0aa1bbc3f0..10ae02849d9a5a 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -424,14 +424,14 @@ def o.foo 1.times { b = lambda } b end - assert_equal(:foo, o.foo { :foo }.call) + assert_raise(ArgumentError) {o.foo { :foo }.call} def o.foo(&b) b = nil 1.times { b = lambda } b end - assert_equal(:foo, o.foo { :foo }.call) + assert_raise(ArgumentError) {o.foo { :foo }.call} end def test_arity2