Skip to content

Commit

Permalink
Fixed #2050: Regression with closures
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Jan 24, 2016
1 parent 5c93802 commit cf019e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
16 changes: 16 additions & 0 deletions spec/compiler/codegen/closure_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,20 @@ describe "Code gen: closure" do
Foo.foo
)).to_b.should be_false
end

it "captures block and accesses local variable (#2050)" do
run(%(
require "prelude"
def capture(&block)
block
end
coco = 1
capture do
coco
end
coco
)).to_i.should eq(1)
end
end
19 changes: 10 additions & 9 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ module Crystal
def initialize(@codegen)
end

def visit(node : FileNode)
true
end

def visit(node : Expressions)
true
end
Expand Down Expand Up @@ -264,16 +268,13 @@ module Crystal
end

def visit(node : FileNode)
old_vars = context.vars
context.vars = LLVMVars.new

file_module = @mod.file_module(node.filename)
if vars = file_module.vars?
alloca_vars vars, file_module
with_context(Context.new(context.fun, context.type)) do
file_module = @mod.file_module(node.filename)
if vars = file_module.vars?
alloca_vars vars, file_module
end
node.node.accept self
end
node.node.accept self

context.vars = old_vars

false
end
Expand Down

0 comments on commit cf019e2

Please sign in to comment.