summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2025-01-22 13:59:35 -0500
committergit <[email protected]>2025-01-22 19:09:42 +0000
commit241ada7b1ca4fd71dc47a83d912ee25162a555d9 (patch)
tree51b6e1063acfa19ac94d17e46ec9894c8b1429ba
parent495b1cad042b30c40b62b5ecea5a728ea3c2f4ac (diff)
[ruby/prism] Do not put empty statements in while because of -n
Fixes [Bug #21085] https://github.com/ruby/prism/commit/ebb9c36a10
-rw-r--r--prism/prism.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/prism/prism.c b/prism/prism.c
index 3cfcdd8be5..257e4773c9 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -22186,6 +22186,10 @@ parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, bool acc
static pm_statements_node_t *
wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {
if (PM_PARSER_COMMAND_LINE_OPTION_P(parser)) {
+ if (statements == NULL) {
+ statements = pm_statements_node_create(parser);
+ }
+
pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
pm_arguments_node_arguments_append(
arguments,
@@ -22201,6 +22205,10 @@ wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {
if (PM_PARSER_COMMAND_LINE_OPTION_N(parser)) {
if (PM_PARSER_COMMAND_LINE_OPTION_A(parser)) {
+ if (statements == NULL) {
+ statements = pm_statements_node_create(parser);
+ }
+
pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
pm_arguments_node_arguments_append(
arguments,
@@ -22269,9 +22277,7 @@ parse_program(pm_parser_t *parser) {
parser_lex(parser);
pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_MAIN, 0);
- if (statements == NULL) {
- statements = pm_statements_node_create(parser);
- } else if (!parser->parsing_eval) {
+ if (statements != NULL && !parser->parsing_eval) {
// If we have statements, then the top-level statement should be
// explicitly checked as well. We have to do this here because
// everywhere else we check all but the last statement.
@@ -22283,13 +22289,6 @@ parse_program(pm_parser_t *parser) {
pm_locals_order(parser, &parser->current_scope->locals, &locals, true);
pm_parser_scope_pop(parser);
- // If this is an empty file, then we're still going to parse all of the
- // statements in order to gather up all of the comments and such. Here we'll
- // correct the location information.
- if (pm_statements_node_body_length(statements) == 0) {
- pm_statements_node_location_set(statements, parser->start, parser->start);
- }
-
// At the top level, see if we need to wrap the statements in a program
// node with a while loop based on the options.
if (parser->command_line & (PM_OPTIONS_COMMAND_LINE_P | PM_OPTIONS_COMMAND_LINE_N)) {
@@ -22299,6 +22298,14 @@ parse_program(pm_parser_t *parser) {
pm_node_list_free(&current_block_exits);
}
+ // If this is an empty file, then we're still going to parse all of the
+ // statements in order to gather up all of the comments and such. Here we'll
+ // correct the location information.
+ if (statements == NULL) {
+ statements = pm_statements_node_create(parser);
+ pm_statements_node_location_set(statements, parser->start, parser->start);
+ }
+
return (pm_node_t *) pm_program_node_create(parser, &locals, statements);
}