Commit 9c0a7b1d
9c0a7b1db31aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Frozen-literal raise for in-place string mutators
An in-place string mutator (insert, prepend, <<, concat, replace, clear, delete_prefix!, delete_suffix!) applied to a frozen string literal now raises FrozenError instead of falling through to the unresolved-call error. Mutable String.new receivers still mutate via reassignment.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 9c0a7b1db31a
- Parent
- 37f49719f32f
src/codegen.cmodifiedView file
@@ -3917,6 +3917,12 @@
39173917
}39183918
return 1;39193919
}3920+
/* `<<` onto a frozen string literal raises FrozenError */3921+
if (rty && !strcmp(rty, "StringNode")) {3922+
emit_indent(b, indent);3923+
buf_puts(b, "sp_raise_cls(\"FrozenError\", \"can't modify frozen String\");\n");3924+
return 1;3925+
}39203926
return 0;39213927
}39223928
@@ -3948,6 +3954,15 @@
39483954
if (rt == TY_STRING) {39493955
const char *rty = nt_type(nt, recv);39503956
int assignable = rty && (!strcmp(rty, "LocalVariableReadNode") || !strcmp(rty, "InstanceVariableReadNode") || !strcmp(rty, "SelfNode"));3957+
/* an in-place mutator on a frozen string literal raises FrozenError */3958+
if (rty && !strcmp(rty, "StringNode") &&3959+
(!strcmp(name, "insert") || !strcmp(name, "prepend") || !strcmp(name, "<<") ||3960+
!strcmp(name, "concat") || !strcmp(name, "replace") || !strcmp(name, "clear") ||3961+
!strcmp(name, "delete_prefix!") || !strcmp(name, "delete_suffix!"))) {3962+
emit_indent(b, indent);3963+
buf_puts(b, "sp_raise_cls(\"FrozenError\", \"can't modify frozen String\");\n");3964+
return 1;3965+
}39513966
if (assignable && !strcmp(name, "replace") && argc == 1) {39523967
emit_indent(b, indent); emit_expr(c, recv, b); buf_puts(b, " = "); emit_expr(c, argv[0], b); buf_puts(b, ";\n");39533968
return 1;