diff --git a/spec/compiler/codegen/is_a_spec.cr b/spec/compiler/codegen/is_a_spec.cr index b7c662bb2628c41ab8cd870e09ff57c0ac97a0d1..cb26dc5d7a11c2f9a80860b8553ea76d1c0caec3 100755 --- a/spec/compiler/codegen/is_a_spec.cr +++ b/spec/compiler/codegen/is_a_spec.cr @@ -419,4 +419,27 @@ describe "Codegen: is_a?" do 1.is_a?(Class) ").to_b.should be_false end + + it "restricts type in else but lazily" do + run(" + class Foo + def initialize(@x) + end + + def x + @x + end + end + + foo = Foo.new(1) + x = foo.x + if x.is_a?(Int32) + z = x + 1 + else + z = x.foo_bar + end + + z + ").to_i.should eq(2) + end end diff --git a/spec/compiler/type_inference/is_a_spec.cr b/spec/compiler/type_inference/is_a_spec.cr index 105be8cc6cd1770c744ab89e2e4ee7a497f5b88e..91e7c3f64747823ed6d755d7b0943f78a8d50853 100755 --- a/spec/compiler/type_inference/is_a_spec.cr +++ b/spec/compiler/type_inference/is_a_spec.cr @@ -166,4 +166,27 @@ describe "Type inference: is_a?" do end ") { int32 } end + + it "restricts type in else but lazily" do + assert_type(" + class Foo + def initialize(@x) + end + + def x + @x + end + end + + foo = Foo.new(1) + x = foo.x + if x.is_a?(Int32) + z = x + 1 + else + z = x.foo_bar + end + + z + ") { int32 } + end end diff --git a/src/compiler/crystal/type_inference/filters.cr b/src/compiler/crystal/type_inference/filters.cr index 1ed883121dd9a7766dbfcd52ba4fe6343c661a61..14f8f713dfc930e11ffd833072b9507ed7cb9c5c 100644 --- a/src/compiler/crystal/type_inference/filters.cr +++ b/src/compiler/crystal/type_inference/filters.cr @@ -160,8 +160,11 @@ module Crystal resulting_types = other_types - types case resulting_types.length when 0 - # TODO: should be nil? - other + if @filter.is_a?(NotNilFilter) + other + else + nil + end when 1 resulting_types.first else