Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Compiler: fix bug in type filtering regarding not (!). Fixes #4243
  • Loading branch information
asterite committed Apr 10, 2017
1 parent 3eef15a commit 9713e42
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
26 changes: 26 additions & 0 deletions spec/compiler/semantic/not_spec.cr
Expand Up @@ -50,4 +50,30 @@ describe "Semantic: not" do
z
)) { int32 }
end

it "doesn't restrict and" do
assert_type(%(
a = 1 || nil
z = nil
if !(a && (1 == 2))
z = a
end
z
)) { nilable int32 }
end

it "doesn't restrict and in while (#4243)" do
assert_type(%(
x = nil
y = nil
z = nil
while !(x && y)
z = x
x = 1
end
z
)) { nilable int32 }
end
end
11 changes: 8 additions & 3 deletions src/compiler/crystal/semantic/main_visitor.cr
Expand Up @@ -2850,11 +2850,16 @@ module Crystal
node.update

if needs_type_filters? && (type_filters = @type_filters)
@type_filters = type_filters.not
else
@type_filters = nil
# `Not` can only deduce type filters for these nodes
case exp = node.exp
when Var, IsA, RespondsTo, Not
@type_filters = type_filters.not
return false
end
end

@type_filters = nil

false
end

Expand Down

0 comments on commit 9713e42

Please sign in to comment.