Feature #22376
closedUndefine the allocator for Class
Description
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#initializewith a Class.new singleton method that allocates and initializes in one step. - Define
Class#dupandClass#cloneso 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:
klass = Class.allocate
Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError)
klass.superclass # 'Class#superclass': uninitialized class (TypeError)
After:
Updated by jhawthorn (John Hawthorn) 2 months ago
- Related to Feature #21852: New improved allocator function interface added
Updated by jhawthorn (John Hawthorn) 2 months ago
- Related to Feature #21963: A solution to completely avoid allocated-but-uninitialized objects added
Updated by Eregon (Benoit Daloze) about 2 months ago
+1, this definitely helps avoid many subtle bugs and simplifies the logic significantly (in fact I already advocated for this in previous tickets).
Updated by matz (Yukihiro Matsumoto) about 2 months ago
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.
Updated by ko1 (Koichi Sasada) about 1 month ago
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:
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.
Updated by Eregon (Benoit Daloze) about 1 month ago
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.
Updated by Eregon (Benoit Daloze) about 1 month ago
- 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