Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Commit 5b7c2639 authored by Ary Borenszweig's avatar Ary Borenszweig
Browse files

Allow `as` to determine a type even if the casted value doesn't have a type (yet)

parent ac8b031e
Branches
Tags
No related merge requests found
......@@ -203,4 +203,20 @@ describe "Type inference: cast" do
),
"can't cast Int32 to Bool"
end
it "casts to target type even if can't infer casted value type" do
assert_type(%(
require "prelude"
class Foo
property! x
end
a = [1, 2, 3]
b = a.map { Foo.new.x as Int32 }
Foo.new.x = 1
b
)) { array_of(int32) }
end
end
......@@ -195,10 +195,15 @@ module Crystal
end
def update(from = nil)
to_type = to.type.instance_type
obj_type = obj.type?
return unless obj_type
to_type = to.type.instance_type
# If we don't know what type we are casting from, leave it as the to_type
unless obj_type
self.type = to_type.virtual_type
return
end
if obj_type.pointer? || to_type.pointer?
self.type = to_type
......@@ -214,6 +219,11 @@ module Crystal
filtered_type = to_type.virtual_type
@upcast = true
end
# If we don't have a matching type, leave it as the to_type:
# later (in after type inference) we will check again.
filtered_type ||= to_type.virtual_type
self.type = filtered_type
end
end
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment