Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions spec/compiler/semantic/restrictions_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ describe "Restrictions" do
end

describe "restriction_of?" do
describe "Metaclass vs Metaclass-with-free-vars" do
it "inserts Metaclass before Metaclass-with-free-vars" do
assert_type(%(
def foo(a : T.class) forall T
1
end

def foo(a : Int32.class)
true
end

foo(Int32)
)) { bool }
end

it "keeps Metaclass before Metaclass-with-free-vars" do
assert_type(%(
def foo(a : Int32.class)
true
end

def foo(a : T.class) forall T
1
end

foo(Int32)
)) { bool }
end
end

describe "GenericClassType vs GenericClassInstanceType" do
it "inserts GenericClassInstanceType before GenericClassType" do
assert_type(%(
Expand Down
13 changes: 12 additions & 1 deletion src/compiler/crystal/semantic/restrictions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,19 @@ module Crystal
other_type = owner.lookup_type?(other)
if self_type && other_type
self_type.restriction_of?(other_type, owner)
elsif self_type
# `other` cannot resolve to a type, it's probably a free variable like:
#
# ```
# def foo(param : T.class) forall T
# end
#
# def foo(param : Int32.class)
# end
# ```
true
else
self == other
false
end
end
end
Expand Down