Stop saying 'can't open-code test of unknown type'.
[sbcl.git] / tests / mop.impure.lisp
blob644dd2ccfee2017a5b08a591cb9adf6886ebfc43
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 "compiler-test-util.lisp")
19 (defpackage "MOP-TEST"
20 (:use "CL" "SB-MOP" "ASSERTOID" "TEST-UTIL"))
22 (in-package "MOP-TEST")
24 ;;; AMOP says these are the defaults
25 (with-test (:name :standard-direct-superclasses)
26 (assert (equal (list (find-class 'standard-object))
27 (sb-mop:class-direct-superclasses (make-instance 'standard-class))))
28 (assert (equal (list (find-class 'sb-mop:funcallable-standard-object))
29 (sb-mop:class-direct-superclasses (make-instance 'sb-mop:funcallable-standard-class)))))
31 ;;; Readers for Class Metaobjects (pp. 212--214 of AMOP)
32 (defclass red-herring (forward-ref) ())
34 (assert (null (class-direct-slots (find-class 'forward-ref))))
35 (assert (null (class-direct-default-initargs
36 (find-class 'forward-ref))))
38 ;;; Readers for Generic Function Metaobjects (pp. 216--218 of AMOP)
39 (defgeneric fn-with-odd-arg-precedence (a b c)
40 (:argument-precedence-order b c a))
42 (assert (equal
43 (generic-function-lambda-list #'fn-with-odd-arg-precedence)
44 '(a b c)))
45 (assert (equal
46 (generic-function-argument-precedence-order #'fn-with-odd-arg-precedence)
47 '(b c a)))
48 ;;; Test for DOCUMENTATION's order, which was wrong until sbcl-0.7.8.39
49 (assert (equal
50 (generic-function-argument-precedence-order #'documentation)
51 (let ((ll (generic-function-lambda-list #'documentation)))
52 (list (nth 1 ll) (nth 0 ll)))))
54 (assert (null
55 (generic-function-declarations #'fn-with-odd-arg-precedence)))
56 (defgeneric gf-with-declarations (x)
57 (declare (optimize (speed 3)))
58 (declare (optimize (safety 0))))
59 (let ((decls (generic-function-declarations #'gf-with-declarations)))
60 (assert (= (length decls) 2))
61 (assert (member '(optimize (speed 3)) decls :test #'equal))
62 (assert (member '(optimize (safety 0)) decls :test #'equal)))
64 ;;; Readers for Slot Definition Metaobjects (pp. 221--224 of AMOP)
66 ;;; Ensure that SLOT-DEFINITION-ALLOCATION returns :INSTANCE/:CLASS as
67 ;;; appropriate.
68 (defclass sdm-test-class ()
69 ((an-instance-slot :accessor an-instance-slot)
70 (a-class-slot :allocation :class :accessor a-class-slot)))
71 (dolist (m (list (list #'an-instance-slot :instance)
72 (list #'a-class-slot :class)))
73 (let ((methods (generic-function-methods (car m))))
74 (assert (= (length methods) 1))
75 (assert (eq (slot-definition-allocation
76 (accessor-method-slot-definition
77 (car methods)))
78 (cadr m)))))
80 ;;; Class Finalization Protocol (see section 5.5.2 of AMOP)
81 (let ((finalized-count 0))
82 (defmethod finalize-inheritance :after ((x standard-class))
83 (incf finalized-count))
84 (defun get-count () finalized-count))
85 (defclass finalization-test-1 () ())
86 (make-instance 'finalization-test-1)
87 (assert (= (get-count) 1))
88 (defclass finalization-test-2 (finalization-test-3) ())
89 (assert (= (get-count) 1))
90 (defclass finalization-test-3 () ())
91 (make-instance 'finalization-test-3)
92 (assert (or (= (get-count) 2) (= (get-count) 3)))
93 (make-instance 'finalization-test-2)
94 (assert (= (get-count) 3))
96 ;;; Bits of FUNCALLABLE-STANDARD-CLASS are easy to break; make sure
97 ;;; that it is at least possible to define classes with that as a
98 ;;; metaclass.
99 (defclass gf-class (standard-generic-function) ()
100 (:metaclass funcallable-standard-class))
101 (defgeneric g (a b c)
102 (:generic-function-class gf-class))
104 ;;; until sbcl-0.7.12.47, PCL wasn't aware of some direct class
105 ;;; relationships. These aren't necessarily true, but are probably
106 ;;; not going to change often.
107 (dolist (x '(number array sequence character symbol))
108 (assert (eq (car (class-direct-superclasses (find-class x)))
109 (find-class t)))
110 (assert (member (find-class x)
111 (class-direct-subclasses (find-class t)))))
113 ;;; the class-prototype of the NULL class used to be some weird
114 ;;; standard-instance-like thing. Make sure it's actually NIL.
116 ;;; (and FIXME: eventually turn this into asserting that the prototype
117 ;;; of all built-in-classes is of the relevant type)
118 (assert (null (class-prototype (find-class 'null))))
120 ;;; simple consistency checks for the SB-MOP package: all of the
121 ;;; functionality specified in AMOP is in functions and classes:
122 (assert (null (loop for x being each external-symbol in "SB-MOP"
123 unless (or (fboundp x) (find-class x)) collect x)))
124 ;;; and all generic functions in SB-MOP have at least one specified
125 ;;; method, except for UPDATE-DEPENDENT
126 (assert (null (loop for x being each external-symbol in "SB-MOP"
127 unless (or (not (fboundp x))
128 (eq x 'update-dependent)
129 (not (typep (fdefinition x) 'generic-function))
130 (> (length (generic-function-methods
131 (fdefinition x)))
133 collect x)))
135 ;;; make sure that ENSURE-CLASS-USING-CLASS's arguments are the right
136 ;;; way round (!)
137 (defvar *e-c-u-c-arg-order* nil)
138 (defmethod ensure-class-using-class :after
139 (class (name (eql 'e-c-u-c-arg-order)) &key &allow-other-keys)
140 (setf *e-c-u-c-arg-order* t))
141 (defclass e-c-u-c-arg-orderoid () ())
142 (assert (null *e-c-u-c-arg-order*))
143 (defclass e-c-u-c-arg-order () ())
144 (assert (eq *e-c-u-c-arg-order* t))
146 ;;; verify that FIND-CLASS works after FINALIZE-INHERITANCE
147 (defclass automethod-class (standard-class) ())
148 (defmethod validate-superclass ((c1 automethod-class) (c2 standard-class))
150 (defmethod finalize-inheritance :after ((x automethod-class))
151 ;; not sure what this output demonstrated
152 (format (make-broadcast-stream) "~&~S ~S~%" x (find-class (class-name x))))
153 (defclass automethod-object () ()
154 (:metaclass automethod-class))
155 (defvar *automethod-object* (make-instance 'automethod-object))
156 (assert (typep *automethod-object* 'automethod-object))
158 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION should take three arguments, one
159 ;;; of which is the name of the slot.
160 (defvar *compute-effective-slot-definition-count* 0)
161 (defmethod compute-effective-slot-definition :before
162 (class (name (eql 'foo)) dsds)
163 (incf *compute-effective-slot-definition-count*))
164 (defclass cesd-test-class ()
165 ((foo :initarg :foo)))
166 (make-instance 'cesd-test-class :foo 3)
167 ;;; FIXME: this assertion seems a little weak. I don't know why
168 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION gets called twice in this
169 ;;; sequence, nor whether that's compliant with AMOP. -- CSR,
170 ;;; 2003-04-17
171 (assert (> *compute-effective-slot-definition-count* 0))
173 ;;; this used to cause a nasty uncaught metacircularity in PCL.
174 (defclass substandard-method (standard-method) ())
175 (defgeneric substandard-defgeneric (x y)
176 (:method-class substandard-method)
177 (:method ((x number) (y number)) (+ x y))
178 (:method ((x string) (y string)) (concatenate 'string x y)))
179 (assert (= (substandard-defgeneric 1 2) 3))
180 (assert (string= (substandard-defgeneric "1" "2") "12"))
182 (let* ((x (find-class 'pathname))
183 (xs (class-direct-subclasses x)))
184 (assert (>= (length xs) 1))
185 (assert (member (find-class 'logical-pathname) xs)))
187 ;;; BUG 338: "MOP specializers as type specifiers"
188 ;;; (reported by Bruno Haible sbcl-devel 2004-06-11)
189 (with-test (:name :eql-specializer-as-type)
190 (let* ((m (defmethod eql-specialized-method ((x (eql 4.0))) 3.0))
191 (spec (first (sb-mop:method-specializers m))))
192 (declare (notinline typep)) ; in case of SSC (sufficiently/super smart compiler)
193 (assert (not (typep 1 spec)))
194 (assert (typep 4.0 spec))
195 ;; TYPEP on spec should not cons. It used to cons 14 words:
196 ;; 6 words for %MAKE-MEMBER-TYPE
197 ;; 4 words for ALLOC-XSET
198 ;; 1 cons in MAKE-EQL-TYPE
199 ;; 1 cons in ADD-TO-XSET
200 ;; The %BITS slot in CTYPE is (unsigned-byte 32) so it's raw for 32-bit word size,
201 ;; which meeans we can't DX-allocate the temporary key in NEW-CTYPE unless the
202 ;; architecture allows raw words on the stack.
203 #+(and (not interpreter) (or 64-bit c-stack-is-control-stack))
204 (ctu:assert-no-consing (typep 4.0 spec))))
206 ;;; BUG #334, relating to programmatic addition of slots to a class
207 ;;; with COMPUTE-SLOTS.
209 ;;; FIXME: the DUMMY classes here are to prevent class finalization
210 ;;; before the compute-slots method is around. This should probably
211 ;;; be done by defining the COMPUTE-SLOTS methods on a metaclass,
212 ;;; which can be defined before.
214 ;;; a. adding an :allocation :instance slot
215 (defclass class-to-add-instance-slot (dummy-ctais) ())
216 (defmethod compute-slots ((c (eql (find-class 'class-to-add-instance-slot))))
217 (append (call-next-method)
218 (list (make-instance 'standard-effective-slot-definition
219 :name 'y
220 :allocation :instance))))
221 (defclass dummy-ctais () ((x :allocation :class)))
222 (finalize-inheritance (find-class 'class-to-add-instance-slot))
223 (assert (equal (mapcar #'slot-definition-allocation
224 (class-slots (find-class 'class-to-add-instance-slot)))
225 ;; FIXME: is the order really guaranteed?
226 '(:class :instance)))
227 (assert (typep (slot-definition-location
228 (cadr (class-slots (find-class 'class-to-add-instance-slot))))
229 'unsigned-byte))
230 #| (assert (typep (slot-definition-location (car ...)) '???)) |#
231 (let ((x (make-instance 'class-to-add-instance-slot)))
232 (assert (not (slot-boundp x 'x)))
233 (setf (slot-value x 'x) t)
234 (assert (not (slot-boundp x 'y)))
235 (setf (slot-value x 'y) 1)
236 (assert (= 1 (slot-value x 'y))))
237 (let ((x (make-instance 'class-to-add-instance-slot)))
238 (assert (slot-boundp x 'x))
239 (assert (eq t (slot-value x 'x)))
240 (assert (not (slot-boundp x 'y))))
242 ;;; b. adding an :allocation :class slot
243 (defclass class-to-add-class-slot (dummy-ctacs) ())
244 (defmethod compute-slots ((c (eql (find-class 'class-to-add-class-slot))))
245 (append (call-next-method)
246 (list (make-instance 'standard-effective-slot-definition
247 :name 'y
248 :allocation :class))))
249 (defclass dummy-ctacs () ((x :allocation :class)))
250 (finalize-inheritance (find-class 'class-to-add-class-slot))
251 (assert (equal (mapcar #'slot-definition-allocation
252 (class-slots (find-class 'class-to-add-class-slot)))
253 '(:class :class)))
254 (let ((x (make-instance 'class-to-add-class-slot)))
255 (assert (not (slot-boundp x 'x)))
256 (setf (slot-value x 'x) nil)
257 (assert (not (slot-boundp x 'y)))
258 (setf (slot-value x 'y) 1)
259 (assert (= 1 (slot-value x 'y))))
260 (let ((x (make-instance 'class-to-add-class-slot)))
261 (assert (slot-boundp x 'x))
262 (assert (eq nil (slot-value x 'x)))
263 (assert (slot-boundp x 'y))
264 (assert (= 1 (slot-value x 'y))))
265 ;;; extra paranoia: check that we haven't broken the instance-slot class
266 (let ((x (make-instance 'class-to-add-instance-slot)))
267 (assert (slot-boundp x 'x))
268 (assert (eq t (slot-value x 'x)))
269 (assert (not (slot-boundp x 'y))))
271 ;;;; the CTOR optimization was insufficiently careful about its
272 ;;;; assumptions: firstly, it failed with a failed AVER for
273 ;;;; non-standard-allocation slots:
274 (defclass class-with-frob-slot ()
275 ((frob-slot :initarg :frob-slot :allocation :frob)))
276 (handler-case
277 (funcall (compile nil '(lambda ()
278 (make-instance 'class-with-frob-slot
279 :frob-slot 1))))
280 (sb-int:bug (c) (error c))
281 (error () "Probably OK: haven't implemented SLOT-BOUNDP-USING-CLASS"))
282 ;;; secondly, it failed to take account of the fact that we might wish
283 ;;; to customize (setf slot-value-using-class)
284 (defclass class-with-special-ssvuc ()
285 ((some-slot :initarg :some-slot)))
286 (defvar *special-ssvuc-counter* 0)
287 (defmethod (setf slot-value-using-class) :before
288 (new-value class (instance class-with-special-ssvuc) slotd)
289 (incf *special-ssvuc-counter*))
290 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc
291 :some-slot 1)))))
292 (assert (= *special-ssvuc-counter* 0))
293 (funcall fun)
294 (assert (= *special-ssvuc-counter* 1))
295 (funcall fun)
296 (assert (= *special-ssvuc-counter* 2)))
297 ;;; and now with the customization after running the function once
298 (defclass class-with-special-ssvuc-2 ()
299 ((some-slot :initarg :some-slot)))
300 (defvar *special-ssvuc-counter-2* 0)
301 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc-2
302 :some-slot 1)))))
303 (assert (= *special-ssvuc-counter-2* 0))
304 (funcall fun)
305 (assert (= *special-ssvuc-counter-2* 0))
306 (defmethod (setf slot-value-using-class) :before
307 (new-value class (instance class-with-special-ssvuc-2) slotd)
308 (incf *special-ssvuc-counter-2*))
309 (funcall fun)
310 (assert (= *special-ssvuc-counter-2* 1)))
312 ;;; vicious metacycle detection and resolution wasn't good enough: it
313 ;;; didn't take account that the slots (and hence the slot readers)
314 ;;; might be inherited from superclasses. This example, due to Bruno
315 ;;; Haible, also tests programmatic addition of accessors.
316 (defclass auto-accessors-direct-slot-definition-class (standard-class)
317 ((containing-class-name :initarg :containing-class-name)))
318 (defmethod validate-superclass
319 ((c1 auto-accessors-direct-slot-definition-class) (c2 standard-class))
321 (defclass auto-accessors-class (standard-class)
323 (defmethod direct-slot-definition-class ((class auto-accessors-class)
324 &rest initargs)
325 (declare (ignore initargs))
326 (let ((dsd-class-name (gensym)))
327 (sb-mop:ensure-class
328 dsd-class-name
329 :metaclass 'auto-accessors-direct-slot-definition-class
330 :direct-superclasses (list (find-class 'standard-direct-slot-definition))
331 :containing-class-name (class-name class))
332 (eval `(defmethod initialize-instance :after ((dsd ,dsd-class-name)
333 &rest args)
334 (declare (ignore args))
335 (when (and (null (slot-definition-readers dsd))
336 (null (slot-definition-writers dsd)))
337 (let* ((containing-class-name
338 (slot-value (class-of dsd) 'containing-class-name))
339 (accessor-name
340 (intern
341 (concatenate 'string
342 (symbol-name containing-class-name)
344 (symbol-name (slot-definition-name dsd)))
345 (symbol-package containing-class-name))))
346 (setf (slot-definition-readers dsd) (list accessor-name))
347 (setf (slot-definition-writers dsd)
348 (list (list 'setf accessor-name)))))))
349 (find-class dsd-class-name)))
350 (defmethod validate-superclass ((c1 auto-accessors-class) (c2 standard-class))
352 (defclass testclass15 ()
353 ((x :initarg :x) (y))
354 (:metaclass auto-accessors-class))
355 (let ((inst (make-instance 'testclass15 :x 12)))
356 (assert (equal (list (testclass15-x inst) (setf (testclass15-y inst) 13))
357 '(12 13))))
359 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-17: incorrect
360 ;;; handling of multiple values for non-standard slot-options
361 (progn
362 (defclass option-slot-definition (sb-mop:standard-direct-slot-definition)
363 ((option :accessor sl-option :initarg :my-option)))
364 (defclass option-slot-class (standard-class)
366 (defmethod sb-mop:direct-slot-definition-class
367 ((c option-slot-class) &rest args)
368 (declare (ignore args))
369 (find-class 'option-slot-definition))
370 (defmethod sb-mop:validate-superclass
371 ((c1 option-slot-class) (c2 standard-class))
373 (eval '(defclass test-multiple-slot-option-bug ()
374 ((x :my-option bar :my-option baz))
375 (:metaclass option-slot-class)))
376 (assert (null (set-difference
377 '(bar baz)
378 (sl-option (first (sb-mop:class-direct-slots
379 (find-class 'test-multiple-slot-option-bug))))))))
381 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-19: AMOP requires
382 ;;; that CLASS-PROTOYPE signals an error if the class is not yet finalized
383 (defclass prototype-not-finalized-sub (prototype-not-finalized-super) ())
384 (multiple-value-bind (val err)
385 (ignore-errors (sb-mop:class-prototype (find-class 'prototype-not-finalized-super)))
386 (assert (null val))
387 (assert (typep err 'error)))
389 ;;; AMOP says so
390 (with-test (:name (allocate-instance built-in-class error))
391 (dolist (class-name '(fixnum bignum symbol t))
392 (let ((class (find-class class-name)))
393 ;; actually T can't be a built-in-class
394 (when (typep class 'built-in-class)
395 (multiple-value-bind (value error)
396 (ignore-errors (allocate-instance class))
397 (assert (null value))
398 (assert (typep error 'error)))))))
400 ;;; bug reported by David Morse: direct-subclass update protocol was broken
401 (defclass vegetable () ())
402 (defclass tomato (vegetable) ())
403 (assert (equal (list (find-class 'tomato)) (sb-mop:class-direct-subclasses (find-class 'vegetable))))
404 (defclass tomato () ())
405 (assert (null (sb-mop:class-direct-subclasses (find-class 'vegetable))))
407 ;;; bug 331: lazy creation of clos classes for defstructs
408 (defstruct bug-331-super)
409 (defstruct (bug-331-sub (:include bug-331-super)))
410 (let ((subs (sb-mop:class-direct-subclasses (find-class 'bug-331-super))))
411 (assert (= 1 (length subs)))
412 (assert (eq (car subs) (find-class 'bug-331-sub))))
413 ;;; (addendum to test for #331: conditions suffered the same problem)
414 (define-condition condition-bug-331-super () ())
415 (define-condition condition-bug-331-sub (condition-bug-331-super) ())
416 (let ((subs (sb-mop:class-direct-subclasses
417 (find-class 'condition-bug-331-super))))
418 (assert (= 1 (length subs)))
419 (assert (eq (car subs) (find-class 'condition-bug-331-sub))))
420 ;;; (addendum to the addendum: the fix for this revealed breakage in
421 ;;; REINITIALIZE-INSTANCE)
422 (define-condition condition-bug-331a () ((slot331a :reader slot331a)))
423 (reinitialize-instance (find-class 'condition-bug-331a))
424 (let* ((gf #'slot331a)
425 (methods (sb-mop:generic-function-methods gf)))
426 (assert (= (length methods) 1))
427 (assert (eq (car methods)
428 (find-method #'slot331a nil
429 (list (find-class 'condition-bug-331a))))))
431 ;;; detection of multiple class options in defclass, reported by Bruno Haible
432 (defclass option-class (standard-class)
433 ((option :accessor cl-option :initarg :my-option)))
434 (defmethod sb-mop:validate-superclass ((c1 option-class) (c2 standard-class))
436 (multiple-value-bind (result error)
437 (ignore-errors (eval '(defclass option-class-instance ()
439 (:my-option bar)
440 (:my-option baz)
441 (:metaclass option-class))))
442 (assert (not result))
443 (assert error))
445 ;;; class as :metaclass
446 (assert (typep
447 (sb-mop:ensure-class-using-class
448 nil 'class-as-metaclass-test
449 :metaclass (find-class 'standard-class)
450 :name 'class-as-metaclass-test
451 :direct-superclasses (list (find-class 'standard-object)))
452 'class))
454 ;;; COMPUTE-DEFAULT-INITARGS protocol mismatch reported by Bruno
455 ;;; Haible
456 (defparameter *extra-initarg-value* 'extra)
457 (defclass custom-default-initargs-class (standard-class)
459 (defmethod compute-default-initargs ((class custom-default-initargs-class))
460 (let ((original-default-initargs
461 (remove-duplicates
462 (reduce #'append
463 (mapcar #'class-direct-default-initargs
464 (class-precedence-list class)))
465 :key #'car
466 :from-end t)))
467 (cons (list ':extra '*extra-initarg-value* #'(lambda () *extra-initarg-value*))
468 (remove ':extra original-default-initargs :key #'car))))
469 (defmethod validate-superclass ((c1 custom-default-initargs-class)
470 (c2 standard-class))
472 (defclass extra-initarg ()
473 ((slot :initarg :extra))
474 (:metaclass custom-default-initargs-class))
475 (assert (eq (slot-value (make-instance 'extra-initarg) 'slot) 'extra))
477 ;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS
478 (defclass standard-class-for-fsc ()
479 ((scforfsc-slot :initarg :scforfsc-slot :accessor scforfsc-slot)))
480 (defvar *standard-class-for-fsc*
481 (make-instance 'standard-class-for-fsc :scforfsc-slot 1))
482 (defclass fsc-with-standard-class-superclass
483 (standard-class-for-fsc funcallable-standard-object)
484 ((fsc-slot :initarg :fsc-slot :accessor fsc-slot))
485 (:metaclass funcallable-standard-class))
486 (defvar *fsc/scs*
487 (make-instance 'fsc-with-standard-class-superclass
488 :scforfsc-slot 2
489 :fsc-slot 3))
490 (assert (= (scforfsc-slot *standard-class-for-fsc*) 1))
491 (assert (= (scforfsc-slot *fsc/scs*) 2))
492 (assert (= (fsc-slot *fsc/scs*) 3))
493 (assert (subtypep 'fsc-with-standard-class-superclass 'function))
494 (assert (not (subtypep 'standard-class-for-fsc 'function)))
496 ;;; also check that our sanity check for functionness is good
497 (assert-error
498 (progn
499 (defclass bad-standard-class (funcallable-standard-object)
501 (:metaclass standard-class))
502 (make-instance 'bad-standard-class)))
503 (assert-error
504 (progn
505 (defclass bad-funcallable-standard-class (standard-object)
507 (:metaclass funcallable-standard-class))
508 (make-instance 'bad-funcallable-standard-class)))
510 ;;; we should be able to make classes with silly names
511 (make-instance 'standard-class :name 3)
512 (defclass foo () ())
513 (reinitialize-instance (find-class 'foo) :name '(a b))
515 ;;; classes (including anonymous ones) and eql-specializers should be
516 ;;; allowed to be specializers.
517 (defvar *anonymous-class*
518 (make-instance 'standard-class
519 :direct-superclasses (list (find-class 'standard-object))))
520 (defvar *object-of-anonymous-class*
521 (make-instance *anonymous-class*))
522 (eval `(defmethod method-on-anonymous-class ((obj ,*anonymous-class*)) 41))
523 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 41))
524 (eval `(defmethod method-on-anonymous-class
525 ((obj ,(intern-eql-specializer *object-of-anonymous-class*)))
526 42))
527 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 42))
529 ;;; accessors can cause early finalization, which caused confusion in
530 ;;; the system, leading to uncompileable TYPEP problems.
531 (defclass funcallable-class-for-typep ()
532 ((some-slot-with-accessor :accessor some-slot-with-accessor))
533 (:metaclass funcallable-standard-class))
534 (compile nil '(lambda (x) (typep x 'funcallable-class-for-typep)))
536 ;;; even anonymous classes should be valid types
537 (let* ((class1 (make-instance 'standard-class :direct-superclasses (list (find-class 'standard-object))))
538 (class2 (make-instance 'standard-class :direct-superclasses (list class1))))
539 (assert (subtypep class2 class1))
540 (assert (typep (make-instance class2) class1)))
542 ;;; ensure-class got its treatment of :metaclass wrong.
543 (ensure-class 'better-be-standard-class :direct-superclasses '(standard-object)
544 :metaclass 'standard-class
545 :metaclass 'funcallable-standard-class)
546 (assert (eq (class-of (find-class 'better-be-standard-class))
547 (find-class 'standard-class)))
549 ;;; CLASS-SLOTS should signal an error for classes that are not yet
550 ;;; finalized. Reported by Levente Meszaros on sbcl-devel.
551 (defclass has-slots-but-isnt-finalized () (a b c))
552 (let ((class (find-class 'has-slots-but-isnt-finalized)))
553 (assert (not (sb-mop:class-finalized-p class)))
554 (assert-error (sb-mop:class-slots class) sb-kernel::reference-condition))
556 ;;; Check that MAKE-METHOD-LAMBDA which wraps the original body doesn't
557 ;;; break RETURN-FROM.
558 (defclass wrapped-generic (standard-generic-function)
560 (:metaclass sb-mop:funcallable-standard-class))
562 (defmethod sb-mop:make-method-lambda ((gf wrapped-generic) method lambda env)
563 (call-next-method gf method
564 `(lambda ,(second lambda)
565 (flet ((default () :default))
566 ,@(cddr lambda)))
567 env))
569 (defgeneric wrapped (x)
570 (:generic-function-class wrapped-generic))
572 (defmethod wrapped ((x cons))
573 (return-from wrapped (default)))
575 (with-test (:name :make-method-lambda-wrapping+return-from)
576 (assert (eq :default (wrapped (cons t t)))))
578 ;; This test tests something a little shady - that a method
579 ;; are accessible by way of FDEFNs. They aren't all.
580 ;; See the comment in src/pcl/low.lisp at SET-FUN-NAME.
581 (with-test (:name :slow-method-is-fboundp)
582 (assert (fboundp '(sb-pcl::slow-method wrapped (cons))))
583 (assert (eq :default (funcall #'(sb-pcl::slow-method wrapped (cons)) (list (cons t t)) nil))))
585 ;;; Check that SLOT-BOUNDP-USING-CLASS doesn't confuse MAKE-INSTANCE
586 ;;; optimizations.
587 (defclass sbuc-mio-test-class (standard-class)
589 (defmethod validate-superclass ((class sbuc-mio-test-class)
590 (superclass standard-class))
592 (defvar *sbuc-counter* 0)
593 (defmethod slot-boundp-using-class ((class sbuc-mio-test-class)
594 (object t)
595 (slot standard-effective-slot-definition))
596 (incf *sbuc-counter*)
597 (call-next-method))
598 (defclass sbuc-mio-test-object ()
599 ((slot :initform 5 :accessor a-slot))
600 (:metaclass sbuc-mio-test-class))
601 (with-test (:name :sbuc-mio-test)
602 (assert (= 5 (funcall
603 (compile
605 `(lambda ()
606 (let ((object (make-instance 'sbuc-mio-test-object)))
607 (slot-value object 'slot)))))))
608 (assert (= 1 *sbuc-counter*)))
610 ;;; Redefining classes so that slot definition class changes.
611 (defclass func-slot-class (standard-class)
614 (defmethod sb-mop:validate-superclass ((class func-slot-class) (super standard-class))
617 (defclass func-slot-definition ()
618 ((function :initform nil :initarg :function :reader slotd-function)))
620 (defclass effective-func-slot-definition (sb-mop:standard-effective-slot-definition
621 func-slot-definition)
624 (defclass direct-func-slot-definition (sb-mop:standard-direct-slot-definition
625 func-slot-definition)
628 (defmethod sb-mop:slot-value-using-class ((class func-slot-class)
629 instance
630 (slotd effective-func-slot-definition))
631 (funcall (slotd-function slotd) (call-next-method)))
633 (defvar *func-slot*)
635 (defmethod sb-mop:effective-slot-definition-class ((class func-slot-class) &key)
636 (if *func-slot*
637 (find-class 'effective-func-slot-definition)
638 (call-next-method)))
640 (defmethod sb-mop:direct-slot-definition-class ((class func-slot-class) &key)
641 (find-class 'direct-func-slot-definition))
643 (defmethod sb-mop:compute-effective-slot-definition ((class func-slot-class) name dslotds)
644 (let* ((*func-slot* (some #'slotd-function dslotds))
645 (slotd (call-next-method)))
646 (when *func-slot*
647 (setf (slot-value slotd 'function) (fdefinition *func-slot*)))
648 slotd))
650 ;; I hope this declamation doesn't change the nature of the test
651 (declaim (ftype function foofoo))
652 (with-test (:name :class-redefinition-changes-custom-slot-type)
653 (eval `(defclass func-slot-object ()
654 ((foo :initarg :foo :reader foofoo))
655 (:metaclass func-slot-class)))
656 (let ((x (cons t t)))
657 (assert (eq x (foofoo (make-instance 'func-slot-object :foo x)))))
658 (eval `(defclass func-slot-object ()
659 ((foo :initarg :foo :reader foofoo :function car))
660 (:metaclass func-slot-class)))
661 (let* ((x (cons t t))
662 (y (list x)))
663 (assert (eq x (foofoo (make-instance 'func-slot-object :foo y))))))
665 (with-test (:name :class-redefinition-changes-custom-slot-type-mio)
666 (eval `(defclass func-slot-object2 ()
667 ((foo :initarg :foo :reader foofoo))
668 (:metaclass func-slot-class)))
669 (let* ((x (cons t t))
670 (y (cons x x))
671 (o (make-instance 'func-slot-object2 :foo y)))
672 (assert (eq y (foofoo o)))
673 (eval `(defclass func-slot-object2 ()
674 ((foo :initarg :foo :reader foofoo :function car))
675 (:metaclass func-slot-class)))
676 (assert (eq x (foofoo o)))))
678 (defclass class-slot-removal-test ()
679 ((instance :initform 1)
680 (class :allocation :class :initform :ok)))
682 (defmethod update-instance-for-redefined-class ((x class-slot-removal-test) added removed plist &rest inits)
683 (throw 'update-instance
684 (list added removed plist inits)))
686 (with-test (:name :class-redefinition-removes-class-slot)
687 (let ((o (make-instance 'class-slot-removal-test)))
688 (assert (equal '(nil nil nil nil)
689 (catch 'update-instance
690 (eval `(defclass class-slot-removal-test ()
691 ((instance :initform 2))))
692 (slot-value o 'instance))))))
694 (defclass class-slot-add-test ()
695 ((instance :initform 1)))
697 (defmethod update-instance-for-redefined-class ((x class-slot-add-test) added removed plist &rest inits)
698 (throw 'update-instance
699 (list added removed plist inits)))
701 (with-test (:name :class-redefinition-adds-class-slot)
702 (let ((o (make-instance 'class-slot-add-test)))
703 (assert (equal '(nil nil nil nil)
704 (catch 'update-instance
705 (eval `(defclass class-slot-add-test ()
706 ((instance :initform 2)
707 (class :allocation :class :initform :ok))))
708 (slot-value o 'instance))))))
710 (defgeneric definitely-a-funcallable-instance (x))
711 (with-test (:name (set-funcallable-instance-function :typechecking))
712 (assert-error (set-funcallable-instance-function
713 (lambda (y) (declare (ignore y)) nil)
714 #'definitely-a-funcallable-instance)
715 type-error))
717 (let ((*error-output* (make-broadcast-stream)))
718 (eval '(defstruct nil-slot-name nil)))
719 (with-test (:name (defstruct :nil-slot-name :bug-633911))
720 (let ((fun (compile nil '(lambda (x) (slot-value x 'nil)))))
721 (assert (= 3 (funcall fun (make-nil-slot-name :nil 3))))))
723 ;;; Duplicated initargs in effective slot definition
725 (defclass duplicated-intiargs.a () ((a :initarg :a)))
726 (defclass duplicated-intiargs.b () ((a :initarg :a)))
727 (defclass duplicated-intiargs.c (duplicated-intiargs.a duplicated-intiargs.b) ())
729 (with-test (:name (compute-effective-slot-definition :duplicated-intiargs))
730 (let* ((class (sb-pcl:ensure-class-finalized (find-class 'duplicated-intiargs.c)))
731 (slot (first (class-slots class))))
732 (assert (equal (slot-definition-initargs slot) '(:a)))))
735 (defclass change-class-test-m (standard-class) ())
736 (defmethod validate-superclass ((c1 change-class-test-m) (c2 standard-class))
739 (defmethod slot-value-using-class ((class change-class-test-m) object slot)
740 (call-next-method))
742 (defclass change-class-test ()
743 ((a :initarg :a)
744 (b :initarg :b
745 :initform nil))
746 (:metaclass change-class-test-m))
748 (defclass change-class-test-2 ()
749 ((a :initarg :a
750 :initform nil)
751 (b :initarg :b
752 :initform nil))
753 (:metaclass change-class-test-m))
755 (with-test (:name :change-class-svuc)
756 (let ((new (change-class (make-instance 'change-class-test :b 20)
757 'change-class-test-2)))
758 (assert (eql (slot-value new 'b) 20))
759 (assert (not (slot-boundp new 'a)))))
761 (defclass chgclass-dx-test-1 () ())
762 (defclass chgclass-dx-test-2 () ())
763 (defclass chgclass-dx-test-fin-1 (funcallable-standard-object) ()
764 (:metaclass funcallable-standard-class))
765 (defclass chgclass-dx-test-fin-2 (funcallable-standard-object
766 chgclass-dx-test-2)
768 (:metaclass funcallable-standard-class))
769 (defvar *uifdc-called*)
770 (defmethod update-instance-for-different-class :before (old (new chgclass-dx-test-2)
771 &rest initargs)
772 (declare (ignore initargs))
773 (setq *uifdc-called* t)
774 (assert (sb-ext:stack-allocated-p old))
775 (when (functionp new)
776 (assert (sb-ext:stack-allocated-p (sb-kernel:%funcallable-instance-fun old)))))
777 (with-test (:name :change-class-temp-on-stack)
778 (let ((i (make-instance 'chgclass-dx-test-1))
779 (*uifdc-called* nil))
780 (change-class i 'chgclass-dx-test-2)
781 (assert *uifdc-called*))
782 (let ((i (make-instance 'chgclass-dx-test-fin-1))
783 (*uifdc-called* nil))
784 (change-class i 'chgclass-dx-test-fin-2)
785 (assert *uifdc-called*)))