Skip to content

Commit

Permalink
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/compiler/codegen/cast_spec.cr
Original file line number Diff line number Diff line change
@@ -389,4 +389,26 @@ describe "Code gen: cast" do
end
)).to_string.should eq("A")
end

it "casts from nilable type to virtual type (#3512)" do
run(%(
require "prelude"
class Foo
def foo
1
end
end
class Bar < Foo
def foo
2
end
end
foo = 1 == 2 ? nil : Foo.new
x = foo.as(Foo)
x.foo
)).to_i.should eq(1)
end
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/codegen/cast.cr
Original file line number Diff line number Diff line change
@@ -325,6 +325,10 @@ class Crystal::CodeGenVisitor
cast_to value, to_type
end

def downcast_distinct(value, to_type : VirtualType, from_type : NilableType)
cast_to value, to_type
end

def downcast_distinct(value, to_type : Type, from_type : NilableType)
value
end

0 comments on commit 2ce8c24

Please sign in to comment.