diff options
| author | Aaron Patterson <tenderlove@ruby-lang.org> | 2024-01-22 16:03:58 -0800 |
|---|---|---|
| committer | Aaron Patterson <aaron.patterson@gmail.com> | 2024-01-22 16:35:58 -0800 |
| commit | cfa15bb173723b8bb6110a81241bcdffa18193fb (patch) | |
| tree | d4519c280dac89df625ed6cc42c5492cf3c7f80d | |
| parent | 6fb9dc5089e970ab977c76e9c13562e516b677ec (diff) | |
Handle trailing commas on blocks
We need to set a special flag on block iseqs when there is a trailing
comma.
Fixes: https://github.com/ruby/prism/issues/2244
| -rw-r--r-- | prism_compile.c | 6 | ||||
| -rw-r--r-- | test/ruby/test_compile_prism.rb | 4 |
2 files changed, 9 insertions, 1 deletions
diff --git a/prism_compile.c b/prism_compile.c index ce08c56c7b..cf9bc74625 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -6218,6 +6218,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_node_list_t *requireds_list = NULL; pm_node_list_t *block_locals = NULL; pm_node_t *block_param_keyword_rest = NULL; + bool trailing_comma = false; struct rb_iseq_constant_body *body = ISEQ_BODY(iseq); @@ -6229,6 +6230,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, block_locals = &block_parameters_node->locals; if (parameters_node) { block_param_keyword_rest = parameters_node->keyword_rest; + if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) { + trailing_comma = true; + } } break; } @@ -6558,7 +6562,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, local_index++; } - if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1) { + if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1 && !trailing_comma) { body->param.flags.ambiguous_param0 = true; } diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb index 328074fc52..10def69ff2 100644 --- a/test/ruby/test_compile_prism.rb +++ b/test/ruby/test_compile_prism.rb @@ -1515,6 +1515,10 @@ a CODE end + def test_trailing_comma_on_block + assert_prism_eval("def self.m; yield [:ok]; end; m {|v0,| v0 }") + end + def test_complex_default_params assert_prism_eval("def self.foo(a:, b: '2'.to_i); [a, b]; end; foo(a: 1)") assert_prism_eval("def self.foo(a:, b: 2, c: '3'.to_i); [a, b, c]; end; foo(a: 1)") |
