Skip to content

Commit

Permalink
Compiler: check if C function needs to be converted to a Crystal proc…
Browse files Browse the repository at this point in the history
… when accesing it through an instance var. Fixes #2515
Ary Borenszweig committed Apr 25, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c1affec commit da078e2
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions spec/compiler/codegen/lib_spec.cr
Original file line number Diff line number Diff line change
@@ -192,4 +192,18 @@ describe "Code gen: lib" do
LibFoo.foo(nil)
))
end

it "casts C fun to Crystal proc when accessing instance var (#2515)" do
codegen(%(
require "prelude"
lib LibFoo
struct Some
x : ->
end
end
LibFoo::Some.new.to_s
))
end
end
7 changes: 7 additions & 0 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
@@ -1000,6 +1000,13 @@ module Crystal
ivar = type.lookup_instance_var_with_owner(name).instance_var
ivar_ptr = instance_var_ptr type, name, value
@last = downcast ivar_ptr, node_type, ivar.type, false
if type.is_a?(CStructOrUnionType)
# When reading the instance variable of a C struct or union
# we need to convert C functions to Crystal procs. This
# can happen for example in Struct#to_s, where all fields
# are inspected.
@last = check_c_fun node_type, @last
end
false
end

0 comments on commit da078e2

Please sign in to comment.