diff --git a/spec/compiler/semantic/if_spec.cr b/spec/compiler/semantic/if_spec.cr index 9ed5199a21ba..38b14e24499a 100644 --- a/spec/compiler/semantic/if_spec.cr +++ b/spec/compiler/semantic/if_spec.cr @@ -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 diff --git a/src/compiler/crystal/semantic/filters.cr b/src/compiler/crystal/semantic/filters.cr index 8612c956aae7..ebc77d01e675 100644 --- a/src/compiler/crystal/semantic/filters.cr +++ b/src/compiler/crystal/semantic/filters.cr @@ -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