8000 merge revision(s) bc002971b6ad483dbf69b8a275c44412bb6ab954: [Backport… · ruby/ruby@d4b780e · GitHub
[go: up one dir, main page]

Skip to content

Commit d4b780e

Browse files
committed
merge revision(s) bc00297: [Backport #20094]
[Bug #20094] Distinguish `begin` and parentheses --- compile.c | 1 + parse.y | 36 +++++++++++++++++++++--------------- test/ruby/test_whileuntil.rb | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 15 deletions(-)
1 parent 5f3dfa1 commit d4b780e

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7097,6 +7097,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c
70977097
case NODE_COLON2:
70987098
case NODE_COLON3:
70997099
case NODE_BEGIN:
7100+
case NODE_BLOCK:
71007101
CHECK(COMPILE(ret, "case in literal", node)); // (1)
71017102
if (in_single_pattern) {
71027103
ADD_INSN1(ret, line_node, dupn, INT2FIX(2));

parse.y

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,6 @@ static void fixpos(NODE*,NODE*);
11861186
static int value_expr_gen(struct parser_params*,NODE*);
11871187
static void void_expr(struct parser_params*,NODE*);
11881188
static NODE *remove_begin(NODE*);
1189-
static NODE *remove_begin_all(NODE*);
11901189
#define value_expr(node) value_expr_gen(p, (node))
11911190
static NODE *void_stmts(struct parser_params*,NODE*);
11921191
static void reduce_nodes(struct parser_params*,NODE**);
@@ -3894,7 +3893,7 @@ primary : literal
38943893
{
38953894
/*%%%*/
38963895
if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3897-
$$ = NEW_BEGIN($2, &@$);
3896+
$$ = NEW_BLOCK($2, &@$);
38983897
/*% %*/
38993898
/*% ripper: paren!($2) %*/
39003899
}
@@ -5545,7 +5544,7 @@ p_var_ref : '^' tIDENTIFIER
55455544
p_expr_ref : '^' tLPAREN expr_value rparen
55465545
{
55475546
/*%%%*/
5548-
$$ = NEW_BEGIN($3, &@$);
5547+
$$ = NEW_BLOCK($3, &@$);
55495548
/*% %*/
55505549
/*% ripper: begin!($3) %*/
55515550
}
@@ -12830,7 +12829,19 @@ kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
1283012829
static NODE *
1283112830
new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
1283212831
{
12833-
return NEW_DEFINED(remove_begin_all(expr), loc);
12832+
NODE *n = expr;
12833+
while (n) {
12834+
if (nd_type_p(n, NODE_BEGIN)) {
12835+
n = RNODE_BEGIN(n)->nd_body;
12836+
}
12837+
else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
12838+
n = RNODE_BLOCK(n)->nd_head;
12839+
}
12840+
else {
12841+
break;
12842+
}
12843+
}
12844+
return NEW_DEFINED(n, loc);
1283412845
}
1283512846

1283612847
static NODE*
@@ -13970,16 +13981,6 @@ remove_begin(NODE *node)
1397013981
return node;
1397113982
}
1397213983

13973-
static NODE *
13974-
remove_begin_all(NODE *node)
13975-
{
13976-
NODE **n = &node, *n1 = node;
13977-
while (n1 && nd_type_p(n1, NODE_BEGIN)) {
13978-
*n = n1 = RNODE_BEGIN(n1)->nd_body;
13979-
}
13980-
return node;
13981-
}
13982-
1398313984
static void
1398413985
reduce_nodes(struct parser_params *p, NODE **body)
1398513986
{
@@ -14149,7 +14150,12 @@ cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *l
1414914150
return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
1415014151

1415114152
case NODE_BLOCK:
14152-
RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head = cond0(p, RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head, type, loc, false);
14153+
{
14154+
NODE *end = RNODE_BLOCK(node)->nd_end;
14155+
NODE **expr = &RNODE_BLOCK(end)->nd_head;
14156+
if (top) top = node == end;
14157+
*expr = cond0(p, *expr, type, loc, top);
14158+
}
1415314159
break;
1415414160

1415514161
case NODE_AND:

test/ruby/test_whileuntil.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ def test_while
7373
}
7474
end
7575

76+
def test_begin_while
77+
i = 0
78+
sum = 0
79+
begin
80+
i += 1
81+
sum += i
82+
end while i < 10
83+
assert_equal([10, 55], [i, sum])
84+
85+
i = 0
86+
sum = 0
87+
(
88+
i += 1
89+
sum += i
90+
) while false
91+
assert_equal([0, 0], [i, sum])
92+
end
93+
7694
def test_until
7795
i = 0
7896
until i>4

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
1212
#define RUBY_VERSION_TEENY 0
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
14-
#define RUBY_PATCHLEVEL 2
14+
#define RUBY_PATCHLEVEL 3
1515

1616
#include "ruby/version.h"
1717
#include "ruby/internal/abi.h"

0 commit comments

Comments
 (0)
0