8000 Do not respect ruby2_keywords on method/proc with post arguments by jeremyevans · Pull Request #13475 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Do not respect ruby2_keywords on method/proc with post arguments #13475

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

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

Conversation

jeremyevans
Copy link
Contributor
@jeremyevans jeremyevans commented May 30, 2025
8000

Previously, ruby2_keywords could be used on a method or proc with
post arguments, but I don't think the behavior is desired:

def a(*c, **kw) [c, kw] end
def b(*a, b) a(*a, b) end
ruby2_keywords(:b)

b({foo: 1}, bar: 1)
# Before: [[{foo: 1}], {bar: 1}]
#  After: [[{foo: 1}, {bar: 1}], {}]

This changes ruby2_keywords to emit a warning and not set the
flag on a method/proc with post arguments.

While here, fix the ruby2_keywords specs for warnings, since they
weren't testing what they should be testing. They all warned
because the method didn't accept a rest argument, not because it
accepted a keyword or keyword rest argument

@jeremyevans jeremyevans requested review from nobu and XrXr May 30, 2025 14:32
Copy link
Member
@XrXr XrXr left a comment

Choose a reason for hiding this comment

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

It's not possible to pass keywords to such methods as the syntax doesn't allow it, since you cannot have keywords arguments before a positional argument, and a positional argument is needed for the post argument.

I'm not very clear on this, so let me try to paraphrase. Do you mean prior to this change, passing keyword args to a ruby2_keywords method that only has post parameters would always end up triggering the kwargs-to-hash conversion, and that the hash would always be absorbed by the last post parameter, so no keywords hash ends up in the rest parameter?

@jeremyevans
Copy link
Contributor Author

It's not possible to pass keywords to such methods as the syntax doesn't allow it, since you cannot have keywords arguments before a positional argument, and a positional argument is needed for the post argument.

I'm not very clear on this, so let me try to paraphrase. Do you mean prior to this change, passing keyword args to a ruby2_keywords method that only has post parameters would always end up triggering the kwargs-to-hash conversion, and that the hash would always be absorbed by the last post parameter, so no keywords hash ends up in the rest parameter?

I originally thought it was not possible to trigger incorrect behavior, but apparently my original testing was insufficient. It is possible to trigger incorrect behavior:

def a(*c, **kw) [c, kw] end
def b(*a, b) a(*a, b) end
ruby2_keywords(:b)

b({foo: 1}, bar: 1)
# => [[{foo: 1}], {bar: 1}]

I'll open up a Redmine bug report for this, since I think we would want to backport this fix.

Previously, ruby2_keywords could be used on a method or proc with
post arguments, but I don't think the behavior is desired:

```ruby
def a(*c, **kw) [c, kw] end
def b(*a, b) a(*a, b) end
ruby2_keywords(:b)

b({foo: 1}, bar: 1)
```

This changes ruby2_keywords to emit a warning and not set the
flag on a method/proc with post arguments.

While here, fix the ruby2_keywords specs for warnings, since they
weren't testing what they should be testing.  They all warned
because the method didn't accept a rest argument, not because it
accepted a keyword or keyword rest argument.
@jeremyevans jeremyevans force-pushed the ruby2_keywords-post-arg branch from eddb3d3 to 1d04b18 Compare June 9, 2025 21:20
@jeremyevans jeremyevans changed the title Have ruby2_keywords emit warning if method/proc uses post arguments Do not respect ruby2_keywords on method/proc with post arguments Jun 9, 2025
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.

2 participants
0