grovel-headers.c: Put win32 stuff into grovel-headers-win32.h.
[sbcl.git] / tests / mop.impure.lisp
blob456da5a9187d5d5d0be97605fb715e43c98d237b
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 (declare (ignore initargs))
307 (let ((dsd-class-name (gensym)))
308 (sb-pcl:ensure-class
309 dsd-class-name
310 :metaclass 'auto-accessors-direct-slot-definition-class
311 :direct-superclasses (list (find-class 'standard-direct-slot-definition))
312 :containing-class-name (class-name class))
313 (eval `(defmethod initialize-instance :after ((dsd ,dsd-class-name)
314 &rest args)
315 (declare (ignore args))
316 (when (and (null (slot-definition-readers dsd))
317 (null (slot-definition-writers dsd)))
318 (let* ((containing-class-name
319 (slot-value (class-of dsd) 'containing-class-name))
320 (accessor-name
321 (intern
322 (concatenate 'string
323 (symbol-name containing-class-name)
325 (symbol-name (slot-definition-name dsd)))
326 (symbol-package containing-class-name))))
327 (setf (slot-definition-readers dsd) (list accessor-name))
328 (setf (slot-definition-writers dsd)
329 (list (list 'setf accessor-name)))))))
330 (find-class dsd-class-name)))
331 (defmethod validate-superclass ((c1 auto-accessors-class) (c2 standard-class))
333 (defclass testclass15 ()
334 ((x :initarg :x) (y))
335 (:metaclass auto-accessors-class))
336 (let ((inst (make-instance 'testclass15 :x 12)))
337 (assert (equal (list (testclass15-x inst) (setf (testclass15-y inst) 13))
338 '(12 13))))
340 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-17: incorrect
341 ;;; handling of multiple values for non-standard slot-options
342 (progn
343 (defclass option-slot-definition (sb-mop:standard-direct-slot-definition)
344 ((option :accessor sl-option :initarg :my-option)))
345 (defclass option-slot-class (standard-class)
347 (defmethod sb-mop:direct-slot-definition-class
348 ((c option-slot-class) &rest args)
349 (declare (ignore args))
350 (find-class 'option-slot-definition))
351 (defmethod sb-mop:validate-superclass
352 ((c1 option-slot-class) (c2 standard-class))
354 (eval '(defclass test-multiple-slot-option-bug ()
355 ((x :my-option bar :my-option baz))
356 (:metaclass option-slot-class)))
357 (assert (null (set-difference
358 '(bar baz)
359 (sl-option (first (sb-mop:class-direct-slots
360 (find-class 'test-multiple-slot-option-bug))))))))
362 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-19: AMOP requires
363 ;;; that CLASS-PROTOYPE signals an error if the class is not yet finalized
364 (defclass prototype-not-finalized-sub (prototype-not-finalized-super) ())
365 (multiple-value-bind (val err)
366 (ignore-errors (sb-mop:class-prototype (find-class 'prototype-not-finalized-super)))
367 (assert (null val))
368 (assert (typep err 'error)))
370 ;;; AMOP says so
371 (with-test (:name (allocate-instance built-in-class error))
372 (dolist (class-name '(fixnum bignum symbol t))
373 (let ((class (find-class class-name)))
374 ;; actually T can't be a built-in-class
375 (when (typep class 'built-in-class)
376 (multiple-value-bind (value error)
377 (ignore-errors (allocate-instance class))
378 (assert (null value))
379 (assert (typep error 'error)))))))
381 ;;; bug reported by David Morse: direct-subclass update protocol was broken
382 (defclass vegetable () ())
383 (defclass tomato (vegetable) ())
384 (assert (equal (list (find-class 'tomato)) (sb-mop:class-direct-subclasses (find-class 'vegetable))))
385 (defclass tomato () ())
386 (assert (null (sb-mop:class-direct-subclasses (find-class 'vegetable))))
388 ;;; bug 331: lazy creation of clos classes for defstructs
389 (defstruct bug-331-super)
390 (defstruct (bug-331-sub (:include bug-331-super)))
391 (let ((subs (sb-mop:class-direct-subclasses (find-class 'bug-331-super))))
392 (assert (= 1 (length subs)))
393 (assert (eq (car subs) (find-class 'bug-331-sub))))
394 ;;; (addendum to test for #331: conditions suffered the same problem)
395 (define-condition condition-bug-331-super () ())
396 (define-condition condition-bug-331-sub (condition-bug-331-super) ())
397 (let ((subs (sb-mop:class-direct-subclasses
398 (find-class 'condition-bug-331-super))))
399 (assert (= 1 (length subs)))
400 (assert (eq (car subs) (find-class 'condition-bug-331-sub))))
401 ;;; (addendum to the addendum: the fix for this revealed breakage in
402 ;;; REINITIALIZE-INSTANCE)
403 (define-condition condition-bug-331a () ((slot331a :reader slot331a)))
404 (reinitialize-instance (find-class 'condition-bug-331a))
405 (let* ((gf #'slot331a)
406 (methods (sb-mop:generic-function-methods gf)))
407 (assert (= (length methods) 1))
408 (assert (eq (car methods)
409 (find-method #'slot331a nil
410 (list (find-class 'condition-bug-331a))))))
412 ;;; detection of multiple class options in defclass, reported by Bruno Haible
413 (defclass option-class (standard-class)
414 ((option :accessor cl-option :initarg :my-option)))
415 (defmethod sb-pcl:validate-superclass ((c1 option-class) (c2 standard-class))
417 (multiple-value-bind (result error)
418 (ignore-errors (eval '(defclass option-class-instance ()
420 (:my-option bar)
421 (:my-option baz)
422 (:metaclass option-class))))
423 (assert (not result))
424 (assert error))
426 ;;; class as :metaclass
427 (assert (typep
428 (sb-mop:ensure-class-using-class
429 nil 'class-as-metaclass-test
430 :metaclass (find-class 'standard-class)
431 :name 'class-as-metaclass-test
432 :direct-superclasses (list (find-class 'standard-object)))
433 'class))
435 ;;; COMPUTE-DEFAULT-INITARGS protocol mismatch reported by Bruno
436 ;;; Haible
437 (defparameter *extra-initarg-value* 'extra)
438 (defclass custom-default-initargs-class (standard-class)
440 (defmethod compute-default-initargs ((class custom-default-initargs-class))
441 (let ((original-default-initargs
442 (remove-duplicates
443 (reduce #'append
444 (mapcar #'class-direct-default-initargs
445 (class-precedence-list class)))
446 :key #'car
447 :from-end t)))
448 (cons (list ':extra '*extra-initarg-value* #'(lambda () *extra-initarg-value*))
449 (remove ':extra original-default-initargs :key #'car))))
450 (defmethod validate-superclass ((c1 custom-default-initargs-class)
451 (c2 standard-class))
453 (defclass extra-initarg ()
454 ((slot :initarg :extra))
455 (:metaclass custom-default-initargs-class))
456 (assert (eq (slot-value (make-instance 'extra-initarg) 'slot) 'extra))
458 ;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS
459 (defclass standard-class-for-fsc ()
460 ((scforfsc-slot :initarg :scforfsc-slot :accessor scforfsc-slot)))
461 (defvar *standard-class-for-fsc*
462 (make-instance 'standard-class-for-fsc :scforfsc-slot 1))
463 (defclass fsc-with-standard-class-superclass
464 (standard-class-for-fsc funcallable-standard-object)
465 ((fsc-slot :initarg :fsc-slot :accessor fsc-slot))
466 (:metaclass funcallable-standard-class))
467 (defvar *fsc/scs*
468 (make-instance 'fsc-with-standard-class-superclass
469 :scforfsc-slot 2
470 :fsc-slot 3))
471 (assert (= (scforfsc-slot *standard-class-for-fsc*) 1))
472 (assert (= (scforfsc-slot *fsc/scs*) 2))
473 (assert (= (fsc-slot *fsc/scs*) 3))
474 (assert (subtypep 'fsc-with-standard-class-superclass 'function))
475 (assert (not (subtypep 'standard-class-for-fsc 'function)))
477 ;;; also check that our sanity check for functionness is good
478 (assert-error
479 (progn
480 (defclass bad-standard-class (funcallable-standard-object)
482 (:metaclass standard-class))
483 (make-instance 'bad-standard-class)))
484 (assert-error
485 (progn
486 (defclass bad-funcallable-standard-class (standard-object)
488 (:metaclass funcallable-standard-class))
489 (make-instance 'bad-funcallable-standard-class)))
491 ;;; we should be able to make classes with silly names
492 (make-instance 'standard-class :name 3)
493 (defclass foo () ())
494 (reinitialize-instance (find-class 'foo) :name '(a b))
496 ;;; classes (including anonymous ones) and eql-specializers should be
497 ;;; allowed to be specializers.
498 (defvar *anonymous-class*
499 (make-instance 'standard-class
500 :direct-superclasses (list (find-class 'standard-object))))
501 (defvar *object-of-anonymous-class*
502 (make-instance *anonymous-class*))
503 (eval `(defmethod method-on-anonymous-class ((obj ,*anonymous-class*)) 41))
504 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 41))
505 (eval `(defmethod method-on-anonymous-class
506 ((obj ,(intern-eql-specializer *object-of-anonymous-class*)))
507 42))
508 (assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 42))
510 ;;; accessors can cause early finalization, which caused confusion in
511 ;;; the system, leading to uncompileable TYPEP problems.
512 (defclass funcallable-class-for-typep ()
513 ((some-slot-with-accessor :accessor some-slot-with-accessor))
514 (:metaclass funcallable-standard-class))
515 (compile nil '(lambda (x) (typep x 'funcallable-class-for-typep)))
517 ;;; even anonymous classes should be valid types
518 (let* ((class1 (make-instance 'standard-class :direct-superclasses (list (find-class 'standard-object))))
519 (class2 (make-instance 'standard-class :direct-superclasses (list class1))))
520 (assert (subtypep class2 class1))
521 (assert (typep (make-instance class2) class1)))
523 ;;; ensure-class got its treatment of :metaclass wrong.
524 (ensure-class 'better-be-standard-class :direct-superclasses '(standard-object)
525 :metaclass 'standard-class
526 :metaclass 'funcallable-standard-class)
527 (assert (eq (class-of (find-class 'better-be-standard-class))
528 (find-class 'standard-class)))
530 ;;; CLASS-SLOTS should signal an error for classes that are not yet
531 ;;; finalized. Reported by Levente Meszaros on sbcl-devel.
532 (defclass has-slots-but-isnt-finalized () (a b c))
533 (let ((class (find-class 'has-slots-but-isnt-finalized)))
534 (assert (not (sb-mop:class-finalized-p class)))
535 (assert-error (sb-mop:class-slots class) sb-kernel::reference-condition))
537 ;;; Check that MAKE-METHOD-LAMBDA which wraps the original body doesn't
538 ;;; break RETURN-FROM.
539 (defclass wrapped-generic (standard-generic-function)
541 (:metaclass sb-mop:funcallable-standard-class))
543 (defmethod sb-mop:make-method-lambda ((gf wrapped-generic) method lambda env)
544 (call-next-method gf method
545 `(lambda ,(second lambda)
546 (flet ((default () :default))
547 ,@(cddr lambda)))
548 env))
550 (defgeneric wrapped (x)
551 (:generic-function-class wrapped-generic))
553 (defmethod wrapped ((x cons))
554 (return-from wrapped (default)))
556 (with-test (:name :make-method-lambda-wrapping+return-from)
557 (assert (eq :default (wrapped (cons t t)))))
559 ;; This test tests something a little shady - that a method
560 ;; are accessible by way of FDEFNs. They aren't all.
561 ;; See the comment in src/pcl/low.lisp at SET-FUN-NAME.
562 (with-test (:name :slow-method-is-fboundp)
563 (assert (fboundp '(sb-pcl::slow-method wrapped (cons))))
564 (assert (eq :default (funcall #'(sb-pcl::slow-method wrapped (cons)) (list (cons t t)) nil))))
566 ;;; Check that SLOT-BOUNDP-USING-CLASS doesn't confuse MAKE-INSTANCE
567 ;;; optimizations.
568 (defclass sbuc-mio-test-class (standard-class)
570 (defmethod validate-superclass ((class sbuc-mio-test-class)
571 (superclass standard-class))
573 (defvar *sbuc-counter* 0)
574 (defmethod slot-boundp-using-class ((class sbuc-mio-test-class)
575 (object t)
576 (slot standard-effective-slot-definition))
577 (incf *sbuc-counter*)
578 (call-next-method))
579 (defclass sbuc-mio-test-object ()
580 ((slot :initform 5 :accessor a-slot))
581 (:metaclass sbuc-mio-test-class))
582 (with-test (:name :sbuc-mio-test)
583 (assert (= 5 (funcall
584 (compile
586 `(lambda ()
587 (let ((object (make-instance 'sbuc-mio-test-object)))
588 (slot-value object 'slot)))))))
589 (assert (= 1 *sbuc-counter*)))
591 ;;; Redefining classes so that slot definition class changes.
592 (defclass func-slot-class (standard-class)
595 (defmethod sb-mop:validate-superclass ((class func-slot-class) (super standard-class))
598 (defclass func-slot-definition ()
599 ((function :initform nil :initarg :function :reader slotd-function)))
601 (defclass effective-func-slot-definition (sb-mop:standard-effective-slot-definition
602 func-slot-definition)
605 (defclass direct-func-slot-definition (sb-mop:standard-direct-slot-definition
606 func-slot-definition)
609 (defmethod sb-mop:slot-value-using-class ((class func-slot-class)
610 instance
611 (slotd effective-func-slot-definition))
612 (funcall (slotd-function slotd) (call-next-method)))
614 (defvar *func-slot*)
616 (defmethod sb-mop:effective-slot-definition-class ((class func-slot-class) &key)
617 (if *func-slot*
618 (find-class 'effective-func-slot-definition)
619 (call-next-method)))
621 (defmethod sb-mop:direct-slot-definition-class ((class func-slot-class) &key)
622 (find-class 'direct-func-slot-definition))
624 (defmethod sb-mop:compute-effective-slot-definition ((class func-slot-class) name dslotds)
625 (let* ((*func-slot* (some #'slotd-function dslotds))
626 (slotd (call-next-method)))
627 (when *func-slot*
628 (setf (slot-value slotd 'function) (fdefinition *func-slot*)))
629 slotd))
631 (with-test (:name :class-redefinition-changes-custom-slot-type)
632 (eval `(defclass func-slot-object ()
633 ((foo :initarg :foo :reader foofoo))
634 (:metaclass func-slot-class)))
635 (let ((x (cons t t)))
636 (assert (eq x (foofoo (make-instance 'func-slot-object :foo x)))))
637 (eval `(defclass func-slot-object ()
638 ((foo :initarg :foo :reader foofoo :function car))
639 (:metaclass func-slot-class)))
640 (let* ((x (cons t t))
641 (y (list x)))
642 (assert (eq x (foofoo (make-instance 'func-slot-object :foo y))))))
644 (with-test (:name :class-redefinition-changes-custom-slot-type-mio)
645 (eval `(defclass func-slot-object2 ()
646 ((foo :initarg :foo :reader foofoo))
647 (:metaclass func-slot-class)))
648 (let* ((x (cons t t))
649 (y (cons x x))
650 (o (make-instance 'func-slot-object2 :foo y)))
651 (assert (eq y (foofoo o)))
652 (eval `(defclass func-slot-object2 ()
653 ((foo :initarg :foo :reader foofoo :function car))
654 (:metaclass func-slot-class)))
655 (assert (eq x (foofoo o)))))
657 (defclass class-slot-removal-test ()
658 ((instance :initform 1)
659 (class :allocation :class :initform :ok)))
661 (defmethod update-instance-for-redefined-class ((x class-slot-removal-test) added removed plist &rest inits)
662 (throw 'update-instance
663 (list added removed plist inits)))
665 (with-test (:name :class-redefinition-removes-class-slot)
666 (let ((o (make-instance 'class-slot-removal-test)))
667 (assert (equal '(nil nil nil nil)
668 (catch 'update-instance
669 (eval `(defclass class-slot-removal-test ()
670 ((instance :initform 2))))
671 (slot-value o 'instance))))))
673 (defclass class-slot-add-test ()
674 ((instance :initform 1)))
676 (defmethod update-instance-for-redefined-class ((x class-slot-add-test) added removed plist &rest inits)
677 (throw 'update-instance
678 (list added removed plist inits)))
680 (with-test (:name :class-redefinition-adds-class-slot)
681 (let ((o (make-instance 'class-slot-add-test)))
682 (assert (equal '(nil nil nil nil)
683 (catch 'update-instance
684 (eval `(defclass class-slot-add-test ()
685 ((instance :initform 2)
686 (class :allocation :class :initform :ok))))
687 (slot-value o 'instance))))))
689 (defgeneric definitely-a-funcallable-instance (x))
690 (with-test (:name (set-funcallable-instance-function :typechecking)
691 ;; This is a bit of a problem. SET-FUNCALLABLE-INSTANCE-FUNCTION
692 ;; accepts any funcallable-instance as its first argument,
693 ;; not just a generic-function.
694 ;; But an interpreted function *is* a funcallable-instance
695 ;; See comment in src/pcl/low about possibly tightening this up.
696 :fails-on :interpreter)
697 (assert-error (set-funcallable-instance-function
698 (lambda (y) (declare (ignore y)) nil)
699 #'definitely-a-funcallable-instance)
700 type-error))
702 (with-test (:name (defstruct :nil-slot-name :bug-633911))
703 (defstruct nil-slot-name nil)
704 (let ((fun (compile nil '(lambda (x) (slot-value x 'nil)))))
705 (assert (= 3 (funcall fun (make-nil-slot-name :nil 3))))))
707 ;;;; success