ruby-changes:49955
From: nobu <ko1@a...>
Date: Sat, 27 Jan 2018 19:41:50 +0900 (JST)
Subject: [ruby-changes:49955] nobu:r62073 (trunk): compile.c: try to convert in massign `for`
nobu 2018-01-27 19:41:43 +0900 (Sat, 27 Jan 2018) New Revision: 62073 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62073 Log: compile.c: try to convert in massign `for` * compile.c (compile_for_masgn): try to convert to an Array if an element of massign `for` is not an Array. [ruby-core:84931] [Bug #14374] Modified files: trunk/compile.c trunk/test/ruby/test_iterator.rb Index: compile.c =================================================================== --- compile.c (revision 62072) +++ compile.c (revision 62073) @@ -5040,7 +5040,7 @@ static int https://github.com/ruby/ruby/blob/trunk/compile.c#L5040 compile_for_masgn(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped) { /* massign to var in "for" - * args.length == 1 && Array === (tmp = args[0]) ? tmp : args + * (args.length == 1 && Array.try_convert(args[0])) || args */ const int line = nd_line(node); const NODE *var = node->nd_var; @@ -5056,8 +5056,9 @@ compile_for_masgn(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L5056 ADD_INSN1(ret, line, putobject, INT2FIX(0)); ADD_CALL(ret, line, idAREF, INT2FIX(1)); ADD_INSN1(ret, line, putobject, rb_cArray); - ADD_INSN1(ret, line, topn, INT2FIX(1)); - ADD_CALL(ret, line, idEqq, INT2FIX(1)); + ADD_INSN(ret, line, swap); + ADD_CALL(ret, line, rb_intern("try_convert"), INT2FIX(1)); + ADD_INSN(ret, line, dup); ADD_INSNL(ret, line, branchunless, not_ary); ADD_INSN(ret, line, swap); ADD_LABEL(ret, not_ary); Index: test/ruby/test_iterator.rb =================================================================== --- test/ruby/test_iterator.rb (revision 62072) +++ test/ruby/test_iterator.rb (revision 62073) @@ -107,6 +107,16 @@ class TestIterator < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iterator.rb#L107 assert_equal([1, 2, 3, 4, 5, 6, 7], x) end + def test_array_for_masgn + a = [Struct.new(:to_ary).new([1,2])] + x = [] + a.each {|i,j|x << [i,j]} + assert_equal([[1,2]], x) + x = [] + for i,j in a; x << [i,j]; end + assert_equal([[1,2]], x) + end + def test_append_method_to_built_in_class x = [[1,2],[3,4],[5,6]] assert_equal(x.iter_test1{|e|e}, x.iter_test2{|e|e}) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/