Issue #20319 has been updated by jeremyevans0 (Jeremy Evans).
Eregon (Benoit Daloze) wrote in #note-3:
I think Kernel#freeze should only freeze the object
itself, which means instance variables (and internal state if core/TypedStruct object)
cannot be changed.
That would be both more consistent semantically and more efficient, while avoiding weird
cases like the above.
I disagree. If you do not freeze the object's singleton class, then you can define or
undefine any method in the singleton class, which is almost the same as being able to
modify the object (from a Ruby perspective, not a C perspective).
However, redefining methods in the singleton class of the singleton class of the object
does not allow you to modify the object, it only allows you to modify the object's
singleton class. However, as the object's singleton class is frozen, we still should
prevent it.
This bug should be fairly simple to fix by having `Kernel#freeze` go up the singleton
class chain and freeze all currently instantiated singleton classes.
----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-107128
* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me
though) when an object becomes frozen only its own singleton class becomes frozen
immediately.
A singleton class of the object immediate singleton class becomes frozen lazily after
`#singleton_class` method call:
```ruby
o = Object.new
klass = o.singleton_class.singleton_class
o.freeze
puts klass.frozen? # false <== here we expect true
puts o.singleton_class.singleton_class.frozen? # true
puts klass.frozen? # true
```
I would expect all created (and visible to user) singleton classes in an object singleton
classes chain to become frozen immediately when the object gets frozen.
--
https://bugs.ruby-lang.org/