fix build with #!-SB-UNICODE
[sbcl.git] / tests / mop.impure.lisp
blob6015c6652f7b23b2773ec20d761fe0952f693805
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 Haibel 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 (find-method (fdefinition 'sb-mop:allocate-instance) () '(built-in-class))
370 (dolist (class-name '(fixnum bignum symbol))
371 (let ((class (find-class class-name)))
372 (multiple-value-bind (value error) (ignore-errors (allocate-instance class))
373 (assert (null value))
374 (assert (typep error 'error)))))
376 ;;; bug reported by David Morse: direct-subclass update protocol was broken
377 (defclass vegetable () ())
378 (defclass tomato (vegetable) ())
379 (assert (equal (list (find-class 'tomato)) (sb-mop:class-direct-subclasses (find-class 'vegetable))))
380 (defclass tomato () ())
381 (assert (null (sb-mop:class-direct-subclasses (find-class 'vegetable))))
383 ;;; bug 331: lazy creation of clos classes for defstructs
384 (defstruct bug-331-super)
385 (defstruct (bug-331-sub (:include bug-331-super)))
386 (let ((subs (sb-mop:class-direct-subclasses (find-class 'bug-331-super))))
387 (assert (= 1 (length subs)))
388 (assert (eq (car subs) (find-class 'bug-331-sub))))
389 ;;; (addendum to test for #331: conditions suffered the same problem)
390 (define-condition condition-bug-331-super () ())
391 (define-condition condition-bug-331-sub (condition-bug-331-super) ())
392 (let ((subs (sb-mop:class-direct-subclasses
393 (find-class 'condition-bug-331-super))))
394 (assert (= 1 (length subs)))
395 (assert (eq (car subs) (find-class 'condition-bug-331-sub))))
396 ;;; (addendum to the addendum: the fix for this revealed breakage in
397 ;;; REINITIALIZE-INSTANCE)
398 (define-condition condition-bug-331a () ((slot331a :reader slot331a)))
399 (reinitialize-instance (find-class 'condition-bug-331a))
400 (let* ((gf #'slot331a)
401 (methods (sb-mop:generic-function-methods gf)))
402 (assert (= (length methods) 1))
403 (assert (eq (car methods)
404 (find-method #'slot331a nil
405 (list (find-class 'condition-bug-331a))))))
407 ;;; detection of multiple class options in defclass, reported by Bruno Haible
408 (defclass option-class (standard-class)
409 ((option :accessor cl-option :initarg :my-option)))
410 (defmethod sb-pcl:validate-superclass ((c1 option-class) (c2 standard-class))
412 (multiple-value-bind (result error)
413 (ignore-errors (eval '(defclass option-class-instance ()
415 (:my-option bar)
416 (:my-option baz)
417 (:metaclass option-class))))
418 (assert (not result))
419 (assert error))
421 ;;; class as :metaclass
422 (assert (typep
423 (sb-mop:ensure-class-using-class
424 nil 'class-as-metaclass-test
425 :metaclass (find-class 'standard-class)
426 :name 'class-as-metaclass-test
427 :direct-superclasses (list (find-class 'standard-object)))
428 'class))
430 ;;; COMPUTE-DEFAULT-INITARGS protocol mismatch reported by Bruno
431 ;;; Haible
432 (defparameter *extra-initarg-value* 'extra)
433 (defclass custom-default-initargs-class (standard-class)
435 (defmethod compute-default-initargs ((class custom-default-initargs-class))
436 (let ((original-default-initargs
437 (remove-duplicates
438 (reduce #'append
439 (mapcar #'class-direct-default-initargs
440 (class-precedence-list class)))
441 :key #'car
442 :from-end t)))
443 (cons (list ':extra '*extra-initarg-value* #'(lambda () *extra-initarg-value*))
444 (remove ':extra original-default-initargs :key #'car))))
445 (defmethod validate-superclass ((c1 custom-default-initargs-class)
446 (c2 standard-class))
448 (defclass extra-initarg ()
449 ((slot :initarg :extra))
450 (:metaclass custom-default-initargs-class))
451 (assert (eq (slot-value (make-instance 'extra-initarg) 'slot) 'extra))
453 ;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS
454 (defclass standard-class-for-fsc ()
455 ((scforfsc-slot :initarg :scforfsc-slot :accessor scforfsc-slot)))
456 (defvar *standard-class-for-fsc*
457 (make-instance 'standard-class-for-fsc :scforfsc-slot 1))
458 (defclass fsc-with-standard-class-superclass
459 (standard-class-for-fsc funcallable-standard-object)
460 ((fsc-slot :initarg :fsc-slot :accessor fsc-slot))
461 (:metaclass funcallable-standard-class))
462 (defvar *fsc/scs*
463 (make-instance 'fsc-with-standard-class-superclass
464 :scforfsc-slot 2
465 :fsc-slot 3))
466 (assert (= (scforfsc-slot *standard-class-for-fsc*) 1))
467 (assert (= (scforfsc-slot *fsc/scs*) 2))
468 (assert (= (fsc-slot *fsc/scs*) 3))
469 (assert (subtypep 'fsc-with-standard-class-superclass 'function))
470 (assert (not (subtypep 'standard-class-for-fsc 'function)))
472 ;;; also check that our sanity check for functionness is good
473 (assert (raises-error?
474 (progn
475 (defclass bad-standard-class (funcallable-standard-object)
477 (:metaclass standard-class))
478 (make-instance 'bad-standard-class))))
479 (assert (raises-error?
480 (progn
481 (defclass bad-funcallable-standard-class (standard-object)
483 (:metaclass funcallable-standard-class))
484 (make-instance 'bad-funcallable-standard-class))))
486 ;;; we should be able to make classes with silly names
487 (make-instance 'standard-class :name 3)
488 (defclass foo () ())
489 (reinitialize-instance (find-class 'foo) :name '(a b))
491 ;;; classes (including anonymous ones) and eql-specializers should be
492 ;;; allowed to be specializers.
493 (defvar *anonymous-class*
494 (make-instance 'standard-class
495 :direct-superclasses (list (find-class 'standard-object))))
496 (defvar *object-of-anonymous-class*
497 (make-instance *anonymous-class*))
498 (eval `(defmethod method-on-anonymous-class ((obj ,*anonymous-class*)) 41))
499 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 41))
500 (eval `(defmethod method-on-anonymous-class
501 ((obj ,(intern-eql-specializer *object-of-anonymous-class*)))
502 42))
503 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 42))
505 ;;; accessors can cause early finalization, which caused confusion in
506 ;;; the system, leading to uncompileable TYPEP problems.
507 (defclass funcallable-class-for-typep ()
508 ((some-slot-with-accessor :accessor some-slot-with-accessor))
509 (:metaclass funcallable-standard-class))
510 (compile nil '(lambda (x) (typep x 'funcallable-class-for-typep)))
512 ;;; even anonymous classes should be valid types
513 (let* ((class1 (make-instance 'standard-class :direct-superclasses (list (find-class 'standard-object))))
514 (class2 (make-instance 'standard-class :direct-superclasses (list class1))))
515 (assert (subtypep class2 class1))
516 (assert (typep (make-instance class2) class1)))
518 ;;; ensure-class got its treatment of :metaclass wrong.
519 (ensure-class 'better-be-standard-class :direct-superclasses '(standard-object)
520 :metaclass 'standard-class
521 :metaclass 'funcallable-standard-class)
522 (assert (eq (class-of (find-class 'better-be-standard-class))
523 (find-class 'standard-class)))
525 ;;; CLASS-SLOTS should signal an error for classes that are not yet
526 ;;; finalized. Reported by Levente Meszaros on sbcl-devel.
527 (defclass has-slots-but-isnt-finalized () (a b c))
528 (let ((class (find-class 'has-slots-but-isnt-finalized)))
529 (assert (not (sb-mop:class-finalized-p class)))
530 (assert (raises-error? (sb-mop:class-slots class) sb-kernel::reference-condition)))
532 ;;; Check that MAKE-METHOD-LAMBDA which wraps the original body doesn't
533 ;;; break RETURN-FROM.
534 (defclass wrapped-generic (standard-generic-function)
536 (:metaclass sb-mop:funcallable-standard-class))
538 (defmethod sb-mop:make-method-lambda ((gf wrapped-generic) method lambda env)
539 (call-next-method gf method
540 `(lambda ,(second lambda)
541 (flet ((default () :default))
542 ,@(cddr lambda)))
543 env))
545 (defgeneric wrapped (x)
546 (:generic-function-class wrapped-generic))
548 (defmethod wrapped ((x cons))
549 (return-from wrapped (default)))
551 (with-test (:name :make-method-lambda-wrapping+return-from)
552 (assert (eq :default (wrapped (cons t t)))))
554 (with-test (:name :slow-method-is-fboundp)
555 (assert (fboundp '(sb-pcl::slow-method wrapped (cons))))
556 (assert (eq :default (funcall #'(sb-pcl::slow-method wrapped (cons)) (list (cons t t)) nil))))
558 ;;; Check that SLOT-BOUNDP-USING-CLASS doesn't confuse MAKE-INSTANCE
559 ;;; optimizations.
560 (defclass sbuc-mio-test-class (standard-class)
562 (defmethod validate-superclass ((class sbuc-mio-test-class)
563 (superclass standard-class))
565 (defvar *sbuc-counter* 0)
566 (defmethod slot-boundp-using-class ((class sbuc-mio-test-class)
567 (object t)
568 (slot standard-effective-slot-definition))
569 (incf *sbuc-counter*)
570 (call-next-method))
571 (defclass sbuc-mio-test-object ()
572 ((slot :initform 5 :accessor a-slot))
573 (:metaclass sbuc-mio-test-class))
574 (with-test (:name :sbuc-mio-test)
575 (assert (= 5 (funcall
576 (compile
578 `(lambda ()
579 (let ((object (make-instance 'sbuc-mio-test-object)))
580 (slot-value object 'slot)))))))
581 (assert (= 1 *sbuc-counter*)))
583 ;;; Redefining classes so that slot definition class changes.
584 (defclass func-slot-class (standard-class)
587 (defmethod sb-mop:validate-superclass ((class func-slot-class) (super standard-class))
590 (defclass func-slot-definition ()
591 ((function :initform nil :initarg :function :reader slotd-function)))
593 (defclass effective-func-slot-definition (sb-mop:standard-effective-slot-definition
594 func-slot-definition)
597 (defclass direct-func-slot-definition (sb-mop:standard-direct-slot-definition
598 func-slot-definition)
601 (defmethod sb-mop:slot-value-using-class ((class func-slot-class)
602 instance
603 (slotd effective-func-slot-definition))
604 (funcall (slotd-function slotd) (call-next-method)))
606 (defvar *func-slot*)
608 (defmethod sb-mop:effective-slot-definition-class ((class func-slot-class) &key)
609 (if *func-slot*
610 (find-class 'effective-func-slot-definition)
611 (call-next-method)))
613 (defmethod sb-mop:direct-slot-definition-class ((class func-slot-class) &key)
614 (find-class 'direct-func-slot-definition))
616 (defmethod sb-mop:compute-effective-slot-definition ((class func-slot-class) name dslotds)
617 (let* ((*func-slot* (some #'slotd-function dslotds))
618 (slotd (call-next-method)))
619 (when *func-slot*
620 (setf (slot-value slotd 'function) (fdefinition *func-slot*)))
621 slotd))
623 (with-test (:name :class-redefinition-changes-custom-slot-type)
624 (eval `(defclass func-slot-object ()
625 ((foo :initarg :foo :reader foofoo))
626 (:metaclass func-slot-class)))
627 (let ((x (cons t t)))
628 (assert (eq x (foofoo (make-instance 'func-slot-object :foo x)))))
629 (eval `(defclass func-slot-object ()
630 ((foo :initarg :foo :reader foofoo :function car))
631 (:metaclass func-slot-class)))
632 (let* ((x (cons t t))
633 (y (list x)))
634 (assert (eq x (foofoo (make-instance 'func-slot-object :foo y))))))
636 (with-test (:name :class-redefinition-changes-custom-slot-type-mio)
637 (eval `(defclass func-slot-object2 ()
638 ((foo :initarg :foo :reader foofoo))
639 (:metaclass func-slot-class)))
640 (let* ((x (cons t t))
641 (y (cons x x))
642 (o (make-instance 'func-slot-object2 :foo y)))
643 (assert (eq y (foofoo o)))
644 (eval `(defclass func-slot-object2 ()
645 ((foo :initarg :foo :reader foofoo :function car))
646 (:metaclass func-slot-class)))
647 (assert (eq x (foofoo o)))))
649 (defclass class-slot-removal-test ()
650 ((instance :initform 1)
651 (class :allocation :class :initform :ok)))
653 (defmethod update-instance-for-redefined-class ((x class-slot-removal-test) added removed plist &rest inits)
654 (throw 'update-instance
655 (list added removed plist inits)))
657 (with-test (:name :class-redefinition-removes-class-slot)
658 (let ((o (make-instance 'class-slot-removal-test)))
659 (assert (equal '(nil nil nil nil)
660 (catch 'update-instance
661 (eval `(defclass class-slot-removal-test ()
662 ((instance :initform 2))))
663 (slot-value o 'instance))))))
665 (defclass class-slot-add-test ()
666 ((instance :initform 1)))
668 (defmethod update-instance-for-redefined-class ((x class-slot-add-test) added removed plist &rest inits)
669 (throw 'update-instance
670 (list added removed plist inits)))
672 (with-test (:name :class-redefinition-adds-class-slot)
673 (let ((o (make-instance 'class-slot-add-test)))
674 (assert (equal '(nil nil nil nil)
675 (catch 'update-instance
676 (eval `(defclass class-slot-add-test ()
677 ((instance :initform 2)
678 (class :allocation :class :initform :ok))))
679 (slot-value o 'instance))))))
681 ;;;; success