Skip to content

Commit

Permalink
Showing 2 changed files with 38 additions and 13 deletions.
36 changes: 36 additions & 0 deletions spec/compiler/codegen/class_spec.cr
Original file line number Diff line number Diff line change
@@ -948,4 +948,40 @@ describe "Code gen: class" do
Foo(Int32).new.x
)).to_i.should eq(123)
end

it "doesn't skip false initializers (#3272)" do
run(%(
class Parent
@foo = true
def foo
@foo
end
end
class Child < Parent
@foo = false
end
Child.new.foo ? 10 : 20
)).to_i.should eq(20)
end

it "doesn't skip zero initializers (#3272)" do
run(%(
class Parent
@foo = 123
def foo
@foo
end
end
class Child < Parent
@foo = 0
end
Child.new.foo
)).to_i.should eq(0)
end
end
15 changes: 2 additions & 13 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
@@ -1724,17 +1724,6 @@ module Crystal

initializers.each do |init|
ivar = real_type.lookup_instance_var(init.name)
value = init.value

# Don't need to initialize false
if ivar.type == @program.bool && value.false?
next
end

# Don't need to initialize zero
if ivar.type == @program.int32 && value.zero?
next
end

with_cloned_context do
# Instance var initializers must run with "self"
@@ -1744,10 +1733,10 @@ module Crystal
context.vars["self"] = LLVMVar.new(type_ptr, real_type)
alloca_vars init.meta_vars

value.accept self
init.value.accept self

ivar_ptr = instance_var_ptr real_type, init.name, type_ptr
assign ivar_ptr, ivar.type, value.type, @last
assign ivar_ptr, ivar.type, init.value.type, @last
end
end
end

0 comments on commit 8e723f5

Please sign in to comment.