mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Make proc/Proc.new without block an error instead of warning
The warning for these was added in 2.7.
This commit is contained in:
parent
f48fce4981
commit
f3e927b0cc
Notes:
git
2020-06-11 09:50:16 +09:00
6 changed files with 11 additions and 63 deletions
|
@ -534,11 +534,11 @@ assert_equal %Q{ENSURE\n}, %q{
|
|||
['[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{
|
|||
['[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
|
||||
|
|
|
@ -367,8 +367,8 @@ assert_equal 'ok', %q{
|
|||
|
||||
assert_equal 'ok', %q{
|
||||
class Foo
|
||||
def call_it
|
||||
p = Proc.new
|
||||
def call_it(&block)
|
||||
p = Proc.new(&block)
|
||||
p.call
|
||||
end
|
||||
end
|
||||
|
|
40
proc.c
40
proc.c
|
@ -21,10 +21,6 @@
|
|||
#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,26 +753,8 @@ proc_new(VALUE klass, int8_t is_lambda, int8_t kernel)
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/* block is in cf */
|
||||
switch (vm_block_handler_type(block_handler)) {
|
||||
|
@ -2084,25 +2062,7 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
|
|||
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];
|
||||
|
|
|
@ -48,7 +48,7 @@ describe "Kernel#proc" do
|
|||
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
|
||||
|
|
|
@ -203,7 +203,7 @@ describe "Proc.new without a block" do
|
|||
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
|
||||
|
|
|
@ -53,11 +53,9 @@ class TestProc < Test::Unit::TestCase
|
|||
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
|
|||
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}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue