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

Set up break contexts correctly for untyped blocks #752

Merged
merged 1 commit into from
Apr 13, 2023
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
97 changes: 58 additions & 39 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3183,46 +3183,65 @@ def type_send(node, send_node:, block_params:, block_body:, unwrap: false, tapp:
receiver_type = checker.factory.deep_expand_alias(recv_type)
private = receiver.nil? || receiver.type == :self

type, constr = case receiver_type
when nil
raise

when AST::Types::Any
constr = constr.synthesize_children(node, skips: [receiver])
constr.add_call(
TypeInference::MethodCall::Untyped.new(
node: node,
context: context.call_context,
method_name: method_name
)
)
type, constr =
case receiver_type
when nil
raise

else
if interface = calculate_interface(receiver_type, private: private)
constr.type_send_interface(
node,
interface: interface,
receiver: receiver,
receiver_type: receiver_type,
method_name: method_name,
arguments: arguments,
block_params: block_params,
block_body: block_body,
tapp: tapp
)
else
constr = constr.synthesize_children(node, skips: [receiver])
constr.add_call(
TypeInference::MethodCall::NoMethodError.new(
node: node,
context: context.call_context,
method_name: method_name,
receiver_type: receiver_type,
error: Diagnostic::Ruby::NoMethod.new(node: node, method: method_name, type: receiver_type)
)
)
end
end
when AST::Types::Any
case node.type
when :block, :numblock
# @type var node: Parser::AST::Node & Parser::AST::_BlockNode
block_annotations = source.annotations(block: node, factory: checker.factory, context: nesting)
block_params or raise

constr = constr.synthesize_children(node.children[0])

constr.type_block_without_hint(
node: node,
block_params: TypeInference::BlockParams.from_node(block_params, annotations: block_annotations),
block_annotations: block_annotations,
block_body: block_body
) do |error|
constr.typing.errors << error
end
else
constr = constr.synthesize_children(node, skips: [receiver])
end

constr.add_call(
TypeInference::MethodCall::Untyped.new(
node: node,
context: context.call_context,
method_name: method_name
)
)
else
if interface = calculate_interface(receiver_type, private: private)
constr.type_send_interface(
node,
interface: interface,
receiver: receiver,
receiver_type: receiver_type,
method_name: method_name,
arguments: arguments,
block_params: block_params,
block_body: block_body,
tapp: tapp
)
else
constr = constr.synthesize_children(node, skips: [receiver])
constr.add_call(
TypeInference::MethodCall::NoMethodError.new(
node: node,
context: context.call_context,
method_name: method_name,
receiver_type: receiver_type,
error: Diagnostic::Ruby::NoMethod.new(node: node, method: method_name, type: receiver_type)
)
)
end
end

Pair.new(type: type, constr: constr)
end
Expand Down
24 changes: 24 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10372,4 +10372,28 @@ def next
end
end
end

def test_untyped_call_block
with_checker(<<~RBS) do |checker|
RBS
source = parse_ruby(<<~RUBY)
(_ = [1, 2, 3]).each {
case
when 1
next
when 2
retry
when 3
break
end
}
RUBY

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

assert_no_error(typing)
end
end
end
end