Make all Regexp object frozen - #15762
Closed
niku wants to merge 6 commits into
Closed
Conversation
Add test to verify Regexp.new returns frozen objects. Implement rb_obj_freeze(self) in rb_reg_initialize_m (re.c). Breaking changes: - Regexp subclassing with instance variables is no longer supported - Singleton methods cannot be defined on Regexp objects Remove incompatible test cases: - test_string.rb: test_match_method (singleton method definition) - test_symbol.rb: test_match_method (singleton method definition) - test_enum.rb: test_grep_optimization (=== override test) - marshaltestlib.rb: test_regexp_subclass and MyRegexp class Remove Enumerable#grep/grep_v Regexp optimization: - enum.c: Remove grep_regexp_i function and optimization branch - This optimization checked if Regexp#=== was overridden - No longer possible since Regexp objects are frozen
Breaking changes:
- //.dup.instance_eval { initialize_copy(nil) } raises FrozenError instead of TypeError.
Support Regexp deserialization while keeping objects frozen. Encoding information is transferred from the intermediate string to the Regexp object through rb_reg_new_str's internal mechanisms, eliminating the need for instance variable copying. Add Marshal.load tests to test_regexp_object_is_frozen: - Standard Marshal.load returns frozen Regexp - Marshal.load with freeze: false also returns frozen Regexp
Verify that Regexp literals created via rb_reg_new_str are frozen. This is part of the Marshal.load testing that now includes literals.
Verify that Regexp.union returns frozen objects. rb_reg_new_ary internally calls rb_reg_new_str which now freezes objects.
❌ 17/67033 Tests Failedspec/ruby/core/marshal/dump_spec.rb#Marshal.dump with a Regexp dumps an extended Regexpspec/ruby/core/marshal/dump_spec.rb#Marshal.dump with a Regexp dumps a Regexp with instance variablesspec/ruby/core/marshal/load_spec.rb#Marshal.load for a Regexp loads an extended Regexpspec/ruby/core/marshal/load_spec.rb#Marshal.load for a Regexp loads a Regexp subclass instance variables when it is extended with a modulespec/ruby/core/marshal/load_spec.rb#Marshal.load for a Regexp restore the regexp instance variables12 more failed testsspec/ruby/core/marshal/load_spec.rb#Marshal.load loads a Regexp subclass /ispec/ruby/core/marshal/restore_spec.rb#Marshal.restore for a Regexp loads an extended Regexpspec/ruby/core/marshal/restore_spec.rb#Marshal.restore for a Regexp loads a Regexp subclass instance variables when it is extended with a modulespec/ruby/core/marshal/restore_spec.rb#Marshal.restore for a Regexp restore the regexp instance variablesspec/ruby/core/marshal/restore_spec.rb#Marshal.restore loads a Regexp subclass /ispec/ruby/core/regexp/compile_spec.rb#Regexp.compile works by default for subclasses with overridden #initializespec/ruby/core/regexp/initialize_spec.rb#Regexp#initialize raises a TypeError on an initialized non-literal Regexpspec/ruby/core/regexp/new_spec.rb#Regexp.new works by default for subclasses with overridden #initializespec/ruby/core/string/match_spec.rb#String#match calls match on the regular expressionspec/ruby/optional/capi/encoding_spec.rb#C-API Encoding function rb_enc_associate_index sets the encoding of a Regexp to the encodingspec/ruby/optional/capi/encoding_spec.rb#C-API Encoding function rb_enc_associate sets the encoding of a Regexp to the encodingspec/ruby/optional/capi/encoding_spec.rb#C-API Encoding function rb_enc_copy sets the encoding of a Regexp to that of the second argument |
Contributor
Author
|
I found another #14547 with the same goal, so I'm closing this one. |
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.
Regexpinstances frozen across every creation path (new,compile,dup,clone, literals,union,Marshal.load).Marshal.loadalways returns frozenRegexp(even withfreeze: false), relying onrb_reg_new_strto carry encoding.Enumerable#grepRegexp-specific optimization that depended on mutableRegexp/Regexp#===.test_regexp_object_is_frozen; drop incompatible tests.Breaking Changes:
Regexpcannot hold instance variables or singleton methods (FrozenError).Regexpsubclass state is effectively unsupported.//.dup.instance_eval { initialize_copy(nil) }now raisesFrozenError(wasTypeError).Marshal.load(..., freeze: false)still returns frozenRegexp.refs: https://bugs.ruby-lang.org/issues/8948