Skip to content

Evaluate instance_eval on a Float or deduplicated String without a singleton class - #10184

Merged
headius merged 1 commit into
jruby:jruby-10.0from
zev:instance-eval-fstring
Dec 2, 2026
Merged

headius merged 1 commit into
jruby:jruby-10.0from
zev:instance-eval-fstring

Conversation

@zev

@zev zev commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

instance_eval and instance_exec create the receiver's singleton class before running the block. A Float or a deduplicated String can't have one, so JRuby raises TypeError: can't define singleton for 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.

Call CRuby 3.3 / 4.0 / 4.1 JRuby before JRuby after
1.5.instance_eval { self } 1.5 TypeError 1.5
(-"a").instance_eval { self } the same String TypeError the same String
(-"a").instance_eval("size") 1 TypeError 1
(-"a").instance_exec(2) { |x| size + x } 3 TypeError 3
(-"a").instance_eval { @x = 1 } FrozenError TypeError FrozenError
1.5.instance_eval { def foo; end } TypeError TypeError TypeError
(-"a").instance_eval { def foo; end } TypeError TypeError TypeError

Integer, Bignum, Symbol, nil, true, Rational, Complex and other frozen Strings already matched CRuby; I checked them all.

Changes

  • RubyFloat and RubyString (for FString only) override getInstanceEvalClass to return the dummy class, as RubyBasicObject already does for immediates. A def in the block still raises TypeError: can't define singleton, as it does for an Integer.

Specs and tests

  • core/basicobject/instance_eval_spec.rb and instance_exec_spec.rb: the block runs on a Float and a deduplicated String, and defining a method there raises TypeError. They pass on CRuby 3.3.12, 4.0.7 and 4.1.0-dev.
  • Removes the TestEval#test_fstring_instance_eval exclude, 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

🤖 Generated with Claude Code

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

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.

@headius headius added this to the JRuby 10.1.4.0 milestone Dec 2, 2026
@headius

headius commented Dec 2, 2026

Copy link
Copy Markdown
Member

@zev I believe there's a bunch of excludes on ruby-4.1 that could be removed with this merged.

@headius
headius merged commit 90bdb73 into jruby:jruby-10.0 Dec 2, 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