Skip to content

Allow a singleton class on a frozen String that is not deduplicated - #10167

Merged
headius merged 1 commit into
jruby:jruby-10.0from
zev:string-singleton-class-fstring
Dec 1, 2026
Merged

headius merged 1 commit into
jruby:jruby-10.0from
zev:string-singleton-class-fstring

Conversation

@zev

@zev zev commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

String#singleton_class raised TypeError: can't define singleton for every frozen String. CRuby raises it only for a deduplicated String (one with the RSTRING_FSTR flag). Any other frozen String gets a frozen singleton class, like any other frozen object.

How this was found. While reviewing #10117 (Symbol#to_s returns a frozen String), the reviewer noticed that :a.to_s.clone.singleton_class raises TypeError on JRuby but works on CRuby 4.1. It isn't caused by #10117. Any frozen String does the same on jruby-10.0, for example "x".dup.freeze.singleton_class, and #10117 only made it easier to hit.

Call CRuby 3.3 / 4.0 / 4.1 JRuby before JRuby after
"x".dup.freeze.singleton_class frozen singleton class TypeError frozen singleton class
(-"x").clone.singleton_class frozen singleton class TypeError frozen singleton class
s = "x".dup.freeze.clone; def s.foo; end FrozenError TypeError FrozenError
(-"x").singleton_class TypeError TypeError TypeError
:a.name.singleton_class, a Hash key, a literal under frozen_string_literal: true TypeError TypeError TypeError

CRuby's check is in singleton_class_of:

case T_STRING:
  if (CHILLED_STRING_P(obj)) CHILLED_STRING_MUTATED(obj);
  else if (FL_TEST_RAW(obj, RSTRING_FSTR)) rb_raise(rb_eTypeError, "can't define singleton");

JRuby has no RSTRING_FSTR flag. Deduplicated strings are instances of RubyString.FString. The chilled-string port (6e5bdd9) used isFrozen() in its place, which also catches strings that are frozen but not deduplicated.

Changes

  • RubyString#singletonClass: raise only for an FString, and annotate it @CRuby("singleton_class_of", note = "T_STRING branch").
  • spec/ruby/core/kernel/singleton_class_spec.rb: new example, "returns a frozen singleton class for a frozen String that is not deduplicated". There's no version guard; it passes on CRuby 3.3, 4.0 and 4.1.
  • Removed the stale fails: tag for "raises TypeError for a frozen deduplicated String". That spec already passed.
  • Removed the test/mri excludes for 10 tests that now pass: test_extend_string, test_string_ivar, test_string_subclass, test_string_subclass_cycle and test_string_subclass_extend in both TestMarshalFreeze and TestMarshalFreezeProc. Their reason said "attempts to instance_eval a frozen String ... (instance_eval should defer creation of singleton class #8638)". The real cause was the TypeError: instance_eval creates the singleton class eagerly (instance_eval should defer creation of singleton class #8638), and that raised for any frozen String. TestEval#test_fstring_instance_eval still fails, because it uses an interned string, and stays excluded under instance_eval should defer creation of singleton class #8638.

Testing

  • core/kernel/singleton_class_spec.rb: passes on JRuby (interpreter and forced JIT) and on CRuby 3.3.12, 4.0.7 and 4.1.0-dev. The new example fails on the unfixed build.
  • mspec ci on core/kernel, core/string, core/basicobject, core/marshal and language: same results as the unfixed build (2 existing core/kernel failures).
  • test/mri: test_marshal.rb has 0 failures in the interpreter and with forced JIT, with the 10 excludes removed. test_string.rb, test_object.rb and test_eval.rb have 0 failures.

Not in this PR

  • Defining a method on a frozen String (or any frozen object) raises FrozenError with the inspect repeated, can't modify frozen object: "x": "x". CRuby's message is can't modify frozen String: "x". This is an existing message bug in RubyBasicObject#testFrozen.

🤖 Generated with Claude Code

String#singleton_class raised TypeError for every frozen String. CRuby
raises only for deduplicated strings (RSTRING_FSTR): -"x", frozen
literals, Symbol names, Hash keys. Any other frozen String gets a frozen
singleton class, like any frozen object. JRuby's equivalent of
RSTRING_FSTR is RubyString.FString, so check for that.

This also lets instance_eval/extend work on frozen non-deduplicated
Strings, so ten TestMarshalFreeze/TestMarshalFreezeProc excludes and a
stale Kernel#singleton_class spec tag are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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.

LGTM. I believe we still trigger a singleton class to be created for instance_eval and friends so that will be a good related issue to fix.

@headius headius added this to the JRuby 10.0.9.0 milestone Dec 1, 2026
@headius
headius merged commit 5136139 into jruby:jruby-10.0 Dec 1, 2026
127 checks passed
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