Project

General

Profile

Actions

Feature #22137

closed

Change `Symbol#to_s` to return frozen strings

Feature #22137: Change `Symbol#to_s` to return frozen strings
2

Added by byroot (Jean Boussier) 23 days ago. Updated 13 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:125891]

Description

Since Ruby 3.4 ([Feature #20350]) Symbol#to_s returns a chilled string, meaning mutating it emits a deprecation warning.

I believe it's now time to act on this deprecation, I'd like to request that on Ruby 4.1, Symbol#to_s always return a frozen string.

Several very large codebases include Shopify and GitHub have been running with Symbol.alias_method(:to_s, :name) without any compatibility issues whatsoever.

Making this change would allow to progressively get rid of an annoying pattern in performance sensitive code:

arg.is_a?(Symbol) ? arg.name : arg.to_s

Related issues 1 (0 open1 closed)

Related to Ruby - Feature #20350: Return chilled string from Symbol#to_sClosedActions

Updated by byroot (Jean Boussier) 23 days ago Actions #1

  • Related to Feature #20350: Return chilled string from Symbol#to_s added

Updated by headius (Charles Nutter) 23 days ago Actions #2 [ruby-core:125897]

Related discussions in https://bugs.ruby-lang.org/issues/16150 where I proposed a way to request frozen strings from to_s. I've always been +1 for all immediate values that return the same string content every time to return frozen strings.

This has shown up as a problem in performance profiles of JRuby apps for decades.

Updated by matz (Yukihiro Matsumoto) 14 days ago Actions #3 [ruby-core:126022]

Accepted.

This is the planned conclusion of the chilled string deprecation introduced in 3.4 (#20350). Two release cycles of warnings, plus the field experience from large codebases running with the frozen behavior, give me enough confidence that the compatibility impact is small.

Matz.

Updated by nobu (Nobuyoshi Nakada) 13 days ago Actions #4 [ruby-core:126031]

I'd suggest to rename STR_CHILLED_LITERAL as STR_CHILLED simply.
We shouldn't introduce any more ad-hoc flags such _TO_S.

Updated by byroot (Jean Boussier) 13 days ago Actions #5

  • Status changed from Open to Closed

Applied in changeset git|105ee02159ceac96df019b7da7067254a072377e.


[ruby/stringio] Update str_chilled_p for Ruby 4.1
(https://github.com/ruby/stringio/pull/219)

Ref: https://github.com/ruby/ruby/pull/17781
[Feature #22137]

The only cause for chilled is now literals.
STR_CHILLED_SYMBOL_TO_S has been removed because Symbol#to_s always
return a frozen string.

https://github.com/ruby/stringio/commit/5915b45c40

Actions

Also available in: PDF Atom