Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #partition_union #840

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/steep/ast/types/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,21 @@ def partition_union(type)
partition_union(unfold)
end
when AST::Types::Union
falsy_types, truthy_types = type.types.partition do |type|
(type.is_a?(AST::Types::Literal) && type.value == false) ||
type.is_a?(AST::Types::Nil)
truthy_types = [] #: Array[AST::Types::t]
falsy_types = [] #: Array[AST::Types::t]

type.types.each do |type|
truthy, falsy = partition_union(type)

truthy_types << truthy if truthy
falsy_types << falsy if falsy
end

[
truthy_types.empty? ? nil : AST::Types::Union.build(types: truthy_types),
falsy_types.empty? ? nil : AST::Types::Union.build(types: falsy_types)
]
when AST::Types::Any, AST::Types::Boolean
when AST::Types::Any, AST::Types::Boolean, AST::Types::Top, AST::Types::Logic::Base
[type, type]
when AST::Types::Nil
[nil, type]
Expand Down
24 changes: 24 additions & 0 deletions test/type_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -841,4 +841,28 @@ def test_type_case__local_variable_narrowing
assert_equal "::foo", typing.type_of(node: node).to_s
end
end

def test_branch_unreachable__logic_type
run_type_check_test(
signatures: {
},
code: {
"a.rb" => <<~RUBY
x = 1
y = x.is_a?(String)

if y
z = 1
else
z = 2
end
RUBY
},
expectations: <<~YAML
---
- file: a.rb
diagnostics: []
YAML
)
end
end
27 changes: 27 additions & 0 deletions test/type_factory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,31 @@ class Bar
end
end
end

def test_partition_union__top
with_factory() do |factory|
factory.partition_union(factory.type(parse_type("top"))).tap do |truthy, falsy|
assert_equal factory.type(parse_type("top")), truthy
assert_equal factory.type(parse_type("top")), falsy
end
end
end

def test_partition_union__boolish
with_factory() do |factory|
factory.partition_union(factory.type(parse_type("::boolish"))).tap do |truthy, falsy|
assert_equal factory.type(parse_type("top")), truthy
assert_equal factory.type(parse_type("top")), falsy
end
end
end

def test_partition_union__bool_union
with_factory() do |factory|
factory.partition_union(factory.type(parse_type("bool | ::Symbol"))).tap do |truthy, falsy|
assert_equal factory.type(parse_type("bool | ::Symbol")), truthy
assert_equal factory.type(parse_type("bool")), falsy
end
end
end
end