Skip to content

Commit

Permalink
Fix bug for c structs assigned to a type (crystal-lang#9743)
Browse files Browse the repository at this point in the history
* Fix bug for c structs assigned to a type

* Call Type#remove_typedef instead of creating a separate method to handle typedefs

* Reassign type argument
  • Loading branch information
matthewmcgarvey committed Sep 29, 2020
1 parent c095449 commit e64e872
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions spec/compiler/codegen/c_struct_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,19 @@ describe "Code gen: struct" do
foo.x.call(1)
)).to_i.should eq(2)
end

it "can access member of uninitialized struct behind type (#8774)" do
run(%(
lib LibFoo
struct Foo
x : Int32
end
type FooT = Foo
end
foo = uninitialized LibFoo::FooT
foo.x
))
end
end
1 change: 1 addition & 0 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@ module Crystal
end

def read_instance_var(node_type, type, name, value)
type = type.remove_typedef
ivar = type.lookup_instance_var(name)
ivar_ptr = instance_var_ptr type, name, value
@last = downcast ivar_ptr, node_type, ivar.type, false
Expand Down

0 comments on commit e64e872

Please sign in to comment.