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

Type check anonymous block forwarding #577

Merged
merged 1 commit into from
Jun 13, 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
14 changes: 12 additions & 2 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,8 @@ def synthesize(node, hint: nil, condition: false)
yield_self do
value = node.children[0]

if hint.is_a?(AST::Types::Proc) && value.type == :sym
case
when hint.is_a?(AST::Types::Proc) && value && value.type == :sym
if hint.one_arg?
# Assumes Symbol#to_proc implementation
param_type = hint.type.params.required[0]
Expand Down Expand Up @@ -2333,9 +2334,18 @@ def synthesize(node, hint: nil, condition: false)
else
Steep.logger.error "Passing multiple args through Symbol#to_proc is not supported yet"
end
when value == nil
type = AST::Types::Proc.new(
type: method_context.method_type.block.type,
location: nil,
block: nil
)
if method_context.method_type.block.optional?
type = AST::Types::Union.build(types: [type, AST::Builtin.nil_type])
end
end

type ||= synthesize(node.children[0], hint: hint).type
type ||= synthesize(value, hint: hint).type

add_typing node, type: type
end
Expand Down
22 changes: 22 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9121,4 +9121,26 @@ module Mod
end
end
end

def test_anonymouse_block_forwarding
with_checker(<<RBS) do |checker|
class AnonBlockForwarding
def foo: () { (String) -> void } -> void
end
RBS
source = parse_ruby(<<RUBY)
class AnonBlockForwarding
def foo(&)
["a", "b"].each(&)
end
end
RUBY

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

assert_no_error(typing)
end
end
end
end