Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Commit c8640a29 authored by Ary Borenszweig's avatar Ary Borenszweig
Browse files

Discard matching types when restricting a union with another one

parent 04b2af6a
Branches
Tags
No related merge requests found
......@@ -543,6 +543,16 @@ describe "Type inference: def overload" do
") { int32.metaclass }
end
it "gets free variable from union restriction (2)" do
assert_type("
def foo(x : Nil | U)
U
end
foo(nil || 1)
") { int32.metaclass }
end
it "gets free variable from union restriction without a union" do
assert_type("
def foo(x : Nil | U)
......
......@@ -203,11 +203,15 @@ module Crystal
def restrict(other : Union, owner, type_lookup, free_vars)
types = [] of Type
discarded = [] of Type
other.types.each do |other_type|
self.union_types.each do |type|
next if discarded.includes?(type)
restricted = type.restrict(other_type, owner, type_lookup, free_vars)
if restricted
types << restricted
discarded << type
break
end
end
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment