Skip to content

Commit a62dff3

Browse files
author
Ary Borenszweig
committedDec 30, 2016
Fixed #3805: incorrect type for generic instance whose generic type has subclasses
1 parent 49aa490 commit a62dff3

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed
 

‎spec/compiler/semantic/instance_var_spec.cr

+23
Original file line numberDiff line numberDiff line change
@@ -4578,4 +4578,27 @@ describe "Semantic: instance var" do
45784578
Foo.new.x
45794579
)) { int32 }
45804580
end
4581+
4582+
it "types generic instance as virtual type if generic type has subclasses (#3805)" do
4583+
assert_type(%(
4584+
class Foo(T)
4585+
end
4586+
4587+
class Bar(T) < Foo(T)
4588+
end
4589+
4590+
class Qux
4591+
def initialize
4592+
@ptr = Pointer(Foo(Int32)).malloc(1_u64)
4593+
end
4594+
4595+
def ptr=(ptr)
4596+
@ptr = ptr
4597+
end
4598+
end
4599+
4600+
Bar(Int32).new
4601+
Qux.new
4602+
)) { types["Qux"] }
4603+
end
45814604
end

‎src/compiler/crystal/types.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1683,9 +1683,9 @@ module Crystal
16831683
end
16841684

16851685
def virtual_type
1686-
if leaf? && !abstract?
1686+
if generic_type.leaf? && !abstract?
16871687
self
1688-
elsif struct? && abstract? && !leaf?
1688+
elsif struct? && abstract? && !generic_type.leaf?
16891689
virtual_type!
16901690
elsif struct?
16911691
self

0 commit comments

Comments
 (0)
Please sign in to comment.