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 error on implicit to_proc syntax when untyped is yielded #291

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
41 changes: 23 additions & 18 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2082,24 +2082,29 @@ def synthesize(node, hint: nil, condition: false)
if hint.one_arg?
# Assumes Symbol#to_proc implementation
param_type = hint.type.params.required[0]
interface = checker.factory.interface(param_type, private: true)
method = interface.methods[value.children[0]]
if method
return_types = method.method_types.select {|method_type|
method_type.type.params.empty?
}.map {|method_type|
method_type.type.return_type
}

unless return_types.empty?
type = AST::Types::Proc.new(
type: Interface::Function.new(
params: Interface::Function::Params.empty.update(required: [param_type]),
return_type: AST::Types::Union.build(types: return_types),
location: nil
),
block: nil
)
case param_type
when AST::Types::Any
type = AST::Types::Any.new
else
interface = checker.factory.interface(param_type, private: true)
method = interface.methods[value.children[0]]
if method
return_types = method.method_types.select {|method_type|
method_type.type.params.empty?
}.map {|method_type|
method_type.type.return_type
}

unless return_types.empty?
type = AST::Types::Proc.new(
type: Interface::Function.new(
params: Interface::Function::Params.empty.update(required: [param_type]),
return_type: AST::Types::Union.build(types: return_types),
location: nil
),
block: nil
)
end
end
end
else
Expand Down
23 changes: 23 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6213,6 +6213,29 @@ def foo: () { (Integer) -> String }-> String
end
end

def test_block_pass_and_untyped
with_checker(<<-RBS) do |checker|
interface _YieldUntyped
def m: [T] () { (untyped) -> T } -> T
end
RBS
source = parse_ruby(<<-EOF)
# @type var x: _YieldUntyped
x = (_ = nil)
r = x.m(&:to_s)
EOF

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

assert_no_error typing

assert_equal parse_type("::_YieldUntyped"), pair.context.lvar_env[:x]
assert_equal parse_type("untyped"), pair.context.lvar_env[:r]
end
end
end

def test_send_unsatisfiable_constraint
with_checker(<<RBS) do |checker|
class SendTest
Expand Down