Skip to content

Commit 9aea689

Browse files
committed
Clear flags on interpolated strings
When inner strings aren't frozen, we need to clear the flags on interpolated string nodes so that we don't emit wrong instructions. The compiler is currently incorrectly emitting frozen strings because the parser is erroneously declaring interpolated strings as "frozen". We need to fix this behavior in the parser so we can fix the compiler in CRuby. This patch is a partial fix for [this bug](https://bugs.ruby-lang.org/issues/21187)
1 parent e5884cd commit 9aea689

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

src/prism.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5279,6 +5279,10 @@ pm_interpolated_string_node_append(pm_interpolated_string_node_t *node, pm_node_
52795279

52805280
switch (PM_NODE_TYPE(part)) {
52815281
case PM_STRING_NODE:
5282+
// If inner string is not frozen, clear flags for this string
5283+
if (!PM_NODE_FLAG_P(part, PM_STRING_FLAGS_FROZEN)) {
5284+
CLEAR_FLAGS(node);
5285+
}
52825286
part->flags = (pm_node_flags_t) ((part->flags | PM_NODE_FLAG_STATIC_LITERAL | PM_STRING_FLAGS_FROZEN) & ~PM_STRING_FLAGS_MUTABLE);
52835287
break;
52845288
case PM_INTERPOLATED_STRING_NODE:

test/prism/result/static_literals_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
module Prism
66
class StaticLiteralsTest < TestCase
7+
def test_concatenanted_string_literal_is_not_static
8+
node = Prism.parse_statement("'a' 'b'")
9+
refute_predicate node, :static_literal?
10+
end
11+
712
def test_static_literals
813
assert_warning("1")
914
assert_warning("0xA", "10", "10")

test/prism/result/string_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ def test_xstring_node_unescaped_frozen
2828
node = Prism.parse_statement("`foo`")
2929
assert_predicate node.unescaped, :frozen?
3030
end
31+
32+
def test_string_literal_concatenation
33+
node = Prism.parse_statement("`foo`")
34+
assert_predicate node.unescaped, :frozen?
35+
end
3136
end
3237
end

0 commit comments

Comments
 (0)