abcl icon indicating copy to clipboard operation
abcl copied to clipboard

Slots of custom slots are unbound on class redefinition

Open lockie opened this issue 1 year ago • 0 comments

Hey! I've stumbled upon a problem on defining custom slot initargs using metaclass and then redefining the class. Let me illustrate with an example:

(ql:quickload :closer-mop)

(defclass custom-slot (c2mop:standard-direct-slot-definition)
  ((custom-arg
    :accessor custom-arg
    :initarg :custom-arg
    :initform nil)))

(defclass custom-metaclass (c2mop:standard-class) ())

(defmethod c2mop:validate-superclass ((class custom-metaclass)
                                      (super c2mop:standard-class))
  t)

(defmethod c2mop:direct-slot-definition-class ((class custom-metaclass)
                                               &rest initargs)
  (declare (ignore class initargs))
  (find-class 'custom-slot))

(defmethod shared-initialize ((class custom-metaclass) slots &rest rest)
  (declare (ignorable rest))
  (unless (eq slots t)
    (dolist (slot (c2mop:class-direct-slots class))
      (format t "s-i custom-arg = ~a~%" (custom-arg slot))))
  (call-next-method))

(defclass my-class ()
  ((my-slot
    :accessor my-slot
    :initform 42
    :type fixnum
    :custom-arg 'bar))
  (:metaclass custom-metaclass))

(defclass my-class ()
  ((my-slot
    :accessor my-slot
    :initform 42
    :type fixnum
    :custom-arg 'buzz))
  (:metaclass custom-metaclass))

(print (my-slot (make-instance 'my-class)))

This example works correctly with SBCL, CCL, ECL, Allegro CL and CLISP, but fails in ABCL saying

#<THREAD "interpreter" native {2F6A2561}>: Debugger invoked on condition of type UNBOUND-SLOT
  The slot CUSTOM-ARG is unbound in the object #<CUSTOM-SLOT {27D76870}>.

(this happens when SHARED-INITIALIZE is called because of class redefinition).

Does my modest knowledge of MOP fail me, or is it a bug in ABCL?

lockie avatar Dec 26 '24 06:12 lockie