Skip to content

Commit

Permalink
Semantic: prevent infinite recursion by alias and generics combination (
Browse files Browse the repository at this point in the history
#5330)

* Semantic: prevent infinite recursion by alias and generics combination

Fixed #5329

* Fix typos

Thank you @jhass and @lachlan.
  • Loading branch information
makenowjust authored and RX14 committed Nov 28, 2017
1 parent 6ee1bf9 commit 7033082
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions spec/compiler/semantic/alias_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,17 @@ describe "Semantic: alias" do
),
"undefined constant T"
end

it "doesn't crash by infinite recursion against type alias and generics (#5329)" do
assert_error %(
class Foo(T)
def initialize(@foo : T)
end
end
alias Bar = Foo(Bar | Int32)
Foo(Bar).new(Foo.new(1).as(Bar))
), "can't cast Foo(Int32) to Bar"
end
end
5 changes: 4 additions & 1 deletion src/compiler/crystal/semantic/restrictions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,10 @@ module Crystal
elsif context.strict?
type_var == other_type_var
else
type_var.restrict(other_type_var, context) == type_var
# To prevent infinite recursion, it checks equality between
# `type_var` and `other_type_var` directly before try to restrict
# `type_var` by `other_type_var`.
type_var == other_type_var || type_var.restrict(other_type_var, context) == type_var
end
end
end
Expand Down

0 comments on commit 7033082

Please sign in to comment.