diff --git a/compile.c b/compile.c index 0c966a7b4b..ef6a7cf89c 100644 --- a/compile.c +++ b/compile.c @@ -5413,8 +5413,10 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, const NODE *node, int poppe { 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; } diff --git a/test/ruby/test_iseq.rb b/test/ruby/test_iseq.rb index 74691854d9..92340bd8fd 100644 --- a/test/ruby/test_iseq.rb +++ b/test/ruby/test_iseq.rb @@ -357,4 +357,37 @@ class TestISeq < Test::Unit::TestCase [["@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 '', name + assert_equal 7, line + when :block + assert_equal 'block in ', name + assert_equal 5, line + else + raise "unknown ary: " + ary.inspect + end + } + end end