Skip to content

Commit

Permalink
[Bug #19877] Assign captures for direct regexp literal only
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Nov 30, 2023
1 parent 0cdad3b commit 1802d14
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
20 changes: 15 additions & 5 deletions parse.y
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) %*/
}
Expand Down Expand Up @@ -12599,8 +12607,6 @@ new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *a
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);
}
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 5 additions & 6 deletions test/ruby/test_parse.rb
Expand Up @@ -1060,20 +1060,19 @@ def test_named_capture_conflict
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
Expand Down

0 comments on commit 1802d14

Please sign in to comment.