fixup NEWS for release script
[sbcl.git] / tests / mop.impure.lisp
blob03571cb498d60378f6e051280ca509a69039377c
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 (assert (typep class 'built-in-class))
373 (multiple-value-bind (value error)
374 (ignore-errors (allocate-instance class))
375 (assert (null value))
376 (assert (typep error 'error))))))
378 ;;; bug reported by David Morse: direct-subclass update protocol was broken
379 (defclass vegetable () ())
380 (defclass tomato (vegetable) ())
381 (assert (equal (list (find-class 'tomato)) (sb-mop:class-direct-subclasses (find-class 'vegetable))))
382 (defclass tomato () ())
383 (assert (null (sb-mop:class-direct-subclasses (find-class 'vegetable))))
385 ;;; bug 331: lazy creation of clos classes for defstructs
386 (defstruct bug-331-super)
387 (defstruct (bug-331-sub (:include bug-331-super)))
388 (let ((subs (sb-mop:class-direct-subclasses (find-class 'bug-331-super))))
389 (assert (= 1 (length subs)))
390 (assert (eq (car subs) (find-class 'bug-331-sub))))
391 ;;; (addendum to test for #331: conditions suffered the same problem)
392 (define-condition condition-bug-331-super () ())
393 (define-condition condition-bug-331-sub (condition-bug-331-super) ())
394 (let ((subs (sb-mop:class-direct-subclasses
395 (find-class 'condition-bug-331-super))))
396 (assert (= 1 (length subs)))
397 (assert (eq (car subs) (find-class 'condition-bug-331-sub))))
398 ;;; (addendum to the addendum: the fix for this revealed breakage in
399 ;;; REINITIALIZE-INSTANCE)
400 (define-condition condition-bug-331a () ((slot331a :reader slot331a)))
401 (reinitialize-instance (find-class 'condition-bug-331a))
402 (let* ((gf #'slot331a)
403 (methods (sb-mop:generic-function-methods gf)))
404 (assert (= (length methods) 1))
405 (assert (eq (car methods)
406 (find-method #'slot331a nil
407 (list (find-class 'condition-bug-331a))))))
409 ;;; detection of multiple class options in defclass, reported by Bruno Haible
410 (defclass option-class (standard-class)
411 ((option :accessor cl-option :initarg :my-option)))
412 (defmethod sb-pcl:validate-superclass ((c1 option-class) (c2 standard-class))
414 (multiple-value-bind (result error)
415 (ignore-errors (eval '(defclass option-class-instance ()
417 (:my-option bar)
418 (:my-option baz)
419 (:metaclass option-class))))
420 (assert (not result))
421 (assert error))
423 ;;; class as :metaclass
424 (assert (typep
425 (sb-mop:ensure-class-using-class
426 nil 'class-as-metaclass-test
427 :metaclass (find-class 'standard-class)
428 :name 'class-as-metaclass-test
429 :direct-superclasses (list (find-class 'standard-object)))
430 'class))
432 ;;; COMPUTE-DEFAULT-INITARGS protocol mismatch reported by Bruno
433 ;;; Haible
434 (defparameter *extra-initarg-value* 'extra)
435 (defclass custom-default-initargs-class (standard-class)
437 (defmethod compute-default-initargs ((class custom-default-initargs-class))
438 (let ((original-default-initargs
439 (remove-duplicates
440 (reduce #'append
441 (mapcar #'class-direct-default-initargs
442 (class-precedence-list class)))
443 :key #'car
444 :from-end t)))
445 (cons (list ':extra '*extra-initarg-value* #'(lambda () *extra-initarg-value*))
446 (remove ':extra original-default-initargs :key #'car))))
447 (defmethod validate-superclass ((c1 custom-default-initargs-class)
448 (c2 standard-class))
450 (defclass extra-initarg ()
451 ((slot :initarg :extra))
452 (:metaclass custom-default-initargs-class))
453 (assert (eq (slot-value (make-instance 'extra-initarg) 'slot) 'extra))
455 ;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS
456 (defclass standard-class-for-fsc ()
457 ((scforfsc-slot :initarg :scforfsc-slot :accessor scforfsc-slot)))
458 (defvar *standard-class-for-fsc*
459 (make-instance 'standard-class-for-fsc :scforfsc-slot 1))
460 (defclass fsc-with-standard-class-superclass
461 (standard-class-for-fsc funcallable-standard-object)
462 ((fsc-slot :initarg :fsc-slot :accessor fsc-slot))
463 (:metaclass funcallable-standard-class))
464 (defvar *fsc/scs*
465 (make-instance 'fsc-with-standard-class-superclass
466 :scforfsc-slot 2
467 :fsc-slot 3))
468 (assert (= (scforfsc-slot *standard-class-for-fsc*) 1))
469 (assert (= (scforfsc-slot *fsc/scs*) 2))
470 (assert (= (fsc-slot *fsc/scs*) 3))
471 (assert (subtypep 'fsc-with-standard-class-superclass 'function))
472 (assert (not (subtypep 'standard-class-for-fsc 'function)))
474 ;;; also check that our sanity check for functionness is good
475 (assert-error
476 (progn
477 (defclass bad-standard-class (funcallable-standard-object)
479 (:metaclass standard-class))
480 (make-instance 'bad-standard-class)))
481 (assert-error
482 (progn
483 (defclass bad-funcallable-standard-class (standard-object)
485 (:metaclass funcallable-standard-class))
486 (make-instance 'bad-funcallable-standard-class)))
488 ;;; we should be able to make classes with silly names
489 (make-instance 'standard-class :name 3)
490 (defclass foo () ())
491 (reinitialize-instance (find-class 'foo) :name '(a b))
493 ;;; classes (including anonymous ones) and eql-specializers should be
494 ;;; allowed to be specializers.
495 (defvar *anonymous-class*
496 (make-instance 'standard-class
497 :direct-superclasses (list (find-class 'standard-object))))
498 (defvar *object-of-anonymous-class*
499 (make-instance *anonymous-class*))
500 (eval `(defmethod method-on-anonymous-class ((obj ,*anonymous-class*)) 41))
501 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 41))
502 (eval `(defmethod method-on-anonymous-class
503 ((obj ,(intern-eql-specializer *object-of-anonymous-class*)))
504 42))
505 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 42))
507 ;;; accessors can cause early finalization, which caused confusion in
508 ;;; the system, leading to uncompileable TYPEP problems.
509 (defclass funcallable-class-for-typep ()
510 ((some-slot-with-accessor :accessor some-slot-with-accessor))
511 (:metaclass funcallable-standard-class))
512 (compile nil '(lambda (x) (typep x 'funcallable-class-for-typep)))
514 ;;; even anonymous classes should be valid types
515 (let* ((class1 (make-instance 'standard-class :direct-superclasses (list (find-class 'standard-object))))
516 (class2 (make-instance 'standard-class :direct-superclasses (list class1))))
517 (assert (subtypep class2 class1))
518 (assert (typep (make-instance class2) class1)))
520 ;;; ensure-class got its treatment of :metaclass wrong.
521 (ensure-class 'better-be-standard-class :direct-superclasses '(standard-object)
522 :metaclass 'standard-class
523 :metaclass 'funcallable-standard-class)
524 (assert (eq (class-of (find-class 'better-be-standard-class))
525 (find-class 'standard-class)))
527 ;;; CLASS-SLOTS should signal an error for classes that are not yet
528 ;;; finalized. Reported by Levente Meszaros on sbcl-devel.
529 (defclass has-slots-but-isnt-finalized () (a b c))
530 (let ((class (find-class 'has-slots-but-isnt-finalized)))
531 (assert (not (sb-mop:class-finalized-p class)))
532 (assert-error (sb-mop:class-slots class) sb-kernel::reference-condition))
534 ;;; Check that MAKE-METHOD-LAMBDA which wraps the original body doesn't
535 ;;; break RETURN-FROM.
536 (defclass wrapped-generic (standard-generic-function)
538 (:metaclass sb-mop:funcallable-standard-class))
540 (defmethod sb-mop:make-method-lambda ((gf wrapped-generic) method lambda env)
541 (call-next-method gf method
542 `(lambda ,(second lambda)
543 (flet ((default () :default))
544 ,@(cddr lambda)))
545 env))
547 (defgeneric wrapped (x)
548 (:generic-function-class wrapped-generic))
550 (defmethod wrapped ((x cons))
551 (return-from wrapped (default)))
553 (with-test (:name :make-method-lambda-wrapping+return-from)
554 (assert (eq :default (wrapped (cons t t)))))
556 (with-test (:name :slow-method-is-fboundp)
557 (assert (fboundp '(sb-pcl::slow-method wrapped (cons))))
558 (assert (eq :default (funcall #'(sb-pcl::slow-method wrapped (cons)) (list (cons t t)) nil))))
560 ;;; Check that SLOT-BOUNDP-USING-CLASS doesn't confuse MAKE-INSTANCE
561 ;;; optimizations.
562 (defclass sbuc-mio-test-class (standard-class)
564 (defmethod validate-superclass ((class sbuc-mio-test-class)
565 (superclass standard-class))
567 (defvar *sbuc-counter* 0)
568 (defmethod slot-boundp-using-class ((class sbuc-mio-test-class)
569 (object t)
570 (slot standard-effective-slot-definition))
571 (incf *sbuc-counter*)
572 (call-next-method))
573 (defclass sbuc-mio-test-object ()
574 ((slot :initform 5 :accessor a-slot))
575 (:metaclass sbuc-mio-test-class))
576 (with-test (:name :sbuc-mio-test)
577 (assert (= 5 (funcall
578 (compile
580 `(lambda ()
581 (let ((object (make-instance 'sbuc-mio-test-object)))
582 (slot-value object 'slot)))))))
583 (assert (= 1 *sbuc-counter*)))
585 ;;; Redefining classes so that slot definition class changes.
586 (defclass func-slot-class (standard-class)
589 (defmethod sb-mop:validate-superclass ((class func-slot-class) (super standard-class))
592 (defclass func-slot-definition ()
593 ((function :initform nil :initarg :function :reader slotd-function)))
595 (defclass effective-func-slot-definition (sb-mop:standard-effective-slot-definition
596 func-slot-definition)
599 (defclass direct-func-slot-definition (sb-mop:standard-direct-slot-definition
600 func-slot-definition)
603 (defmethod sb-mop:slot-value-using-class ((class func-slot-class)
604 instance
605 (slotd effective-func-slot-definition))
606 (funcall (slotd-function slotd) (call-next-method)))
608 (defvar *func-slot*)
610 (defmethod sb-mop:effective-slot-definition-class ((class func-slot-class) &key)
611 (if *func-slot*
612 (find-class 'effective-func-slot-definition)
613 (call-next-method)))
615 (defmethod sb-mop:direct-slot-definition-class ((class func-slot-class) &key)
616 (find-class 'direct-func-slot-definition))
618 (defmethod sb-mop:compute-effective-slot-definition ((class func-slot-class) name dslotds)
619 (let* ((*func-slot* (some #'slotd-function dslotds))
620 (slotd (call-next-method)))
621 (when *func-slot*
622 (setf (slot-value slotd 'function) (fdefinition *func-slot*)))
623 slotd))
625 (with-test (:name :class-redefinition-changes-custom-slot-type)
626 (eval `(defclass func-slot-object ()
627 ((foo :initarg :foo :reader foofoo))
628 (:metaclass func-slot-class)))
629 (let ((x (cons t t)))
630 (assert (eq x (foofoo (make-instance 'func-slot-object :foo x)))))
631 (eval `(defclass func-slot-object ()
632 ((foo :initarg :foo :reader foofoo :function car))
633 (:metaclass func-slot-class)))
634 (let* ((x (cons t t))
635 (y (list x)))
636 (assert (eq x (foofoo (make-instance 'func-slot-object :foo y))))))
638 (with-test (:name :class-redefinition-changes-custom-slot-type-mio)
639 (eval `(defclass func-slot-object2 ()
640 ((foo :initarg :foo :reader foofoo))
641 (:metaclass func-slot-class)))
642 (let* ((x (cons t t))
643 (y (cons x x))
644 (o (make-instance 'func-slot-object2 :foo y)))
645 (assert (eq y (foofoo o)))
646 (eval `(defclass func-slot-object2 ()
647 ((foo :initarg :foo :reader foofoo :function car))
648 (:metaclass func-slot-class)))
649 (assert (eq x (foofoo o)))))
651 (defclass class-slot-removal-test ()
652 ((instance :initform 1)
653 (class :allocation :class :initform :ok)))
655 (defmethod update-instance-for-redefined-class ((x class-slot-removal-test) added removed plist &rest inits)
656 (throw 'update-instance
657 (list added removed plist inits)))
659 (with-test (:name :class-redefinition-removes-class-slot)
660 (let ((o (make-instance 'class-slot-removal-test)))
661 (assert (equal '(nil nil nil nil)
662 (catch 'update-instance
663 (eval `(defclass class-slot-removal-test ()
664 ((instance :initform 2))))
665 (slot-value o 'instance))))))
667 (defclass class-slot-add-test ()
668 ((instance :initform 1)))
670 (defmethod update-instance-for-redefined-class ((x class-slot-add-test) added removed plist &rest inits)
671 (throw 'update-instance
672 (list added removed plist inits)))
674 (with-test (:name :class-redefinition-adds-class-slot)
675 (let ((o (make-instance 'class-slot-add-test)))
676 (assert (equal '(nil nil nil nil)
677 (catch 'update-instance
678 (eval `(defclass class-slot-add-test ()
679 ((instance :initform 2)
680 (class :allocation :class :initform :ok))))
681 (slot-value o 'instance))))))
683 (defgeneric definitely-a-funcallable-instance (x))
684 (with-test (:name (set-funcallable-instance-function :typechecking))
685 (assert-error (set-funcallable-instance-function
686 (lambda (y) nil)
687 #'definitely-a-funcallable-instance)
688 type-error))
690 (with-test (:name (defstruct :nil-slot-name :bug-633911))
691 (defstruct nil-slot-name nil)
692 (let ((fun (compile nil '(lambda (x) (slot-value x 'nil)))))
693 (assert (= 3 (funcall fun (make-nil-slot-name :nil 3))))))
695 ;;;; success