From: "Eregon (Benoit Daloze) via ruby-core" Date: 2026-10-28T21:51:31+00:00 Subject: [ruby-core:127268] [Ruby Feature#22376] Undefine the allocator for Class 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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/