ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:99507] [Ruby master Feature#15504] Freeze all Range object
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
@ 2020-08-07  6:12 ` ko1
  2020-08-07 11:21 ` [ruby-core:99511] [Ruby master Feature#15504] Freeze all Range objects eregontp
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: ko1 @ 2020-08-07  6:12 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by ko1 (Koichi Sasada).


I got an issue on Ractor.

```ruby
def test
  (1..2)
end

r1 = test

Ractor.new do
  r2 = test
end.take
```

* compiler cached Range `(1..2)` because they begin and end are frozen literals. The test method returns the same range object.
* it means `test` returns not-immutable but same object. It violate the Ractor's memory model.

To solve it, there are two options.

(1) avoid cache at compile time.
(2) freeze Range objects which will be cached by th compiler.

For performance reason, I want to choose (2).

After that, could you please discuss all Range objects should be frozen or not.

Thanks,
Koichi

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

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

Range is now non-frozen. How about to freeze all of Range objects?

# Background

We freeze some type of objects, Numerics (r47523) and Symbols [Feature #8906].
I believe making objects immutable solves some kind of programming difficulties.

`Range` is mutable, at least it is written in Range literal. So we can write the following weird program. 

```
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):

```
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 usage of mutable Range in tests.

* (1) taint-flag
* (2) add singleton methods.
* (3) subclass with mutable states

Maybe (2) and (3) are points.

Thanks,
Koichi



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

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:99511] [Ruby master Feature#15504] Freeze all Range objects
       [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
  2020-09-25  8:49 ` [ruby-core:100130] " matz
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: eregontp @ 2020-08-07 11:21 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:100130] [Ruby master Feature#15504] Freeze all Range objects
       [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 ` [ruby-core:99511] [Ruby master Feature#15504] Freeze all Range objects eregontp
@ 2020-09-25  8:49 ` matz
  2020-09-25 11:53 ` [ruby-core:100142] " ko1
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: matz @ 2020-09-25  8:49 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by matz (Yukihiro Matsumoto).


I agree with making ranges frozen. I don't see any particular case that could be broken by frozen ranges.
Since there's possiblity of breakage, I'd like to experiment it.

Matz.


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

* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:100142] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2020-09-25  8:49 ` [ruby-core:100130] " matz
@ 2020-09-25 11:53 ` ko1
  2020-09-25 13:58 ` [ruby-core:100146] " eregontp
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: ko1 @ 2020-09-25 11:53 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by ko1 (Koichi Sasada).


Ok, I freeze all Ranges except sub-classes.
https://github.com/ruby/ruby/pull/3583

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

* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:100146] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2020-09-25 11:53 ` [ruby-core:100142] " ko1
@ 2020-09-25 13:58 ` eregontp
  2020-09-25 15:09 ` [ruby-core:100149] " ko1
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: eregontp @ 2020-09-25 13:58 UTC (permalink / raw)
  To: ruby-core

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


Great!

Related, should Enumerator::ArithmeticSequence be frozen too?

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:100149] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2020-09-25 13:58 ` [ruby-core:100146] " eregontp
@ 2020-09-25 15:09 ` ko1
  2020-09-26 13:21 ` [ruby-core:100175] " eregontp
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: ko1 @ 2020-09-25 15:09 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by ko1 (Koichi Sasada).


Eregon (Benoit Daloze) wrote in #note-10:
> Related, should Enumerator::ArithmeticSequence be frozen too?

new ticket?

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:100175] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2020-09-25 15:09 ` [ruby-core:100149] " ko1
@ 2020-09-26 13:21 ` eregontp
  2021-01-26 20:52 ` [ruby-core:102258] " alex.wayfer
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: eregontp @ 2020-09-26 13:21 UTC (permalink / raw)
  To: ruby-core

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


ko1 (Koichi Sasada) wrote in #note-11:
> Eregon (Benoit Daloze) wrote in #note-10:
> > Related, should Enumerator::ArithmeticSequence be frozen too?
> 
> new ticket?

I filed #17195

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:102258] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2020-09-26 13:21 ` [ruby-core:100175] " eregontp
@ 2021-01-26 20:52 ` alex.wayfer
  2021-01-26 21:04 ` [ruby-core:102259] " zverok.offline
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: alex.wayfer @ 2021-01-26 20:52 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by AlexWayfer (Alexander Popov).


I can't now mock Range objects with RSpec (`gorilla_patch` gem). What should I do? I see no work-around, like `+'foo'` for strings. `Range.new` gives frozen objects too.

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:102259] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2021-01-26 20:52 ` [ruby-core:102258] " alex.wayfer
@ 2021-01-26 21:04 ` zverok.offline
  2021-02-06  0:29 ` [ruby-core:102405] " alex.wayfer
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: zverok.offline @ 2021-01-26 21:04 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by zverok (Victor Shepelev).


@AlexWayfer

https://github.com/AlexWayfer/gorilla_patch/blob/master/lib/gorilla_patch/cover.rb#L8 -- may be for this particular case it is better to have version guard as an outer check?..

```ruby
if RUBY_VERSION < '2.6'
  def cover?(value)
    #...
  end
end
```
...and have the same guard in specs?..

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:102405] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2021-01-26 21:04 ` [ruby-core:102259] " zverok.offline
@ 2021-02-06  0:29 ` alex.wayfer
  2021-02-06 10:47 ` [ruby-core:102410] " zverok.offline
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: alex.wayfer @ 2021-02-06  0:29 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by AlexWayfer (Alexander Popov).


zverok (Victor Shepelev) wrote in #note-15:
> @AlexWayfer
> 
> https://github.com/AlexWayfer/gorilla_patch/blob/master/lib/gorilla_patch/cover.rb#L8 -- may be for this particular case it is better to have version guard as an outer check?..
> 
> ```ruby
> if RUBY_VERSION < '2.6'
>   def cover?(value)
>     #...
>   end
> end
> ```
> ...and have the same guard in specs?..

Thank you, I agree, it's better. But… if I want to check was called refined method or core? Right now I'm doing it via `have_received` once or never, and for this RSpec should change object (Range), but it's frozen. Do I have other ways to check which implementation of method was used? `value.method(:cover?).source_location` returns `nil` in both cases.

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:102410] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2021-02-06  0:29 ` [ruby-core:102405] " alex.wayfer
@ 2021-02-06 10:47 ` zverok.offline
  2021-03-06 21:16 ` [ruby-core:102760] " alex.wayfer
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: zverok.offline @ 2021-02-06 10:47 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by zverok (Victor Shepelev).


> if I want to check was called refined method or core?

It actually might be a good feature proposal for Ruby. Because currently, you can tell whether the method is defined by this class, by its parent, by included module, by singleton class... via `Method#owner`. But as far as I can recall, there is no way to ask "whether the method is defined by refinement".

But this whole discussion is unrelated to Range frozenness, honestly :)

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:102760] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2021-02-06 10:47 ` [ruby-core:102410] " zverok.offline
@ 2021-03-06 21:16 ` alex.wayfer
  2021-10-22 14:31 ` [ruby-core:105752] " Eregon (Benoit Daloze)
  2021-12-03  3:28 ` [ruby-core:106434] " ko1 (Koichi Sasada)
  13 siblings, 0 replies; 14+ messages in thread
