Project

General

Profile

Actions

Feature #17256

closed

Freeze all Regexp objects

Added by ko1 (Koichi Sasada) about 4 years ago. Updated about 4 years ago.

Status:
Closed
Target version:
-
[ruby-core:100345]

Description

To be shared between ractors, Regexp objects should be frozen.

Now, Matz has proposed to make all Regexp objects frozen.

Ten months ago, there was a note https://bugs.ruby-lang.org/issues/16377#note-7 :

For the record: Regexp.new should continue to return unfrozen Regexp instance.

So I'm not sure whether it is a good way or not.

Could you give me your comment on it?

If no comments are given, I'll try to freeze them before Ruby 3.0 preview 2.

BTW, I believe /#{expr}/o should be frozen because this expression only returns one Regexp object.


Related issues 2 (1 open1 closed)

Related to Ruby master - Misc #17412: Regexp vs Range: freezing differencesClosedActions
Related to Ruby master - Feature #8948: Frozen regexAssignedEregon (Benoit Daloze)Actions
Actions #1

Updated by ko1 (Koichi Sasada) about 4 years ago

  • Description updated (diff)
Actions #2

Updated by ko1 (Koichi Sasada) about 4 years ago

  • Description updated (diff)
Actions #3

Updated by sawa (Tsuyoshi Sawada) about 4 years ago

  • Description updated (diff)

Updated by ko1 (Koichi Sasada) about 4 years ago

I tried it and several errors are observed on tests.
It is possible that people want to tweak Regexp objects (unlike Range objects).

How about to freeze literals includes /#{expr}/?
https://github.com/ruby/ruby/pull/3676
It is easy to fix by .dup method.

Updated by Eregon (Benoit Daloze) about 4 years ago

@ko1 (Koichi Sasada) Could you show the errors from the tests?
Some tests are there just for regression or corner cases, I think actual user code matters far more than tests when it comes to backward-incompatible changes.
It seems odd to store anything on a Regexp.

Updated by Dan0042 (Daniel DeLorme) about 4 years ago

Given that string literals with interpolation were recently unfrozen, it would appear slightly inconsistent to freeze regexp literals with interpolation.
/#{str}/ is equivalent to Regexp.new(str) so I would find it a bit weird if they had a different behavior w/r freezing. It would make more sense to say that all new regexps (including Regexp.new and Regexp.compile) are frozen. That way all constants such as UNSAFE = Regexp.new("[^#{SAFE_STRING}]", false) would be automatically ractor-safe.

And if someone really needs to modify a regexp maybe they can use dup?
rx = Regexp.new(str).dup.extend(PrintWhenMatch)

Updated by marcandre (Marc-Andre Lafortune) about 4 years ago

Dan0042 (Daniel DeLorme) wrote in #note-6:

Given that string literals with interpolation were recently unfrozen, it would appear slightly inconsistent to freeze regexp literals with interpolation.

The difference is that there are many methods and usecases to mutate a String, and none that we know of to mutate a Regexp.

Updated by ko1 (Koichi Sasada) about 4 years ago

ko1 (Koichi Sasada) Could you show the errors from the tests?

Sorry I lost the patch.

Anyway, at last dev-meeting, freezing all Regexp literals including dynamically created objects are accepted.
I don't have objection to freeze all regexp objects, but we need to observe the incompatibilities in existing code and I have no time to handle them. Try on 3.1 if someone can try?

Actions #10

Updated by Eregon (Benoit Daloze) almost 2 years ago

  • Related to Misc #17412: Regexp vs Range: freezing differences added
Actions #11

Updated by Eregon (Benoit Daloze) almost 2 years ago

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0