ruby-changes:38890
From: nobu <ko1@a...>
Date: Fri, 19 Jun 2015 14:54:05 +0900 (JST)
Subject: [ruby-changes:38890] nobu:r50971 (trunk): proc.c: ArgumentError if no block
nobu 2015-06-19 14:53:41 +0900 (Fri, 19 Jun 2015) New Revision: 50971 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50971 Log: 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] Modified files: trunk/ChangeLog trunk/NEWS trunk/proc.c trunk/test/ruby/test_method.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 50970) +++ ChangeLog (revision 50971) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Jun 19 14:53:35 2015 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * proc.c (rb_mod_define_method): get rid of inadvertent ID Index: proc.c =================================================================== --- proc.c (revision 50970) +++ proc.c (revision 50971) @@ -14,6 +14,10 @@ https://github.com/ruby/ruby/blob/trunk/proc.c#L14 #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) https://github.com/ruby/ruby/blob/trunk/proc.c#L579 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) https://github.com/ruby/ruby/blob/trunk/proc.c#L587 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 *ar https://github.com/ruby/ruby/blob/trunk/proc.c#L1679 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]; Index: NEWS =================================================================== --- NEWS (revision 50970) +++ NEWS (revision 50971) @@ -61,6 +61,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L61 * 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] Index: test/ruby/test_method.rb =================================================================== --- test/ruby/test_method.rb (revision 50970) +++ test/ruby/test_method.rb (revision 50971) @@ -285,8 +285,9 @@ class TestMethod < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_method.rb#L285 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_method.rb#L296 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) } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/