Repository navigation
Allow a singleton class on a frozen String that is not deduplicated - #10167
Merged
Merged
Conversation
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
approved these changes
Dec 1, 2026
headius
left a comment
Member
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
String#singleton_classraisedTypeError: can't define singletonfor every frozen String. CRuby raises it only for a deduplicated String (one with theRSTRING_FSTRflag). Any other frozen String gets a frozen singleton class, like any other frozen object.How this was found. While reviewing #10117 (
Symbol#to_sreturns a frozen String), the reviewer noticed that:a.to_s.clone.singleton_classraisesTypeErroron JRuby but works on CRuby 4.1. It isn't caused by #10117. Any frozen String does the same onjruby-10.0, for example"x".dup.freeze.singleton_class, and #10117 only made it easier to hit."x".dup.freeze.singleton_classTypeError(-"x").clone.singleton_classTypeErrors = "x".dup.freeze.clone; def s.foo; endFrozenErrorTypeErrorFrozenError(-"x").singleton_classTypeErrorTypeErrorTypeError:a.name.singleton_class, a Hash key, a literal underfrozen_string_literal: trueTypeErrorTypeErrorTypeErrorCRuby's check is in
singleton_class_of:JRuby has no
RSTRING_FSTRflag. Deduplicated strings are instances ofRubyString.FString. The chilled-string port (6e5bdd9) usedisFrozen()in its place, which also catches strings that are frozen but not deduplicated.Changes
RubyString#singletonClass: raise only for anFString, 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.fails:tag for "raises TypeError for a frozen deduplicated String". That spec already passed.test/mriexcludes for 10 tests that now pass:test_extend_string,test_string_ivar,test_string_subclass,test_string_subclass_cycleandtest_string_subclass_extendin bothTestMarshalFreezeandTestMarshalFreezeProc. Their reason said "attempts to instance_eval a frozen String ... (instance_eval should defer creation of singleton class #8638)". The real cause was theTypeError:instance_evalcreates the singleton class eagerly (instance_eval should defer creation of singleton class #8638), and that raised for any frozen String.TestEval#test_fstring_instance_evalstill 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 cioncore/kernel,core/string,core/basicobject,core/marshalandlanguage: same results as the unfixed build (2 existingcore/kernelfailures).test/mri:test_marshal.rbhas 0 failures in the interpreter and with forced JIT, with the 10 excludes removed.test_string.rb,test_object.rbandtest_eval.rbhave 0 failures.Not in this PR
FrozenErrorwith the inspect repeated,can't modify frozen object: "x": "x". CRuby's message iscan't modify frozen String: "x". This is an existing message bug inRubyBasicObject#testFrozen.🤖 Generated with Claude Code