Repository navigation
Conversation
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>
Contributor
Author
|
Review and merge order for the Regexp freeze work (Feature #8948):
|
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.
Based on the
test/mrisync 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 thejruby-10.0PR (#10126). It is included here because the freeze depends on it, and the identical change will merge cleanly whenjruby-10.0reachesruby-4.1.In Ruby 4.1, all instances of
Regexpare frozen, not only literals (Feature #8948). Instances of a subclass ofRegexpare not frozen, for compatibility. Part of #9746.CRuby change: ruby/ruby@cb03bf43eb. It freezes the Regexp at the end of
rb_reg_initialize,reg_copyandrb_reg_initialize_mwhen its class isRegexp. Marshal now allocates the Regexp from its user class (r_object_for).Regexp.new("a").frozen?truefalsetrueRegexp.union("a", "b").frozen?truefalsetrue/a/.dup.frozen?truefalsetrueClass.new(Regexp).new("a").frozen?falsefalsefalse"a,b".sub(",", "x"); $~.regexp.frozen?truefalsetrueRegexp.new("a").send(:initialize, "b")FrozenErrorTypeErrorFrozenErrorMarshal.load(Marshal.dump(/a/)).frozen?truefalsetrueMarshal.load("\x04\bI/\nhello\x00\a:\x06EF:\x11@regexp_ivar[\x06i/")FrozenError/hello/with@regexp_ivarFrozenErrorUserRegexpwith@noise@noiser = Class.new(Regexp).new("a")r.send(:initialize, "b", Regexp::IGNORECASE) rescue nilr/a//a/i/a/Changes
RubyRegexp#regexpInitialize(rb_reg_initialize) freezes the Regexp when its class isRegexp.Regexp.new,Regexp.union, literals,#dup,#cloneandMarshal.loadall go through it.MatchData#regexpfor a String pattern builds its Regexp withoutregexpInitialize, so it freezes it there too.MarshalLoaderpasses the user class of aCentry down to the Regexp loader, as CRuby does, and allocates the Regexp from it. A loadedUserRegexpis not frozen and keeps its instance variables. For a plain Regexp, ivars other than the encoding now raiseFrozenError, as in CRuby. For a user class that is not a Regexp subclass, it allocates a plain Regexp, so we still raisedump format error (user class)(see below).regexpInitializeset 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, likerb_reg_initialize_check.Specs
core/regexp/shared/new.rb,core/regexp/initialize_spec.rb,core/marshal/*andcore/string/match_spec.rbalready 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",$~.regexpaftergsub(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 cioncore/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.rbandtest_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 forTestRegexp#test_dupandTestMarshal#test_regexp2, which now pass.TestMarshalFreeze#test_proc_returned_object_are_not_frozenpasses too; CRuby removed/foo/from it.Not in this PR
FrozenErrormessage fromcheckFrozen()is wrong for any object:can't modify frozen Object: /a/: /a/(CRuby:can't modify frozen Regexp: /a/).RubyBasicObject#testFrozenpasses"Object: " + inspectas the type name tonewFrozenError(String, IRubyObject), which adds the inspect again. This also happens on the base branch, for example on a frozen Regexp subclass instance.Marshal.load("\x04\be:\nMeths/...")) raisesFrozenErroras 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")(aC:Regexpentry with an encoding that differs from the pattern's) raisesFrozenErrorin CRuby. JRuby ignores the encoding, becauseRubyRegexp#setEncodingdoes nothing.inspect) afterMarshal.load("\x04\bC:\vString/\x06a\x00"). The new code allocates a T_REGEXP with classString. JRuby keeps raisingdump format error (user class), as 4.0 does. This may be worth an upstream report.UnmarshalStream(Java API, not used byMarshal.load) still allocates aRegexpand changes its class afterwards. Through it, a Regexp subclass instance would be frozen. I didn't test or change it.RubyDate) are built with constructors that don't freeze them. Ruby code never sees them.🤖 Generated with Claude Code