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

In meth(*foo, bar) both foo and bar are splatted #2260

Closed
whitequark opened this issue Apr 7, 2013 · 3 comments
Closed

In meth(*foo, bar) both foo and bar are splatted #2260

whitequark opened this issue Apr 7, 2013 · 3 comments
Assignees

Comments

@whitequark
Copy link

Here is an example of a CI failure I'm experiencing on rbx: https://travis-ci.org/whitequark/parser/jobs/6120901.

All of the failures follow the same pattern: a method of form s(:foo, *bar, baz) is called, where both bar and baz implement #to_a; in particular, bar is an Array and baz is an AST::Node which implements #to_a, returning an Array.

The problem is that both bar and baz are splatted, instead of just bar.

I also made a minimal repro here: https://github.com/whitequark/ast/blob/master/test/test_ast.rb#L112 and it also fails the same way: https://travis-ci.org/whitequark/ast/builds/6121338.

@whitequark
Copy link
Author

Even more minimal repro, courtesy of @tsion:

def foo(*args); p args; end
foo(*[1,2,3], [4,5,6]) # ok
a = [1,2,3]; b = [4,5,6]; foo(*a, b) # fail

@solson
Copy link

solson commented Apr 7, 2013

Here's a few more curious cases that might make it easier to track this down:

>> a = [1,2,3]; foo(*a, [4,5,6])
=> [1, 2, 3, 4, 5, 6]
>> a = [1,2,3]; foo(*a, [4,5,6], [7,8,9])
=> [1, 2, 3, [4, 5, 6], [7, 8, 9]]
>> a = [1,2,3]; foo([0,0,0], *a, [4,5,6])
=> [[0, 0, 0], 1, 2, 3, 4, 5, 6]
>> a = [1,2,3]; foo([0,0,0], *a, [4,5,6], [7,8,9])
=> [[0, 0, 0], 1, 2, 3, [4, 5, 6], [7, 8, 9]]

@leocassarani
Copy link
Member

I'm having exactly the same issue. This was particularly sneaky in my own code, as one of the non-splatted arguments was a Struct instance, which the splat will erroneously transform into its attributes. Before realising this had already been reported, I'd come up with the following to illustrate the problem:

def bar(one, two)
  puts "this will not be reached"
end

a = ['foo']
b = [1, 2]
bar(*a, b) # method 'bar': given 3, expected 2 (ArgumentError)

This is not an issue in MRI 1.9 and above.

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

No branches or pull requests

3 participants