Skip to content

Return a frozen string from Symbol#to_s (Feature #22137) - #10117

Draft
zev wants to merge 1 commit into
jruby:ruby-4.1from
zev:symbol-to-s-frozen
Draft

zev wants to merge 1 commit into
jruby:ruby-4.1from
zev:symbol-to-s-frozen

Conversation

@zev

@zev zev commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Ruby 4.1 makes Symbol#to_s return a frozen string (Feature #22137, CRuby commit 2c574ca7e2). It returns the symbol's own name string, the same object as Symbol#name. id2name and name are now aliases of to_s. JRuby still returned a new chilled string that warns when mutated.

Call CRuby 4.1 JRuby before JRuby after
:a.to_s.frozen? true false true
:a.to_s.equal?(:a.name) true false true
:a.to_s << "b" FrozenError "ab" (deprecation warning with -W:deprecated) FrozenError
String(:a).frozen? true false true
Symbol.instance_method(:name) #<UnboundMethod: Symbol#name(to_s)()> #<UnboundMethod: Symbol#name()> #<UnboundMethod: Symbol#name(to_s)()>
Symbol.instance_method(:id2name) == Symbol.instance_method(:to_s) true false true
"#{:a}".frozen? false false false

Changes

  • RubySymbol#to_s returns the symbol's frozen fstring, and id2name and name are declared as its aliases. The Java methods name(ThreadContext) and id2name(ThreadContext) are kept and delegate to to_s.
  • Strings from Symbol#to_s are no longer chilled, so the CHILLED_SYMBOL_TO_S flag goes away, as it does in CRuby. This removes StringSupport.CHILLED_SYMBOL_TO_S, the deprecated and unused ObjectFlags.CHILLED_SYMBOL_TO_S_F, RubyString#chill_symbol_string(), RubyString#isChilledLiteral() and the "string returned by :x.to_s will be frozen in the future" warning. Chilled string literals work as before.

Testing

  • spec/ruby/core/symbol/to_s_spec.rb, id2name_spec.rb, name_spec.rb and spec/ruby/core/string/chilled_string_spec.rb already match CRuby. They pass on JRuby (interpreter and JIT), CRuby 4.1-dev and CRuby 4.0.7.
  • Removed spec/tags/ruby/core/symbol/id2name_tags.txt. Its "is an alias of Symbol#to_s" spec now passes. Its other two entries were for specs that no longer exist.
  • spec/ruby/core/symbol and spec/ruby/core/string: the only change from before is that "Symbol#to_s returns a frozen string" now passes.
  • The fast specs (:fast target of spec/jruby.mspec): no new failures.
  • test/mri/ruby/test_symbol.rb and test_string.rb: no failures, as before.
  • JRuby's own Ruby code (core/src/main/ruby and the tracked files in lib/ruby/stdlib) does not mutate strings returned by Symbol#to_s. I also checked the Java callers of RubySymbol#to_s and asString(). They only read the string or copy it first.

Not in this PR

  • RubySymbol#to_s(Ruby) and RubySymbol#asString() now return the shared frozen string too, as rb_sym_to_s does in CRuby. Java code that writes to the ByteList of that result directly, without a frozen check, would change the symbol's name. I found no such caller in core.

🤖 Generated with Claude Code

Ruby 4.1 makes Symbol#to_s return the symbol's frozen name string, the
same object Symbol#name returns, and makes id2name and name aliases of
to_s [Feature #22137]. Since strings returned by Symbol#to_s are no
longer chilled, the CHILLED_SYMBOL_TO_S flag and its deprecation
warning are removed, as in CRuby commit 2c574ca7e2. Chilled string
literals are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant