From 938a074756dfa891825174a7e12d9258b709ee95 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 14:38:11 -0500 Subject: [PATCH 1/8] Preserve assignment precedence for shareable constants with implicit array values --- src/prism.c | 9 ++++----- .../errors/shareable_constant_assignment_precedence.txt | 9 +++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 test/prism/errors/shareable_constant_assignment_precedence.txt diff --git a/src/prism.c b/src/prism.c index ada2aa8a52..2f6c6782ee 100644 --- a/src/prism.c +++ b/src/prism.c @@ -21748,12 +21748,11 @@ parse_equal(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t previous_bi } pm_node_t *write = parse_write(parser, node, &operator, value.node); - if (PM_REDUCTION_KIND(value) == PM_REDUCTION_STATEMENT) { - /* A shareable constant wraps the write. An invalid call target - * keeps its original arguments and discards the new RHS. */ - if (PM_NODE_TYPE_P(write, PM_SHAREABLE_CONSTANT_NODE)) return pm_reduction(write); - if (PM_NODE_TYPE_P(write, PM_CALL_NODE) && !PM_NODE_FLAG_P(write, PM_CALL_NODE_FLAGS_IMPLICIT_ARRAY)) return pm_reduction(write); + /* An invalid call target keeps its original arguments and discards the new RHS. */ + if (PM_REDUCTION_KIND(value) == PM_REDUCTION_STATEMENT && PM_NODE_TYPE_P(write, PM_CALL_NODE) && !PM_NODE_FLAG_P(write, PM_CALL_NODE_FLAGS_IMPLICIT_ARRAY)) { + return pm_reduction(write); } + value.node = write; return value; } diff --git a/test/prism/errors/shareable_constant_assignment_precedence.txt b/test/prism/errors/shareable_constant_assignment_precedence.txt new file mode 100644 index 0000000000..083ac9bd8c --- /dev/null +++ b/test/prism/errors/shareable_constant_assignment_precedence.txt @@ -0,0 +1,9 @@ +# shareable_constant_value: literal +X = [*b] and X +X = *b and X + ^~~ unexpected 'and', expecting end-of-input + ^~~ unexpected 'and', ignoring it +A::X = 1, 2 or X + ^~ unexpected 'or', expecting end-of-input + ^~ unexpected 'or', ignoring it + From 8331364badc46d6d1e5afca270a189200b88945e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 14:40:58 -0500 Subject: [PATCH 2/8] Allow yield across scope boundaries inside defined? --- src/prism.c | 73 +++++---------------------------------- test/prism/errors_test.rb | 8 +++++ 2 files changed, 16 insertions(+), 65 deletions(-) diff --git a/src/prism.c b/src/prism.c index 2f6c6782ee..c9f36c9542 100644 --- a/src/prism.c +++ b/src/prism.c @@ -18395,70 +18395,6 @@ ensure_valid_retry(pm_parser_t *parser, const pm_node_t *node) { } } -/** - * Ensures that the current yield token is valid in the current context. - */ -static void -ensure_valid_yield(pm_parser_t *parser, const pm_yield_node_t *node) { - for (const pm_context_frame_t *frame = context_top(parser); frame->context != PM_CONTEXT_NONE; frame--) { - switch (frame->context) { - case PM_CONTEXT_DEF: - case PM_CONTEXT_DEF_PARAMS: - case PM_CONTEXT_DEFINED: - // These are the good cases. We're allowed to have a block exit - // in these contexts. - return; - case PM_CONTEXT_CLASS: - case PM_CONTEXT_MAIN: - case PM_CONTEXT_MODULE: - case PM_CONTEXT_SCLASS: - // These are the bad cases. We're not allowed to have a retry in - // these contexts. - pm_parser_err_node(parser, UP(node), PM_ERR_INVALID_YIELD); - return; - case PM_CONTEXT_NONE: - case PM_CONTEXT_MAXIMUM: - // This case should never happen. - PRISM_ASSERT(false && "unreachable"); - break; - case PM_CONTEXT_BEGIN: - case PM_CONTEXT_RESCUE_ELSE: - case PM_CONTEXT_ENSURE: - case PM_CONTEXT_RESCUE: - case PM_CONTEXT_RESCUE_HEADER: - case PM_CONTEXT_BLOCK_BRACES: - case PM_CONTEXT_BLOCK_KEYWORDS: - case PM_CONTEXT_BLOCK_PARAMETERS: - case PM_CONTEXT_CASE_IN: - case PM_CONTEXT_CASE_WHEN: - case PM_CONTEXT_DEFAULT_PARAMS: - case PM_CONTEXT_ELSE: - case PM_CONTEXT_ELSIF: - case PM_CONTEXT_EMBEXPR: - case PM_CONTEXT_FOR_INDEX: - case PM_CONTEXT_FOR: - case PM_CONTEXT_IF: - case PM_CONTEXT_LAMBDA_BRACES: - case PM_CONTEXT_LAMBDA_DO_END: - case PM_CONTEXT_LAMBDA_PARAMETERS: - case PM_CONTEXT_LOOP_PREDICATE: - case PM_CONTEXT_MULTI_TARGET: - case PM_CONTEXT_PARENS: - case PM_CONTEXT_POSTEXE: - case PM_CONTEXT_PREDICATE: - case PM_CONTEXT_PREEXE: - case PM_CONTEXT_RESCUE_MODIFIER: - case PM_CONTEXT_TERNARY: - case PM_CONTEXT_UNLESS: - case PM_CONTEXT_UNTIL: - case PM_CONTEXT_WHILE: - // In these contexts we should continue walking up the list of - // contexts. - break; - } - } -} - /** * Parse a case expression (the `case` keyword). This handles both case-when and * case-in (pattern matching) forms. @@ -20874,7 +20810,14 @@ parse_yield(pm_parser_t *parser, uint8_t flags, uint16_t depth) { } pm_yield_node_t *node = pm_yield_node_create(parser, &keyword, &arguments.opening_loc, pm_arguments_node_create(parser, &arguments), &arguments.closing_loc); - if (!parser->parsing_eval && !parser->partial_script) ensure_valid_yield(parser, node); + + /* A surrounding defined? permits yield even across class and module boundaries. */ + if ( + !parser->parsing_eval && !parser->partial_script && + !pm_parser_context_p(parser, PM_CONTEXT_DEFINED) && !context_def_p(context_top(parser)) + ) { + pm_parser_err_node(parser, UP(node), PM_ERR_INVALID_YIELD); + } return pm_call_reduction(UP(node), &arguments); } diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index a23fdd242c..90814dea68 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -246,6 +246,14 @@ def test_top_level_yield_is_invalid_in_dead_code assert Prism.parse_success?("def f; yield if false; end") end + def test_yield_inside_defined_across_scope_boundaries + ["class C; yield; end", "module M; yield; end", "class << self; yield; end"].each do |body| + errors = Prism.parse("defined?(#{body})\n#{body}").errors + assert_equal [:invalid_yield], errors.map(&:type), body + assert_equal 2, errors.first.location.start_line, body + end + end + def test_endless_method_rescue_rejects_unattached_do_block result = Prism.parse("relay def f = 1 rescue 2 do end") From 1ef5f2413c0d5894f416ee7444f198951e2749a0 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 14:45:01 -0500 Subject: [PATCH 3/8] Parse single-quoted hash labels that span a heredoc --- src/prism.c | 22 +++++++++------------- test/prism/result/heredoc_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/prism.c b/src/prism.c index c9f36c9542..75970ec778 100644 --- a/src/prism.c +++ b/src/prism.c @@ -17097,17 +17097,8 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 content = parser->previous; } - // It is unfortunately possible to have multiple string content - // nodes in a row in the case that there's heredoc content in - // the middle of the string, like this cursed example: - // - // <<-END+'b - // a - // END - // c'+'d' - // - // In that case we need to switch to an interpolated string to - // be able to contain all of the parts. + /* A pending heredoc can split a single-quoted string or label + * into multiple content tokens with separate source locations. */ if (match1(parser, PM_TOKEN_STRING_CONTENT)) { pm_node_list_t parts = { 0 }; pm_node_t *part = UP(pm_string_node_create_unescaped(parser, NULL, &content, NULL, &unescaped)); @@ -17119,8 +17110,13 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 parser_lex(parser); } while (match1(parser, PM_TOKEN_STRING_CONTENT)); - expect1(parser, PM_TOKEN_STRING_END, PM_ERR_STRING_LITERAL_EOF); - node = UP(pm_interpolated_string_node_create(parser, &opening, &parts, &parser->previous)); + if (accept1(parser, PM_TOKEN_LABEL_END)) { + node = UP(pm_interpolated_symbol_node_create(parser, &opening, &opening, &parts, &parser->previous)); + 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)); + } } 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))); if (!label_allowed) pm_parser_err_node(parser, node, PM_ERR_UNEXPECTED_LABEL); diff --git a/test/prism/result/heredoc_test.rb b/test/prism/result/heredoc_test.rb index d78dd7a148..36935962c6 100644 --- a/test/prism/result/heredoc_test.rb +++ b/test/prism/result/heredoc_test.rb @@ -4,6 +4,29 @@ module Prism class HeredocTest < TestCase + def test_quoted_hash_label_spanning_a_heredoc + ["'", '"'].each do |quote| + result = Prism.parse(<<~RUBY) + p(< [], "first\nbody\nA\n second" => %w[first second] }.each do |contents, expected| From 5688d9228bce2576c4f178c4cae91b1bf086c5da Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 14:53:52 -0500 Subject: [PATCH 4/8] Allow a newline after a required keyword parameter in a pattern guard --- src/prism.c | 4 ++-- test/prism/errors_test.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/prism.c b/src/prism.c index 75970ec778..8811c33a3a 100644 --- a/src/prism.c +++ b/src/prism.c @@ -14834,6 +14834,7 @@ parse_parameters( switch (parser->current.type) { case PM_TOKEN_COMMA: + case PM_TOKEN_NEWLINE: case PM_TOKEN_PARENTHESIS_RIGHT: case PM_TOKEN_PIPE: { context_pop(parser); @@ -14846,8 +14847,7 @@ parse_parameters( pm_parameters_keywords_append(parser, ¶ms, param); break; } - case PM_TOKEN_SEMICOLON: - case PM_TOKEN_NEWLINE: { + case PM_TOKEN_SEMICOLON: { context_pop(parser); if (uses_parentheses) { diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index 90814dea68..dd44da7faa 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -403,6 +403,16 @@ def test_pattern_newlines_in_pins_and_guards end end + def test_required_keyword_parameter_before_a_newline_in_a_pattern_guard + result = Prism.parse("case x; in [] if def f(b:\n) end; end") + assert_empty result.errors + parameter = result.statement.conditions.first.pattern.predicate.parameters.keywords.fetch(0) + assert_instance_of RequiredKeywordParameterNode, parameter + assert_equal :b, parameter.name + + refute Prism.parse_success?("case x; in [] if def f(b:\n1) end; end") + end + def test_range_patterns_require_literal_bounds %w[.. ... 1.. 1...].each do |range| result = Prism.parse("case x; in #{range}-2**2; end") From 9feb6fe18667fdba5fff91ae0fec4cce5bc88e93 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 15:15:00 -0500 Subject: [PATCH 5/8] Check encoding when concatenating a heredoc with a string literal --- src/prism.c | 38 +++++++++++++-------- test/prism/encoding/string_encoding_test.rb | 19 +++++++++++ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/prism.c b/src/prism.c index 8811c33a3a..eada061738 100644 --- a/src/prism.c +++ b/src/prism.c @@ -13147,7 +13147,7 @@ static pm_reduction_t parse_expression_infixes(pm_parser_t *parser, pm_reduction_t left, pm_binding_power_t binding_power, uint8_t flags, uint16_t depth); static pm_node_t * -parse_heredoc(pm_parser_t *parser, uint16_t depth); +parse_heredoc(pm_parser_t *parser, uint16_t depth, const pm_encoding_t **trailing_encoding); static pm_node_t * parse_variable(pm_parser_t *parser, const pm_token_t *name); @@ -13199,7 +13199,7 @@ parse_resource_recovery(pm_parser_t *parser, uint16_t depth) { static void parse_discard_token(pm_parser_t *parser, uint16_t depth) { if (match1(parser, PM_TOKEN_HEREDOC_START)) { - (void) parse_heredoc(parser, depth); + (void) parse_heredoc(parser, depth, NULL); } else { parser_lex(parser); } @@ -16925,8 +16925,7 @@ pm_string_concat_encoding(pm_parser_t *parser, const pm_node_t *part, const pm_e * parts. Return false on the first incompatible pair, after finishing the trim. */ static bool -parse_heredoc_dedent(pm_parser_t *parser, pm_node_list_t *nodes, size_t common_whitespace) { - const pm_encoding_t *encoding = NULL; +parse_heredoc_dedent(pm_parser_t *parser, pm_node_list_t *nodes, size_t common_whitespace, const pm_encoding_t **encoding) { bool valid_encoding = true; // The next node should be dedented if it's the first node in the list or if @@ -16940,7 +16939,7 @@ parse_heredoc_dedent(pm_parser_t *parser, pm_node_list_t *nodes, size_t common_w pm_node_t *node; PM_NODE_LIST_FOREACH(nodes, read_index, node) { if (parser->encoding_changed && valid_encoding) { - valid_encoding = pm_string_concat_encoding(parser, node, &encoding) != PM_STRING_CONCAT_INVALID; + valid_encoding = pm_string_concat_encoding(parser, node, encoding) != PM_STRING_CONCAT_INVALID; } // We're not manipulating child nodes that aren't strings. In this case @@ -17041,9 +17040,9 @@ pm_string_parts_to_symbol(pm_parser_t *parser, const pm_token_t *opening, pm_str * Parse a set of strings that could be concatenated together. */ static PRISM_INLINE pm_node_t * -parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint16_t depth) { +parse_strings(pm_parser_t *parser, pm_node_t *current, const pm_encoding_t *current_encoding, bool accepts_label, uint16_t depth) { PRISM_ASSERT(parser->current.type == PM_TOKEN_STRING_BEGIN); - pm_string_parts_t container = { 0 }; + pm_string_parts_t container = { .encoding = current_encoding }; const pm_node_t *current_tail = current; while (match1(parser, PM_TOKEN_STRING_BEGIN)) { @@ -17240,7 +17239,11 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 pm_parser_err_node(parser, current, PM_ERR_STRING_CONCATENATION); } - pm_string_parts_append_operand(parser, &container, current, current_tail, current_tail); + /* A multipart heredoc supplies the encoding of its trailing literal text. */ + if (parser->encoding_changed && container.encoding == NULL && current_tail != NULL) { + pm_string_concat_encoding(parser, current_tail, &container.encoding); + } + pm_node_list_open_append(&parser->node_chunks, &container.list, current); } pm_string_parts_append_operand(parser, &container, node, first_part, last_part); @@ -17645,7 +17648,7 @@ parse_pattern_hash(pm_parser_t *parser, pm_constant_id_set_t *captures, pm_node_ pm_node_t *key; if (match1(parser, PM_TOKEN_STRING_BEGIN)) { - key = parse_strings(parser, NULL, true, (uint16_t) (depth + 1)); + key = parse_strings(parser, NULL, NULL, true, (uint16_t) (depth + 1)); if (PM_NODE_TYPE_P(key, PM_INTERPOLATED_SYMBOL_NODE)) { pm_parser_err_node(parser, key, PM_ERR_PATTERN_HASH_KEY_INTERPOLATED); @@ -19513,7 +19516,7 @@ parse_hash(pm_parser_t *parser, uint16_t depth) { } static pm_node_t * -parse_heredoc(pm_parser_t *parser, uint16_t depth) { +parse_heredoc(pm_parser_t *parser, uint16_t depth, const pm_encoding_t **trailing_encoding) { if (PRISM_UNLIKELY(depth >= PRISM_DEPTH_MAXIMUM || pm_parser_stack_exhausted(parser))) { return parse_resource_recovery(parser, depth); } @@ -19629,10 +19632,14 @@ parse_heredoc(pm_parser_t *parser, uint16_t depth) { pm_node_flag_set(node, PM_INTERPOLATED_STRING_NODE_FLAGS_DEDENTED); } - if (!parse_heredoc_dedent(parser, nodes, common_whitespace) && PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) { + if (!parse_heredoc_dedent(parser, nodes, common_whitespace, &parts.encoding) && PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) { pm_interpolated_string_node_static_flags_clear(node); } } + + if (trailing_encoding != NULL && PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) { + *trailing_encoding = parts.encoding; + } } /* If a missing terminator left this heredoc's lex mode on the stack, it @@ -20844,7 +20851,7 @@ parse_char(pm_parser_t *parser, uint16_t depth) { /* Characters can be followed by strings in which case they are * automatically concatenated. */ if (match1(parser, PM_TOKEN_STRING_BEGIN)) { - return parse_strings(parser, node, false, (uint16_t) (depth + 1)); + return parse_strings(parser, node, NULL, false, (uint16_t) (depth + 1)); } return node; @@ -21053,9 +21060,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, u case PM_TOKEN_METHOD_NAME: return parse_identifier(parser, binding_power, flags, depth); case PM_TOKEN_HEREDOC_START: { - pm_node_t *node = parse_heredoc(parser, depth); + const pm_encoding_t *encoding = NULL; + pm_node_t *node = parse_heredoc(parser, depth, &encoding); if (match1(parser, PM_TOKEN_STRING_BEGIN)) { - return pm_reduction(parse_strings(parser, node, false, (uint16_t) (depth + 1))); + return pm_reduction(parse_strings(parser, node, encoding, false, (uint16_t) (depth + 1))); } return pm_reduction(node); } @@ -21211,7 +21219,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, u case PM_TOKEN_UPLUS: return parse_unary_call(parser, binding_power, flags, depth, "+@", 2); case PM_TOKEN_STRING_BEGIN: - return pm_reduction(parse_strings(parser, NULL, flags & PM_PARSE_ACCEPTS_LABEL, (uint16_t) (depth + 1))); + return pm_reduction(parse_strings(parser, NULL, NULL, flags & PM_PARSE_ACCEPTS_LABEL, (uint16_t) (depth + 1))); case PM_TOKEN_SYMBOL_BEGIN: return pm_reduction(parse_symbol(parser, PM_LEX_STATE_END, (uint16_t) (depth + 1))); default: { diff --git a/test/prism/encoding/string_encoding_test.rb b/test/prism/encoding/string_encoding_test.rb index bbac148c22..20badaf6b9 100644 --- a/test/prism/encoding/string_encoding_test.rb +++ b/test/prism/encoding/string_encoding_test.rb @@ -176,6 +176,25 @@ def test_encoding_at_heredoc_interpolation assert_equal unicode, result.errors.first.location.slice end + def test_encoding_between_a_heredoc_and_an_adjacent_string + encoding = ENV["PRISM_BUILD_MINIMAL"] ? "ASCII-8BIT" : "Windows-31J" + unicode = '#{?\u3042}' + suffix = '"\xA2"' + + [["H", unicode], ["-H", unicode], ["~H", unicode], ["~H", " #{unicode}"]].each do |opening, body| + source = "<<#{opening}#{suffix}\n#{body}\nH\n" + result = Prism.parse(source, encoding: encoding) + assert_equal ["string literal encodings differ (UTF-8 / #{encoding})"], result.errors.map(&:message), source + assert_equal suffix, result.errors.first.location.slice + end + + source = "< Date: Wed, 2 Dec 2026 15:22:34 -0500 Subject: [PATCH 6/8] Check the encoding of adjacent string literals inside interpolation --- src/prism.c | 36 ++++++++++++++++++--- test/prism/encoding/string_encoding_test.rb | 20 ++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/prism.c b/src/prism.c index eada061738..2a8fb8b529 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16892,6 +16892,35 @@ typedef enum { PM_STRING_CONCAT_INVALID } pm_string_concat_result_t; +static PRISM_INLINE const pm_encoding_t * +pm_string_non_ascii_encoding(const pm_string_node_t *string, const pm_encoding_t *source_encoding) { + return pm_string_ascii_only_p(&string->unescaped) ? NULL : pm_string_node_encoding(string, source_encoding); +} + +/* Adjacent plain strings inside an interpolation fold into one literal. + * A nested interpolation, even of a literal, starts a separate run. */ +static bool +pm_string_concat_literal_encoding(const pm_node_t *value, const pm_encoding_t *source_encoding, const pm_encoding_t **encoding) { + if (value == NULL) return false; + if (PM_NODE_TYPE_P(value, PM_STRING_NODE)) { + *encoding = pm_string_non_ascii_encoding((const pm_string_node_t *) value, source_encoding); + return true; + } + if (!PM_NODE_TYPE_P(value, PM_INTERPOLATED_STRING_NODE)) return false; + const pm_interpolated_string_node_t *concat = (const pm_interpolated_string_node_t *) value; + if (concat->opening_loc.length != 0) return false; + + const pm_node_t *node; + PM_NODE_LIST_FOREACH(&concat->parts, index, node) { + if (!PM_NODE_TYPE_P(node, PM_STRING_NODE)) return false; + const pm_encoding_t *next = pm_string_non_ascii_encoding((const pm_string_node_t *) node, source_encoding); + /* An incompatible pair was diagnosed while parsing the concatenation. */ + if (!pm_encoding_concat(encoding, next)) *encoding = NULL; + } + + return true; +} + /* Check one operand in a run of literal string parts. */ static pm_string_concat_result_t pm_string_concat_encoding(pm_parser_t *parser, const pm_node_t *part, const pm_encoding_t **left) { @@ -16900,14 +16929,13 @@ pm_string_concat_encoding(pm_parser_t *parser, const pm_node_t *part, const pm_e value = pm_statements_node_single_expression_unwrap(((const pm_embedded_statements_node_t *) part)->statements); } - if (value == NULL || !PM_NODE_TYPE_P(value, PM_STRING_NODE)) { + const pm_encoding_t *right = NULL; + if (!pm_string_concat_literal_encoding(value, parser->encoding, &right)) { *left = NULL; return PM_STRING_CONCAT_SIGNIFICANT; } - const pm_string_node_t *string = (const pm_string_node_t *) value; - const pm_encoding_t *right = pm_string_node_encoding(string, parser->encoding); - if (pm_string_ascii_only_p(&string->unescaped)) return PM_STRING_CONCAT_NEUTRAL; + if (right == NULL) return PM_STRING_CONCAT_NEUTRAL; if (pm_encoding_concat(left, right)) return PM_STRING_CONCAT_SIGNIFICANT; pm_diagnostic_list_append_slice_slice( diff --git a/test/prism/encoding/string_encoding_test.rb b/test/prism/encoding/string_encoding_test.rb index 20badaf6b9..eefab9969e 100644 --- a/test/prism/encoding/string_encoding_test.rb +++ b/test/prism/encoding/string_encoding_test.rb @@ -143,6 +143,26 @@ def test_incompatible_encodings_in_string_interpolation assert_equal '\u3042', errors.first.location.slice end + def test_encoding_of_adjacent_string_literals_inside_interpolation + [ + ['"\xcd#{"\u3042"""}"', "ASCII-8BIT / UTF-8", '#{"\u3042"""}'], + ['"#{"" "ascii" "\u3042"}\xcd"', "UTF-8 / ASCII-8BIT", '\xcd'], + ['"\xcd#{"a" "b"}\u3042"', "ASCII-8BIT / UTF-8", '\u3042'] + ].each do |source, encodings, location| + errors = Prism.parse(source, encoding: "US-ASCII").errors + assert_equal ["string literal encodings differ (#{encodings})"], errors.map(&:message), source + assert_equal location, errors.first.location.slice + end + + ['"\xcd#{"\u3042" "#{value}"}"', '"\xcd#{"" "#{"\u3042"}"}"', '"\xcd#{"ascii" "\xff"}"'].each do |source| + assert_predicate Prism.parse(source, encoding: "US-ASCII"), :success?, source + end + + source = '"\xcd#{"\u3042" "\xcd" "\u3042"}"' + errors = Prism.parse(source, encoding: "US-ASCII").errors + assert_equal ['"\xcd"', '#{"\u3042" "\xcd" "\u3042"}'], errors.map { |error| error.location.slice } + end + def test_encoding_at_heredoc_interpolation unicode = '#{?\u3042}' raw = "\x9Da".b From 08b1fe0c1a5a4f5bab92b6923cc1568539d51b1d Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 16:07:40 -0500 Subject: [PATCH 7/8] Cache concatenated string encodings on Prism nodes --- include/prism/internal/node.h | 11 +++ src/prism.c | 80 ++++++++++++++------- templates/lib/prism/node.rb.erb | 3 +- test/prism/encoding/string_encoding_test.rb | 10 +++ test/prism/result/equality_test.rb | 8 +++ 5 files changed, 85 insertions(+), 27 deletions(-) diff --git a/include/prism/internal/node.h b/include/prism/internal/node.h index ec86c8721c..ee577c55f3 100644 --- a/include/prism/internal/node.h +++ b/include/prism/internal/node.h @@ -66,6 +66,17 @@ static const pm_node_flags_t PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE = (( /* Dedenting a heredoc folds interpolated string literals into its text. */ static const pm_node_flags_t PM_INTERPOLATED_STRING_NODE_FLAGS_DEDENTED = ((PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 2); +/* Adjacent plain strings contribute one encoding when interpolated. Store the + * result of checking their parts on the concatenation. Zero means it is unknown. */ +enum { + PM_STRING_CONCAT_ENCODING_ASCII_ONLY = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 3, + PM_STRING_CONCAT_ENCODING_SOURCE = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 4, + PM_STRING_CONCAT_ENCODING_UTF8 = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 5, + PM_STRING_CONCAT_ENCODING_BINARY = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 6, + PM_STRING_CONCAT_ENCODING_MASK = PM_STRING_CONCAT_ENCODING_ASCII_ONLY | PM_STRING_CONCAT_ENCODING_SOURCE | + PM_STRING_CONCAT_ENCODING_UTF8 | PM_STRING_CONCAT_ENCODING_BINARY +}; + /* * 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/src/prism.c b/src/prism.c index 2a8fb8b529..6a5490174b 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16892,33 +16892,38 @@ typedef enum { PM_STRING_CONCAT_INVALID } pm_string_concat_result_t; -static PRISM_INLINE const pm_encoding_t * -pm_string_non_ascii_encoding(const pm_string_node_t *string, const pm_encoding_t *source_encoding) { - return pm_string_ascii_only_p(&string->unescaped) ? NULL : pm_string_node_encoding(string, source_encoding); -} - -/* Adjacent plain strings inside an interpolation fold into one literal. - * A nested interpolation, even of a literal, starts a separate run. */ static bool pm_string_concat_literal_encoding(const pm_node_t *value, const pm_encoding_t *source_encoding, const pm_encoding_t **encoding) { - if (value == NULL) return false; + if (value == NULL) { + return false; + } + if (PM_NODE_TYPE_P(value, PM_STRING_NODE)) { - *encoding = pm_string_non_ascii_encoding((const pm_string_node_t *) value, source_encoding); + const pm_string_node_t *string = (const pm_string_node_t *) value; + *encoding = pm_string_ascii_only_p(&string->unescaped) ? NULL : pm_string_node_encoding(string, source_encoding); return true; } - if (!PM_NODE_TYPE_P(value, PM_INTERPOLATED_STRING_NODE)) return false; - const pm_interpolated_string_node_t *concat = (const pm_interpolated_string_node_t *) value; - if (concat->opening_loc.length != 0) return false; - const pm_node_t *node; - PM_NODE_LIST_FOREACH(&concat->parts, index, node) { - if (!PM_NODE_TYPE_P(node, PM_STRING_NODE)) return false; - const pm_encoding_t *next = pm_string_non_ascii_encoding((const pm_string_node_t *) node, source_encoding); - /* An incompatible pair was diagnosed while parsing the concatenation. */ - if (!pm_encoding_concat(encoding, next)) *encoding = NULL; + if (!PM_NODE_TYPE_P(value, PM_INTERPOLATED_STRING_NODE)) { + return false; } - return true; + switch (value->flags & PM_STRING_CONCAT_ENCODING_MASK) { + case PM_STRING_CONCAT_ENCODING_ASCII_ONLY: + *encoding = NULL; + return true; + case PM_STRING_CONCAT_ENCODING_SOURCE: + *encoding = source_encoding; + return true; + case PM_STRING_CONCAT_ENCODING_UTF8: + *encoding = PM_ENCODING_UTF_8_ENTRY; + return true; + case PM_STRING_CONCAT_ENCODING_BINARY: + *encoding = PM_ENCODING_ASCII_8BIT_ENTRY; + return true; + default: + return false; + } } /* Check one operand in a run of literal string parts. */ @@ -17072,6 +17077,7 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, const pm_encoding_t *curr PRISM_ASSERT(parser->current.type == PM_TOKEN_STRING_BEGIN); pm_string_parts_t container = { .encoding = current_encoding }; const pm_node_t *current_tail = current; + bool plain_literals = true; while (match1(parser, PM_TOKEN_STRING_BEGIN)) { pm_node_t *node = NULL; @@ -17254,17 +17260,23 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, const pm_encoding_t *curr // Otherwise we need to check the type of the node we just parsed. // If it cannot be concatenated with the previous node, then we'll // need to add a syntax error. - if (!PM_NODE_TYPE_P(node, PM_STRING_NODE) && !PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) { - pm_parser_err_node(parser, node, PM_ERR_STRING_CONCATENATION); - first_part = NULL; - last_part = NULL; + if (!PM_NODE_TYPE_P(node, PM_STRING_NODE)) { + plain_literals = false; + if (!PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) { + pm_parser_err_node(parser, node, PM_ERR_STRING_CONCATENATION); + first_part = NULL; + last_part = NULL; + } } // If we haven't already started the parts of our container for // concatenation, we'll do that now. if (container.list.size == 0) { - if (!PM_NODE_TYPE_P(current, PM_STRING_NODE) && !PM_NODE_TYPE_P(current, PM_INTERPOLATED_STRING_NODE)) { - pm_parser_err_node(parser, current, PM_ERR_STRING_CONCATENATION); + if (!PM_NODE_TYPE_P(current, PM_STRING_NODE)) { + plain_literals = false; + if (!PM_NODE_TYPE_P(current, PM_INTERPOLATED_STRING_NODE)) { + pm_parser_err_node(parser, current, PM_ERR_STRING_CONCATENATION); + } } /* A multipart heredoc supplies the encoding of its trailing literal text. */ @@ -17279,7 +17291,23 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, const pm_encoding_t *curr } if (container.list.size > 0) { - return pm_string_parts_to_string(parser, NULL, &container, NULL); + pm_node_t *node = pm_string_parts_to_string(parser, NULL, &container, NULL); + + if (parser->encoding_changed && plain_literals) { + pm_node_flags_t flag = PM_STRING_CONCAT_ENCODING_SOURCE; + + if (container.encoding == NULL) { + flag = PM_STRING_CONCAT_ENCODING_ASCII_ONLY; + } else if (container.encoding == PM_ENCODING_UTF_8_ENTRY) { + flag = PM_STRING_CONCAT_ENCODING_UTF8; + } else if (container.encoding == PM_ENCODING_ASCII_8BIT_ENTRY) { + flag = PM_STRING_CONCAT_ENCODING_BINARY; + } + + pm_node_flag_set(node, flag); + } + + return node; } return current; diff --git a/templates/lib/prism/node.rb.erb b/templates/lib/prism/node.rb.erb index b4f3521edb..c23358e35f 100644 --- a/templates/lib/prism/node.rb.erb +++ b/templates/lib/prism/node.rb.erb @@ -691,7 +691,8 @@ module Prism (<%= field.name %>.length == other.<%= field.name %>.length) && <%= field.name %>.zip(other.<%= field.name %>).all? { |left, right| left === right }<%= " &&" if index != fields.length - 1 %> <%- elsif field.is_a?(Prism::Template::Flags) -%> - (flags === other.flags)<%= " &&" if index != fields.length - 1 %> + <%- public_flags_mask = (1 << (Prism::Template::COMMON_FLAGS_COUNT + field.values.length)) - 1 -%> + ((flags & <%= public_flags_mask %>) == (other.flags & <%= public_flags_mask %>))<%= " &&" if index != fields.length - 1 %> <%- else -%> (<%= field.name %> === other.<%= field.name %>)<%= " &&" if index != fields.length - 1 %> <%- end -%> diff --git a/test/prism/encoding/string_encoding_test.rb b/test/prism/encoding/string_encoding_test.rb index eefab9969e..e3e8a89ecb 100644 --- a/test/prism/encoding/string_encoding_test.rb +++ b/test/prism/encoding/string_encoding_test.rb @@ -163,6 +163,16 @@ def test_encoding_of_adjacent_string_literals_inside_interpolation assert_equal ['"\xcd"', '#{"\u3042" "\xcd" "\u3042"}'], errors.map { |error| error.location.slice } end + def test_concatenation_encodings_survive_until_heredoc_dedent + first = '#{"\u3042" ""}' + middle = '#{"a" "b"}' * 32 + last = '#{"" "\xcd"}' + source = "<<~H\n #{first}#{middle}#{last}\nH\n" + errors = Prism.parse(source, encoding: "US-ASCII").errors + assert_equal ["string literal encodings differ (UTF-8 / ASCII-8BIT)"], errors.map(&:message) + assert_equal last, errors.first.location.slice + end + def test_encoding_at_heredoc_interpolation unicode = '#{?\u3042}' raw = "\x9Da".b diff --git a/test/prism/result/equality_test.rb b/test/prism/result/equality_test.rb index 4f6e665a88..f91ccdb65d 100644 --- a/test/prism/result/equality_test.rb +++ b/test/prism/result/equality_test.rb @@ -18,5 +18,13 @@ def test_equality complex_source_2 = "class Something; @var = something.else { _2 }; end" refute_operator Prism.parse_statement(complex_source_1), :===, Prism.parse_statement(complex_source_2) end + + def test_equality_ignores_cached_concatenation_encoding + source = '"a" "b"' + utf8 = Prism.parse_statement(source, encoding: "UTF-8") + ascii = Prism.parse_statement(source, encoding: "US-ASCII") + + assert_operator utf8, :===, ascii + end end end From 222f822a0655031c54e73cab18169e5560ca64f9 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 2 Dec 2026 16:26:04 -0500 Subject: [PATCH 8/8] Preserve command-argument state in conditional branches --- src/prism.c | 8 +------- test/prism/errors_test.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/prism.c b/src/prism.c index 6a5490174b..2ecf1f7ea2 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16180,10 +16180,9 @@ parse_conditional(pm_parser_t *parser, pm_context_t context, size_t opening_newl pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_COMPOSITION, context, &then_keyword, (uint16_t) (depth + 1)); pm_statements_node_t *statements = NULL; + /* Conditional branches inherit the enclosing command-argument state for `do`. */ if (!match3(parser, PM_TOKEN_KEYWORD_ELSIF, PM_TOKEN_KEYWORD_ELSE, PM_TOKEN_KEYWORD_END)) { - pm_accepts_block_stack_push(parser, true); statements = parse_statements(parser, context, (uint16_t) (depth + 1)); - pm_accepts_block_stack_pop(parser); accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); } @@ -16216,10 +16215,7 @@ parse_conditional(pm_parser_t *parser, pm_context_t context, size_t opening_newl parser_lex(parser); pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_COMPOSITION, PM_CONTEXT_ELSIF, &then_keyword, (uint16_t) (depth + 1)); - pm_accepts_block_stack_push(parser, true); - pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_ELSIF, (uint16_t) (depth + 1)); - pm_accepts_block_stack_pop(parser); accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); pm_node_t *elsif = UP(pm_if_node_create(parser, &elsif_keyword, predicate, NTOK2PTR(then_keyword), statements, NULL, NULL)); @@ -16235,9 +16231,7 @@ parse_conditional(pm_parser_t *parser, pm_context_t context, size_t opening_newl parser_lex(parser); pm_token_t else_keyword = parser->previous; - pm_accepts_block_stack_push(parser, true); pm_statements_node_t *else_statements = parse_statements(parser, PM_CONTEXT_ELSE, (uint16_t) (depth + 1)); - pm_accepts_block_stack_pop(parser); accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); parser_warn_indentation_mismatch(parser, opening_newline_index, &else_keyword, false, false); diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index dd44da7faa..fb54f6e5b8 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -261,6 +261,33 @@ def test_endless_method_rescue_rejects_unattached_do_block assert_equal "do end", result.errors.first.location.slice end + def test_command_blocks_in_conditional_branches_inside_command_arguments + command = "dispatch build () {} do work end" + branches = ["if flag;", "if flag; elsif other;", "if flag; else", "unless flag;", "unless flag; else"] + branches.each do |branch| + source = "outer case value; in Integer; else #{branch} #{command}; end; end" + result = Prism.parse(source) + assert_predicate result, :success?, source + + assert_nil result.statement.block + dispatch = result.statement.breadth_first_search { |node| node.is_a?(CallNode) && node.name == :dispatch } + assert_equal "do work end", dispatch.block.slice + assert_equal "{}", dispatch.arguments.arguments.first.block.slice + end + + refute Prism.parse_success?("if flag; #{command}; end") + end + + def test_plain_do_blocks_in_a_conditional_command_argument + assert Prism.parse_success?("outer :first, if flag; work do end; end") + assert Prism.parse_success?("outer(:first, if flag; items.each do end; end)") + + ["items.each", "run()", "items[0]"].each do |call| + source = "outer :first, if flag; #{call} do end; end" + refute Prism.parse_success?(source), source + end + end + def test_command_block_in_a_loop_header_inside_command_arguments ["while", "until", "for i in"].each do |header| source = "outer value = #{header} check def task = inner item {} do work end do loop_body end"