Actions
Feature #22376
openUndefine the allocator for Class
Feature #22376:
Undefine the allocator for Class
Status:
Open
Assignee:
-
Target version:
-
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) about 9 hours ago
- Related to Feature #21852: New improved allocator function interface added
Updated by jhawthorn (John Hawthorn) about 9 hours ago
- Related to Feature #21963: A solution to completely avoid allocated-but-uninitialized objects added
Actions