Project

General

Profile

Actions

Bug #10687

closed

ArgumentError in certain cases of instance_exec (new bug in 2.2)

Added by Anonymous about 9 years ago. Updated about 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]
Backport:
[ruby-core:67282]

Description

a.rb:


foo = ->(a = nil) do
  p(a)
end
instance_exec(["a", "b", "c", "d"], &foo)
#=> rvm use 2.1 && ruby -v && ruby a.rb 
Using /home/micaeked/.rvm/gems/ruby-2.1.5
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
["a", "b", "c", "d"]
#=> rvm use 2.2 && ruby -v && ruby a.rb 
Using /home/micaeked/.rvm/gems/ruby-2.2.0
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]
a.rb:1:in `block in <main>': wrong number of arguments (4 for 0..1) (ArgumentError)
	from a.rb:5:in `instance_exec'
	from a.rb:5:in `<main>'

Files

relaxed.patch (654 Bytes) relaxed.patch hanachin (Seiei Miyagi), 03/15/2015 01:16 PM
test_lambda_relaxed.rb (10.6 KB) test_lambda_relaxed.rb hanachin (Seiei Miyagi), 03/15/2015 01:17 PM

Related issues 1 (0 open1 closed)

Has duplicate Ruby master - Bug #12568: wrong ArgumentError if an array is given for instance_exec with optional argumentClosedActions

Updated by stoodfarback (stood far back) about 9 years ago

Still broken in 2.2.1:

#=> ruby -v && ruby a.rb
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
a.rb:1:in `block in <main>': wrong number of arguments (4 for 0..1) (ArgumentError)
	from a.rb:4:in `instance_exec'
	from a.rb:4:in `<main>'

Updated by hanachin (Seiei Miyagi) about 9 years ago

It seems broken at r48239

$ ./ruby -v --disable-gems a.rb
ruby 2.2.0dev (2014-11-03 trunk 48238) [x86_64-darwin14]
["a", "b", "c", "d"]
$ ./ruby -v --disable-gems a.rb
ruby 2.2.0dev (2014-11-03 trunk 48239) [x86_64-darwin14]
a.rb:1:in `block in <main>': wrong number of arguments (4 for 0..1) (ArgumentError)
	from a.rb:4:in `instance_exec'
	from a.rb:4:in `<main>'

I wrote a patch for fix this issue.

diff --git vm_args.c vm_args.c
index e4ade6b..cd52227 100644
--- vm_args.c
+++ vm_args.c
@@ -562,10 +562,14 @@ setup_parameters_complex(rb_thread_t * const th, const rb_iseq_t * const iseq, r
 	break;
       case arg_setup_lambda:
 	if (given_argc == 1 &&
-	    given_argc != iseq->param.lead_num &&
+	    given_argc != min_argc &&
+	    (min_argc > 0 || iseq->param.flags.has_block) &&
 	    !iseq->param.flags.has_rest &&
 	    args_check_block_arg0(args, th, msl)) {
-	    given_argc = RARRAY_LENINT(args->rest);
+	    int len = RARRAY_LENINT(args->rest);
+	    if (len >= min_argc && len <= max_argc) {
+	      given_argc = len;
+	    }
 	}
     }

And I wrote some test, passing a lambda as a block, then yield an array.
All test cases passed at r48238, But failed in trunk, and with this patch too.

Updated by stoodfarback (stood far back) about 8 years ago

Still broken at 2.3.0:

$ ruby -v a.rb        
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
a.rb:1:in `block in <main>': wrong number of arguments (given 4, expected 0..1) (ArgumentError)
	from a.rb:4:in `instance_exec'
	from a.rb:4:in `<main>'
Actions #4

Updated by nobu (Nobuyoshi Nakada) about 7 years ago

  • Has duplicate Bug #12568: wrong ArgumentError if an array is given for instance_exec with optional argument added

Updated by nobu (Nobuyoshi Nakada) about 7 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0