Skip to content

Support &method(:name) call for block_pass#1276

Merged
soutaro merged 3 commits intosoutaro:masterfrom
tk0miya:type_construction/support_amp_method_for_block_pass
Nov 25, 2024
Merged

Support &method(:name) call for block_pass#1276
soutaro merged 3 commits intosoutaro:masterfrom
tk0miya:type_construction/support_amp_method_for_block_pass

Conversation

@tk0miya
Copy link
Contributor

@tk0miya tk0miya commented Oct 9, 2024

Pass &method(:name) to the block argument on method call is usually used, and well-known idiom in Ruby. Therefore it is valuable to support the idiom on type check.

This gives a special treatment to it to support type checking.

@tk0miya tk0miya force-pushed the type_construction/support_amp_method_for_block_pass branch from 66880ac to f285471 Compare October 9, 2024 16:24
if hint.is_a?(AST::Types::Proc) && value_node.type == :send && value_node.children[1] == :method && type.is_a?(AST::Types::Name::Instance) && type.name.to_s == '::Method'
method_name = value_node.children[2].children[0]
if method = calculate_interface(AST::Types::Self.instance, private: true)&.methods&.[](method_name)
# TODO: select appropriate method type from `method.method_types`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure how to choose the appropriate method type from method.method_types. So I use the first one.

assert_typing_error typing, size: 1 do |errors|
assert_any!(errors) do |error|
assert_instance_of Diagnostic::Ruby::BlockTypeMismatch, error
assert /^\^\(::Integer\) -> X\(\d+\)$/ =~ error.expected.to_s
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use regexp to check the BlockTypeMismatch#expected. Is there a better way?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, in fact, you may want to use the newer type_check_test.rb.

tk0miya and others added 2 commits November 25, 2024 14:21
Pass `&method(:name)` to the block argument on method call is usually
used, and well-known idiom in Ruby.  Therefore it is valuable to
support the idiom on type check.

This gives a special treatment to it to support type checking.
@soutaro soutaro force-pushed the type_construction/support_amp_method_for_block_pass branch from f285471 to 5bb36c1 Compare November 25, 2024 05:28
@soutaro soutaro added this to the Steep 1.9 milestone Nov 25, 2024
@soutaro soutaro enabled auto-merge November 25, 2024 05:29
if method = calculate_interface(AST::Types::Self.instance, private: true)&.methods&.[](method_name)
if overload = method.method_types.find {|method_type| method_type.type.params.nil? || method_type.type.params.optional? }
type = AST::Types::Proc.new(
type: method.method_types.first&.type || raise,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to use overload here?

Suggested change
type: method.method_types.first&.type || raise,
type: overload,

@soutaro soutaro force-pushed the type_construction/support_amp_method_for_block_pass branch from 5bb36c1 to cca5a96 Compare November 25, 2024 08:32
@soutaro soutaro merged commit aef81cf into soutaro:master Nov 25, 2024
@tk0miya tk0miya deleted the type_construction/support_amp_method_for_block_pass branch November 25, 2024 11:01
@soutaro soutaro added the Released The PR is already included in a published release label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Released The PR is already included in a published release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants