ruby-changes:61663
From: Jeremy <ko1@a...>
Date: Thu, 11 Jun 2020 09:50:08 +0900 (JST)
Subject: [ruby-changes:61663] f3e927b0cc (master): Make proc/Proc.new without block an error instead of warning
https://git.ruby-lang.org/ruby.git/commit/?id=f3e927b0cc From f3e927b0cc1fcbf03abea7f66b1a3736a270a8de Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Wed, 10 Jun 2020 15:17:54 -0700 Subject: Make proc/Proc.new without block an error instead of warning The warning for these was added in 2.7. diff --git a/bootstraptest/test_flow.rb b/bootstraptest/test_flow.rb index 9da6d45..35f19db 100644 --- a/bootstraptest/test_flow.rb +++ b/bootstraptest/test_flow.rb @@ -534,11 +534,11 @@ assert_equal %Q{ENSURE\n}, %q{ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_flow.rb#L534 ['[ruby-core:39125]', %q{ class Bug5234 include Enumerable - def each + def each(&block) begin yield :foo ensure - proc + proc(&block) end end end @@ -547,11 +547,11 @@ assert_equal %Q{ENSURE\n}, %q{ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_flow.rb#L547 ['[ruby-dev:45656]', %q{ class Bug6460 include Enumerable - def each + def each(&block) begin yield :foo ensure - 1.times { Proc.new } + 1.times { Proc.new(&block) } end end end diff --git a/bootstraptest/test_proc.rb b/bootstraptest/test_proc.rb index 6d2c557..6376032 100644 --- a/bootstraptest/test_proc.rb +++ b/bootstraptest/test_proc.rb @@ -367,8 +367,8 @@ assert_equal 'ok', %q{ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_proc.rb#L367 assert_equal 'ok', %q{ class Foo - def call_it - p = Proc.new + def call_it(&block) + p = Proc.new(&block) p.call end end diff --git a/proc.c b/proc.c index 249bc65..ffc87b7 100644 --- a/proc.c +++ b/proc.c @@ -21,10 +21,6 @@ https://github.com/ruby/ruby/blob/trunk/proc.c#L21 #include "iseq.h" #include "vm_core.h" -/* Proc.new with no block will raise an exception in the future - * versions */ -#define PROC_NEW_REQUIRES_BLOCK 0 - #if !defined(__GNUC__) || __GNUC__ < 5 || defined(__MINGW32__) # define NO_CLOBBERED(v) (*(volatile VALUE *)&(v)) #else @@ -757,25 +753,7 @@ proc_new(VALUE klass, int8_t is_lambda, int8_t kernel) https://github.com/ruby/ruby/blob/trunk/proc.c#L753 VALUE block_handler; if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) { -#if !PROC_NEW_REQUIRES_BLOCK - cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); - - if ((block_handler = rb_vm_frame_block_handler(cfp)) != VM_BLOCK_HANDLER_NONE) { - if (is_lambda) { - rb_raise(rb_eArgError, proc_without_block); - } - else { - const char *name = kernel ? "Kernel#proc" : "Proc.new"; - rb_warn_deprecated("Capturing the given block using %s", - "`&block`", name); - } - } -#else - if (0); -#endif - else { - rb_raise(rb_eArgError, proc_without_block); - } + rb_raise(rb_eArgError, proc_without_block); } /* block is in cf */ @@ -2084,25 +2062,7 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod) https://github.com/ruby/ruby/blob/trunk/proc.c#L2062 name = argv[0]; id = rb_check_id(&name); if (argc == 1) { -#if PROC_NEW_REQUIRES_BLOCK body = rb_block_lambda(); -#else - const rb_execution_context_t *ec = GET_EC(); - VALUE block_handler = rb_vm_frame_block_handler(ec->cfp); - if (block_handler == VM_BLOCK_HANDLER_NONE) rb_raise(rb_eArgError, proc_without_block); - - switch (vm_block_handler_type(block_handler)) { - case block_handler_type_proc: - body = VM_BH_TO_PROC(block_handler); - break; - case block_handler_type_symbol: - body = rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler)); - break; - case block_handler_type_iseq: - case block_handler_type_ifunc: - body = rb_vm_make_lambda(ec, VM_BH_TO_CAPT_BLOCK(block_handler), rb_cProc); - } -#endif } else { body = argv[1]; diff --git a/spec/ruby/core/kernel/proc_spec.rb b/spec/ruby/core/kernel/proc_spec.rb index 2a79548..3930715 100644 --- a/spec/ruby/core/kernel/proc_spec.rb +++ b/spec/ruby/core/kernel/proc_spec.rb @@ -48,7 +48,7 @@ describe "Kernel#proc" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/kernel/proc_spec.rb#L48 end end - ruby_version_is "2.7" do + ruby_version_is "2.7" ... "2.8" do it "can be created when called with no block" do def some_method proc diff --git a/spec/ruby/core/proc/new_spec.rb b/spec/ruby/core/proc/new_spec.rb index cc03346..faaf85f 100644 --- a/spec/ruby/core/proc/new_spec.rb +++ b/spec/ruby/core/proc/new_spec.rb @@ -203,7 +203,7 @@ describe "Proc.new without a block" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/proc/new_spec.rb#L203 end end - ruby_version_is "2.7" do + ruby_version_is "2.7" ... "2.8" do it "can be created if invoked from within a method with a block" do -> { ProcSpecs.new_proc_in_method { "hello" } }.should complain(/Capturing the given block using Proc.new is deprecated/) end diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 14b7938..765b400 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -53,11 +53,9 @@ class TestProc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_proc.rb#L53 assert_equal(5, x) end - def assert_arity(n) + def assert_arity(n, &block) meta = class << self; self; end - b = assert_warn(/Capturing the given block using Proc\.new is deprecated/) do - Proc.new - end + b = Proc.new(&block) meta.class_eval { remove_method(:foo_arity) if method_defined?(:foo_arity) define_method(:foo_arity, b) @@ -1433,16 +1431,6 @@ class TestProc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_proc.rb#L1431 end; end - def method_for_test_proc_without_block_for_symbol - assert_warn(/Capturing the given block using Kernel#proc is deprecated/) do - binding.eval('proc') - end - end - - def test_proc_without_block_for_symbol - assert_equal('1', method_for_test_proc_without_block_for_symbol(&:to_s).call(1), '[Bug #14782]') - end - def test_compose f = proc {|x| x * 2} g = proc {|x| x + 1} -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/