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

Raise UnsupportedSyntax if a proc object is given as block-pass-argument #1245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tk0miya
Copy link
Contributor

@tk0miya tk0miya commented Sep 30, 2024

At present, users will see BlockTypeMismatch diagnostic when a proc object is passed as block-pass-argument. We'll usually see the diagnostic with &method idiom.

[1, 2, 3].map(&method(:puts))

But there is nothing to do from the user side because such a call is not invalid. It seems like a false positive.

This changes the diagnostic type for the case to UnsupportedSyntax. It notifies users that the source code is valid and that change is unnecessary.

refs: #149

@tk0miya tk0miya force-pushed the type_construction/proc_for_block_pass_argument branch 3 times, most recently from 44ace21 to ca398f1 Compare October 2, 2024 17:14
@@ -4191,6 +4191,16 @@ def try_method_type(node, receiver_type:, method_name:, method_overload:, argume
)
else
# non-nil value is given
if node_type.is_a?(AST::Types::Name::Instance) && node_type.name.to_s == '::Proc'
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 move this into #synthesize?

At present, users will see BlockTypeMismatch diagnostic when a proc
object is passed as block-pass-argument.  We'll usually see the diagnostic
with `&method` idiom.

```ruby
[1, 2, 3].map(&method(:puts))
```

But there is nothing to do from the user side because such a call is
not invalid.  It seems like a false positive.

This changes the diagnostic type for the case to UnsupportedSyntax.  It
notifies users that the source code is valid and that change is
unnecessary.

refs: soutaro#149
@tk0miya tk0miya force-pushed the type_construction/proc_for_block_pass_argument branch from ca398f1 to 3271811 Compare October 9, 2024 16:36
@tk0miya
Copy link
Contributor Author

tk0miya commented Nov 26, 2024

Now, #1276 has been merged into the master. But this is still needed, I believe.
We still have the possibility of getting BlockTypeMismatch because #1276 only covers the case of &method(:name).

For example, the following code will cause the diagnostic:

meth = method(:puts)
[1, 2, 3].map(&meth)

block = proc { |n| n.to_s }
[1, 2, 3].map(&block)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant