Skip to content

Commit 927797b

Browse files
committed
Make defined? (x;) return expression when using parse.y parser
Follow up [Bug #21029]. Currently, `defined? (x;)` returns `expression` when using Prism parser. See: - #12949 - https://bugs.ruby-lang.org/issues/21029 However, `defined? (x;)` returns nil when using parse.y, as reported in bug ticket comment and test-all (when using parse.y parser) test result. This change adds a context flag to track trailing semicolons in defined? scope. When a trailing semicolon is detected within a defined? scope, the generated AST node is wrapped with NODE_BLOCK. This change ensures consistent behavior with `defined? (;x)` .
1 parent f5acefc commit 927797b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

parse.y

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ struct lex_context {
314314
unsigned int in_argdef: 1;
315315
unsigned int in_def: 1;
316316
unsigned int in_class: 1;
317+
unsigned int has_trailing_semicolon: 1;
317318
BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
318319
BITFIELD(enum rescue_context, in_rescue, 2);
319320
unsigned int cant_return: 1;
@@ -6706,7 +6707,14 @@ trailer : '\n'?
67066707
| ','
67076708
;
67086709

6709-
term : ';' {yyerrok;token_flush(p);}
6710+
term : ';'
6711+
{
6712+
yyerrok;
6713+
token_flush(p);
6714+
if (p->ctxt.in_defined) {
6715+
p->ctxt.has_trailing_semicolon = 1;
6716+
}
6717+
}
67106718
| '\n'
67116719
{
67126720
@$.end_pos = @$.beg_pos;
@@ -13013,6 +13021,9 @@ kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
1301313021
static NODE *
1301413022
new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
1301513023
{
13024+
int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13025+
p->ctxt.has_trailing_semicolon = 0;
13026+
1301613027
NODE *n = expr;
1301713028
while (n) {
1301813029
if (nd_type_p(n, NODE_BEGIN)) {
@@ -13025,6 +13036,12 @@ new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
1302513036
break;
1302613037
}
1302713038
}
13039+
13040+
if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13041+
NODE *block = NEW_BLOCK(expr, loc);
13042+
return NEW_DEFINED(block, loc);
13043+
}
13044+
1302813045
return NEW_DEFINED(n, loc);
1302913046
}
1303013047

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy