From 4c145f7249b5f24a76d5af7d00011f6646e7cde2 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 22:35:36 -0500 Subject: [PATCH] Track which #{} contain literal content We need this to entirely match the semantics where literals can get folded. It is a very specific optimization to do with folding and static literals and stuff. --- config.yml | 10 +++++ include/prism/internal/node.h | 3 ++ snapshots/encoding_binary.txt | 2 +- .../heredoc_percent_newline_delimiter.txt | 14 +++---- .../heredoc_percent_q_newline_delimiter.txt | 10 ++--- snapshots/heredocs_nested.txt | 2 +- snapshots/seattlerb/dstr_evstr.txt | 2 +- snapshots/seattlerb/dstr_str.txt | 2 +- snapshots/seattlerb/heredoc_nested.txt | 2 +- .../seattlerb/pct_w_heredoc_interp_nested.txt | 2 +- snapshots/seattlerb/str_str.txt | 2 +- snapshots/seattlerb/str_str_str.txt | 2 +- snapshots/unparser/corpus/literal/literal.txt | 4 +- snapshots/whitequark/dedenting_heredoc.txt | 2 +- src/prism.c | 40 ++++++++++++++----- test/prism/result/static_literal_flag_test.rb | 40 +++++++++++++++++++ 16 files changed, 105 insertions(+), 34 deletions(-) diff --git a/config.yml b/config.yml index 7da25f37cf..5db0037de7 100644 --- a/config.yml +++ b/config.yml @@ -1244,6 +1244,11 @@ flags: - name: IGNORE_VISIBILITY comment: "a call that ignores method visibility" comment: Flags for call nodes. + - name: EmbeddedStatementsNodeFlags + values: + - name: LITERAL_CONTENT + comment: "the interpolation contributes literal string content to the enclosing literal" + comment: Flags for embedded statements nodes. - name: EncodingFlags values: - name: FORCED_UTF8_ENCODING @@ -2935,6 +2940,7 @@ nodes: if a then b else c end ^^^^^^^^^^ - name: EmbeddedStatementsNode + flags: EmbeddedStatementsNodeFlags fields: - name: opening_loc type: location @@ -2948,6 +2954,10 @@ nodes: "foo #{bar}" ^^^^^^ + + The `literal_content?` flag is set on `#{begin "a" end}`, which folds + into the enclosing literal. A leading semicolon in `#{; "a"}` keeps + the interpolation as a runtime expression. - name: EmbeddedVariableNode fields: - name: operator_loc diff --git a/include/prism/internal/node.h b/include/prism/internal/node.h index d4a6c5fb7f..313c4c43a0 100644 --- a/include/prism/internal/node.h +++ b/include/prism/internal/node.h @@ -79,6 +79,9 @@ enum { PM_STRING_CONCAT_ENCODING_UTF8 | PM_STRING_CONCAT_ENCODING_BINARY }; +/* The string supplies literal text when it is itself interpolated. */ +static const pm_node_flags_t PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT = ((PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 7); + /* * A growable list of nodes for bookkeeping that never becomes part of the tree, * such as the block exits and implicit parameters that are tracked while diff --git a/snapshots/encoding_binary.txt b/snapshots/encoding_binary.txt index 5945c1d085..7a7c1f4874 100644 --- a/snapshots/encoding_binary.txt +++ b/snapshots/encoding_binary.txt @@ -22,7 +22,7 @@ │ ├── opening_loc: (7,0)-(7,1) = "/" │ ├── parts: (length: 1) │ │ └── @ EmbeddedStatementsNode (location: (7,1)-(7,10)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (7,1)-(7,3) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (7,3)-(7,9)) diff --git a/snapshots/heredoc_percent_newline_delimiter.txt b/snapshots/heredoc_percent_newline_delimiter.txt index 2b408b6831..20af3511ac 100644 --- a/snapshots/heredoc_percent_newline_delimiter.txt +++ b/snapshots/heredoc_percent_newline_delimiter.txt @@ -13,7 +13,7 @@ │ │ ├── opening_loc: ∅ │ │ ├── parts: (length: 1) │ │ │ └── @ EmbeddedStatementsNode (location: (2,0)-(2,6)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: literal_content │ │ │ ├── opening_loc: (2,0)-(2,2) = "\#{" │ │ │ ├── statements: │ │ │ │ @ StatementsNode (location: (2,2)-(2,5)) @@ -37,7 +37,7 @@ │ │ ├── opening_loc: ∅ │ │ ├── parts: (length: 1) │ │ │ └── @ EmbeddedStatementsNode (location: (6,0)-(6,6)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: literal_content │ │ │ ├── opening_loc: (6,0)-(6,2) = "\#{" │ │ │ ├── statements: │ │ │ │ @ StatementsNode (location: (6,2)-(6,5)) @@ -58,7 +58,7 @@ │ ├── opening_loc: (9,0)-(10,0) = "%r\n" │ ├── parts: (length: 1) │ │ └── @ EmbeddedStatementsNode (location: (10,0)-(10,6)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (10,0)-(10,2) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (10,2)-(10,5)) @@ -86,7 +86,7 @@ │ │ │ │ ├── closing_loc: ∅ │ │ │ │ └── unescaped: "a" │ │ │ ├── @ EmbeddedStatementsNode (location: (14,1)-(14,7)) - │ │ │ │ ├── flags: ∅ + │ │ │ │ ├── flags: literal_content │ │ │ │ ├── opening_loc: (14,1)-(14,3) = "\#{" │ │ │ │ ├── statements: │ │ │ │ │ @ StatementsNode (location: (14,3)-(14,6)) @@ -122,7 +122,7 @@ │ │ │ │ ├── closing_loc: ∅ │ │ │ │ └── unescaped: "a" │ │ │ ├── @ EmbeddedStatementsNode (location: (19,1)-(19,7)) - │ │ │ │ ├── flags: ∅ + │ │ │ │ ├── flags: literal_content │ │ │ │ ├── opening_loc: (19,1)-(19,3) = "\#{" │ │ │ │ ├── statements: │ │ │ │ │ @ StatementsNode (location: (19,3)-(19,6)) @@ -155,7 +155,7 @@ │ │ │ ├── closing_loc: ∅ │ │ │ └── unescaped: "a" │ │ ├── @ EmbeddedStatementsNode (location: (24,1)-(24,7)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: literal_content │ │ │ ├── opening_loc: (24,1)-(24,3) = "\#{" │ │ │ ├── statements: │ │ │ │ @ StatementsNode (location: (24,3)-(24,6)) @@ -180,7 +180,7 @@ ├── opening_loc: (29,0)-(30,0) = "%r\n" ├── parts: (length: 1) │ └── @ EmbeddedStatementsNode (location: (30,0)-(30,6)) - │ ├── flags: ∅ + │ ├── flags: literal_content │ ├── opening_loc: (30,0)-(30,2) = "\#{" │ ├── statements: │ │ @ StatementsNode (location: (30,2)-(30,5)) diff --git a/snapshots/heredoc_percent_q_newline_delimiter.txt b/snapshots/heredoc_percent_q_newline_delimiter.txt index 75c436b670..56fdae523d 100644 --- a/snapshots/heredoc_percent_q_newline_delimiter.txt +++ b/snapshots/heredoc_percent_q_newline_delimiter.txt @@ -10,7 +10,7 @@ │ ├── opening_loc: (1,0)-(2,0) = "%Q\n" │ ├── parts: (length: 1) │ │ └── @ EmbeddedStatementsNode (location: (2,0)-(2,6)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (2,0)-(2,2) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (2,2)-(2,5)) @@ -29,7 +29,7 @@ │ ├── opening_loc: (5,0)-(6,0) = "%\n" │ ├── parts: (length: 1) │ │ └── @ EmbeddedStatementsNode (location: (6,0)-(6,6)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (6,0)-(6,2) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (6,2)-(6,5)) @@ -54,7 +54,7 @@ │ ├── opening_loc: (9,5)-(10,0) = "%Q\n" │ ├── parts: (length: 1) │ │ └── @ EmbeddedStatementsNode (location: (11,0)-(11,6)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (11,0)-(11,2) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (11,2)-(11,5)) @@ -79,7 +79,7 @@ │ ├── opening_loc: (14,5)-(15,0) = "%\n" │ ├── parts: (length: 1) │ │ └── @ EmbeddedStatementsNode (location: (16,0)-(16,6)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (16,0)-(16,2) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (16,2)-(16,5)) @@ -98,7 +98,7 @@ ├── opening_loc: (20,0)-(21,0) = "%Q\r\n" ├── parts: (length: 1) │ └── @ EmbeddedStatementsNode (location: (21,0)-(21,6)) - │ ├── flags: ∅ + │ ├── flags: literal_content │ ├── opening_loc: (21,0)-(21,2) = "\#{" │ ├── statements: │ │ @ StatementsNode (location: (21,2)-(21,5)) diff --git a/snapshots/heredocs_nested.txt b/snapshots/heredocs_nested.txt index c2d13e4bdb..01d6f7f753 100644 --- a/snapshots/heredocs_nested.txt +++ b/snapshots/heredocs_nested.txt @@ -16,7 +16,7 @@ │ │ │ ├── closing_loc: ∅ │ │ │ └── unescaped: "pre\n" │ │ ├── @ EmbeddedStatementsNode (location: (3,0)-(7,1)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: literal_content │ │ │ ├── opening_loc: (3,0)-(3,2) = "\#{" │ │ │ ├── statements: │ │ │ │ @ StatementsNode (location: (4,0)-(4,6)) diff --git a/snapshots/seattlerb/dstr_evstr.txt b/snapshots/seattlerb/dstr_evstr.txt index c3b5b23628..1e51023383 100644 --- a/snapshots/seattlerb/dstr_evstr.txt +++ b/snapshots/seattlerb/dstr_evstr.txt @@ -10,7 +10,7 @@ ├── opening_loc: (1,0)-(1,1) = "\"" ├── parts: (length: 2) │ ├── @ EmbeddedStatementsNode (location: (1,1)-(1,7)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (1,1)-(1,3) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (1,3)-(1,6)) diff --git a/snapshots/seattlerb/dstr_str.txt b/snapshots/seattlerb/dstr_str.txt index 9e26a7ebc4..1d276bea78 100644 --- a/snapshots/seattlerb/dstr_str.txt +++ b/snapshots/seattlerb/dstr_str.txt @@ -10,7 +10,7 @@ ├── opening_loc: (1,0)-(1,1) = "\"" ├── parts: (length: 2) │ ├── @ EmbeddedStatementsNode (location: (1,1)-(1,7)) - │ │ ├── flags: ∅ + │ │ ├── flags: literal_content │ │ ├── opening_loc: (1,1)-(1,3) = "\#{" │ │ ├── statements: │ │ │ @ StatementsNode (location: (1,3)-(1,6)) diff --git a/snapshots/seattlerb/heredoc_nested.txt b/snapshots/seattlerb/heredoc_nested.txt index 64a7d9ddca..5118223604 100644 --- a/snapshots/seattlerb/heredoc_nested.txt +++ b/snapshots/seattlerb/heredoc_nested.txt @@ -13,7 +13,7 @@ │ │ ├── opening_loc: (1,1)-(1,4) = "<arena, ++parser->node_id, - 0, + flags, PM_LOCATION_INIT_TOKENS(parser, opening, closing), TOK2LOC(parser, opening), statements, @@ -5055,7 +5062,7 @@ pm_interpolated_regular_expression_node_create(pm_parser_t *parser, const pm_tok * for as long as every part is a string literal or a string with this flag. */ static pm_interpolated_string_node_t * -pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *opening, pm_node_list_t *parts, const pm_token_t *closing) { +pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *opening, pm_node_list_t *parts, const pm_token_t *closing, bool dedent) { #define CLEAR_FLAGS() \ flags = (pm_node_flags_t) (flags & ~(PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN | PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) @@ -5065,7 +5072,8 @@ pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *openin pm_node_list_finalize(&parser->node_chunks, parser->arena, parts); pm_location_t location = pm_interpolated_location(parser, opening, parts, closing); - pm_node_flags_t flags = PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE; + pm_node_flags_t flags = PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT; + if (dedent) flags |= PM_INTERPOLATED_STRING_NODE_FLAGS_DEDENTED; switch (parser->frozen_string_literal) { case PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED: @@ -5099,11 +5107,20 @@ pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *openin CLEAR_FLAGS(); } + if (!PM_NODE_FLAG_P(part, PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT)) { + flags &= (pm_node_flags_t) ~PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT; + } + if (!PM_NODE_FLAG_P(part, PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE)) { flags &= (pm_node_flags_t) ~PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE; } break; case PM_EMBEDDED_STATEMENTS_NODE: { + /* Only heredoc dedenting can fold an interpolation back into literal text. */ + if (!dedent || !PM_NODE_FLAG_P(part, PM_EMBEDDED_STATEMENTS_NODE_FLAGS_LITERAL_CONTENT)) { + flags &= (pm_node_flags_t) ~PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT; + } + pm_embedded_statements_node_t *cast = (pm_embedded_statements_node_t *) part; pm_node_t *embedded = pm_statements_node_single_expression(cast->statements); @@ -5143,7 +5160,7 @@ pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *openin // Embedded variables clear static literal, which means we also // should clear the mutability flags. CLEAR_FLAGS(); - flags &= (pm_node_flags_t) ~PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE; + flags &= (pm_node_flags_t) ~(PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT); break; case PM_X_STRING_NODE: case PM_INTERPOLATED_X_STRING_NODE: @@ -5152,12 +5169,12 @@ pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *openin // These will only happen in error cases. But we want to handle it // here so that we don't fail the assertion. CLEAR_FLAGS(); - flags &= (pm_node_flags_t) ~PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE; + flags &= (pm_node_flags_t) ~(PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT); parts->nodes[idx] = UP(pm_error_recovery_node_create_unexpected(parser, part)); break; case PM_ERROR_RECOVERY_NODE: CLEAR_FLAGS(); - flags &= (pm_node_flags_t) ~PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE; + flags &= (pm_node_flags_t) ~(PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | PM_INTERPOLATED_STRING_NODE_FLAGS_LITERAL_CONTENT); break; default: PRISM_ASSERT(false && "unexpected node type"); @@ -17011,6 +17028,7 @@ typedef struct { pm_node_list_t list; const pm_encoding_t *encoding; bool invalid_encoding; + bool dedent; } pm_string_parts_t; /* Check the join in source order and return the result for its first part. */ @@ -17048,7 +17066,7 @@ pm_interpolated_string_node_static_flags_clear(pm_node_t *node) { static pm_node_t * pm_string_parts_to_string(pm_parser_t *parser, const pm_token_t *opening, pm_string_parts_t *parts, const pm_token_t *closing) { - pm_node_t *node = UP(pm_interpolated_string_node_create(parser, opening, &parts->list, closing)); + pm_node_t *node = UP(pm_interpolated_string_node_create(parser, opening, &parts->list, closing, parts->dedent)); if (parts->invalid_encoding) { pm_interpolated_string_node_static_flags_clear(node); @@ -17145,7 +17163,7 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, const pm_encoding_t *curr if (!label_allowed) pm_parser_err_node(parser, node, PM_ERR_UNEXPECTED_LABEL); } else { expect1(parser, PM_TOKEN_STRING_END, PM_ERR_STRING_LITERAL_EOF); - node = UP(pm_interpolated_string_node_create(parser, &opening, &parts, &parser->previous)); + node = UP(pm_interpolated_string_node_create(parser, &opening, &parts, &parser->previous, false)); } } else if (accept1(parser, PM_TOKEN_LABEL_END)) { node = UP(pm_symbol_node_create_unescaped(parser, &opening, &content, &parser->previous, &unescaped, parse_symbol_encoding(parser, explicit_encoding, &content, &unescaped, true))); @@ -19655,6 +19673,7 @@ parse_heredoc(pm_parser_t *parser, uint16_t depth, const pm_encoding_t **trailin // interpolated node. expect1_heredoc_term(parser, lex_mode.ident_start, lex_mode.ident_length); + parts.dedent = lex_mode.indent == PM_HEREDOC_INDENT_TILDE && common_whitespace != 0; if (lex_mode.quote == PM_HEREDOC_QUOTE_BACKTICK) { /* The parts of a backtick heredoc are kept as they are, without * the processing that pm_interpolated_xstring_node_create does. */ @@ -19676,13 +19695,12 @@ parse_heredoc(pm_parser_t *parser, uint16_t depth, const pm_encoding_t **trailin // If this is a heredoc that is indented with a ~, then we need // to dedent each line by the common leading whitespace. - if (lex_mode.indent == PM_HEREDOC_INDENT_TILDE && common_whitespace != 0) { + if (parts.dedent) { pm_node_list_t *nodes; if (lex_mode.quote == PM_HEREDOC_QUOTE_BACKTICK) { nodes = &((pm_interpolated_x_string_node_t *) node)->parts; } else { nodes = &((pm_interpolated_string_node_t *) node)->parts; - pm_node_flag_set(node, PM_INTERPOLATED_STRING_NODE_FLAGS_DEDENTED); } if (!parse_heredoc_dedent(parser, nodes, common_whitespace, &parts.encoding) && PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) { @@ -20495,7 +20513,7 @@ parse_uninterpolated_string_array(pm_parser_t *parser) { } if (parts.size > 0) { - pm_array_elements_append(parser, &elements, UP(pm_interpolated_string_node_create(parser, NULL, &parts, NULL))); + pm_array_elements_append(parser, &elements, UP(pm_interpolated_string_node_create(parser, NULL, &parts, NULL, false))); parts = (pm_node_list_t) { 0 }; } else if (current != NULL) { pm_array_elements_append(parser, &elements, current); diff --git a/test/prism/result/static_literal_flag_test.rb b/test/prism/result/static_literal_flag_test.rb index 58cab30181..52a8000a44 100644 --- a/test/prism/result/static_literal_flag_test.rb +++ b/test/prism/result/static_literal_flag_test.rb @@ -4,6 +4,46 @@ module Prism class StaticLiteralFlagTest < TestCase + def test_interpolation_literal_content + parts = '#{begin "A" end}#{; "B"}#{value}#{"C" "D"}#{"E#{"F"}"}#{("G")}#{begin "H" ensure value end}' + expected = [true, false, false, true, false, false, false] + + ['"%s"', ':"%s"', '%%x{%s}', '/%s/', '%%W[%s]', '%%I[%s]'].each do |format| + source = format % parts + result = Prism.parse(source) + assert_empty result.errors, source + nodes = [result.statement] + nodes << Prism.load(source, Prism.dump(source)).statement unless ENV["PRISM_BUILD_MINIMAL"] + + nodes.each do |node| + node = node.elements.first if node.is_a?(ArrayNode) + assert_equal expected, node.parts.map(&:literal_content?), source + nested_string = node.parts[4].statements.body.first + assert_predicate nested_string.parts.last, :literal_content? + end + end + end + + def test_interpolated_heredoc_literal_content + ["", " "].each do |indent| + ['<<~TEXT', '<<~TEXT "C"'].each do |expression| + source = '/#{' + expression + '}#{value}/' + "\n" + indent + 'A#{begin "B" end}' + "\nTEXT\n" + result = Prism.parse(source) + assert_empty result.errors, source + assert_equal [!indent.empty?, false], result.statement.parts.map(&:literal_content?), source + end + end + end + + def test_literal_content_split_by_a_heredoc + array = Prism.parse_statement(<<~'RUBY') + [<