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 typing of union collections #151

Merged
merged 2 commits into from
Jun 13, 2020
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
6 changes: 3 additions & 3 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1037,10 +1037,10 @@ def synthesize(node, hint: nil)
case child.type
when :pair
key, value = child.children
key_types << synthesize(key).type.yield_self do |type|
key_types << synthesize(key, hint: key_hint).type.yield_self do |type|
select_super_type(type, key_hint)
end
value_types << synthesize(value).type.yield_self do |type|
value_types << synthesize(value, hint: value_hint).type.yield_self do |type|
select_super_type(type, value_hint)
end
when :kwsplat
Expand Down Expand Up @@ -1256,7 +1256,7 @@ def synthesize(node, hint: nil)
end
end
else
[select_super_type(synthesize(e).type, element_hint)]
[select_super_type(synthesize(e, hint: element_hint).type, element_hint)]
end
end
array_type = AST::Builtin::Array.instance_type(AST::Types::Union.build(types: element_types))
Expand Down
42 changes: 42 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3682,6 +3682,27 @@ def test_array_optional
end
end

def test_array_union
with_checker <<EOF do |checker|
class Animal
end

type animal = :dog | :cat
Animal::ALL: Array[animal]
EOF

source = parse_ruby(<<EOF)
Animal::ALL = [:dog, :cat]
EOF

with_standard_construction(checker, source) do |construction, typing|
construction.synthesize(source.node)

assert_no_error typing
end
end
end

def test_passing_empty_block
with_checker do |checker|
source = parse_ruby(<<EOF)
Expand Down Expand Up @@ -4047,6 +4068,27 @@ def self.f: [A] ({ foo: A }) -> A
end
end

def test_hash_union
with_checker <<EOF do |checker|
class Animal
end

type animal = :dog | :cat
Animal::ALL: Hash[Symbol, animal]
EOF

source = parse_ruby(<<EOF)
Animal::ALL = { :dog => :dog, :cat => :cat }
EOF

with_standard_construction(checker, source) do |construction, typing|
construction.synthesize(source.node)

assert_no_error typing
end
end
end

def test_polymorphic_method
with_checker <<-EOF do |checker|
interface _Ref[X]
Expand Down