Skip to content

Commit 2d6f8e9

Browse files
committed
compile.c: Handle anonymous variables in outer_variable_cmp
[Bug #21370]
1 parent 9a29252 commit 2d6f8e9

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

compile.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13378,6 +13378,13 @@ outer_variable_cmp(const void *a, const void *b, void *arg)
1337813378
{
1337913379
const struct outer_variable_pair *ap = (const struct outer_variable_pair *)a;
1338013380
const struct outer_variable_pair *bp = (const struct outer_variable_pair *)b;
13381+
13382+
if (!ap->name) {
13383+
return -1;
13384+
} else if (!bp->name) {
13385+
return 1;
13386+
}
13387+
1338113388
return rb_str_cmp(ap->name, bp->name);
1338213389
}
1338313390

test/ruby/test_iseq.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,25 @@ def test_unreachable_next_in_block
859859
end
860860
end
861861

862+
def test_serialize_anonymous_outer_variables
863+
iseq = RubyVM::InstructionSequence.compile(<<~'RUBY')
864+
obj = Object.new
865+
def obj.test
866+
[1].each do
867+
raise "Oops"
868+
rescue
869+
return it
870+
end
871+
end
872+
obj
873+
RUBY
874+
875+
binary = iseq.to_binary # [Bug # 21370]
876+
roundtripped_iseq = RubyVM::InstructionSequence.load_from_binary(binary)
877+
object = roundtripped_iseq.eval
878+
assert_equal 1, object.test
879+
end
880+
862881
def test_loading_kwargs_memory_leak
863882
assert_no_memory_leak([], "#{<<~"begin;"}", "#{<<~'end;'}", rss: true)
864883
a = iseq_to_binary(RubyVM::InstructionSequence.compile("foo(bar: :baz)"))

0 commit comments

Comments
 (0)