ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: eregontp@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:99511] [Ruby master Feature#15504] Freeze all Range objects
Date: Fri, 07 Aug 2020 11:21:01 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-86969.20200807112101.17@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15504.20190104031246.17@ruby-lang.org

Issue #15504 has been updated by Eregon (Benoit Daloze).


Right, every object cached at parse time must be deeply immutable, I would think that was an oversight.

----------------------------------------
Feature #15504: Freeze all Range objects
https://bugs.ruby-lang.org/issues/15504#change-86969

* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
# Abstract

Range is currently non-frozen. How about freezing all Range objects?

# Background

We froze some types of objects: Numerics (r47523) and Symbols [Feature #8906]. I believe that making objects immutable solves some kinds of programming difficulties.

`Range` is mutable at least when written as Range literal. So we can write the following weird program:

```ruby
2.times{
  r = (1..3)
  p r.instance_variable_get(:@foo)
  #=> 1st time: nil
  #=> 2nd time: :bar
  r.instance_variable_set(:@foo, :bar)
}
```

In `range.c`, there is a comment (thanks znz-san):

```c
static void
range_modify(VALUE range)
{
    rb_check_frozen(range);
    /* Ranges are immutable, so that they should be initialized only once. */
    if (RANGE_EXCL(range) != Qnil) {
	rb_name_err_raise("`initialize' called twice", range, ID2SYM(idInitialize));
    }
}
```

# Patch

```
Index: range.c
===================================================================
--- range.c	(リビジョン 66699)
+++ range.c	(作業コピー)
@@ -45,6 +45,8 @@
     RANGE_SET_EXCL(range, exclude_end);
     RANGE_SET_BEG(range, beg);
     RANGE_SET_END(range, end);
+
+    rb_obj_freeze(range);
 }
 
 VALUE
```

# Discussion

There are several usages of mutable Range in the tests.

* (1) Taint-flag
* (2) Add singleton methods.
* (3) Subclass with mutable states

Maybe (2) and (3) are crucial.

Thanks,
Koichi



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2020-08-07 11:21 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
2020-08-07  6:12 ` [ruby-core:99507] [Ruby master Feature#15504] Freeze all Range object ko1
2020-08-07 11:21 ` eregontp [this message]
2020-09-25  8:49 ` [ruby-core:100130] [Ruby master Feature#15504] Freeze all Range objects matz
2020-09-25 11:53 ` [ruby-core:100142] " ko1
2020-09-25 13:58 ` [ruby-core:100146] " eregontp
2020-09-25 15:09 ` [ruby-core:100149] " ko1
2020-09-26 13:21 ` [ruby-core:100175] " eregontp
2021-01-26 20:52 ` [ruby-core:102258] " alex.wayfer
2021-01-26 21:04 ` [ruby-core:102259] " zverok.offline
2021-02-06  0:29 ` [ruby-core:102405] " alex.wayfer
2021-02-06 10:47 ` [ruby-core:102410] " zverok.offline
2021-03-06 21:16 ` [ruby-core:102760] " alex.wayfer
2021-10-22 14:31 ` [ruby-core:105752] " Eregon (Benoit Daloze)
2021-12-03  3:28 ` [ruby-core:106434] " ko1 (Koichi Sasada)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-86969.20200807112101.17@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).