diff options
author | Kevin Newton <[email protected]> | 2024-05-22 14:16:36 -0400 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2024-05-22 16:34:04 -0400 |
commit | 6d81ae3f013c83bf05371c923898ec4f777cfdba (patch) | |
tree | 47d063a05e5e1d12fd0a1867b01a30b58f43f2bb | |
parent | e575954887a8e8ae26a4122c33c66b8cb82dfa36 (diff) |
[PRISM] Properly support 'it'
-rw-r--r-- | prism/prism.c | 1 | ||||
-rw-r--r-- | prism_compile.c | 35 | ||||
-rw-r--r-- | test/.excludes-prism/TestSyntax.rb | 1 | ||||
-rw-r--r-- | test/ruby/test_syntax.rb | 2 |
4 files changed, 22 insertions, 17 deletions
diff --git a/prism/prism.c b/prism/prism.c index 71cb4ad698..5b27333f22 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -18511,7 +18511,6 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b if (match2(parser, PM_TOKEN_DOT, PM_TOKEN_COLON_COLON)) { receiver = parse_variable_call(parser); - receiver = pm_node_check_it(parser, receiver); pm_parser_scope_push(parser, true); lex_state_set(parser, PM_LEX_STATE_FNAME); diff --git a/prism_compile.c b/prism_compile.c index b6e43d37f6..5edc1ba2e7 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -6915,6 +6915,15 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, return; } + case PM_IT_LOCAL_VARIABLE_READ_NODE: { + // -> { it } + // ^^ + if (!popped) { + PUSH_GETLOCAL(ret, location, scope_node->local_table_for_iseq_size, 0); + } + + return; + } case PM_KEYWORD_HASH_NODE: { // foo(bar: baz) // ^^^^^^^^ @@ -7018,9 +7027,8 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, case PM_LOCAL_VARIABLE_READ_NODE: { // foo // ^^^ - const pm_local_variable_read_node_t *cast = (const pm_local_variable_read_node_t *) node; - if (!popped) { + const pm_local_variable_read_node_t *cast = (const pm_local_variable_read_node_t *) node; pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth); PUSH_GETLOCAL(ret, location, index.index, index.level); } @@ -7962,6 +7970,12 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, } } + // If we have the `it` implicit local variable, we need to account for + // it in the local table size. + if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) { + table_size++; + } + // Ensure there is enough room in the local table for any // parameters that have been repeated // ex: def underscore_parameters(_, _ = 1, _ = 2); _; end @@ -8110,6 +8124,11 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, body->param.flags.has_lead = true; } + if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) { + ID local = rb_make_temporary_id(local_index); + local_table_for_iseq->ids[local_index++] = local; + } + // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n) // ^^^^^ if (optionals_list && optionals_list->size) { @@ -8470,18 +8489,6 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, body->param.flags.has_lead = true; } - // Fill in the it variable, if it exists - if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) { - const uint8_t param_name[] = { '0', 'i', 't' }; - pm_constant_id_t constant_id = pm_constant_pool_find(&parser->constant_pool, param_name, 3); - RUBY_ASSERT(constant_id && "parser should have inserted 0it for 'it' local"); - - ID local = rb_make_temporary_id(local_index); - local_table_for_iseq->ids[local_index] = local; - st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index); - local_index++; - } - //********END OF STEP 3********** //********STEP 4********** diff --git a/test/.excludes-prism/TestSyntax.rb b/test/.excludes-prism/TestSyntax.rb index 4809924acc..b07e2e1e0f 100644 --- a/test/.excludes-prism/TestSyntax.rb +++ b/test/.excludes-prism/TestSyntax.rb @@ -1,5 +1,4 @@ exclude(:test_dedented_heredoc_continued_line, "heredoc line continuation dedent calculation") -exclude(:test_it, "https://github.com/ruby/prism/issues/2323") exclude(:test_unterminated_heredoc_cr, "quoted \r heredoc terminators should not match \r\n") exclude(:test_duplicated_when, "https://bugs.ruby-lang.org/issues/20401") diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 2f311c859e..f4c9f06459 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1922,7 +1922,7 @@ eom ] end assert_valid_syntax('proc {def foo(_);end;it}') - assert_syntax_error('p { [it **2] }', /unexpected \*\*arg/) + assert_syntax_error('p { [it **2] }', /unexpected \*\*/) end def test_value_expr_in_condition |