From e77f9fcb13a157c2478a0e78cdb0b9696e8ded8c Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 19 Jun 2015 05:53:41 +0000 Subject: 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 --- ChangeLog | 5 +++++ NEWS | 5 +++++ proc.c | 18 ++++++++++++++++++ test/ruby/test_method.rb | 17 +++++++++++++++-- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index c3610d8a44..116b9ec18d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Jun 19 14:53:35 2015 Nobuyoshi Nakada + + * 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 * proc.c (rb_mod_define_method): get rid of inadvertent ID diff --git a/NEWS b/NEWS index 05ae35a978..19171f3169 100644 --- a/NEWS +++ b/NEWS @@ -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] diff --git a/proc.c b/proc.c index e333048af4..adb9f674a2 100644 --- a/proc.c +++ b/proc.c @@ -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]; diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index d54000d745..911896d0b0 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -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) } -- cgit v1.2.3