diff options
author | Nobuyoshi Nakada <[email protected]> | 2023-11-30 19:04:29 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2023-11-30 21:40:22 +0900 |
commit | 1802d14ca8924bd67e0915c5ad9f1fad5dba0602 (patch) | |
tree | 8fd0c1c99598196b4e9416d522688c8558bcdd7a | |
parent | 0cdad3b92a7e117bc7e36779140f5c83b07ca7ce (diff) |
[Bug #19877] Assign captures for direct regexp literal only
-rw-r--r-- | parse.y | 20 | ||||
-rw-r--r-- | test/ruby/test_parse.rb | 11 |
2 files changed, 20 insertions, 11 deletions
@@ -1149,8 +1149,16 @@ set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_l static NODE * last_expr_node(NODE *expr) { - if (nd_type_p(expr, NODE_BLOCK)) { - expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head; + while (expr) { + if (nd_type_p(expr, NODE_BLOCK)) { + expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head; + } + else if (nd_type_p(expr, NODE_BEGIN)) { + expr = RNODE_BEGIN(expr)->nd_body; + } + else { + break; + } } return expr; } @@ -3887,7 +3895,7 @@ primary : literal { /*%%%*/ if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0; - $$ = $2; + $$ = NEW_BEGIN($2, &@$); /*% %*/ /*% ripper: paren!($2) %*/ } @@ -12600,8 +12608,6 @@ static NODE* last_expr_once_body(NODE *node) { if (!node) return 0; - node = last_expr_node(node); - if (!node) return 0; return nd_once_body(node); } @@ -14096,6 +14102,10 @@ cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *l assign_in_cond(p, node); switch (nd_type(node)) { + case NODE_BEGIN: + RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc); + break; + case NODE_DSTR: case NODE_EVSTR: case NODE_STR: diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index dcc04a441c..2960a3c295 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -1060,20 +1060,19 @@ x = __ENCODING__ end def test_named_capture_in_block - [ + all_assertions_foreach(nil, '(/(?<a>.*)/)', '(;/(?<a>.*)/)', '(%s();/(?<a>.*)/)', '(%w();/(?<a>.*)/)', '(1; (2; 3; (4; /(?<a>.*)/)))', '(1+1; /(?<a>.*)/)', - ].each do |code| + ) do |code, pass| token = Random.bytes(4).unpack1("H*") - begin - $VERBOSE, verbose_bak = nil, $VERBOSE + if pass assert_equal(token, eval("#{code} =~ #{token.dump}; a")) - ensure - $VERBOSE = verbose_bak + else + assert_nil(eval("#{code} =~ #{token.dump}; defined?(a)"), code) end end end |