Skip to content

Commit 2ce8c24

Browse files
author
Ary Borenszweig
committedNov 8, 2016
Compiler: incorrect codegen code for casting a nilable type to a virtual type. Fixes #3512
1 parent b8a18c1 commit 2ce8c24

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
 

Diff for: ‎spec/compiler/codegen/cast_spec.cr

+22
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,26 @@ describe "Code gen: cast" do
389389
end
390390
)).to_string.should eq("A")
391391
end
392+
393+
it "casts from nilable type to virtual type (#3512)" do
394+
run(%(
395+
require "prelude"
396+
397+
class Foo
398+
def foo
399+
1
400+
end
401+
end
402+
403+
class Bar < Foo
404+
def foo
405+
2
406+
end
407+
end
408+
409+
foo = 1 == 2 ? nil : Foo.new
410+
x = foo.as(Foo)
411+
x.foo
412+
)).to_i.should eq(1)
413+
end
392414
end

Diff for: ‎src/compiler/crystal/codegen/cast.cr

+4
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ class Crystal::CodeGenVisitor
325325
cast_to value, to_type
326326
end
327327

328+
def downcast_distinct(value, to_type : VirtualType, from_type : NilableType)
329+
cast_to value, to_type
330+
end
331+
328332
def downcast_distinct(value, to_type : Type, from_type : NilableType)
329333
value
330334
end

0 commit comments

Comments
 (0)
Please sign in to comment.