Repository navigation
Evaluate instance_eval on a Float or deduplicated String without a singleton class - #10184
Merged
Merged
Conversation
…ngleton class
instance_eval and instance_exec asked for the receiver's singleton class
before running the block. A Float or a deduplicated String (FString)
can't have one, so every call raised "can't define singleton", even
`1.5.instance_eval { self }`. CRuby only needs the singleton class when
the block defines something.
Run these receivers under the dummy class, as Integer, Symbol and
Bignum already are. Defining a method there still raises TypeError.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
headius
approved these changes
Dec 2, 2026
headius
left a comment
Member
There was a problem hiding this comment.
This looks fine to me and I'll merge it. I'd like to get the original issue solved, though, and make sure we are not form-fitting these fixes for each core type if CRuby does not do the same.
Member
|
@zev I believe there's a bunch of excludes on ruby-4.1 that could be removed with this merged. |
This was referenced Dec 5, 2026
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.
instance_evalandinstance_execcreate the receiver's singleton class before running the block. A Float or a deduplicated String can't have one, so JRuby raisesTypeError: can't define singletonfor every call on them, even when the block defines nothing. CRuby creates the singleton class only when the block needs it. Follow-up to #10167, part of #8638.1.5.instance_eval { self }1.5TypeError1.5(-"a").instance_eval { self }TypeError(-"a").instance_eval("size")1TypeError1(-"a").instance_exec(2) { |x| size + x }3TypeError3(-"a").instance_eval { @x = 1 }FrozenErrorTypeErrorFrozenError1.5.instance_eval { def foo; end }TypeErrorTypeErrorTypeError(-"a").instance_eval { def foo; end }TypeErrorTypeErrorTypeErrorInteger, Bignum, Symbol,
nil,true, Rational, Complex and other frozen Strings already matched CRuby; I checked them all.Changes
RubyFloatandRubyString(forFStringonly) overridegetInstanceEvalClassto return the dummy class, asRubyBasicObjectalready does for immediates. Adefin the block still raisesTypeError: can't define singleton, as it does for an Integer.Specs and tests
core/basicobject/instance_eval_spec.rbandinstance_exec_spec.rb: the block runs on a Float and a deduplicated String, and defining a method there raisesTypeError. They pass on CRuby 3.3.12, 4.0.7 and 4.1.0-dev.TestEval#test_fstring_instance_evalexclude, which now passes.Testing
core/basicobject,kernel/singleton_class,kernel/instance_variable_set,module/class_eval,language/def: 0 failures, interpreter and forced JIT.test_eval.rb,test_float.rb,test_string.rb,test_object.rb: 0 failures, interpreter and forced JIT.Not in this PR
"literal".instance_eval { }still warns about a chilled string, andObject.new.instance_eval { }still creates a singleton class. This PR only covers receivers that can't have a singleton class at all, where the eager creation turns a working call into aTypeError.🤖 Generated with Claude Code