From 7058d35d300fbed0e448acb629c67bb8617f64a7 Mon Sep 17 00:00:00 2001
From: Ary Borenszweig <aborenszweig@manas.com.ar>
Date: Tue, 25 Jun 2013 14:26:44 -0300
Subject: [PATCH] Fixed struct set inside struct

---
 lib/crystal/codegen.rb        |  4 +++-
 spec/codegen/c_struct_spec.rb | 22 ++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/lib/crystal/codegen.rb b/lib/crystal/codegen.rb
index 2e57d8c29e..7c86588893 100644
--- a/lib/crystal/codegen.rb
+++ b/lib/crystal/codegen.rb
@@ -753,7 +753,9 @@ module Crystal
     def visit_struct_set(node)
       ptr = gep llvm_self, 0, @type.index_of_var(node.name)
       @last = @vars['value'][:ptr]
-      @builder.store @last, ptr
+      value = @last
+      value = @builder.load @last if node.type.c_struct? || node.type.c_union?
+      @builder.store value, ptr
     end
 
     def visit_union_alloc(node)
diff --git a/spec/codegen/c_struct_spec.rb b/spec/codegen/c_struct_spec.rb
index 3a28c4f11b..bfcc51d50a 100644
--- a/spec/codegen/c_struct_spec.rb
+++ b/spec/codegen/c_struct_spec.rb
@@ -57,4 +57,26 @@ describe 'Code gen: struct' do
       foo.bar.y
       )).to_i.should eq(2)
   end
+
+  it "codegens struct set inside struct" do
+    run(%q(
+      lib C
+        struct Bar
+          y : Int32
+        end
+
+        struct Foo
+          x : Int32
+          bar : Bar
+        end
+      end
+
+      foo = C::Foo.new
+      bar = C::Bar.new
+      bar.y = 2
+      foo.bar = bar
+
+      foo.bar.y
+      )).to_i.should eq(2)
+  end
 end
-- 
GitLab