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
@ -534,11 +534,11 @@ assert_equal %Q{ENSURE\n}, %q{
|
|||||||
['[ruby-core:39125]', %q{
|
['[ruby-core:39125]', %q{
|
||||||
class Bug5234
|
class Bug5234
|
||||||
include Enumerable
|
include Enumerable
|
||||||
def each
|
def each(&block)
|
||||||
begin
|
begin
|
||||||
yield :foo
|
yield :foo
|
||||||
ensure
|
ensure
|
||||||
proc
|
proc(&block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -547,11 +547,11 @@ assert_equal %Q{ENSURE\n}, %q{
|
|||||||
['[ruby-dev:45656]', %q{
|
['[ruby-dev:45656]', %q{
|
||||||
class Bug6460
|
class Bug6460
|
||||||
include Enumerable
|
include Enumerable
|
||||||
def each
|
def each(&block)
|
||||||
begin
|
begin
|
||||||
yield :foo
|
yield :foo
|
||||||
ensure
|
ensure
|
||||||
1.times { Proc.new }
|
1.times { Proc.new(&block) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -367,8 +367,8 @@ assert_equal 'ok', %q{
|
|||||||
|
|
||||||
assert_equal 'ok', %q{
|
assert_equal 'ok', %q{
|
||||||
class Foo
|
class Foo
|
||||||
def call_it
|
def call_it(&block)
|
||||||
p = Proc.new
|
p = Proc.new(&block)
|
||||||
p.call
|
p.call
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
40
proc.c
40
proc.c
@ -21,10 +21,6 @@
|
|||||||
#include "iseq.h"
|
#include "iseq.h"
|
||||||
#include "vm_core.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__)
|
#if !defined(__GNUC__) || __GNUC__ < 5 || defined(__MINGW32__)
|
||||||
# define NO_CLOBBERED(v) (*(volatile VALUE *)&(v))
|
# define NO_CLOBBERED(v) (*(volatile VALUE *)&(v))
|
||||||
#else
|
#else
|
||||||
@ -757,26 +753,8 @@ proc_new(VALUE klass, int8_t is_lambda, int8_t kernel)
|
|||||||
VALUE block_handler;
|
VALUE block_handler;
|
||||||
|
|
||||||
if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) {
|
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);
|
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 */
|
/* block is in cf */
|
||||||
switch (vm_block_handler_type(block_handler)) {
|
switch (vm_block_handler_type(block_handler)) {
|
||||||
@ -2084,25 +2062,7 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
|
|||||||
name = argv[0];
|
name = argv[0];
|
||||||
id = rb_check_id(&name);
|
id = rb_check_id(&name);
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
#if PROC_NEW_REQUIRES_BLOCK
|
|
||||||
body = rb_block_lambda();
|
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 {
|
else {
|
||||||
body = argv[1];
|
body = argv[1];
|
||||||
|
@ -48,7 +48,7 @@ describe "Kernel#proc" do
|
|||||||
end
|
end
|
||||||
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
|
it "can be created when called with no block" do
|
||||||
def some_method
|
def some_method
|
||||||
proc
|
proc
|
||||||
|
@ -203,7 +203,7 @@ describe "Proc.new without a block" do
|
|||||||
end
|
end
|
||||||
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
|
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/)
|
-> { ProcSpecs.new_proc_in_method { "hello" } }.should complain(/Capturing the given block using Proc.new is deprecated/)
|
||||||
end
|
end
|
||||||
|
@ -53,11 +53,9 @@ class TestProc < Test::Unit::TestCase
|
|||||||
assert_equal(5, x)
|
assert_equal(5, x)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_arity(n)
|
def assert_arity(n, &block)
|
||||||
meta = class << self; self; end
|
meta = class << self; self; end
|
||||||
b = assert_warn(/Capturing the given block using Proc\.new is deprecated/) do
|
b = Proc.new(&block)
|
||||||
Proc.new
|
|
||||||
end
|
|
||||||
meta.class_eval {
|
meta.class_eval {
|
||||||
remove_method(:foo_arity) if method_defined?(:foo_arity)
|
remove_method(:foo_arity) if method_defined?(:foo_arity)
|
||||||
define_method(:foo_arity, b)
|
define_method(:foo_arity, b)
|
||||||
@ -1433,16 +1431,6 @@ class TestProc < Test::Unit::TestCase
|
|||||||
end;
|
end;
|
||||||
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
|
def test_compose
|
||||||
f = proc {|x| x * 2}
|
f = proc {|x| x * 2}
|
||||||
g = proc {|x| x + 1}
|
g = proc {|x| x + 1}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user