Skip to content

Commit 2459cdd

Browse files
committed
Fix encodings check for embedded into Regexp String literals
1 parent 3b0a28c commit 2459cdd

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/main/java/org/truffleruby/parser/YARPTranslator.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,10 +2400,20 @@ public RubyNode visitInterpolatedRegularExpressionNode(Nodes.InterpolatedRegular
24002400
}
24012401

24022402
for (ToSNode child : children) {
2403-
// assume string fragments are represented with FrozenStringLiteralNode and not StringLiteralNode
2404-
if (child.getValueNode() instanceof FrozenStringLiteralNode frozenStringLiteralNode) {
2405-
ImmutableRubyString frozenString = frozenStringLiteralNode.getFrozenString();
2406-
var fragment = new TStringWithEncoding(frozenString.tstring, frozenString.encoding);
2403+
// Expect that String fragments are represented either with FrozenStringLiteralNode or StringLiteralNode.
2404+
// StringLiteralNode is possible in the following case: /a #{ "b" } c/
2405+
if (child.getValueNode() instanceof FrozenStringLiteralNode ||
2406+
child.getValueNode() instanceof StringLiteralNode) {
2407+
final TStringWithEncoding fragment;
2408+
2409+
if (child.getValueNode() instanceof FrozenStringLiteralNode frozenStringLiteralNode) {
2410+
ImmutableRubyString frozenString = frozenStringLiteralNode.getFrozenString();
2411+
fragment = new TStringWithEncoding(frozenString.tstring, frozenString.encoding);
2412+
} else if (child.getValueNode() instanceof StringLiteralNode stringLiteralNode) {
2413+
fragment = new TStringWithEncoding(stringLiteralNode.getTString(), stringLiteralNode.getEncoding());
2414+
} else {
2415+
throw CompilerDirectives.shouldNotReachHere();
2416+
}
24072417

24082418
try {
24092419
// MRI: reg_fragment_check

0 commit comments

Comments
 (0)