Skip to content

Commit

Permalink
Merge pull request #577 from soutaro/anonymous-block-forwarding
Browse files Browse the repository at this point in the history
Type check anonymous block forwarding
  • Loading branch information
soutaro committed Jun 13, 2022
2 parents 6f1f011 + 48da3ea commit dcb3d74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit dcb3d74

Please sign in to comment.