Skip to content

Commit

Permalink
Showing 3 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions spec/compiler/codegen/named_tuple_spec.cr
Original file line number Diff line number Diff line change
@@ -297,4 +297,17 @@ describe "Code gen: named tuple" do
t[:x]
)).to_i.should eq(2)
end

it "downcasts union inside tuple to value (#3907)" do
codegen(%(
struct Foo
end
foo = Foo.new
x = {a: 0, b: foo}
z = x[:a]
x = {a: 0, b: z}
))
end
end
13 changes: 13 additions & 0 deletions spec/compiler/codegen/tuple_spec.cr
Original file line number Diff line number Diff line change
@@ -351,4 +351,17 @@ describe "Code gen: tuple" do
t.as(Tuple)[0]
)).to_i.should eq(1)
end

it "downcasts union inside tuple to value (#3907)" do
codegen(%(
struct Foo
end
foo = Foo.new
x = {0, foo}
z = x[0]
x = {0, z}
))
end
end
2 changes: 2 additions & 0 deletions src/compiler/crystal/codegen/cast.cr
Original file line number Diff line number Diff line change
@@ -467,6 +467,7 @@ class Crystal::CodeGenVisitor
value_ptr = gep value, 0, index
loaded_value = to_lhs(value_ptr, value_tuple_type)
downcasted_value = downcast(loaded_value, target_tuple_type, value_tuple_type, true)
downcasted_value = to_rhs(downcasted_value, target_tuple_type)
store downcasted_value, target_ptr
index += 1
end
@@ -481,6 +482,7 @@ class Crystal::CodeGenVisitor
target_index = to_type.name_index(entry.name).not_nil!
target_index_type = to_type.name_type(entry.name)
downcasted_value = downcast(value_at_index, target_index_type, entry.type, true)
downcasted_value = to_rhs(downcasted_value, target_index_type)
store downcasted_value, aggregate_index(target_pointer, target_index)
end
target_pointer

0 comments on commit 0cda903

Please sign in to comment.