Skip to content

Commit

Permalink
Fixed #3805: incorrect type for generic instance whose generic type h…
Browse files Browse the repository at this point in the history
…as subclasses
  • Loading branch information
asterite committed Dec 30, 2016
1 parent 49aa490 commit a62dff3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions spec/compiler/semantic/instance_var_spec.cr
Expand Up @@ -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
Expand Up @@ -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
Expand Down

0 comments on commit a62dff3

Please sign in to comment.