From d76cdeea2cd8207c359a9a71913295554cece70a Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 31 Oct 2022 10:56:03 -0300 Subject: [PATCH] Compiler: ignore type filters when accepting cast obj and to (#12668) --- spec/compiler/semantic/nilable_cast_spec.cr | 12 ++++++++++++ src/compiler/crystal/semantic/main_visitor.cr | 8 ++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/spec/compiler/semantic/nilable_cast_spec.cr b/spec/compiler/semantic/nilable_cast_spec.cr index 526ff6e7c581..f2b46110f415 100644 --- a/spec/compiler/semantic/nilable_cast_spec.cr +++ b/spec/compiler/semantic/nilable_cast_spec.cr @@ -74,4 +74,16 @@ describe "Semantic: nilable cast" do base.as?(Moo) )) { union_of([types["Foo"], types["Bar"], nil_type] of Type) } end + + it "doesn't introduce type filter for nilable cast object (#12661)" do + assert_type(%( + val = 1 || false + + if val.as?(Char) + true + else + val + end + )) { union_of(int32, bool) } + end end diff --git a/src/compiler/crystal/semantic/main_visitor.cr b/src/compiler/crystal/semantic/main_visitor.cr index 3dfa321b7407..a6c021e84653 100644 --- a/src/compiler/crystal/semantic/main_visitor.cr +++ b/src/compiler/crystal/semantic/main_visitor.cr @@ -1766,10 +1766,14 @@ module Crystal typed_def.raises = true end - node.obj.accept self + ignoring_type_filters do + node.obj.accept self + end @in_type_args += 1 - node.to.accept self + ignoring_type_filters do + node.to.accept self + end @in_type_args -= 1 node.obj.add_observer node