Parse raw slot data when printing structures in LDB
[sbcl.git] / tests / mop.impure.lisp
blob1fb3811528dbbb14f733f3e3dc99b730bd6b6dc4
1 ;;;; miscellaneous side-effectful tests of the MOP
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 ;;;; Note that the MOP is not in an entirely supported state.
15 ;;;; However, this seems a good a way as any of ensuring that we have
16 ;;;; no regressions.
18 (load "test-util.lisp")
20 (defpackage "MOP-TEST"
21 (:use "CL" "SB-MOP" "ASSERTOID" "TEST-UTIL"))
23 (in-package "MOP-TEST")
25 ;;; Readers for Class Metaobjects (pp. 212--214 of AMOP)
26 (defclass red-herring (forward-ref) ())
28 (assert (null (class-direct-slots (find-class 'forward-ref))))
29 (assert (null (class-direct-default-initargs
30 (find-class 'forward-ref))))
32 ;;; Readers for Generic Function Metaobjects (pp. 216--218 of AMOP)
33 (defgeneric fn-with-odd-arg-precedence (a b c)
34 (:argument-precedence-order b c a))
36 (assert (equal
37 (generic-function-lambda-list #'fn-with-odd-arg-precedence)
38 '(a b c)))
39 (assert (equal
40 (generic-function-argument-precedence-order #'fn-with-odd-arg-precedence)
41 '(b c a)))
42 ;;; Test for DOCUMENTATION's order, which was wrong until sbcl-0.7.8.39
43 (assert (equal
44 (generic-function-argument-precedence-order #'documentation)
45 (let ((ll (generic-function-lambda-list #'documentation)))
46 (list (nth 1 ll) (nth 0 ll)))))
48 (assert (null
49 (generic-function-declarations #'fn-with-odd-arg-precedence)))
50 (defgeneric gf-with-declarations (x)
51 (declare (optimize (speed 3)))
52 (declare (optimize (safety 0))))
53 (let ((decls (generic-function-declarations #'gf-with-declarations)))
54 (assert (= (length decls) 2))
55 (assert (member '(optimize (speed 3)) decls :test #'equal))
56 (assert (member '(optimize (safety 0)) decls :test #'equal)))
58 ;;; Readers for Slot Definition Metaobjects (pp. 221--224 of AMOP)
60 ;;; Ensure that SLOT-DEFINITION-ALLOCATION returns :INSTANCE/:CLASS as
61 ;;; appropriate.
62 (defclass sdm-test-class ()
63 ((an-instance-slot :accessor an-instance-slot)
64 (a-class-slot :allocation :class :accessor a-class-slot)))
65 (dolist (m (list (list #'an-instance-slot :instance)
66 (list #'a-class-slot :class)))
67 (let ((methods (generic-function-methods (car m))))
68 (assert (= (length methods) 1))
69 (assert (eq (slot-definition-allocation
70 (accessor-method-slot-definition
71 (car methods)))
72 (cadr m)))))
74 ;;; Class Finalization Protocol (see section 5.5.2 of AMOP)
75 (let ((finalized-count 0))
76 (defmethod finalize-inheritance :after ((x standard-class))
77 (incf finalized-count))
78 (defun get-count () finalized-count))
79 (defclass finalization-test-1 () ())
80 (make-instance 'finalization-test-1)
81 (assert (= (get-count) 1))
82 (defclass finalization-test-2 (finalization-test-3) ())
83 (assert (= (get-count) 1))
84 (defclass finalization-test-3 () ())
85 (make-instance 'finalization-test-3)
86 (assert (or (= (get-count) 2) (= (get-count) 3)))
87 (make-instance 'finalization-test-2)
88 (assert (= (get-count) 3))
90 ;;; Bits of FUNCALLABLE-STANDARD-CLASS are easy to break; make sure
91 ;;; that it is at least possible to define classes with that as a
92 ;;; metaclass.
93 (defclass gf-class (standard-generic-function) ()
94 (:metaclass funcallable-standard-class))
95 (defgeneric g (a b c)
96 (:generic-function-class gf-class))
98 ;;; until sbcl-0.7.12.47, PCL wasn't aware of some direct class
99 ;;; relationships. These aren't necessarily true, but are probably
100 ;;; not going to change often.
101 (dolist (x '(number array sequence character symbol))
102 (assert (eq (car (class-direct-superclasses (find-class x)))
103 (find-class t)))
104 (assert (member (find-class x)
105 (class-direct-subclasses (find-class t)))))
107 ;;; the class-prototype of the NULL class used to be some weird
108 ;;; standard-instance-like thing. Make sure it's actually NIL.
110 ;;; (and FIXME: eventually turn this into asserting that the prototype
111 ;;; of all built-in-classes is of the relevant type)
112 (assert (null (class-prototype (find-class 'null))))
114 ;;; simple consistency checks for the SB-MOP package: all of the
115 ;;; functionality specified in AMOP is in functions and classes:
116 (assert (null (loop for x being each external-symbol in "SB-MOP"
117 unless (or (fboundp x) (find-class x)) collect x)))
118 ;;; and all generic functions in SB-MOP have at least one specified
119 ;;; method, except for UPDATE-DEPENDENT
120 (assert (null (loop for x being each external-symbol in "SB-MOP"
121 unless (or (not (fboundp x))
122 (eq x 'update-dependent)
123 (not (typep (fdefinition x) 'generic-function))
124 (> (length (generic-function-methods
125 (fdefinition x)))
127 collect x)))
129 ;;; make sure that ENSURE-CLASS-USING-CLASS's arguments are the right
130 ;;; way round (!)
131 (defvar *e-c-u-c-arg-order* nil)
132 (defmethod ensure-class-using-class :after
133 (class (name (eql 'e-c-u-c-arg-order)) &key &allow-other-keys)
134 (setf *e-c-u-c-arg-order* t))
135 (defclass e-c-u-c-arg-orderoid () ())
136 (assert (null *e-c-u-c-arg-order*))
137 (defclass e-c-u-c-arg-order () ())
138 (assert (eq *e-c-u-c-arg-order* t))
140 ;;; verify that FIND-CLASS works after FINALIZE-INHERITANCE
141 (defclass automethod-class (standard-class) ())
142 (defmethod validate-superclass ((c1 automethod-class) (c2 standard-class))
144 (defmethod finalize-inheritance :after ((x automethod-class))
145 (format t "~&~S ~S~%" x (find-class (class-name x))))
146 (defclass automethod-object () ()
147 (:metaclass automethod-class))
148 (defvar *automethod-object* (make-instance 'automethod-object))
149 (assert (typep *automethod-object* 'automethod-object))
151 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION should take three arguments, one
152 ;;; of which is the name of the slot.
153 (defvar *compute-effective-slot-definition-count* 0)
154 (defmethod compute-effective-slot-definition :before
155 (class (name (eql 'foo)) dsds)
156 (incf *compute-effective-slot-definition-count*))
157 (defclass cesd-test-class ()
158 ((foo :initarg :foo)))
159 (make-instance 'cesd-test-class :foo 3)
160 ;;; FIXME: this assertion seems a little weak. I don't know why
161 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION gets called twice in this
162 ;;; sequence, nor whether that's compliant with AMOP. -- CSR,
163 ;;; 2003-04-17
164 (assert (> *compute-effective-slot-definition-count* 0))
166 ;;; this used to cause a nasty uncaught metacircularity in PCL.
167 (defclass substandard-method (standard-method) ())
168 (defgeneric substandard-defgeneric (x y)
169 (:method-class substandard-method)
170 (:method ((x number) (y number)) (+ x y))
171 (:method ((x string) (y string)) (concatenate 'string x y)))
172 (assert (= (substandard-defgeneric 1 2) 3))
173 (assert (string= (substandard-defgeneric "1" "2") "12"))
175 (let* ((x (find-class 'pathname))
176 (xs (class-direct-subclasses x)))
177 (assert (>= (length xs) 1))
178 (assert (member (find-class 'logical-pathname) xs)))
180 ;;; BUG 338: "MOP specializers as type specifiers"
181 ;;; (reported by Bruno Haible sbcl-devel 2004-06-11)
182 (let* ((m (defmethod eql-specialized-method ((x (eql 4.0))) 3.0))
183 (spec (first (sb-mop:method-specializers m))))
184 (assert (not (typep 1 spec)))
185 (assert (typep 4.0 spec)))
187 ;;; BUG #334, relating to programmatic addition of slots to a class
188 ;;; with COMPUTE-SLOTS.
190 ;;; FIXME: the DUMMY classes here are to prevent class finalization
191 ;;; before the compute-slots method is around. This should probably
192 ;;; be done by defining the COMPUTE-SLOTS methods on a metaclass,
193 ;;; which can be defined before.
195 ;;; a. adding an :allocation :instance slot
196 (defclass class-to-add-instance-slot (dummy-ctais) ())
197 (defmethod compute-slots ((c (eql (find-class 'class-to-add-instance-slot))))
198 (append (call-next-method)
199 (list (make-instance 'standard-effective-slot-definition
200 :name 'y
201 :allocation :instance))))
202 (defclass dummy-ctais () ((x :allocation :class)))
203 (finalize-inheritance (find-class 'class-to-add-instance-slot))
204 (assert (equal (mapcar #'slot-definition-allocation
205 (class-slots (find-class 'class-to-add-instance-slot)))
206 ;; FIXME: is the order really guaranteed?
207 '(:class :instance)))
208 (assert (typep (slot-definition-location
209 (cadr (class-slots (find-class 'class-to-add-instance-slot))))
210 'unsigned-byte))
211 #| (assert (typep (slot-definition-location (car ...)) '???)) |#
212 (let ((x (make-instance 'class-to-add-instance-slot)))
213 (assert (not (slot-boundp x 'x)))
214 (setf (slot-value x 'x) t)
215 (assert (not (slot-boundp x 'y)))
216 (setf (slot-value x 'y) 1)
217 (assert (= 1 (slot-value x 'y))))
218 (let ((x (make-instance 'class-to-add-instance-slot)))
219 (assert (slot-boundp x 'x))
220 (assert (eq t (slot-value x 'x)))
221 (assert (not (slot-boundp x 'y))))
223 ;;; b. adding an :allocation :class slot
224 (defclass class-to-add-class-slot (dummy-ctacs) ())
225 (defmethod compute-slots ((c (eql (find-class 'class-to-add-class-slot))))
226 (append (call-next-method)
227 (list (make-instance 'standard-effective-slot-definition
228 :name 'y
229 :allocation :class))))
230 (defclass dummy-ctacs () ((x :allocation :class)))
231 (finalize-inheritance (find-class 'class-to-add-class-slot))
232 (assert (equal (mapcar #'slot-definition-allocation
233 (class-slots (find-class 'class-to-add-class-slot)))
234 '(:class :class)))
235 (let ((x (make-instance 'class-to-add-class-slot)))
236 (assert (not (slot-boundp x 'x)))
237 (setf (slot-value x 'x) nil)
238 (assert (not (slot-boundp x 'y)))
239 (setf (slot-value x 'y) 1)
240 (assert (= 1 (slot-value x 'y))))
241 (let ((x (make-instance 'class-to-add-class-slot)))
242 (assert (slot-boundp x 'x))
243 (assert (eq nil (slot-value x 'x)))
244 (assert (slot-boundp x 'y))
245 (assert (= 1 (slot-value x 'y))))
246 ;;; extra paranoia: check that we haven't broken the instance-slot class
247 (let ((x (make-instance 'class-to-add-instance-slot)))
248 (assert (slot-boundp x 'x))
249 (assert (eq t (slot-value x 'x)))
250 (assert (not (slot-boundp x 'y))))
252 ;;;; the CTOR optimization was insufficiently careful about its
253 ;;;; assumptions: firstly, it failed with a failed AVER for
254 ;;;; non-standard-allocation slots:
255 (defclass class-with-frob-slot ()
256 ((frob-slot :initarg :frob-slot :allocation :frob)))
257 (handler-case
258 (funcall (compile nil '(lambda ()
259 (make-instance 'class-with-frob-slot
260 :frob-slot 1))))
261 (sb-int:bug (c) (error c))
262 (error () "Probably OK: haven't implemented SLOT-BOUNDP-USING-CLASS"))
263 ;;; secondly, it failed to take account of the fact that we might wish
264 ;;; to customize (setf slot-value-using-class)
265 (defclass class-with-special-ssvuc ()
266 ((some-slot :initarg :some-slot)))
267 (defvar *special-ssvuc-counter* 0)
268 (defmethod (setf slot-value-using-class) :before
269 (new-value class (instance class-with-special-ssvuc) slotd)
270 (incf *special-ssvuc-counter*))
271 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc
272 :some-slot 1)))))
273 (assert (= *special-ssvuc-counter* 0))
274 (funcall fun)
275 (assert (= *special-ssvuc-counter* 1))
276 (funcall fun)
277 (assert (= *special-ssvuc-counter* 2)))
278 ;;; and now with the customization after running the function once
279 (defclass class-with-special-ssvuc-2 ()
280 ((some-slot :initarg :some-slot)))
281 (defvar *special-ssvuc-counter-2* 0)
282 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc-2
283 :some-slot 1)))))
284 (assert (= *special-ssvuc-counter-2* 0))
285 (funcall fun)
286 (assert (= *special-ssvuc-counter-2* 0))
287 (defmethod (setf slot-value-using-class) :before
288 (new-value class (instance class-with-special-ssvuc-2) slotd)
289 (incf *special-ssvuc-counter-2*))
290 (funcall fun)
291 (assert (= *special-ssvuc-counter-2* 1)))
293 ;;; vicious metacycle detection and resolution wasn't good enough: it
294 ;;; didn't take account that the slots (and hence the slot readers)
295 ;;; might be inherited from superclasses. This example, due to Bruno
296 ;;; Haible, also tests programmatic addition of accessors.
297 (defclass auto-accessors-direct-slot-definition-class (standard-class)
298 ((containing-class-name :initarg :containing-class-name)))
299 (defmethod validate-superclass
300 ((c1 auto-accessors-direct-slot-definition-class) (c2 standard-class))
302 (defclass auto-accessors-class (standard-class)
304 (defmethod direct-slot-definition-class ((class auto-accessors-class)
305 &rest initargs)
306 (let ((dsd-class-name (gensym)))
307 (sb-pcl:ensure-class
308 dsd-class-name
309 :metaclass 'auto-accessors-direct-slot-definition-class
310 :direct-superclasses (list (find-class 'standard-direct-slot-definition))
311 :containing-class-name (class-name class))
312 (eval `(defmethod initialize-instance :after ((dsd ,dsd-class-name)
313 &rest args)
314 (when (and (null (slot-definition-readers dsd))
315 (null (slot-definition-writers dsd)))
316 (let* ((containing-class-name
317 (slot-value (class-of dsd) 'containing-class-name))
318 (accessor-name
319 (intern
320 (concatenate 'string
321 (symbol-name containing-class-name)
323 (symbol-name (slot-definition-name dsd)))
324 (symbol-package containing-class-name))))
325 (setf (slot-definition-readers dsd) (list accessor-name))
326 (setf (slot-definition-writers dsd)
327 (list (list 'setf accessor-name)))))))
328 (find-class dsd-class-name)))
329 (defmethod validate-superclass ((c1 auto-accessors-class) (c2 standard-class))
331 (defclass testclass15 ()
332 ((x :initarg :x) (y))
333 (:metaclass auto-accessors-class))
334 (let ((inst (make-instance 'testclass15 :x 12)))
335 (assert (equal (list (testclass15-x inst) (setf (testclass15-y inst) 13))
336 '(12 13))))
338 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-17: incorrect
339 ;;; handling of multiple values for non-standard slot-options
340 (progn
341 (defclass option-slot-definition (sb-mop:standard-direct-slot-definition)
342 ((option :accessor sl-option :initarg :my-option)))
343 (defclass option-slot-class (standard-class)
345 (defmethod sb-mop:direct-slot-definition-class
346 ((c option-slot-class) &rest args)
347 (declare (ignore args))
348 (find-class 'option-slot-definition))
349 (defmethod sb-mop:validate-superclass
350 ((c1 option-slot-class) (c2 standard-class))
352 (eval '(defclass test-multiple-slot-option-bug ()
353 ((x :my-option bar :my-option baz))
354 (:metaclass option-slot-class)))
355 (assert (null (set-difference
356 '(bar baz)
357 (sl-option (first (sb-mop:class-direct-slots
358 (find-class 'test-multiple-slot-option-bug))))))))
360 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-19: AMOP requires
361 ;;; that CLASS-PROTOYPE signals an error if the class is not yet finalized
362 (defclass prototype-not-finalized-sub (prototype-not-finalized-super) ())
363 (multiple-value-bind (val err)
364 (ignore-errors (sb-mop:class-prototype (find-class 'prototype-not-finalized-super)))
365 (assert (null val))
366 (assert (typep err 'error)))
368 ;;; AMOP says so
369 (with-test (:name (allocate-instance built-in-class error))
370 (dolist (class-name '(fixnum bignum symbol t))
371 (let ((class (find-class class-name)))
372 ;; actually T can't be a built-in-class
373 (when (typep class 'built-in-class)
374 (multiple-value-bind (value error)
375 (ignore-errors (allocate-instance class))
376 (assert (null value))
377 (assert (typep error 'error)))))))
379 ;;; bug reported by David Morse: direct-subclass update protocol was broken
380 (defclass vegetable () ())
381 (defclass tomato (vegetable) ())
382 (assert (equal (list (find-class 'tomato)) (sb-mop:class-direct-subclasses (find-class 'vegetable))))
383 (defclass tomato () ())
384 (assert (null (sb-mop:class-direct-subclasses (find-class 'vegetable))))
386 ;;; bug 331: lazy creation of clos classes for defstructs
387 (defstruct bug-331-super)
388 (defstruct (bug-331-sub (:include bug-331-super)))
389 (let ((subs (sb-mop:class-direct-subclasses (find-class 'bug-331-super))))
390 (assert (= 1 (length subs)))
391 (assert (eq (car subs) (find-class 'bug-331-sub))))
392 ;;; (addendum to test for #331: conditions suffered the same problem)
393 (define-condition condition-bug-331-super () ())
394 (define-condition condition-bug-331-sub (condition-bug-331-super) ())
395 (let ((subs (sb-mop:class-direct-subclasses
396 (find-class 'condition-bug-331-super))))
397 (assert (= 1 (length subs)))
398 (assert (eq (car subs) (find-class 'condition-bug-331-sub))))
399 ;;; (addendum to the addendum: the fix for this revealed breakage in
400 ;;; REINITIALIZE-INSTANCE)
401 (define-condition condition-bug-331a () ((slot331a :reader slot331a)))
402 (reinitialize-instance (find-class 'condition-bug-331a))
403 (let* ((gf #'slot331a)
404 (methods (sb-mop:generic-function-methods gf)))
405 (assert (= (length methods) 1))
406 (assert (eq (car methods)
407 (find-method #'slot331a nil
408 (list (find-class 'condition-bug-331a))))))
410 ;;; detection of multiple class options in defclass, reported by Bruno Haible
411 (defclass option-class (standard-class)
412 ((option :accessor cl-option :initarg :my-option)))
413 (defmethod sb-pcl:validate-superclass ((c1 option-class) (c2 standard-class))
415 (multiple-value-bind (result error)
416 (ignore-errors (eval '(defclass option-class-instance ()
418 (:my-option bar)
419 (:my-option baz)
420 (:metaclass option-class))))
421 (assert (not result))
422 (assert error))
424 ;;; class as :metaclass
425 (assert (typep
426 (sb-mop:ensure-class-using-class
427 nil 'class-as-metaclass-test
428 :metaclass (find-class 'standard-class)
429 :name 'class-as-metaclass-test
430 :direct-superclasses (list (find-class 'standard-object)))
431 'class))
433 ;;; COMPUTE-DEFAULT-INITARGS protocol mismatch reported by Bruno
434 ;;; Haible
435 (defparameter *extra-initarg-value* 'extra)
436 (defclass custom-default-initargs-class (standard-class)
438 (defmethod compute-default-initargs ((class custom-default-initargs-class))
439 (let ((original-default-initargs
440 (remove-duplicates
441 (reduce #'append
442 (mapcar #'class-direct-default-initargs
443 (class-precedence-list class)))
444 :key #'car
445 :from-end t)))
446 (cons (list ':extra '*extra-initarg-value* #'(lambda () *extra-initarg-value*))
447 (remove ':extra original-default-initargs :key #'car))))
448 (defmethod validate-superclass ((c1 custom-default-initargs-class)
449 (c2 standard-class))
451 (defclass extra-initarg ()
452 ((slot :initarg :extra))
453 (:metaclass custom-default-initargs-class))
454 (assert (eq (slot-value (make-instance 'extra-initarg) 'slot) 'extra))
456 ;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS
457 (defclass standard-class-for-fsc ()
458 ((scforfsc-slot :initarg :scforfsc-slot :accessor scforfsc-slot)))
459 (defvar *standard-class-for-fsc*
460 (make-instance 'standard-class-for-fsc :scforfsc-slot 1))
461 (defclass fsc-with-standard-class-superclass
462 (standard-class-for-fsc funcallable-standard-object)
463 ((fsc-slot :initarg :fsc-slot :accessor fsc-slot))
464 (:metaclass funcallable-standard-class))
465 (defvar *fsc/scs*
466 (make-instance 'fsc-with-standard-class-superclass
467 :scforfsc-slot 2
468 :fsc-slot 3))
469 (assert (= (scforfsc-slot *standard-class-for-fsc*) 1))
470 (assert (= (scforfsc-slot *fsc/scs*) 2))
471 (assert (= (fsc-slot *fsc/scs*) 3))
472 (assert (subtypep 'fsc-with-standard-class-superclass 'function))
473 (assert (not (subtypep 'standard-class-for-fsc 'function)))
475 ;;; also check that our sanity check for functionness is good
476 (assert-error
477 (progn
478 (defclass bad-standard-class (funcallable-standard-object)
480 (:metaclass standard-class))
481 (make-instance 'bad-standard-class)))
482 (assert-error
483 (progn
484 (defclass bad-funcallable-standard-class (standard-object)
486 (:metaclass funcallable-standard-class))
487 (make-instance 'bad-funcallable-standard-class)))
489 ;;; we should be able to make classes with silly names
490 (make-instance 'standard-class :name 3)
491 (defclass foo () ())
492 (reinitialize-instance (find-class 'foo) :name '(a b))
494 ;;; classes (including anonymous ones) and eql-specializers should be
495 ;;; allowed to be specializers.
496 (defvar *anonymous-class*
497 (make-instance 'standard-class
498 :direct-superclasses (list (find-class 'standard-object))))
499 (defvar *object-of-anonymous-class*
500 (make-instance *anonymous-class*))
501 (eval `(defmethod method-on-anonymous-class ((obj ,*anonymous-class*)) 41))
502 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 41))
503 (eval `(defmethod method-on-anonymous-class
504 ((obj ,(intern-eql-specializer *object-of-anonymous-class*)))
505 42))
506 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 42))
508 ;;; accessors can cause early finalization, which caused confusion in
509 ;;; the system, leading to uncompileable TYPEP problems.
510 (defclass funcallable-class-for-typep ()
511 ((some-slot-with-accessor :accessor some-slot-with-accessor))
512 (:metaclass funcallable-standard-class))
513 (compile nil '(lambda (x) (typep x 'funcallable-class-for-typep)))
515 ;;; even anonymous classes should be valid types
516 (let* ((class1 (make-instance 'standard-class :direct-superclasses (list (find-class 'standard-object))))
517 (class2 (make-instance 'standard-class :direct-superclasses (list class1))))
518 (assert (subtypep class2 class1))
519 (assert (typep (make-instance class2) class1)))
521 ;;; ensure-class got its treatment of :metaclass wrong.
522 (ensure-class 'better-be-standard-class :direct-superclasses '(standard-object)
523 :metaclass 'standard-class
524 :metaclass 'funcallable-standard-class)
525 (assert (eq (class-of (find-class 'better-be-standard-class))
526 (find-class 'standard-class)))
528 ;;; CLASS-SLOTS should signal an error for classes that are not yet
529 ;;; finalized. Reported by Levente Meszaros on sbcl-devel.
530 (defclass has-slots-but-isnt-finalized () (a b c))
531 (let ((class (find-class 'has-slots-but-isnt-finalized)))
532 (assert (not (sb-mop:class-finalized-p class)))
533 (assert-error (sb-mop:class-slots class) sb-kernel::reference-condition))
535 ;;; Check that MAKE-METHOD-LAMBDA which wraps the original body doesn't
536 ;;; break RETURN-FROM.
537 (defclass wrapped-generic (standard-generic-function)
539 (:metaclass sb-mop:funcallable-standard-class))
541 (defmethod sb-mop:make-method-lambda ((gf wrapped-generic) method lambda env)
542 (call-next-method gf method
543 `(lambda ,(second lambda)
544 (flet ((default () :default))
545 ,@(cddr lambda)))
546 env))
548 (defgeneric wrapped (x)
549 (:generic-function-class wrapped-generic))
551 (defmethod wrapped ((x cons))
552 (return-from wrapped (default)))
554 (with-test (:name :make-method-lambda-wrapping+return-from)
555 (assert (eq :default (wrapped (cons t t)))))
557 (with-test (:name :slow-method-is-fboundp)
558 (assert (fboundp '(sb-pcl::slow-method wrapped (cons))))
559 (assert (eq :default (funcall #'(sb-pcl::slow-method wrapped (cons)) (list (cons t t)) nil))))
561 ;;; Check that SLOT-BOUNDP-USING-CLASS doesn't confuse MAKE-INSTANCE
562 ;;; optimizations.
563 (defclass sbuc-mio-test-class (standard-class)
565 (defmethod validate-superclass ((class sbuc-mio-test-class)
566 (superclass standard-class))
568 (defvar *sbuc-counter* 0)
569 (defmethod slot-boundp-using-class ((class sbuc-mio-test-class)
570 (object t)
571 (slot standard-effective-slot-definition))
572 (incf *sbuc-counter*)
573 (call-next-method))
574 (defclass sbuc-mio-test-object ()
575 ((slot :initform 5 :accessor a-slot))
576 (:metaclass sbuc-mio-test-class))
577 (with-test (:name :sbuc-mio-test)
578 (assert (= 5 (funcall
579 (compile
581 `(lambda ()
582 (let ((object (make-instance 'sbuc-mio-test-object)))
583 (slot-value object 'slot)))))))
584 (assert (= 1 *sbuc-counter*)))
586 ;;; Redefining classes so that slot definition class changes.
587 (defclass func-slot-class (standard-class)
590 (defmethod sb-mop:validate-superclass ((class func-slot-class) (super standard-class))
593 (defclass func-slot-definition ()
594 ((function :initform nil :initarg :function :reader slotd-function)))
596 (defclass effective-func-slot-definition (sb-mop:standard-effective-slot-definition
597 func-slot-definition)
600 (defclass direct-func-slot-definition (sb-mop:standard-direct-slot-definition
601 func-slot-definition)
604 (defmethod sb-mop:slot-value-using-class ((class func-slot-class)
605 instance
606 (slotd effective-func-slot-definition))
607 (funcall (slotd-function slotd) (call-next-method)))
609 (defvar *func-slot*)
611 (defmethod sb-mop:effective-slot-definition-class ((class func-slot-class) &key)
612 (if *func-slot*
613 (find-class 'effective-func-slot-definition)
614 (call-next-method)))
616 (defmethod sb-mop:direct-slot-definition-class ((class func-slot-class) &key)
617 (find-class 'direct-func-slot-definition))
619 (defmethod sb-mop:compute-effective-slot-definition ((class func-slot-class) name dslotds)
620 (let* ((*func-slot* (some #'slotd-function dslotds))
621 (slotd (call-next-method)))
622 (when *func-slot*
623 (setf (slot-value slotd 'function) (fdefinition *func-slot*)))
624 slotd))
626 (with-test (:name :class-redefinition-changes-custom-slot-type)
627 (eval `(defclass func-slot-object ()
628 ((foo :initarg :foo :reader foofoo))
629 (:metaclass func-slot-class)))
630 (let ((x (cons t t)))
631 (assert (eq x (foofoo (make-instance 'func-slot-object :foo x)))))
632 (eval `(defclass func-slot-object ()
633 ((foo :initarg :foo :reader foofoo :function car))
634 (:metaclass func-slot-class)))
635 (let* ((x (cons t t))
636 (y (list x)))
637 (assert (eq x (foofoo (make-instance 'func-slot-object :foo y))))))
639 (with-test (:name :class-redefinition-changes-custom-slot-type-mio)
640 (eval `(defclass func-slot-object2 ()
641 ((foo :initarg :foo :reader foofoo))
642 (:metaclass func-slot-class)))
643 (let* ((x (cons t t))
644 (y (cons x x))
645 (o (make-instance 'func-slot-object2 :foo y)))
646 (assert (eq y (foofoo o)))
647 (eval `(defclass func-slot-object2 ()
648 ((foo :initarg :foo :reader foofoo :function car))
649 (:metaclass func-slot-class)))
650 (assert (eq x (foofoo o)))))
652 (defclass class-slot-removal-test ()
653 ((instance :initform 1)
654 (class :allocation :class :initform :ok)))
656 (defmethod update-instance-for-redefined-class ((x class-slot-removal-test) added removed plist &rest inits)
657 (throw 'update-instance
658 (list added removed plist inits)))
660 (with-test (:name :class-redefinition-removes-class-slot)
661 (let ((o (make-instance 'class-slot-removal-test)))
662 (assert (equal '(nil nil nil nil)
663 (catch 'update-instance
664 (eval `(defclass class-slot-removal-test ()
665 ((instance :initform 2))))
666 (slot-value o 'instance))))))
668 (defclass class-slot-add-test ()
669 ((instance :initform 1)))
671 (defmethod update-instance-for-redefined-class ((x class-slot-add-test) added removed plist &rest inits)
672 (throw 'update-instance
673 (list added removed plist inits)))
675 (with-test (:name :class-redefinition-adds-class-slot)
676 (let ((o (make-instance 'class-slot-add-test)))
677 (assert (equal '(nil nil nil nil)
678 (catch 'update-instance
679 (eval `(defclass class-slot-add-test ()
680 ((instance :initform 2)
681 (class :allocation :class :initform :ok))))
682 (slot-value o 'instance))))))
684 (defgeneric definitely-a-funcallable-instance (x))
685 (with-test (:name (set-funcallable-instance-function :typechecking))
686 (assert-error (set-funcallable-instance-function
687 (lambda (y) nil)
688 #'definitely-a-funcallable-instance)
689 type-error))
691 (with-test (:name (defstruct :nil-slot-name :bug-633911))
692 (defstruct nil-slot-name nil)
693 (let ((fun (compile nil '(lambda (x) (slot-value x 'nil)))))
694 (assert (= 3 (funcall fun (make-nil-slot-name :nil 3))))))
696 ;;;; success