Skip to content

Commit

Permalink
Fix variance checks between generic instances for Proc#call and abs…
Browse files Browse the repository at this point in the history
…tract defs (#10899)
  • Loading branch information
HertzDevil authored Oct 4, 2021
1 parent f90d914 commit cf0a351
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
60 changes: 58 additions & 2 deletions spec/compiler/semantic/abstract_def_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ describe "Semantic: abstract def" do
end

it "doesn't error if implements with parent class" do
semantic %(
assert_no_errors %(
class Parent; end
class Child < Parent; end
Expand All @@ -359,8 +359,64 @@ describe "Semantic: abstract def" do
)
end

it "doesn't error if implements with generic parent class instance" do
assert_no_errors %(
class Parent(T); end
class Child(T) < Parent(T); end
abstract class Foo
abstract def foo(x : Child(Int32))
end
class Bar < Foo
def foo(x : Parent(Int32))
end
end
)
end

it "doesn't error if implements with included module" do
assert_no_errors %(
module Moo
end
module Moo2
include Moo
end
abstract class Foo
abstract def foo(x : Moo2)
end
class Bar < Foo
def foo(x : Moo)
end
end
)
end

it "doesn't error if implements with generic included module instance" do
assert_no_errors %(
module Moo(T)
end
module Moo2(T)
include Moo(T)
end
abstract class Foo
abstract def foo(x : Moo2(Int32))
end
class Bar < Foo
def foo(x : Moo(Int32))
end
end
)
end

it "doesn't error if implements with parent module" do
semantic %(
assert_no_errors %(
module Moo
end
Expand Down
24 changes: 23 additions & 1 deletion spec/compiler/semantic/proc_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ describe "Semantic: proc" do
"no overload matches"
end

it "allows invoking a function with a generic subtype" do
it "allows invoking a function with a generic subtype (1)" do
assert_type(%(
module Moo
def foo
Expand All @@ -678,6 +678,28 @@ describe "Semantic: proc" do
)) { int32 }
end

it "allows invoking a function with a generic subtype (2)" do
assert_type(%(
module Moo(T)
def foo
1
end
end
class Foo(T)
include Moo(T)
end
def func(&block : Moo(Int32) -> _)
block
end
foo = Foo(Int32).new
f = func { |moo| moo.foo }
f.call foo
)) { int32 }
end

it "gets pointer to lib fun without specifying types" do
assert_type(%(
lib LibFoo
Expand Down

0 comments on commit cf0a351

Please sign in to comment.