Forum | Documentation | Website | Blog

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

Fixed: moved the "can't cast to T because it was never instantiated" error...

Fixed: moved the "can't cast to T because it was never instantiated" error from compile-time to runtime
parent f9023602
Branches
Tags
No related merge requests found
......@@ -271,4 +271,27 @@ describe "Code gen: cast" do
(a as Void*).address
)).to_i.should_not eq(0)
end
it "errors if casting to a non-allocated type" do
run(%(
require "prelude"
class Foo
end
class Bar < Foo
end
class Baz < Foo
end
foo = Foo.new || Bar.new
begin
foo as Baz
rescue ex
ex.message.includes?("can't cast to Baz because it was never instantiated")
end
)).to_b.should be_true
end
end
......@@ -129,23 +129,6 @@ describe "Type inference: cast" do
"can't cast Pointer(Void) to ( -> Int32)"
end
it "errors if casting to a non-allocated type" do
assert_error %(
class Foo
end
class Bar < Foo
end
class Baz < Foo
end
foo = Foo.new || Bar.new
foo as Baz
),
"can't cast to Baz because it was never instantiated"
end
it "doesn't error if casting to a generic type" do
assert_type(%(
class Foo(T)
......
......@@ -334,7 +334,11 @@ module Crystal
end
end
call = Call.global("raise", StringLiteral.new(ex_msg))
build_raise ex_msg
end
def build_raise(msg)
call = Call.global("raise", StringLiteral.new(msg))
call.accept TypeVisitor.new(@program)
call
end
......@@ -481,7 +485,7 @@ module Crystal
end
else
unless to_type.allocated
node.to.raise "can't cast to #{to_type} because it was never instantiated"
return build_raise "can't cast to #{to_type} because it was never instantiated"
end
resulting_type = obj_type.filter_by(to_type)
......
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