Skip to content

Commit

Permalink
Showing 2 changed files with 62 additions and 0 deletions.
38 changes: 38 additions & 0 deletions spec/compiler/codegen/alias_spec.cr
Original file line number Diff line number Diff line change
@@ -136,4 +136,42 @@ describe "Code gen: alias" do
end
)).to_i.should eq(2)
end

it "overloads alias against generic (1) (#3261)" do
run(%(
class Foo(T)
end
alias FooString = Foo(String)
def take(foo : Foo(String))
1
end
def take(foo : FooString)
2
end
take(Foo(String).new)
), inject_primitives: false).to_i.should eq(2)
end

it "overloads alias against generic (2) (#3261)" do
run(%(
class Foo(T)
end
alias FooString = Foo(String)
def take(foo : FooString)
2
end
def take(foo : Foo(String))
1
end
take(Foo(String).new)
), inject_primitives: false).to_i.should eq(1)
end
end
24 changes: 24 additions & 0 deletions src/compiler/crystal/semantic/restrictions.cr
Original file line number Diff line number Diff line change
@@ -224,6 +224,18 @@ module Crystal
other.types.any? { |o| self.restriction_of?(o, owner) }
end

def restriction_of?(other : Generic, owner)
self_type = owner.lookup_path(self)
if self_type
other_type = owner.lookup_type?(other)
if other_type
return self_type.restriction_of?(other_type, owner)
end
end

false
end

def restriction_of?(other, owner)
false
end
@@ -240,6 +252,18 @@ module Crystal
end

class Generic
def restriction_of?(other : Path, owner)
other_type = owner.lookup_type?(self)
if other_type
self_type = owner.lookup_path(other)
if self_type
return self_type.restriction_of?(other_type, owner)
end
end

false
end

def restriction_of?(other : Generic, owner)
return true if self == other
return false unless name == other.name && type_vars.size == other.type_vars.size

0 comments on commit e9ab595

Please sign in to comment.