[前][次][番号順一覧][スレッド一覧]

ruby-changes:49340

From: ko1 <ko1@a...>
Date: Mon, 25 Dec 2017 09:27:22 +0900 (JST)
Subject: [ruby-changes:49340] ko1:r61457 (trunk): Set first line numbers for empty iseqs.

ko1	2017-12-25 09:27:17 +0900 (Mon, 25 Dec 2017)

  New Revision: 61457

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61457

  Log:
    Set first line numbers for empty iseqs.
    
    * compile.c (iseq_compile_each): for empty method, block and so on,
      `last_line` is not set so that line number of `putnil` instruction
      will be zero. This patch set `first_lineno` for such `putnil`.
    
      Problem is reported by deivid-rodriguez via Yuichiro Kaneko.
    
    * test/ruby/test_iseq.rb: add a test for this spec.

  Modified files:
    trunk/compile.c
    trunk/test/ruby/test_iseq.rb
Index: test/ruby/test_iseq.rb
===================================================================
--- test/ruby/test_iseq.rb	(revision 61456)
+++ test/ruby/test_iseq.rb	(revision 61457)
@@ -357,4 +357,37 @@ class TestISeq < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iseq.rb#L357
                    [["<class:D>@17", [[17, :class],
                                       [18, :end]]]]], collect_iseq.call(sample_iseq)
   end
+
+  def test_empty_iseq_lineno
+    iseq = ISeq.compile(<<-EOS)
+    # 1
+    # 2
+    def foo   # line 3 empty method
+    end       # line 4
+    1.time do # line 5 empty block
+    end       # line 6
+    class C   # line 7 empty class
+    end
+    EOS
+
+    iseq.each_child{|ci|
+      ary = ci.to_a
+      type = ary[9]
+      name = ary[5]
+      line = ary[13].first
+      case ary[9]
+      when :method
+        assert_equal "foo", name
+        assert_equal 3, line
+      when :class
+        assert_equal '<class:C>', name
+        assert_equal 7, line
+      when :block
+        assert_equal 'block in <compiled>', name
+        assert_equal 5, line
+      else
+        raise "unknown ary: " + ary.inspect
+      end
+    }
+  end
 end
Index: compile.c
===================================================================
--- compile.c	(revision 61456)
+++ compile.c	(revision 61457)
@@ -5413,8 +5413,10 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L5413
 {
     if (node == 0) {
 	if (!popped) {
+	    int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
+	    if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq));
 	    debugs("node: NODE_NIL(implicit)\n");
-	    ADD_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, putnil);
+	    ADD_INSN(ret, lineno, putnil);
 	}
 	return COMPILE_OK;
     }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]