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 for #flat_map #504

Merged
merged 2 commits into from
Mar 8, 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
2 changes: 2 additions & 0 deletions lib/steep/ast/types/any.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def to_s

include Helper::NoFreeVariables

include Helper::NoChild

def level
[1]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def to_s

include Helper::NoFreeVariables

include Helper::NoChild

def level
[0]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def to_s

include Helper::NoFreeVariables

include Helper::NoChild

def level
[2]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def free_variables()
@fvs = Set.new([self])
end

include Helper::NoChild

def level
[0]
end
Expand Down
8 changes: 8 additions & 0 deletions lib/steep/ast/types/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def free_variables()
@fvs ||= Set.new
end
end

module NoChild
def each_child(&block)
unless block
enum_for :each_child
end
end
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def free_variables()
@fvs = Set.new([self])
end

include Helper::NoChild

def to_s
"instance"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/steep/ast/types/intersection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def free_variables()

include Helper::ChildrenLevel

def each_child(&block)
types.each(&block)
end

def level
[0] + level_of_children(types)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def to_s

include Helper::NoFreeVariables

include Helper::NoChild

def level
[0]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def free_variables
@fvs ||= Set[]
end

include Helper::NoChild

def hash
self.class.hash
end
Expand Down
6 changes: 6 additions & 0 deletions lib/steep/ast/types/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def free_variables
end
end

def each_child(&block)
args.each(&block)
end

include Helper::ChildrenLevel

def level
Expand All @@ -98,6 +102,8 @@ def to_s
def with_location(new_location)
self.class.new(name: name, location: new_location)
end

include Helper::NoChild
end

class Instance < Applying
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/nil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def to_s

include Helper::NoFreeVariables

include Helper::NoChild

def level
[0]
end
Expand Down
9 changes: 9 additions & 0 deletions lib/steep/ast/types/proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ def back_type
def block_required?
block && !block.optional?
end

def each_child(&block)
if block_given?
type.each_child(&block)
self.block&.type&.each_child(&block)
else
enum_for :each_child
end
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/steep/ast/types/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def free_variables()

include Helper::ChildrenLevel

def each_child(&block)
elements.each_value(&block)
end

def level
[0] + level_of_children(elements.values)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/self.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def to_s
"self"
end

include Helper::NoChild

def subst(s)
s.self_type or raise "Unexpected substitution: #{inspect}"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/top.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def to_s

include Helper::NoFreeVariables

include Helper::NoChild

def level
[2]
end
Expand Down
4 changes: 4 additions & 0 deletions lib/steep/ast/types/tuple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def free_variables()

include Helper::ChildrenLevel

def each_child(&block)
types.each(&block)
end

def level
[0] + level_of_children(types)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/steep/ast/types/union.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def free_variables
end
end

def each_child(&block)
types.each(&block)
end

include Helper::ChildrenLevel

def level
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/var.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def free_variables()
@fvs ||= Set.new([name])
end

include Helper::NoChild

def level
[0]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/steep/ast/types/void.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def to_s

include Helper::NoFreeVariables

include Helper::NoChild

def level
[0]
end
Expand Down
4 changes: 4 additions & 0 deletions lib/steep/interface/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,10 @@ def subst(s)
)
end

def each_child(&block)
each_type(&block)
end

def each_type(&block)
if block_given?
params.each_type(&block)
Expand Down
22 changes: 20 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.each do |super_type|
relation.super_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : 1 }.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.each do |sub_type|
relation.sub_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : 1 }.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 Expand Up @@ -974,6 +974,24 @@ def match_params(name, relation)
def expand_alias(type, &block)
factory.expand_alias(type, &block)
end

# Returns the shortest type paths for one of the _unknown_ type variables.
# Returns nil if there is no path.
def hole_path(type, path = [])
case type
when AST::Types::Var
if constraints.unknown?(type.name)
[type]
else
nil
end
else
paths = type.each_child.map do |ty|
hole_path(ty, path)&.unshift(ty)
end
paths.compact.min_by(&:size)
end
end
end
end
end
22 changes: 22 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8419,4 +8419,26 @@ def foo(&block)
end
end
end

def test_flat_map
with_checker(<<-RBS) do |checker|
class FlatMap
def flat_map: [A] () { (String) -> (A | Array[A]) } -> Array[A]
end
RBS

source = parse_ruby(<<-'RUBY')
# @type var a: FlatMap
a = _ = nil
a.flat_map {|s| [s] }
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
end