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
Possible bug with *rest parameter followed by required parameters #1436
Comments
I was trying to get test/natalie/method_test.rb parsing (and passing), but I think I found a few unexpected results from YARP. I think these are bugs, which I filed here: ruby/prism#1435 ruby/prism#1436
I was trying to get test/natalie/method_test.rb parsing (and passing), but I found a few unexpected results from YARP. I think these are bugs, which I filed here: ruby/prism#1435 ruby/prism#1436
If I change the code at https://github.com/ruby/yarp/blob/main/src/yarp.c#L9115-L9120 from: if (params->rest == NULL) {
yp_parameters_node_rest_set(params, param);
} else {
yp_diagnostic_list_append(&parser->error_list, param->base.location.start, param->base.location.end, YP_ERR_PARAMETER_SPLAT_MULTI);
yp_parameters_node_posts_append(params, (yp_node_t *) param);
} ...to just: yp_diagnostic_list_append(&parser->error_list, param->base.location.start, param->base.location.end, YP_ERR_PARAMETER_SPLAT_MULTI);
yp_parameters_node_posts_append(params, (yp_node_t *) param); ...it fixes the issue. But I don't think that is the proper solution, since then we aren't setting the [edit] And now I understand that |
So I think this behavior is actually as expected, though very confusing. It's easier if you look at the inspect output:
|
@kddnewton won't this get fixed if we just switch posts and rest in config.yml and regenerate the templates? |
Very good, that makes sense. This is the first case in YARP where I noticed the child_nodes "out of order," but now I understand there is no guarantee there. My solution for getting the parameters in order for Natalie is something like: in_order = node.requireds +
[node.rest].compact +
node.optionals +
node.posts +
[node.block].compact +
node.keywords +
[node.keyword_rest].compact I wonder if there is an easier way, or if this is the proper way to get the parameters back out in the right order. |
@seven1m |
OK this works. Thanks for explaining it to me. I think we can close this issue! |
I was trying to get test/natalie/method_test.rb parsing (and passing), but I found a few unexpected results from YARP. I think these are bugs, which I filed here: ruby/prism#1435 ruby/prism#1436
Agreed, that seems a lot more intuitive. |
I'm fine with merging that for this particular issue, but note that there are some nodes where reordering will not fix the issue. |
Yes, I think that makes sense to document. I think when there is a list of nodes then it's rather confusing if the order is not as close as possible to order in the source, like parameters here. |
Just a small comment for anyone else being confused by this behaviour and leveraging @seven1m's excellent suggestion: Prism will include compact_child_nodes.sort_by { |node| node.location.start_offset } |
@heyvito there is also |
@kddnewton oooh! Didn't notice that method! Thank you so much for pointing that out! 🙌🏻 Just updated my last comment! 💕 |
Is this a bug?
I would expect to see:
...but instead I see
The text was updated successfully, but these errors were encountered: