[PRISM] Correct depth offset for block local vars

Blocks should always look at their own local table first, even when
defined inside an ensure/rescue or something else that uses depth
offset. We can ignore the depth offset if we're doing local lookups
inside a block
This commit is contained in:
Matt Valentine-House 2023-12-06 20:22:18 +00:00
parent fe6ee5e921
commit c8b60c8ac2
2 changed files with 27 additions and 0 deletions

View File

@ -1377,6 +1377,7 @@ pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_
}
scope->body = cast->body;
scope->locals = cast->locals;
scope->local_depth_offset = 0;
break;
}
case PM_CLASS_NODE: {

View File

@ -789,6 +789,23 @@ module Prism
end
end
CODE
assert_prism_eval(<<~CODE)
def test
ensure
{}.each do |key, value|
{}[key] = value
end
end
CODE
assert_prism_eval(<<~CODE)
def test
a = 1
ensure
{}.each do |key, value|
{}[key] = a
end
end
CODE
end
def test_NextNode
@ -924,6 +941,15 @@ module Prism
a + b + c
CODE
assert_prism_eval("begin; rescue; end")
assert_prism_eval(<<~CODE)
begin
rescue
args.each do |key, value|
tmp[key] = 1
end
end
CODE
end
def test_RescueModiferNode