Skip to content

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

Merged
headius merged 3 commits into
jruby:ruby-4.1from
zev:symbol-to-s-frozen
Dec 3, 2026
Merged

headius merged 3 commits 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

@headius

headius commented Nov 21, 2026

Copy link
Copy Markdown
Member

@zev There's a few of these PRs still in draft and I'm not sure what your intent is. If they're ready to be reviewed and merged, move out of draft and request a review. These are mostly pretty simple things so I assume it was just an oversight.

@headius headius added this to the JRuby 10.2.0.0 milestone Nov 21, 2026
@zev
zev force-pushed the symbol-to-s-frozen branch from 27160cc to 1bc2e4f Compare November 24, 2026 03:36

@zev zev left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note on the removed members. CRuby removed the chilled Symbol#to_s state entirely in ruby/ruby@2c574ca7e2 (STR_CHILLED_SYMBOL_TO_S is gone), since Symbol#to_s now returns the frozen name string. Nothing sets these anymore, so I removed them too:

  • StringSupport.CHILLED_SYMBOL_TO_S (public)
  • ObjectFlags.CHILLED_SYMBOL_TO_S_F (public, already @Deprecated(since = "10.0.3.0"))
  • RubyString#chill_symbol_string() (public) and isChilledLiteral() (protected)

@headius if you'd rather keep them for API compatibility, I can restore them as deprecated no-ops instead.

@zev
zev marked this pull request as ready for review November 24, 2026 07:39
@zev

zev commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@zev There's a few of these PRs still in draft and I'm not sure what your intent is. If they're ready to be reviewed and merged, move out of draft and request a review. These are mostly pretty simple things so I assume it was just an oversight.

@headius this is ready for review. I had some in draft because I wanted to review the PRs.

@zev

zev commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@headius following up on your question about drafts: these are all ready for review now.

The ruby-4.1 PRs fail the same ~21 spec jobs that fail on ruby-4.1 itself (4.1 specs nobody has implemented yet: &nil block params, Dir.glob with ***/, Class#allocate), so those failures don't come from these changes. #10128 stays in draft until I fix 4 MRI test errors it causes.

@headius headius left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to see this is finally landing. Check my math on merged stuff. I'll be merging everything forward from 10.0 shortly.

zev and others added 3 commits December 1, 2026 16:47
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>
asString went through to_s(getCurrentContext()) and a cast only to
return fstring. to_s(Ruby) has no callers left.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both are public API, so keep them for now (review on jruby#10117).
chill_symbol_string() does nothing, since Symbol#to_s no longer returns
a chilled String. ObjectFlags.CHILLED_SYMBOL_TO_S_F stays removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zev
zev force-pushed the symbol-to-s-frozen branch from ef8eacb to 5ca55b2 Compare December 1, 2026 22:49
@zev

zev commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@headius done: StringSupport.CHILLED_SYMBOL_TO_S and RubyString#chill_symbol_string() are back as @Deprecated(since = "10.1.4.0") (the method is now a no-op that returns this), ObjectFlags.CHILLED_SYMBOL_TO_S_F stays removed, and the branch is rebased on current ruby-4.1 (6f4e39b).

On checking the math: I did the next merge-forward locally (current jruby-10.0 → master → ruby-4.1), built it, and ran the areas these PRs touch.

@headius
headius merged commit dcd37e5 into jruby:ruby-4.1 Dec 3, 2026
127 of 149 checks passed
@headius

headius commented Dec 3, 2026

Copy link
Copy Markdown
Member

The #8638 instance_eval excludes, test_string_ivar, test_extend_string and test_hash_subclass_extend can all go.

Go ahead and push a PR removing any now-working excludes or tags. Everything mentioned here is merged.

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.

2 participants