Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Commit 03267c26 authored by Ary Borenszweig's avatar Ary Borenszweig
Browse files

Make closured work in blocks that don't closure vars but an outer context has closured vars

parent cd6b059b
No related merge requests found
......@@ -135,6 +135,20 @@ describe "Code gen: closure" do
").to_i.should eq(9)
end
it "codegens closure with nested context without new closured vars" do
run("
def foo
yield
end
a = 1
f = foo do
-> { a }
end
f.call
").to_i.should eq(1)
end
# pending "transforms block to fun literal" do
# run("
# def foo(&block : Int32 ->)
......
......@@ -2635,15 +2635,19 @@ module Crystal
end
def malloc_closure(closure_vars, current_context, parent_context = nil)
if closure_vars
closure_type = @llvm_typer.closure_context_type(closure_vars, parent_context.try &.closure_type)
parent_closure_type = parent_context.try &.closure_type
if closure_vars || parent_closure_type
closure_vars ||= [] of MetaVar
closure_type = @llvm_typer.closure_context_type(closure_vars, parent_closure_type)
closure_ptr = malloc closure_type
closure_vars.each_with_index do |var, i|
current_context.vars[var.name] = LLVMVar.new(gep(closure_ptr, 0, i, var.name), var.type)
end
if parent_context && parent_context.closure_type
store parent_context.closure_ptr.not_nil!, gep(closure_ptr, 0, closure_vars.length, "parent")
if parent_closure_type
store parent_context.not_nil!.closure_ptr.not_nil!, gep(closure_ptr, 0, closure_vars.length, "parent")
end
end
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment