Skip to content

Commit

Permalink
Fix: Do not merge union types in truthy filter (#12752)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Nov 23, 2022
1 parent 524fb88 commit 33c1c2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions spec/compiler/semantic/if_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ describe "Semantic: if" do
)) { int32 }
end

it "restricts and doesn't unify union types" do
assert_type(%(
class Foo
end
module M
def m
1
end
end
class Bar < Foo
include M
end
class Baz < Foo
include M
end
a = Bar.new.as(Foo)
if b = a.as?(M)
b.m
else
nil
end
)) { union_of(nil_type, int32) }
end

it "types variable after unreachable else of && (#3360)" do
assert_type(%(
def test
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/filters.cr
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ module Crystal
when NilType
nil
when UnionType
Type.merge(other.union_types.reject &.nil_type?)
other.program.union_of(other.union_types.reject &.nil_type?)
else
other
end
Expand Down

0 comments on commit 33c1c2e

Please sign in to comment.