proc.c: ArgumentError if no block

* proc.c (rb_mod_define_method): now requires a block direct to
  this method call.  [ruby-core:69655] [Bug #11283]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50971 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-06-19 05:53:41 +00:00
parent 19d2532a79
commit e77f9fcb13
4 changed files with 43 additions and 2 deletions

View File

@ -1,3 +1,8 @@
Fri Jun 19 14:53:35 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* proc.c (rb_mod_define_method): now requires a block direct to
this method call. [ruby-core:69655] [Bug #11283]
Fri Jun 19 13:54:43 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* proc.c (rb_mod_define_method): get rid of inadvertent ID

5
NEWS
View File

@ -61,6 +61,11 @@ with all sufficient information, see the ChangeLog file.
* IO
* IO#close doesn't raise when the IO object is closed. [Feature #10718]
* Module
* Module#define_method and Object.define_singleton_method now
require method body, Proc, Method, or a block, and raise
ArgumentError if no block is given directly. [Bug #11283]
* pack/unpack (Array/String)
* j and J directives for pointer width integer type. [Feature #11215]

18
proc.c
View File

@ -14,6 +14,10 @@
#include "gc.h"
#include "iseq.h"
/* Proc.new with no block will raise an exception in the future
* versions */
#define PROC_NEW_REQUIRES_BLOCK 0
const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
struct METHOD {
@ -575,6 +579,7 @@ proc_new(VALUE klass, int8_t is_lambda)
rb_block_t *block;
if (!(block = rb_vm_control_frame_block_ptr(cfp))) {
#if !PROC_NEW_REQUIRES_BLOCK
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
if ((block = rb_vm_control_frame_block_ptr(cfp)) != 0) {
@ -582,6 +587,9 @@ proc_new(VALUE klass, int8_t is_lambda)
rb_warn(proc_without_block);
}
}
#else
if (0)
#endif
else {
rb_raise(rb_eArgError, proc_without_block);
}
@ -1671,7 +1679,17 @@ 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
rb_thread_t *th = GET_THREAD();
rb_block_t *block = rb_vm_control_frame_block_ptr(th->cfp);
if (!block) rb_raise(rb_eArgError, proc_without_block);
body = block->proc;
if (!body) {
body = rb_vm_make_proc_lambda(th, block, rb_cProc, TRUE);
}
#endif
}
else {
body = argv[1];

View File

@ -285,8 +285,9 @@ class TestMethod < Test::Unit::TestCase
end
c = Class.new
assert_raise(ArgumentError) {o.foo(c)}
o.foo(c) { :foo }
assert_equal(:foo, c.new.foo)
bug11283 = '[ruby-core:69655] [Bug #11283]'
assert_raise(ArgumentError, bug11283) {o.foo(c) {:foo}}
end
def test_define_singleton_method
@ -295,6 +296,18 @@ class TestMethod < Test::Unit::TestCase
assert_equal(:foo, o.foo)
end
def test_define_singleton_method_no_proc
assert_raise(ArgumentError) {
o.instance_eval { define_singleton_method(:bar) }
}
bug11283 = '[ruby-core:69655] [Bug #11283]'
def o.define(n)
define_singleton_method(n)
end
assert_raise(ArgumentError) {o.define(:bar) {:bar}}
end
def test_define_method_invalid_arg
assert_raise(TypeError) do
Class.new.class_eval { define_method(:foo, Object.new) }