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

Better union-union subtyping #531

Merged
merged 1 commit into from
Apr 6, 2022
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
4 changes: 2 additions & 2 deletions lib/steep/subtyping/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def check_type0(relation)

when relation.super_type.is_a?(AST::Types::Union)
Any(relation) do |result|
relation.super_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : 1 }.each do |super_type|
relation.super_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : -Float::INFINITY }.each do |super_type|
rel = Relation.new(sub_type: relation.sub_type, super_type: super_type)
result.add(rel) do
check_type(rel)
Expand All @@ -357,7 +357,7 @@ def check_type0(relation)

when relation.sub_type.is_a?(AST::Types::Intersection)
Any(relation) do |result|
relation.sub_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : 1 }.each do |sub_type|
relation.sub_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : -Float::INFINITY }.each do |sub_type|
rel = Relation.new(sub_type: sub_type, super_type: relation.super_type)
result.add(rel) do
check_type(rel)
Expand Down
47 changes: 47 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8577,6 +8577,53 @@ def flat_map: [A] () { (String) -> (A | Array[A]) } -> Array[A]
end
end

def test_filter_map
with_checker(<<-RBS) do |checker|
class FilterMap
def filter_map: [A] () { (String) -> (A | nil | false) } -> Array[A]
end
RBS

source = parse_ruby(<<-'RUBY')
# @type var a: FilterMap
a = _ = nil
a.filter_map do |s|
if s
s.size
end
end
RUBY

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

assert_no_error typing
assert_equal parse_type("::Array[::Integer]"), type
end
end
end

def test_filter_map_compact
with_checker(<<-RBS) do |checker|
class Array[unchecked out Element]
def filter_map: [A] () { (Element) -> (A | nil | false) } -> Array[A]
end
RBS

source = parse_ruby(<<-'RUBY')
a = ["1", nil]
a.filter_map(&:itself)
RUBY

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

assert_no_error typing
assert_equal parse_type("::Array[::String]"), type
end
end
end

def test_to_proc_syntax_optional_arg
with_checker(<<-RBS) do |checker|
class OptionalArgMethod
Expand Down