Skip to content

Commit

Permalink
Merge pull request #2355 from herwinw/for_locals
Browse files Browse the repository at this point in the history
Fix scoping of variables in for loop
  • Loading branch information
seven1m authored Nov 23, 2024
2 parents 36b37b5 + 36d1ec7 commit ba14971
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/natalie/compiler/pass1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,8 @@ def transform_for_declare_args(args)
end

def transform_for_node(node, used:)
instructions = transform_for_declare_args(node.index)
instructions = @locals_stack.fetch(-1, []).map { |name| VariableDeclareInstruction.new(name) }
instructions += transform_for_declare_args(node.index)
instructions << DefineBlockInstruction.new(arity: 1)
instructions += transform_block_args_for_for(node.index, used: true)
instructions += transform_expression(node.statements, used: true) if node.statements
Expand Down
10 changes: 4 additions & 6 deletions spec/language/for_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,20 @@ def each
j.should == 6
end

xit "executes code in containing variable scope" do
it "executes code in containing variable scope" do
for i in 1..2
a = 123
end

# NATFIXME: Compile time error: "variable not defined a"
#a.should == 123
a.should == 123
end

xit "executes code in containing variable scope with 'do'" do
it "executes code in containing variable scope with 'do'" do
for i in 1..2 do
a = 123
end

# NATFIXME: Compile time error: "variable not defined a"
#a.should == 123
a.should == 123
end

it "does not try to access variables outside the method" do
Expand Down

0 comments on commit ba14971

Please sign in to comment.