Skip to content

Freeze all instances of Regexp, except subclass instances - #10128

Draft
zev wants to merge 3 commits into
jruby:ruby-4.1from
zev:regexp-frozen-instances
Draft

zev wants to merge 3 commits into
jruby:ruby-4.1from
zev:regexp-frozen-instances

Conversation

@zev

@zev zev commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Based on the test/mri sync PR (#10127). Please merge that one first; review only the last two commits. The fix commit (Check Regexp state before setting options) is also in the jruby-10.0 PR (#10126). It is included here because the freeze depends on it, and the identical change will merge cleanly when jruby-10.0 reaches ruby-4.1.

In Ruby 4.1, all instances of Regexp are frozen, not only literals (Feature #8948). Instances of a subclass of Regexp are not frozen, for compatibility. Part of #9746.

CRuby change: ruby/ruby@cb03bf43eb. It freezes the Regexp at the end of rb_reg_initialize, reg_copy and rb_reg_initialize_m when its class is Regexp. Marshal now allocates the Regexp from its user class (r_object_for).

Call CRuby 4.1 JRuby before JRuby after
Regexp.new("a").frozen? true false true
Regexp.union("a", "b").frozen? true false true
/a/.dup.frozen? true false true
Class.new(Regexp).new("a").frozen? false false false
"a,b".sub(",", "x"); $~.regexp.frozen? true false true
Regexp.new("a").send(:initialize, "b") FrozenError TypeError FrozenError
Marshal.load(Marshal.dump(/a/)).frozen? true false true
Marshal.load("\x04\bI/\nhello\x00\a:\x06EF:\x11@regexp_ivar[\x06i/") FrozenError /hello/ with @regexp_ivar FrozenError
Marshal round trip of a UserRegexp with @noise not frozen, keeps @noise same same
r = Class.new(Regexp).new("a")
r.send(:initialize, "b", Regexp::IGNORECASE) rescue nil
r
/a/ /a/i /a/

Changes

  • RubyRegexp#regexpInitialize (rb_reg_initialize) freezes the Regexp when its class is Regexp. Regexp.new, Regexp.union, literals, #dup, #clone and Marshal.load all go through it.
  • MatchData#regexp for a String pattern builds its Regexp without regexpInitialize, so it freezes it there too.
  • MarshalLoader passes the user class of a C entry down to the Regexp loader, as CRuby does, and allocates the Regexp from it. A loaded UserRegexp is not frozen and keeps its instance variables. For a plain Regexp, ivars other than the encoding now raise FrozenError, as in CRuby. For a user class that is not a Regexp subclass, it allocates a plain Regexp, so we still raise dump format error (user class) (see below).
  • Fix commit (also in Check Regexp state before setting options in rb_reg_initialize #10126): regexpInitialize set the new options and timeout before it checked whether the Regexp was frozen or already initialized. A failed re-initialize changed the Regexp (/a/ became /a/i). Without this fix, the freeze above would still let a frozen Regexp change. It now does the checks first, like rb_reg_initialize_check.

Specs

core/regexp/shared/new.rb, core/regexp/initialize_spec.rb, core/marshal/* and core/string/match_spec.rb already match CRuby's copy. Their 4.1 examples now pass. I added:

  • core/regexp/initialize_spec.rb: re-initializing a Regexp subclass instance doesn't change its options or timeout (no guard, passes on 4.0 and 4.1).
  • core/matchdata/regexp_spec.rb: ruby_version_is "4.1", $~.regexp after gsub(String) is frozen.

Testing

  • core/regexp/{initialize,new,compile}_spec.rb, core/matchdata/regexp_spec.rb, core/marshal/{dump,load,restore}_spec.rb, core/string/match_spec.rb, language/regexp_spec.rb: pass on JRuby (interpreter and forced JIT), CRuby 4.1.0-dev and CRuby 4.0.7. One forced-JIT failure, "Literal Regexps caches the Regexp object", also fails on the base branch.
  • mspec ci on core/regexp, core/marshal, core/matchdata, core/string, language/regexp: 5 fewer failures, no new ones.
  • spec/jruby.mspec :fast: 5 fewer failures, no new ones.
  • test/mri/ruby/test_regexp.rb and test_marshal.rb (synced in Update test_regexp.rb and test_marshal.rb from CRuby #10127), interpreter and forced JIT: 0 failures. This PR removes the excludes for TestRegexp#test_dup and TestMarshal#test_regexp2, which now pass. TestMarshalFreeze#test_proc_returned_object_are_not_frozen passes too; CRuby removed /foo/ from it.

Not in this PR

  • The FrozenError message from checkFrozen() is wrong for any object: can't modify frozen Object: /a/: /a/ (CRuby: can't modify frozen Regexp: /a/). RubyBasicObject#testFrozen passes "Object: " + inspect as the type name to newFrozenError(String, IRubyObject), which adds the inspect again. This also happens on the base branch, for example on a frozen Regexp subclass instance.
  • Extending a loaded Regexp (Marshal.load("\x04\be:\nMeths/...")) raises FrozenError as in CRuby, but the message names the singleton class (can't modify frozen module: #<Class:#<Regexp:...>>).
  • Marshal.load("\x04\bIC:\vRegexp/\x06a\x00\x06:\x06ET") (a C:Regexp entry with an encoding that differs from the pattern's) raises FrozenError in CRuby. JRuby ignores the encoding, because RubyRegexp#setEncoding does nothing.
  • CRuby 4.1.0-dev crashes (bus error on inspect) after Marshal.load("\x04\bC:\vString/\x06a\x00"). The new code allocates a T_REGEXP with class String. JRuby keeps raising dump format error (user class), as 4.0 does. This may be worth an upstream report.
  • The deprecated UnmarshalStream (Java API, not used by Marshal.load) still allocates a Regexp and changes its class afterwards. Through it, a Regexp subclass instance would be frozen. I didn't test or change it.
  • Date's internal parse regexps (RubyDate) are built with constructors that don't freeze them. Ruby code never sees them.

🤖 Generated with Claude Code

zev and others added 3 commits November 20, 2026 00:59
Copied whole from ruby/ruby@197b8f0103 (Ruby 4.1.0-dev). The new
tests that fail on JRuby are excluded with the reason. test_dup and
test_regexp2 now expect frozen Regexp instances (Feature jruby#8948).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
regexpInitialize set the new options and timeout before it checked
whether the Regexp was frozen or already initialized. Re-initializing
an initialized Regexp raised TypeError, but the Regexp kept the new
options, so `r.send(:initialize, "b", Regexp::IGNORECASE)` turned /a/
into /a/i. CRuby runs rb_reg_initialize_check first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ruby 4.1 freezes every Regexp, not only literals (Feature jruby#8948).
Instances of a subclass of Regexp stay unfrozen for compatibility.
Ported from ruby/ruby@cb03bf43eb.

* regexpInitialize (rb_reg_initialize) freezes the Regexp when its
  class is Regexp. Regexp.new, Regexp.union, #dup, #clone and
  Marshal.load all go through it.
* MatchData#regexp for a String pattern builds its Regexp without
  regexpInitialize, so it is frozen there.
* Marshal.load allocates a Regexp from its user class ('C' entry),
  as CRuby now does, so a loaded subclass instance is not frozen and
  keeps its instance variables. A plain Regexp with instance
  variables now raises FrozenError, as in CRuby.

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

zev commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Review and merge order for the Regexp freeze work (Feature #8948):

  1. Check Regexp state before setting options in rb_reg_initialize #10126 (jruby-10.0): check the Regexp state before setting options in rb_reg_initialize. It's independent and can merge any time.
  2. Update test_regexp.rb and test_marshal.rb from CRuby #10127 (ruby-4.1): sync test_regexp.rb and test_marshal.rb from CRuby, with excludes.
  3. Freeze all instances of Regexp, except subclass instances #10128 (ruby-4.1): freeze Regexp instances. It's stacked on Update test_regexp.rb and test_marshal.rb from CRuby #10127, so merge that first. It also carries the Check Regexp state before setting options in rb_reg_initialize #10126 commit, because the freeze depends on it. ← this PR

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.

1 participant