Skip to content

Commit 241ada7

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Do not put empty statements in while because of -n
Fixes [Bug #21085] ruby/prism@ebb9c36a10
1 parent 495b1ca commit 241ada7

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

prism/prism.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22186,6 +22186,10 @@ parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, bool acc
2218622186
static pm_statements_node_t *
2218722187
wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {
2218822188
if (PM_PARSER_COMMAND_LINE_OPTION_P(parser)) {
22189+
if (statements == NULL) {
22190+
statements = pm_statements_node_create(parser);
22191+
}
22192+
2218922193
pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
2219022194
pm_arguments_node_arguments_append(
2219122195
arguments,
@@ -22201,6 +22205,10 @@ wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {
2220122205

2220222206
if (PM_PARSER_COMMAND_LINE_OPTION_N(parser)) {
2220322207
if (PM_PARSER_COMMAND_LINE_OPTION_A(parser)) {
22208+
if (statements == NULL) {
22209+
statements = pm_statements_node_create(parser);
22210+
}
22211+
2220422212
pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
2220522213
pm_arguments_node_arguments_append(
2220622214
arguments,
@@ -22269,9 +22277,7 @@ parse_program(pm_parser_t *parser) {
2226922277
parser_lex(parser);
2227022278
pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_MAIN, 0);
2227122279

22272-
if (statements == NULL) {
22273-
statements = pm_statements_node_create(parser);
22274-
} else if (!parser->parsing_eval) {
22280+
if (statements != NULL && !parser->parsing_eval) {
2227522281
// If we have statements, then the top-level statement should be
2227622282
// explicitly checked as well. We have to do this here because
2227722283
// everywhere else we check all but the last statement.
@@ -22283,13 +22289,6 @@ parse_program(pm_parser_t *parser) {
2228322289
pm_locals_order(parser, &parser->current_scope->locals, &locals, true);
2228422290
pm_parser_scope_pop(parser);
2228522291

22286-
// If this is an empty file, then we're still going to parse all of the
22287-
// statements in order to gather up all of the comments and such. Here we'll
22288-
// correct the location information.
22289-
if (pm_statements_node_body_length(statements) == 0) {
22290-
pm_statements_node_location_set(statements, parser->start, parser->start);
22291-
}
22292-
2229322292
// At the top level, see if we need to wrap the statements in a program
2229422293
// node with a while loop based on the options.
2229522294
if (parser->command_line & (PM_OPTIONS_COMMAND_LINE_P | PM_OPTIONS_COMMAND_LINE_N)) {
@@ -22299,6 +22298,14 @@ parse_program(pm_parser_t *parser) {
2229922298
pm_node_list_free(&current_block_exits);
2230022299
}
2230122300

22301+
// If this is an empty file, then we're still going to parse all of the
22302+
// statements in order to gather up all of the comments and such. Here we'll
22303+
// correct the location information.
22304+
if (statements == NULL) {
22305+
statements = pm_statements_node_create(parser);
22306+
pm_statements_node_location_set(statements, parser->start, parser->start);
22307+
}
22308+
2230222309
return (pm_node_t *) pm_program_node_create(parser, &locals, statements);
2230322310
}
2230422311

0 commit comments

Comments
 (0)