Skip to content

Commit

Permalink
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions spec/compiler/semantic/instance_var_spec.cr
Original file line number Diff line number Diff line change
@@ -4578,4 +4578,27 @@ describe "Semantic: instance var" do
Foo.new.x
)) { int32 }
end

it "types generic instance as virtual type if generic type has subclasses (#3805)" do
assert_type(%(
class Foo(T)
end
class Bar(T) < Foo(T)
end
class Qux
def initialize
@ptr = Pointer(Foo(Int32)).malloc(1_u64)
end
def ptr=(ptr)
@ptr = ptr
end
end
Bar(Int32).new
Qux.new
)) { types["Qux"] }
end
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
@@ -1683,9 +1683,9 @@ module Crystal
end

def virtual_type
if leaf? && !abstract?
if generic_type.leaf? && !abstract?
self
elsif struct? && abstract? && !leaf?
elsif struct? && abstract? && !generic_type.leaf?
virtual_type!
elsif struct?
self

0 comments on commit a62dff3

Please sign in to comment.