From: alex.wayfer @ 2021-03-06 21:16 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by AlexWayfer (Alexander Popov).


zverok (Victor Shepelev) wrote in #note-17:
> > if I want to check was called refined method or core?
> 
> It actually might be a good feature proposal for Ruby. Because currently, you can tell whether the method is defined by this class, by its parent, by included module, by singleton class... via `Method#owner`. But as far as I can recall, there is no way to ask "whether the method is defined by refinement".

I'm too lazy for such proposals, especially well formatted, but I can try.

zverok (Victor Shepelev) wrote in #note-17:
> But this whole discussion is unrelated to Range frozenness, honestly :)

No, I disagree! My issue was raised by resolving this issue, even without changes mention in the article about Ruby 3: https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/


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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:105752] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2021-03-06 21:16 ` [ruby-core:102760] " alex.wayfer
@ 2021-10-22 14:31 ` Eregon (Benoit Daloze)
  2021-12-03  3:28 ` [ruby-core:106434] " ko1 (Koichi Sasada)
  13 siblings, 0 replies; 14+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-10-22 14:31 UTC (permalink / raw)
  To: ruby-core

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


@ko1 Do you know why only Range instances and not Range subclass instances were frozen? (https://bugs.ruby-lang.org/issues/15504#note-8)

This issue title is a bit confusing, also https://bugs.ruby-lang.org/issues/15504#note-9.
Probably https://bugs.ruby-lang.org/issues/15504#note-9 was reverted?

I guess "Freeze all Range objects" means all Range instances, literal or not, but still not subclass instances, that confused me.

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:106434] [Ruby master Feature#15504] Freeze all Range objects
       [not found] <redmine.issue-15504.20190104031246.17@ruby-lang.org>
                   ` (12 preceding siblings ...)
  2021-10-22 14:31 ` [ruby-core:105752] " Eregon (Benoit Daloze)
@ 2021-12-03  3:28 ` ko1 (Koichi Sasada)
  13 siblings, 0 replies; 14+ messages in thread
From: ko1 (Koichi Sasada) @ 2021-12-03  3:28 UTC (permalink / raw)
  To: ruby-core

Issue #15504 has been updated by ko1 (Koichi Sasada).


Eregon (Benoit Daloze) wrote in #note-19:
> This issue title is a bit confusing, also https://bugs.ruby-lang.org/issues/15504#note-9.
> Probably https://bugs.ruby-lang.org/issues/15504#note-9 was reverted?
> 
> I guess "Freeze all Range objects" means all Range instances, literal or not, but still not subclass instances, that confused me.

I see. Not sure but maybe it is only implementation issue.

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

* Author: ko1 (Koichi Sasada)
* Status: Closed
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2021-12-03  3:28 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:99511] [Ruby master Feature#15504] Freeze all Range objects eregontp
2020-09-25  8:49 ` [ruby-core:100130] " 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)

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).