[ruby-core:126832] [Ruby Feature#22376] Undefine the allocator for Class
Issue #22376 has been reported by jhawthorn (John Hawthorn). ---------------------------------------- Feature #22376: Undefine the allocator for Class https://bugs.ruby-lang.org/issues/22376 * Author: jhawthorn (John Hawthorn) * Status: Open ---------------------------------------- Uninitialized classes, created by `Class.allocate`, are a recurring source of bugs (most recently #22341). Every place that inspects a class's superclass chain has to check whether the class has been initialized, and it's easy to forget one. I'd like to propose that we forbid Class.allocate, as we already do for several other core types (Proc, Method, UnboundMethod, Binding). To do this we: * Undefine the allocator for Class (`rb_undef_alloc_func`) * Replace `Class#initialize` with a Class.new singleton method that allocates and initializes in one step. * Define `Class#dup` and `Class#clone` so that copying a class still works `Class` can't be subclassed, so I don't think we need to worry about calling initialize. The only place I found `Class.allocate` via `gem-codesearch` were copies of ruby/spec, so I don't think forbidding that will cause problems. Before: ```ruby klass = Class.allocate Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError) klass.superclass # 'Class#superclass': uninitialized class (TypeError) ``` After: ```ruby Class.allocate # undefined method 'allocate' for class Class (NoMethodError) Class.instance_method(:allocate).bind_call(Class) # 'Class#allocate': allocator undefined for Class (TypeError) ``` PR: https://github.com/ruby/ruby/pull/18964 -- https://bugs.ruby-lang.org/
Issue #22376 has been updated by Eregon (Benoit Daloze). +1, this definitely helps avoid many subtle bugs and simplifies the logic significantly (in fact I already advocated for this in previous tickets). ---------------------------------------- Feature #22376: Undefine the allocator for Class https://bugs.ruby-lang.org/issues/22376#change-119335 * Author: jhawthorn (John Hawthorn) * Status: Open ---------------------------------------- Uninitialized classes, created by `Class.allocate`, are a recurring source of bugs (most recently #22341). Every place that inspects a class's superclass chain has to check whether the class has been initialized, and it's easy to forget one. I'd like to propose that we forbid Class.allocate, as we already do for several other core types (Proc, Method, UnboundMethod, Binding). To do this we: * Undefine the allocator for Class (`rb_undef_alloc_func`) * Replace `Class#initialize` with a Class.new singleton method that allocates and initializes in one step. * Define `Class#dup` and `Class#clone` so that copying a class still works `Class` can't be subclassed, so I don't think we need to worry about calling initialize. The only place I found `Class.allocate` via `gem-codesearch` were copies of ruby/spec, so I don't think forbidding that will cause problems. Before: ```ruby klass = Class.allocate Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError) klass.superclass # 'Class#superclass': uninitialized class (TypeError) ``` After: ```ruby Class.allocate # undefined method 'allocate' for class Class (NoMethodError) Class.instance_method(:allocate).bind_call(Class) # 'Class#allocate': allocator undefined for Class (TypeError) ``` PR: https://github.com/ruby/ruby/pull/18964 -- https://bugs.ruby-lang.org/
Issue #22376 has been updated by matz (Yukihiro Matsumoto). Accepted. An uninitialized class is of no use, so there is no reason to allow `Class.allocate`. I leave the details to you, including whether it should raise NoMethodError like `Module.allocate` or TypeError like `Proc.allocate`. Matz. ---------------------------------------- Feature #22376: Undefine the allocator for Class https://bugs.ruby-lang.org/issues/22376#change-119354 * Author: jhawthorn (John Hawthorn) * Status: Open ---------------------------------------- Uninitialized classes, created by `Class.allocate`, are a recurring source of bugs (most recently #22341). Every place that inspects a class's superclass chain has to check whether the class has been initialized, and it's easy to forget one. I'd like to propose that we forbid Class.allocate, as we already do for several other core types (Proc, Method, UnboundMethod, Binding). To do this we: * Undefine the allocator for Class (`rb_undef_alloc_func`) * Replace `Class#initialize` with a Class.new singleton method that allocates and initializes in one step. * Define `Class#dup` and `Class#clone` so that copying a class still works `Class` can't be subclassed, so I don't think we need to worry about calling initialize. The only place I found `Class.allocate` via `gem-codesearch` were copies of ruby/spec, so I don't think forbidding that will cause problems. Before: ```ruby klass = Class.allocate Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError) klass.superclass # 'Class#superclass': uninitialized class (TypeError) ``` After: ```ruby Class.allocate # undefined method 'allocate' for class Class (NoMethodError) Class.instance_method(:allocate).bind_call(Class) # 'Class#allocate': allocator undefined for Class (TypeError) ``` PR: https://github.com/ruby/ruby/pull/18964 -- https://bugs.ruby-lang.org/
Issue #22376 has been updated by ko1 (Koichi Sasada). Claude code reports that (I'm not sure it is important or not): --- FYI, this changes the result of `Class.method(:new)`, and code that rebinds it to another class now fails: ```ruby class H; def initialize(x) = (@x = x); end class H2 < H; end Class.method(:new).unbind.bind(H2).call(1) # Ruby 4.0: #<H2 @x=1> # master: 'UnboundMethod#bind': singleton method called for a different object (TypeError) ``` `Class.method(:new)` used to be `Class#new`; now it is the new singleton method `#<Class:Class>#new`, which cannot be bound to other classes. I found this in xmlrpc's test (test/test_client.rb:19, `Class.method(:new).unbind.bind(self).call(*args, &block)` to bypass `Net::HTTP.new`): 18 of 20 tests in test_client.rb error on master. xmlrpc's CI has not run since this change, so it does not show there yet. `Class.instance_method(:new).bind_call(klass, ...)` works on both old and new versions, so the fix on the gem side is easy. I'm not sure whether this pattern is common enough to need anything on the Ruby side; gem-codesearch for `Class.method(:new)` may tell. ---------------------------------------- Feature #22376: Undefine the allocator for Class https://bugs.ruby-lang.org/issues/22376#change-119687 * Author: jhawthorn (John Hawthorn) * Status: Open ---------------------------------------- Uninitialized classes, created by `Class.allocate`, are a recurring source of bugs (most recently #22341). Every place that inspects a class's superclass chain has to check whether the class has been initialized, and it's easy to forget one. I'd like to propose that we forbid Class.allocate, as we already do for several other core types (Proc, Method, UnboundMethod, Binding). To do this we: * Undefine the allocator for Class (`rb_undef_alloc_func`) * Replace `Class#initialize` with a Class.new singleton method that allocates and initializes in one step. * Define `Class#dup` and `Class#clone` so that copying a class still works `Class` can't be subclassed, so I don't think we need to worry about calling initialize. The only place I found `Class.allocate` via `gem-codesearch` were copies of ruby/spec, so I don't think forbidding that will cause problems. Before: ```ruby klass = Class.allocate Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError) klass.superclass # 'Class#superclass': uninitialized class (TypeError) ``` After: ```ruby Class.allocate # undefined method 'allocate' for class Class (NoMethodError) Class.instance_method(:allocate).bind_call(Class) # 'Class#allocate': allocator undefined for Class (TypeError) ``` PR: https://github.com/ruby/ruby/pull/18964 -- https://bugs.ruby-lang.org/
Issue #22376 has been updated by Eregon (Benoit Daloze). There used to be a similar issue in RSpec (IIRC) but I've fixed it a long time ago, when TruffleRuby did the same change as proposed here. I think `Class.method(:new)` is quite rare, and `Class.instance_method(:new)` is more correct anyway. ---------------------------------------- Feature #22376: Undefine the allocator for Class https://bugs.ruby-lang.org/issues/22376#change-119688 * Author: jhawthorn (John Hawthorn) * Status: Open ---------------------------------------- Uninitialized classes, created by `Class.allocate`, are a recurring source of bugs (most recently #22341). Every place that inspects a class's superclass chain has to check whether the class has been initialized, and it's easy to forget one. I'd like to propose that we forbid Class.allocate, as we already do for several other core types (Proc, Method, UnboundMethod, Binding). To do this we: * Undefine the allocator for Class (`rb_undef_alloc_func`) * Replace `Class#initialize` with a Class.new singleton method that allocates and initializes in one step. * Define `Class#dup` and `Class#clone` so that copying a class still works `Class` can't be subclassed, so I don't think we need to worry about calling initialize. The only place I found `Class.allocate` via `gem-codesearch` were copies of ruby/spec, so I don't think forbidding that will cause problems. Before: ```ruby klass = Class.allocate Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError) klass.superclass # 'Class#superclass': uninitialized class (TypeError) ``` After: ```ruby Class.allocate # undefined method 'allocate' for class Class (NoMethodError) Class.instance_method(:allocate).bind_call(Class) # 'Class#allocate': allocator undefined for Class (TypeError) ``` PR: https://github.com/ruby/ruby/pull/18964 -- https://bugs.ruby-lang.org/
Issue #22376 has been updated by Eregon (Benoit Daloze). Status changed from Open to Closed Assignee set to jhawthorn (John Hawthorn) Target version set to 4.1 BTW, this was merged in https://github.com/ruby/ruby/pull/18964, updating the issue status ---------------------------------------- Feature #22376: Undefine the allocator for Class https://bugs.ruby-lang.org/issues/22376#change-119689 * Author: jhawthorn (John Hawthorn) * Status: Closed * Assignee: jhawthorn (John Hawthorn) * Target version: 4.1 ---------------------------------------- Uninitialized classes, created by `Class.allocate`, are a recurring source of bugs (most recently #22341). Every place that inspects a class's superclass chain has to check whether the class has been initialized, and it's easy to forget one. I'd like to propose that we forbid Class.allocate, as we already do for several other core types (Proc, Method, UnboundMethod, Binding). To do this we: * Undefine the allocator for Class (`rb_undef_alloc_func`) * Replace `Class#initialize` with a Class.new singleton method that allocates and initializes in one step. * Define `Class#dup` and `Class#clone` so that copying a class still works `Class` can't be subclassed, so I don't think we need to worry about calling initialize. The only place I found `Class.allocate` via `gem-codesearch` were copies of ruby/spec, so I don't think forbidding that will cause problems. Before: ```ruby klass = Class.allocate Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError) klass.superclass # 'Class#superclass': uninitialized class (TypeError) ``` After: ```ruby Class.allocate # undefined method 'allocate' for class Class (NoMethodError) Class.instance_method(:allocate).bind_call(Class) # 'Class#allocate': allocator undefined for Class (TypeError) ``` PR: https://github.com/ruby/ruby/pull/18964 -- https://bugs.ruby-lang.org/
participants (4)
-
Eregon (Benoit Daloze) -
jhawthorn (John Hawthorn) -
ko1 (Koichi Sasada) -
matz (Yukihiro Matsumoto)