diff options
author | Jean byroot Boussier <[email protected]> | 2024-11-13 15:20:00 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-11-13 09:20:00 -0500 |
commit | 6deeec5d459ecff5ec4628523b14ac7379fd942e (patch) | |
tree | 1815b684da615359165055c3a12df4dca45a1ae7 /string.c | |
parent | 37a16c7812f5b7e6faa762b927e9f04065cc495a (diff) |
Mark strings returned by Symbol#to_s as chilled (#12065)
* Use FL_USER0 for ELTS_SHARED
This makes space in RString for two bits for chilled strings.
* Mark strings returned by `Symbol#to_s` as chilled
[Feature #20350]
`STR_CHILLED` now spans on two user flags. If one bit is set it
marks a chilled string literal, if it's the other it marks a
`Symbol#to_s` chilled string.
Since it's not possible, and doesn't make much sense to include
debug info when `--debug-frozen-string-literal` is set, we can't
include allocation source, but we can safely include the symbol
name in the warning message, making it much easier to find the source
of the issue.
Co-Authored-By: Étienne Barrié <[email protected]>
---------
Co-authored-by: Étienne Barrié <[email protected]>
Co-authored-by: Jean Boussier <[email protected]>
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -80,15 +80,19 @@ VALUE rb_cSymbol; /* Flags of RString * + * 0: STR_SHARED (equal to ELTS_SHARED) + * The string is shared. The buffer this string points to is owned by + * another string (the shared root). * 1: RSTRING_NOEMBED * The string is not embedded. When a string is embedded, the contents * follow the header. When a string is not embedded, the contents is * on a separately allocated buffer. - * 2: STR_SHARED (equal to ELTS_SHARED) - * The string is shared. The buffer this string points to is owned by - * another string (the shared root). - * 3: STR_CHILLED (will be frozen in a future version) - * The string appears frozen but can be mutated with a warning. + * 2: STR_CHILLED_LITERAL (will be frozen in a future version) + * The string was allocated as a literal in a file without an explicit `frozen_string_literal` comment. + * It emits a deprecation warning when mutated for the first time. + * 3: STR_CHILLED_SYMBOL_TO_S (will be frozen in a future version) + * The string was allocated by the `Symbol#to_s` method. + * It emits a deprecation warning when mutated for the first time. * 4: STR_PRECOMPUTED_HASH * The string is embedded and has its precomputed hascode stored * after the terminator. @@ -1959,7 +1963,7 @@ rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str, bool chil str_duplicate_setup_heap(klass, str, new_str); } if (chilled) { - STR_CHILL_RAW(new_str); + FL_SET_RAW(new_str, STR_CHILLED_LITERAL); } return new_str; } @@ -1970,7 +1974,7 @@ rb_str_with_debug_created_info(VALUE str, VALUE path, int line) VALUE debug_info = rb_ary_new_from_args(2, path, INT2FIX(line)); if (OBJ_FROZEN_RAW(str)) str = rb_str_dup(str); rb_ivar_set(str, id_debug_created_info, rb_ary_freeze(debug_info)); - STR_CHILL_RAW(str); + FL_SET_RAW(str, STR_CHILLED_LITERAL); return rb_str_freeze(str); } @@ -12142,7 +12146,9 @@ sym_inspect(VALUE sym) VALUE rb_sym_to_s(VALUE sym) { - return str_new_shared(rb_cString, rb_sym2str(sym)); + VALUE str = str_new_shared(rb_cString, rb_sym2str(sym)); + FL_SET_RAW(str, STR_CHILLED_SYMBOL_TO_S); + return str; } VALUE |