Skip to content

Commit 6882012

Browse files
authored
[3.4] compile.c: Handle anonymous variables in outer_variable_cmp (#13493)
1 parent b5346f2 commit 6882012

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
@@ -13236,6 +13236,13 @@ outer_variable_cmp(const void *a, const void *b, void *arg)
1323613236
{
1323713237
const struct outer_variable_pair *ap = (const struct outer_variable_pair *)a;
1323813238
const struct outer_variable_pair *bp = (const struct outer_variable_pair *)b;
13239+
13240+
if (!ap->name) {
13241+
return -1;
13242+
} else if (!bp->name) {
13243+
return 1;
13244+
}
13245+
1323913246
return rb_str_cmp(ap->name, bp->name);
1324013247
}
1324113248

test/ruby/test_iseq.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,25 @@ def test_unreachable_next_in_block
905905
end
906906
end
907907

908+
def test_serialize_anonymous_outer_variables
909+
iseq = RubyVM::InstructionSequence.compile(<<~'RUBY')
910+
obj = Object.new
911+
def obj.test
912+
[1].each do
913+
raise "Oops"
914+
rescue
915+
return it
916+
end
917+
end
918+
obj
919+
RUBY
920+
921+
binary = iseq.to_binary # [Bug # 21370]
922+
roundtripped_iseq = RubyVM::InstructionSequence.load_from_binary(binary)
923+
object = roundtripped_iseq.eval
924+
assert_equal 1, object.test
925+
end
926+
908927
def test_loading_kwargs_memory_leak
909928
assert_no_memory_leak([], "#{<<~"begin;"}", "#{<<~'end;'}", rss: true)
910929
a = RubyVM::InstructionSequence.compile("foo(bar: :baz)").to_binary

0 commit comments

Comments
 (0)