Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variance checks between generic instances for Proc#call and abstract defs #10899

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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