Skip to content

Commit

Permalink
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 10 additions & 0 deletions spec/compiler/codegen/return_spec.cr
Original file line number Diff line number Diff line change
@@ -61,4 +61,14 @@ describe "Code gen: return" do
test
)).to_i.should eq(2)
end

it "doesn't crash when method returns nil and can be inlined" do
codegen(%(
def foo : Nil
1
end
foo
))
end
end
14 changes: 11 additions & 3 deletions src/compiler/crystal/codegen/call.cr
Original file line number Diff line number Diff line change
@@ -393,27 +393,35 @@ class Crystal::CodeGenVisitor
return true unless @needs_value

accept body
@last = upcast(@last, target_def.type, body.type)
inline_call_return_value target_def, body
return true
when Var
if body.name == "self"
return true unless @needs_value

@last = self_type.passed_as_self? ? call_args.first : type_id(self_type)
@last = upcast(@last, target_def.type, body.type)
inline_call_return_value target_def, body
return true
end
when InstanceVar
return true unless @needs_value

read_instance_var(body.type, self_type, body.name, call_args.first)
@last = upcast(@last, target_def.type, body.type)
inline_call_return_value target_def, body
return true
end

false
end

def inline_call_return_value(target_def, body)
if target_def.type.nil_type?
@last = llvm_nil
else
@last = upcast(@last, target_def.type, body.type)
end
end

def codegen_call_or_invoke(node, target_def, self_type, func, call_args, raises, type, is_closure = false, fun_type = nil)
set_current_debug_location node if @debug

0 comments on commit c7f06c7

Please sign in to comment.