Skip to content

Commit 0d0f26c

Browse files
authored
Merge pull request #3444 from ruby/no-empty-statements-in-while
Do not put empty statements in while because of -n
2 parents 28dc4ff + ebb9c36 commit 0d0f26c

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/prism.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -22190,6 +22190,10 @@ parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, bool acc
2219022190
static pm_statements_node_t *
2219122191
wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {
2219222192
if (PM_PARSER_COMMAND_LINE_OPTION_P(parser)) {
22193+
if (statements == NULL) {
22194+
statements = pm_statements_node_create(parser);
22195+
}
22196+
2219322197
pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
2219422198
pm_arguments_node_arguments_append(
2219522199
arguments,
@@ -22205,6 +22209,10 @@ wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {
2220522209

2220622210
if (PM_PARSER_COMMAND_LINE_OPTION_N(parser)) {
2220722211
if (PM_PARSER_COMMAND_LINE_OPTION_A(parser)) {
22212+
if (statements == NULL) {
22213+
statements = pm_statements_node_create(parser);
22214+
}
22215+
2220822216
pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
2220922217
pm_arguments_node_arguments_append(
2221022218
arguments,
@@ -22273,9 +22281,7 @@ parse_program(pm_parser_t *parser) {
2227322281
parser_lex(parser);
2227422282
pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_MAIN, 0);
2227522283

22276-
if (statements == NULL) {
22277-
statements = pm_statements_node_create(parser);
22278-
} else if (!parser->parsing_eval) {
22284+
if (statements != NULL && !parser->parsing_eval) {
2227922285
// If we have statements, then the top-level statement should be
2228022286
// explicitly checked as well. We have to do this here because
2228122287
// everywhere else we check all but the last statement.
@@ -22287,13 +22293,6 @@ parse_program(pm_parser_t *parser) {
2228722293
pm_locals_order(parser, &parser->current_scope->locals, &locals, true);
2228822294
pm_parser_scope_pop(parser);
2228922295

22290-
// If this is an empty file, then we're still going to parse all of the
22291-
// statements in order to gather up all of the comments and such. Here we'll
22292-
// correct the location information.
22293-
if (pm_statements_node_body_length(statements) == 0) {
22294-
pm_statements_node_location_set(statements, parser->start, parser->start);
22295-
}
22296-
2229722296
// At the top level, see if we need to wrap the statements in a program
2229822297
// node with a while loop based on the options.
2229922298
if (parser->command_line & (PM_OPTIONS_COMMAND_LINE_P | PM_OPTIONS_COMMAND_LINE_N)) {
@@ -22303,6 +22302,14 @@ parse_program(pm_parser_t *parser) {
2230322302
pm_node_list_free(&current_block_exits);
2230422303
}
2230522304

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

0 commit comments

Comments
 (0)