Better handling of children deletion in delete-lambda.
[sbcl.git] / tests / clos.impure.lisp
blobfa86b48f91bda151ce7a9ae53120c2bb695f8ae3
1 ;;;; miscellaneous side-effectful tests of CLOS
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 #+interpreter (sb-ext:exit :code 104)
16 (load "compiler-test-util.lisp")
17 (defpackage "CLOS-IMPURE"
18 (:use "CL" "SB-EXT" "ASSERTOID" "TEST-UTIL" "COMPILER-TEST-UTIL"))
19 (in-package "CLOS-IMPURE")
21 ;;; It should be possible to do DEFGENERIC and DEFMETHOD referring to
22 ;;; structure types defined earlier in the file.
23 (defstruct struct-a x y)
24 (defstruct struct-b x y z)
25 (defmethod wiggle ((a struct-a))
26 (+ (struct-a-x a)
27 (struct-a-y a)))
28 (defgeneric jiggle (arg))
29 (defmethod jiggle ((a struct-a))
30 (- (struct-a-x a)
31 (struct-a-y a)))
32 (defmethod jiggle ((b struct-b))
33 (- (struct-b-x b)
34 (struct-b-y b)
35 (struct-b-z b)))
37 (with-test (:name (defmethod defstruct :same-file))
38 (assert (= (wiggle (make-struct-a :x 6 :y 5))
39 (jiggle (make-struct-b :x 19 :y 6 :z 2)))))
41 ;;; Compiling DEFGENERIC should prevent "undefined function" style
42 ;;; warnings from code within the same file.
43 (defgeneric gf-defined-in-this-file (x y))
44 (defun function-using-gf-defined-in-this-file (x y n)
45 (unless (minusp n)
46 (gf-defined-in-this-file x y)))
48 ;;; Until Martin Atzmueller ported Pierre Mai's CMU CL fixes in
49 ;;; sbcl-0.6.12.25, the implementation of NO-APPLICABLE-METHOD was
50 ;;; broken in such a way that the code here would signal an error.
51 (defgeneric zut-n-a-m (a b c))
52 (defmethod no-applicable-method ((zut-n-a-m (eql #'zut-n-a-m)) &rest args)
53 (declare (ignore args))
54 :no-applicable-method)
55 (with-test (:name no-applicable-method)
56 (assert (eq :no-applicable-method (zut-n-a-m 1 2 3))))
58 ;;; bug reported and fixed by Alexey Dejneka sbcl-devel 2001-09-10:
59 ;;; This DEFGENERIC shouldn't cause an error.
60 (defgeneric ad-gf (a) (:method :around (x) x))
62 ;;; DEFGENERIC and DEFMETHOD shouldn't accept &REST when it's not
63 ;;; followed by a variable:
64 ;;; e.g. (DEFMETHOD FOO ((X T) &REST) NIL) should signal an error.
65 ;;; Of course most of these are totally redundant
66 ;;; now that there is only one function to parse all lambda lists.
67 (eval-when (:load-toplevel :compile-toplevel :execute)
68 (defmacro expect-error (form)
69 `(multiple-value-bind (res condition)
70 (let ((*error-output* (make-broadcast-stream)))
71 (ignore-errors (eval ',form))) ; delay until *error-output* is bound
72 (declare (ignore res))
73 (typep condition 'error))))
74 (assert (expect-error (defmethod foo0 ((x t) &rest) nil)))
75 (assert (expect-error (defgeneric foo1 (x &rest))))
76 (assert (expect-error (defgeneric foo2 (x a &rest))))
77 (defgeneric foo3 (x &rest y))
78 (defmethod foo3 ((x t) &rest y) y nil)
79 (defgeneric foo4 (x &rest z &key y))
80 (defmethod foo4 ((x t) &rest z &key y) z y nil)
81 (assert (expect-error (defgeneric foo5 (x &rest))))
82 (assert (expect-error (defmethod foo6 (x &rest))))
84 ;;; legal method specializers
85 (defclass bug-525916-1 () ())
86 (defclass bug-525916-2 () ())
87 (with-test (:name (defmethod :specializer-syntax :bug-525916))
88 (assert (expect-error (defmethod invalid ((arg)) arg)))
89 (assert (expect-error (defmethod invalid (nil) 1)))
90 (assert (expect-error (defmethod invalid ((arg . bug-525916-1)) arg)))
91 (assert (expect-error (defmethod invalid ((arg bug-525916-1 bug-525916-2)) arg))))
93 ;;; more lambda-list checking
94 ;;;
95 ;;; DEFGENERIC lambda lists are subject to various limitations, as per
96 ;;; section 3.4.2 of the ANSI spec. Since Alexey Dejneka's patch for
97 ;;; bug 191-b ca. sbcl-0.7.22, these limitations should be enforced.
98 (flet ((test-case (operator lambda-list
99 &optional expect-failure-p expect-warnings-p &rest messages)
100 (multiple-value-bind
101 (fun failure-p warnings style-warnings notes errors)
102 (checked-compile `(lambda () (,operator ,(gensym) ,lambda-list))
103 :allow-failure expect-failure-p
104 :allow-warnings expect-warnings-p
105 :allow-style-warnings expect-warnings-p)
106 (declare (ignore fun notes))
107 (let ((warnings (append style-warnings warnings)))
108 (when expect-failure-p (assert failure-p))
109 (when expect-warnings-p (assert warnings))
110 (assert (= (length messages) (+ (length warnings) (length errors))))
111 (loop for message in messages
112 for error in (append warnings errors)
113 do (assert (search message (princ-to-string error))))))))
115 (with-test (:name (defgeneric :lambda-list))
116 (mapc
117 (lambda (spec) (apply #'test-case 'defgeneric spec))
118 '(;; basic sanity
119 (("a" #p"b")
120 t nil "Required argument is not a symbol: \"a\"")
121 (())
122 ((x))
123 ;; repeated names and keywords
124 ((x x)
125 t nil "The variable X occurs more than once")
126 ((x &rest x)
127 t nil "The variable X occurs more than once")
128 ((&optional x x)
129 t nil "The variable X occurs more than once")
130 ((&key x ((:y x)))
131 t nil "The variable X occurs more than once")
132 ((&key x ((:x y)))
133 t nil "The keyword :X occurs more than once")
134 ((&key ((:x a)) ((:x b)))
135 t nil "The keyword :X occurs more than once")
136 ;; illegal variable names
137 ((nil)
138 t nil "NIL cannot be used")
139 ((:pi)
140 t nil ":PI is a keyword and cannot be used")
141 ((pi)
142 t nil "COMMON-LISP:PI names a defined constant")
143 ;; forbidden default or supplied-p for &OPTIONAL or &KEY arguments
144 ((x &optional (y 0))
145 t nil "Invalid &OPTIONAL argument specifier (Y 0)")
146 ((x &optional y))
147 ((x y &key (z :z z-p))
148 t nil "Invalid &KEY argument specifier (Z :Z Z-P)")
149 ((x y &key z))
150 ((x &optional (y 0) &key z)
152 "&OPTIONAL and &KEY found in the same lambda list"
153 "Invalid &OPTIONAL argument specifier (Y 0)")
154 ((x &optional y &key z)
155 nil t "&OPTIONAL and &KEY found in the same lambda list")
156 ((x &optional y &key (z :z))
158 "&OPTIONAL and &KEY found in the same lambda list"
159 "Invalid &KEY argument specifier (Z :Z)")
160 ((&optional &key (k :k k-p))
161 t nil "Invalid &KEY argument specifier (K :K K-P)")
162 ((&optional &key k))
163 ;; forbidden &AUX
164 ((x y z &optional a &aux g h)
165 t nil "&AUX is not allowed in a generic function lambda list")
166 ((x y z &optional a))
167 ((x &aux)
168 t nil "&AUX is not allowed in a generic function lambda list")
169 ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations
170 ;; on required arguments
171 (((arg t))
172 t nil "Required argument is not a symbol: (ARG T)"))))
174 (with-test (:name (defmethod :lambda-list))
175 (mapc
176 (lambda (spec) (apply #'test-case 'defmethod spec))
177 '(;; Invalid specialized required argument
178 (((x t t))
179 t nil "arg is not a non-NIL symbol or a list of two elements: (X T T)")
180 ;; Repeated names and keywords
181 (((x t) (x t))
182 t nil "The variable X occurs more than once")
183 (((x t) &rest x)
184 t nil "The variable X occurs more than once")
185 ((&optional x x)
186 t nil "The variable X occurs more than once")
187 ((&key x ((:y x)))
188 t nil "The variable X occurs more than once")
189 ((&key x ((:x y)))
190 t nil "The keyword :X occurs more than once")
191 ;; Illegal variable names
192 (((nil t))
193 t nil "NIL cannot be used")
194 (((:pi t))
195 t nil ":PI is a keyword and cannot be used")
196 (((pi t))
197 t nil "COMMON-LISP:PI names a defined constant")))))
200 ;;; Explicit :metaclass option with structure-class and
201 ;;; standard-class.
203 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
204 (defclass structure-class-foo2 (structure-class-foo1)
205 () (:metaclass cl:structure-class))
206 (with-test (:name (defclass :metaclass cl:structure-class))
207 (assert (typep (class-of (make-instance 'structure-class-foo1))
208 'structure-class))
209 (assert (typep (make-instance 'structure-class-foo1) 'structure-class-foo1))
210 (assert (typep (class-of (make-instance 'structure-class-foo2))
211 'structure-class))
212 (assert (typep (make-instance 'structure-class-foo2) 'structure-class-foo2)))
214 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
215 (defclass standard-class-foo2 (standard-class-foo1)
216 () (:metaclass cl:standard-class))
217 (with-test (:name (defclass :metaclass cl:standard-class))
218 (assert (typep (make-instance 'standard-class-foo1) 'standard-class-foo1))
219 (assert (typep (make-instance 'standard-class-foo2) 'standard-class-foo2)))
221 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
222 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
223 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
224 ;;; DEFGENERIC-containing files does the right thing instead of
225 ;;; randomly slicing your generic functions. (APD made this work
226 ;;; in sbcl-0.7.0.2.)
227 (defgeneric born-to-be-redefined (x)
228 (:method ((x integer))
229 'integer))
230 (defmethod born-to-be-redefined ((x real))
231 'real)
232 (assert (eq (born-to-be-redefined 1) 'integer))
233 (defgeneric born-to-be-redefined (x))
234 (assert (eq (born-to-be-redefined 1) 'real)) ; failed until sbcl-0.7.0.2
235 (defgeneric born-to-be-redefined (x)
236 (:method ((x integer))
237 'integer))
238 (defmethod born-to-be-redefined ((x integer))
239 'int)
240 (assert (eq (born-to-be-redefined 1) 'int))
241 (defgeneric born-to-be-redefined (x))
242 (assert (eq (born-to-be-redefined 1) 'int))
244 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
245 ;;; preventing forward-references and also CHANGE-CLASS (which
246 ;;; forward-references used interally) from working properly.
248 ;;; One symptom was reported by Brian Spilsbury (sbcl-devel
249 ;;; 2002-04-08), and another on IRC by Dan Barlow simultaneously.
250 ;;; Better check that it doesn't happen again.
252 ;;; Some more CHANGE-CLASS testing (which only applies to the now
253 ;;; ANSI-compliant version) thanks to Espen Johnsen)
255 ;; First, the forward references:
256 (defclass forward-ref-a (forward-ref-b) ())
257 (defclass forward-ref-b () ())
258 ;; (a couple more complicated examples found by Paul Dietz' test
259 ;; suite):
260 (defclass forward-ref-c1 (forward-ref-c2) ())
261 (defclass forward-ref-c2 (forward-ref-c3) ())
263 (defclass forward-ref-d1 (forward-ref-d2 forward-ref-d3)
265 (defclass forward-ref-d2 (forward-ref-d4 forward-ref-d5)
268 ;; Then CHANGE-CLASS
269 (defun change-class-test-case (spec)
270 (destructuring-bind (from-class from-initargs
271 to-class to-initargs
272 expected-slots)
273 spec
274 (let ((from (typecase from-class
275 (symbol
276 (apply #'make-instance from-class from-initargs))
277 ((cons (eql :class) (cons symbol))
278 (find-class (second from-class))))))
279 (flet ((change ()
280 (apply #'change-class from to-class to-initargs))
281 ;; These local functions make ASSERT produce better error
282 ;; messages.
283 (slot-value-equal (instance name value expected)
284 (declare (ignore instance name))
285 (equal value expected))
286 (slot-not-bound (instance name)
287 (not (slot-boundp instance name))))
288 (case expected-slots
289 (error
290 (assert-error (change)))
291 (type-error
292 (assert-error (change) type-error))
294 (let ((to (change)))
295 (loop :for (name value) :in expected-slots
296 :do (case value
297 (:unbound
298 (assert (slot-not-bound to name)))
300 (assert
301 (slot-value-equal
302 to name (slot-value to name) value))))))))))))
304 (defclass change-class.smoke.1 ()
305 ((foo :initarg :foo)))
306 (defclass change-class.smoke.2 (change-class.smoke.1) ())
307 (defclass change-class.smoke.3 (change-class.smoke.1)
308 ((foo :initarg :foo)))
309 (defclass change-class.smoke.4 ()
310 ((foo :initarg :foo) (bar :initarg :bar)))
311 (defclass change-class.smoke.5 ()
312 ((a :initarg :a) (b :initarg :b) (c :initarg :c)))
313 (defclass change-class.smoke.6 () ())
315 (with-test (:name (change-class :smoke))
316 (mapc
317 #'change-class-test-case
318 '(;; Test unbound slots.
319 (change-class.smoke.1 () change-class.smoke.1 ()
320 ((foo :unbound)))
321 (change-class.smoke.1 () change-class.smoke.2 ()
322 ((foo :unbound)))
323 (change-class.smoke.1 () change-class.smoke.3 ()
324 ((foo :unbound)))
325 (change-class.smoke.1 () change-class.smoke.4 ()
326 ((foo :unbound) (bar :unbound)))
327 (change-class.smoke.4 (:bar 1) change-class.smoke.1 ()
328 ((foo :unbound)))
330 ;; Bound slots are retained.
331 (change-class.smoke.1 (:foo :baz) change-class.smoke.1 ()
332 ((foo :baz)))
333 (change-class.smoke.1 (:foo :baz) change-class.smoke.2 ()
334 ((foo :baz)))
335 (change-class.smoke.1 (:foo :baz) change-class.smoke.3 ()
336 ((foo :baz)))
337 (change-class.smoke.1 (:foo :baz) change-class.smoke.4 ()
338 ((foo :baz) (bar :unbound)))
339 (change-class.smoke.4 (:foo :baz) change-class.smoke.1 ()
340 ((foo :baz)))
342 ;; Original test.
343 (change-class.smoke.5 (:a 1 :b 2 :c 3) change-class.smoke.5 ()
344 ((a 1) (b 2) (c 3)))
346 ;; Original test by Espen Johnsen
347 (change-class.smoke.1 (:foo 1) change-class.smoke.4 (:bar 2)
348 ((foo 1) (bar 2)))
350 ;; Cannot change objects into metaobjects.
351 (change-class.smoke.6 () class ()
352 error)
353 (change-class.smoke.6 () generic-function ()
354 error)
355 (change-class.smoke.6 () method ()
356 error)
357 (change-class.smoke.6 () slot-definition ()
358 error))))
360 ;; Test for type-checking
362 (locally (declare (optimize (safety 3))) ; force slot type-checking
363 (defclass change-class.type-check.1 ()
364 ((foo :initarg :foo :type real)))
365 (defclass change-class.type-check.2 ()
366 ((foo :initarg :foo :type integer))))
368 (with-test (:name (change-class :type-check))
369 (mapc
370 #'change-class-test-case
371 '(;; These are allowed.
372 (change-class.type-check.1 () change-class.type-check.2 ()
373 ((foo :unbound)))
374 (change-class.type-check.1 (:foo 1) change-class.type-check.2 ()
375 ((foo 1)))
376 (change-class.type-check.1 (:foo 1.0) change-class.type-check.2 (:foo 2)
377 ((foo 2)))
379 ;; These are not allowed.
380 (change-class.type-check.1 () change-class.type-check.2 (:foo 1.0)
381 type-error)
382 (change-class.type-check.1 (:foo 1.0) change-class.type-check.2 ()
383 type-error)))
385 ;; Type-mismatches should be recoverable via USE-VALUE restart.
386 (let* ((from (make-instance 'change-class.type-check.1 :foo 1.0))
387 (to (handler-bind ((type-error (lambda (condition)
388 (declare (ignore condition))
389 (use-value 3))))
390 (change-class from 'change-class.type-check.2))))
391 (assert (equal (slot-value to 'foo) 3))))
393 ;; Test interaction with initforms and -args
395 (defclass change-class.initforms.1 ()
397 (defclass change-class.initforms.2 ()
398 ((foo :initarg :foo)))
399 (defclass change-class.initforms.3 ()
400 ((foo :initarg :foo :initform :bar)))
401 (defclass change-class.initforms.4 ()
402 ((foo :initarg :foo))
403 (:default-initargs
404 :foo :bar))
406 (with-test (:name (change-class :initforms))
407 (mapc
408 #'change-class-test-case
409 '(;; Initialization of added slot.
410 (change-class.initforms.1 () change-class.initforms.3 ()
411 ((foo :bar)))
412 (change-class.initforms.1 () change-class.initforms.3 (:foo :fez)
413 ((foo :fez)))
414 (change-class.initforms.1 () change-class.initforms.4 ()
415 ((foo :unbound))) ; default initargs are not used
416 (change-class.initforms.1 () change-class.initforms.4 (:foo :fez)
417 ((foo :fez)))
419 ;; Unbound slot remains unbound.
420 (change-class.initforms.2 () change-class.initforms.3 ()
421 ((foo :unbound)))
422 (change-class.initforms.2 () change-class.initforms.3 (:foo :fez)
423 ((foo :fez)))
424 (change-class.initforms.2 () change-class.initforms.4 ()
425 ((foo :unbound)))
426 (change-class.initforms.2 () change-class.initforms.4 (:foo :fez)
427 ((foo :fez)))
429 ;; Value is retained.
430 (change-class.initforms.2 (:foo :baz) change-class.initforms.3 ()
431 ((foo :baz)))
432 (change-class.initforms.2 (:foo :baz) change-class.initforms.3 (:foo :fez)
433 ((foo :fez)))
434 (change-class.initforms.2 (:foo :baz) change-class.initforms.4 ()
435 ((foo :baz)))
436 (change-class.initforms.2 (:foo :baz) change-class.initforms.4 (:foo :fez)
437 ((foo :fez))))))
439 ;; Test for FORWARD-REFERENCED-CLASS
441 (defclass change-class.forward-referenced.1 () ())
442 ;; CHANGE-CLASS.FORWARD-REFERENCED.2 is only needed to create the
443 ;; FORWARD-REFERENCED-CLASS CHANGE-CLASS.FORWARD-REFERENCED.3.
444 (defclass change-class.forward-referenced.2 (change-class.forward-referenced.3) ())
446 (with-test (:name (change-class sb-pcl:forward-referenced-class))
447 (mapc
448 #'change-class-test-case
449 '(;; Changing instances of "ordinary classes" to classes which are
450 ;; instances of FORWARD-REFERENCED-CLASS is not allowed.
451 (change-class.forward-referenced.1 () change-class.forward-referenced.3 ()
452 error)
454 ;; Changing instances of FORWARD-REFERENCED-CLASS into
455 ;; non-CLASSes and in particular non-CLASS metaobjects is not
456 ;; allowed.
457 ((:class change-class.forward-referenced.3) () change-class.forward-referenced.1 ()
458 error)
459 ((:class change-class.forward-referenced.3) () generic-function ()
460 error)
461 ((:class change-class.forward-referenced.3) () method ()
462 error)
463 ((:class change-class.forward-referenced.3) () slot-definition ()
464 error)
466 ;; Changing instances of FORWARD-REFERENCED-CLASS into CLASS is
467 ;; allowed but destructive. Therefore has to be final test case.
468 ((:class change-class.forward-referenced.3) () standard-class ()
469 ()))))
471 ;; Test for FUNCALLABLE-STANDARD-CLASS
473 (defclass change-class.funcallable.1 () ())
474 (defclass change-class.funcallable.2 () ()
475 (:metaclass sb-mop:funcallable-standard-class))
476 (defclass change-class.funcallable.3 () ()
477 (:metaclass sb-mop:funcallable-standard-class))
479 (with-test (:name (change-class sb-mop:funcallable-standard-class))
480 (mapc
481 #'change-class-test-case
482 '(;; Cannot change STANDARD-OBJECT into FUNCALLABLE-STANDARD-OBJECT
483 ;; and vice-versa.
484 (change-class.funcallable.1 () change-class.funcallable.2 ()
485 error)
486 (change-class.funcallable.2 () change-class.funcallable.1 ()
487 error)
488 ;; FUNCALLABLE-STANDARD-OBJECTs should work.
489 (change-class.funcallable.2 () change-class.funcallable.2 ()
491 (change-class.funcallable.2 () change-class.funcallable.3 ()
492 ()))))
494 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
495 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
496 (defgeneric bug180 (x)
497 (:method-combination list :most-specific-last))
498 (defmethod bug180 list ((x number))
499 'number)
500 (defmethod bug180 list ((x fixnum))
501 'fixnum)
502 (assert (equal (bug180 14) '(number fixnum)))
504 ;;; printing a structure class should not loop indefinitely (or cause
505 ;;; a stack overflow):
506 (defclass test-printing-structure-class ()
507 ((slot :initarg :slot))
508 (:metaclass structure-class))
509 (with-test (:name :test-printing-structure-class)
510 (write-to-string (make-instance 'test-printing-structure-class :slot 2)))
512 ;;; structure-classes should behave nicely when subclassed
513 (defclass super-structure ()
514 ((a :initarg :a :accessor a-accessor)
515 (b :initform 2 :reader b-reader))
516 (:metaclass structure-class))
517 (defclass sub-structure (super-structure)
518 ((c :initarg :c :writer c-writer :accessor c-accessor))
519 (:metaclass structure-class))
520 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
521 (assert (= (a-accessor foo) 1))
522 (assert (= (b-reader foo) 2))
523 (assert (= (c-accessor foo) 3))
524 (setf (a-accessor foo) 4)
525 (c-writer 5 foo)
526 (assert (= (a-accessor foo) 4))
527 (assert (= (c-accessor foo) 5)))
529 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
530 ;;; encoding of effective method functions for slot accessors as
531 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
532 ;;; functions to get broken in special ways even though ordinary
533 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
534 ;;; for that possibility. Now we have a few tests:
535 (defclass fish ()
536 ((fin :reader ffin :writer ffin!)
537 (tail :reader ftail :writer ftail!)))
538 (defvar *fish* (make-instance 'fish))
539 (ffin! 'triangular-fin *fish*)
540 (defclass cod (fish) ())
541 (defvar *cod* (make-instance 'cod))
542 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
543 (defmethod ffin! (new-fin (cod cod))
544 ; (format t "~&about to set ~S fin to ~S~%" cod new-fin)
545 (vector-push-extend '(cod) *clos-dispatch-side-fx*)
546 (prog1
547 (call-next-method)
548 ; (format t "~&done setting ~S fin to ~S~%" cod new-fin)
550 (defmethod ffin! :before (new-fin (cod cod))
551 (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
552 ;(format t "~&exploring the CLOS dispatch zoo with COD fins~%")
554 (ffin! 'almost-triang-fin *cod*)
555 (assert (eq (ffin *cod*) 'almost-triang-fin))
556 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
558 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
559 ;;; ignored its options; Gerd Moellmann found and fixed the problem
560 ;;; for cmucl (cmucl-imp 2002-06-18).
561 (define-method-combination test-mc (x)
562 ;; X above being a method-group-specifier
563 ((primary () :required t))
565 `(call-method ,(first primary)))
567 (defgeneric gf (obj)
568 (:method-combination test-mc 1))
570 (defmethod gf (obj)
571 obj)
573 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
574 ;;; some others were of the wrong type:
575 (macrolet ((assert-program-error (form)
576 `(multiple-value-bind (value error)
577 (ignore-errors (let ((*error-output* (make-broadcast-stream)))
578 (eval ',form)))
579 (unless (and (null value) (typep error 'program-error))
580 (error "~S failed: ~S, ~S" ',form value error)))))
581 (assert-program-error (defclass foo001 () (a b a)))
582 (assert-program-error (defclass foo002 ()
583 (a b)
584 (:default-initargs x 'a x 'b)))
585 (assert-program-error (defclass foo003 ()
586 ((a :allocation :class :allocation :class))))
587 (assert-program-error (defclass foo004 ()
588 ((a :silly t))))
589 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
590 ;; Moellmann in sbcl-0.7.8.x:
591 (assert-program-error (progn
592 (defmethod odd-key-args-checking (&key (key 42)) key)
593 (odd-key-args-checking 3)))
594 (assert (= (odd-key-args-checking) 42))
595 (assert (eq (odd-key-args-checking :key t) t))
596 ;; yet some more, fixed in sbcl-0.7.9.xx
597 (assert-program-error (defclass foo005 ()
598 (:metaclass sb-pcl::funcallable-standard-class)
599 (:metaclass 1)))
600 (assert-program-error (defclass foo006 ()
601 ((a :reader (setf a)))))
602 (assert-program-error (defclass foo007 ()
603 ((a :initarg 1))))
604 (assert-program-error (defclass foo008 ()
605 (a :initarg :a)
606 (:default-initargs :a 1)
607 (:default-initargs :a 2)))
608 ;; and also BUG 47d, fixed in sbcl-0.8alpha.0.26
609 (assert-program-error (defgeneric if (x)))
610 ;; DEFCLASS should detect an error if slot names aren't suitable as
611 ;; variable names:
612 (assert-program-error (defclass foo009 ()
613 ((:a :initarg :a))))
614 (assert-program-error (defclass foo010 ()
615 (("a" :initarg :a))))
616 (assert-program-error (defclass foo011 ()
617 ((#1a() :initarg :a))))
618 (assert-program-error (defclass foo012 ()
619 ((t :initarg :t))))
620 (assert-program-error (defclass foo013 () ("a")))
621 ;; specialized lambda lists have certain restrictions on ordering,
622 ;; repeating keywords, and the like:
623 (assert-program-error (defmethod foo014 ((foo t) &rest) nil))
624 (assert-program-error (defmethod foo015 ((foo t) &rest x y) nil))
625 (assert-program-error (defmethod foo016 ((foo t) &allow-other-keys) nil))
626 (assert-program-error (defmethod foo017 ((foo t)
627 &optional x &optional y) nil))
628 (assert-program-error (defmethod foo018 ((foo t) &rest x &rest y) nil))
629 (assert-program-error (defmethod foo019 ((foo t) &rest x &optional y) nil))
630 (assert-program-error (defmethod foo020 ((foo t) &key x &optional y) nil))
631 (assert-program-error (defmethod foo021 ((foo t) &key x &rest y) nil)))
633 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
634 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
635 ;;; (thanks to Gerd Moellmann)
636 (with-test (:name (documentation :argument-precedence-order))
637 (defun foo022 ()
638 "Documentation"
640 (let ((answer (documentation 'foo022 'function)))
641 (assert (stringp answer))
642 (defmethod documentation ((x (eql 'foo022)) y) "WRONG")
643 (assert (string= (documentation 'foo022 'function) answer))))
645 ;;; only certain declarations are permitted in DEFGENERIC
646 (macrolet ((assert-program-error (form)
647 `(multiple-value-bind (value error)
648 (ignore-errors (let ((*error-output* (make-broadcast-stream)))
649 (eval ',form)))
650 (assert (null value))
651 (assert (typep error 'program-error)))))
652 (assert-program-error (defgeneric bogus-declaration (x)
653 (declare (special y))))
654 (assert-program-error (defgeneric bogus-declaration2 (x)
655 (declare (notinline concatenate)))))
656 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
657 ;;; method.
658 (defmethod no-next-method-test ((x integer)) (call-next-method))
659 (assert-error (no-next-method-test 1) sb-pcl::no-next-method-error)
660 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
661 (declare (ignore args))
662 'success)
663 (assert (eq (no-next-method-test 1) 'success))
664 (assert-error (no-next-method-test 'foo) sb-pcl::no-applicable-method-error)
666 ;;; regression test for bug 176, following a fix that seems
667 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
668 ;;; Moellmann, merged in sbcl-0.7.9.12).
669 (dotimes (i 10)
670 (let ((lastname (intern (format nil "C176-~D" (1- i))))
671 (name (intern (format nil "C176-~D" i))))
672 (eval `(defclass ,name
673 (,@(if (= i 0) nil (list lastname)))
674 ()))
675 (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
676 (declare (ignore any))))))
677 (defclass b176 () (aslot-176))
678 (defclass c176-0 (b176) ())
679 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
681 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
682 ;;; primary methods:
683 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
684 ((around (:around))
685 (primary (dmc-test-mc) :order order :required t))
686 (let ((form (if (rest primary)
687 `(and ,@(mapcar #'(lambda (method)
688 `(call-method ,method))
689 primary))
690 `(call-method ,(first primary)))))
691 (if around
692 `(call-method ,(first around)
693 (,@(rest around)
694 (make-method ,form)))
695 form)))
697 (defgeneric dmc-test-mc (&key k)
698 (:method-combination dmc-test-mc))
700 (defmethod dmc-test-mc dmc-test-mc (&key k)
703 (dmc-test-mc :k 1)
704 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
705 ;;; the NAME argument, not some random method object. So:
706 (assert (eq (define-method-combination dmc-test-return-foo)
707 'dmc-test-return-foo))
708 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
709 'dmc-test-return-bar))
710 (assert (eq (define-method-combination dmc-test-return
711 (&optional (order :most-specific-first))
712 ((around (:around))
713 (primary (dmc-test-return) :order order :required t))
714 (let ((form (if (rest primary)
715 `(and ,@(mapcar #'(lambda (method)
716 `(call-method ,method))
717 primary))
718 `(call-method ,(first primary)))))
719 (if around
720 `(call-method ,(first around)
721 (,@(rest around)
722 (make-method ,form)))
723 form)))
724 'dmc-test-return))
726 ;;; DEFINE-METHOD-COMBINATION should, according to the description in 7.7,
727 ;;; allow you to do everything in the body forms yourself if you specify
728 ;;; exactly one method group whose qualifier-pattern is *
730 ;;; The specific language is:
731 ;;; "The use of method group specifiers provides a convenient syntax to select
732 ;;; methods, to divide them among the possible roles, and to perform the
733 ;;; necessary error checking. It is possible to perform further filtering of
734 ;;; methods in the body forms by using normal list-processing operations and
735 ;;; the functions method-qualifiers and invalid-method-error. It is permissible
736 ;;; to use setq on the variables named in the method group specifiers and to
737 ;;; bind additional variables. It is also possible to bypass the method group
738 ;;; specifier mechanism and do everything in the body forms. This is
739 ;;; accomplished by writing a single method group with * as its only
740 ;;; qualifier-pattern; the variable is then bound to a list of all of the
741 ;;; applicable methods, in most-specific-first order."
742 (define-method-combination wam-test-method-combination-a ()
743 ((all-methods *))
744 (do ((methods all-methods (rest methods))
745 (primary nil)
746 (around nil))
747 ((null methods)
748 (let ((primary (nreverse primary))
749 (around (nreverse around)))
750 (if primary
751 (let ((form (if (rest primary)
752 `(call-method ,(first primary) ,(rest primary))
753 `(call-method ,(first primary)))))
754 (if around
755 `(call-method ,(first around) (,@(rest around)
756 (make-method ,form)))
757 form))
758 `(make-method (error "No primary methods")))))
759 (let* ((method (first methods))
760 (qualifier (first (method-qualifiers method))))
761 (cond
762 ((equal :around qualifier)
763 (push method around))
764 ((null qualifier)
765 (push method primary))))))
767 (defgeneric wam-test-mc-a (val)
768 (:method-combination wam-test-method-combination-a))
769 (assert-error (wam-test-mc-a 13))
770 (defmethod wam-test-mc-a ((val number))
771 (+ val (if (next-method-p) (call-next-method) 0)))
772 (assert (= (wam-test-mc-a 13) 13))
773 (defmethod wam-test-mc-a :around ((val number))
774 (+ val (if (next-method-p) (call-next-method) 0)))
775 (assert (= (wam-test-mc-a 13) 26))
777 ;;; DEFINE-METHOD-COMBINATION
778 ;;; When two methods are in the same method group and have the same
779 ;;; specializers, their sort order within the group may be ambiguous. Therefore,
780 ;;; we should throw an error when we have two methods in the same group with
781 ;;; the same specializers /as long as/ we have more than one method group
782 ;;; or our single method group qualifier-pattern is not *. This resolves the
783 ;;; apparent conflict with the above 'It is also possible to bypass' language.
785 ;;; The language specifying this behavior is:
786 ;;; "Note that two methods with identical specializers, but with different
787 ;;; qualifiers, are not ordered by the algorithm described in Step 2 of the
788 ;;; method selection and combination process described in Section 7.6.6
789 ;;; (Method Selection and Combination). Normally the two methods play different
790 ;;; roles in the effective method because they have different qualifiers, and
791 ;;; no matter how they are ordered in the result of Step 2, the effective
792 ;;; method is the same. If the two methods play the same role and their order
793 ;;; matters, an error is signaled. This happens as part of the qualifier
794 ;;; pattern matching in define-method-combination."
796 ;;; Note that the spec pretty much equates 'method group' and 'role'.
797 ;; First we ensure that it fails correctly when there is more than one
798 ;; method group
799 (define-method-combination wam-test-method-combination-b ()
800 ((around (:around))
801 (primary * :required t))
802 (let ((form (if (rest primary)
803 `(call-method ,(first primary) ,(rest primary))
804 `(call-method ,(first primary)))))
805 (if around
806 `(call-method ,(first around) (,@(rest around)
807 (make-method ,form)))
808 form)))
810 (defgeneric wam-test-mc-b (val)
811 (:method-combination wam-test-method-combination-b))
812 (defmethod wam-test-mc-b ((val number))
813 (+ val (if (next-method-p) (call-next-method) 0)))
814 (assert (= (wam-test-mc-b 13) 13))
815 (defmethod wam-test-mc-b :around ((val number))
816 (+ val (if (next-method-p) (call-next-method) 0)))
817 (assert (= (wam-test-mc-b 13) 26))
818 (defmethod wam-test-mc-b :somethingelse ((val number))
819 (+ val (if (next-method-p) (call-next-method) 0)))
820 (let ((*error-output* (make-broadcast-stream)))
821 (assert-error (wam-test-mc-b 13)))
823 ;;; now, ensure that it fails with a single group with a qualifier-pattern
824 ;;; that is not *
825 (define-method-combination wam-test-method-combination-c ()
826 ((methods listp :required t))
827 (if (rest methods)
828 `(call-method ,(first methods) ,(rest methods))
829 `(call-method ,(first methods))))
831 (defgeneric wam-test-mc-c (val)
832 (:method-combination wam-test-method-combination-c))
833 (assert-error (wam-test-mc-c 13))
834 (defmethod wam-test-mc-c :foo ((val number))
835 (+ val (if (next-method-p) (call-next-method) 0)))
836 (assert (= (wam-test-mc-c 13) 13))
837 (defmethod wam-test-mc-c :bar ((val number))
838 (+ val (if (next-method-p) (call-next-method) 0)))
839 (let ((*error-output* (make-broadcast-stream)))
840 (assert-error (wam-test-mc-c 13)))
842 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
843 ;;; given:
844 (defmethod incompatible-ll-test-1 (x) x)
845 (assert-error (defmethod incompatible-ll-test-1 (x y) y))
846 (assert-error (defmethod incompatible-ll-test-1 (x &rest y) y))
847 ;;; Sneakily using a bit of MOPness to check some consistency
848 (assert (= (length
849 (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
851 (defmethod incompatible-ll-test-2 (x &key bar) bar)
852 (assert-error (defmethod incompatible-ll-test-2 (x) x))
853 (defmethod incompatible-ll-test-2 (x &rest y) y)
854 (assert (= (length
855 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
856 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
857 (assert (= (length
858 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
860 ;;; Per Christophe, this is an illegal method call because of 7.6.5
861 (handler-bind ((style-warning #'muffle-warning))
862 (eval '(assert-error (incompatible-ll-test-2 t 1 2))))
864 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
866 (defmethod incompatible-ll-test-3 ((x integer)) x)
867 (remove-method #'incompatible-ll-test-3
868 (find-method #'incompatible-ll-test-3
870 (list (find-class 'integer))))
871 (assert-error (defmethod incompatible-ll-test-3 (x y) (list x y)))
874 ;;; Attempting to instantiate classes with forward references in their
875 ;;; CPL should signal errors (FIXME: of what type?)
876 (defclass never-finished-class (this-one-unfinished-too) ())
877 (multiple-value-bind (result error)
878 (ignore-errors (make-instance 'never-finished-class))
879 (assert (null result))
880 (assert (typep error 'error)))
881 (multiple-value-bind (result error)
882 (ignore-errors (make-instance 'this-one-unfinished-too))
883 (assert (null result))
884 (assert (typep error 'error)))
886 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
887 ;;; weren't for a while in sbcl-0.7.9.xx)
888 (defclass superclass-with-slot ()
889 ((a :allocation :class)))
890 (defclass subclass-for-class-allocation (superclass-with-slot) ())
891 (make-instance 'subclass-for-class-allocation)
893 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
894 ;;; resulting in failure in the following:
895 (defmethod call-next-method-lexical-args ((x integer))
897 (defmethod call-next-method-lexical-args :around ((x integer))
898 (let ((x (1+ x)))
899 (declare (ignorable x))
900 (call-next-method)))
901 (assert (= (call-next-method-lexical-args 3) 3))
903 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
904 ;;; until 0.7.9.5x
905 (defvar *d-m-c-args-test* nil)
906 (define-method-combination progn-with-lock ()
907 ((methods ()))
908 (:arguments object)
909 `(unwind-protect
910 (progn (lock (object-lock ,object))
911 ,@(mapcar #'(lambda (method)
912 `(call-method ,method))
913 methods))
914 (unlock (object-lock ,object))))
915 (defun object-lock (obj)
916 (push "object-lock" *d-m-c-args-test*)
917 obj)
918 (defun unlock (obj)
919 (push "unlock" *d-m-c-args-test*)
920 obj)
921 (defun lock (obj)
922 (push "lock" *d-m-c-args-test*)
923 obj)
924 (defgeneric d-m-c-args-test (x)
925 (:method-combination progn-with-lock))
926 (defmethod d-m-c-args-test ((x symbol))
927 (push "primary" *d-m-c-args-test*))
928 (defmethod d-m-c-args-test ((x number))
929 (error "foo"))
930 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
931 (assert (equal *d-m-c-args-test*
932 '("unlock" "object-lock" "primary" "lock" "object-lock")))
933 (setf *d-m-c-args-test* nil)
934 (ignore-errors (d-m-c-args-test 1))
935 (assert (equal *d-m-c-args-test*
936 '("unlock" "object-lock" "lock" "object-lock")))
938 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
939 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
940 ;;; doesn't, but it does well enough to compile the following without
941 ;;; error (the problems remain in asking for a complete macroexpansion
942 ;;; of an arbitrary form).
943 (defgeneric bug222 (z))
944 (symbol-macrolet ((x 1))
945 (defmethod bug222 (z)
946 (macrolet ((frob (form) `(progn ,form ,x)))
947 (frob (princ-to-string x)))))
948 (assert (= (bug222 t) 1))
950 ;;; also, a test case to guard against bogus environment hacking:
952 (eval-when (:compile-toplevel :load-toplevel :execute)
953 (set 'bug222-b 3))
954 (defgeneric bug222-b (z stream))
955 ;;; this should at the least compile:
956 (let ((bug222-b 1))
957 (defmethod bug222-b (z stream)
958 (macrolet ((frob (form)
959 ;; This macro's expander should see the global value of BUG222-B.
960 ;; But forcing it do to so by explicit call of SYMBOL-VALUE
961 ;; would defeat the purpose of the test. So ignore the warning.
962 (declare (muffle-conditions warning))
963 `(progn ,form ,bug222-b)))
964 (frob (format stream "~D~%" bug222-b)))))
965 ;;; and it would be nice (though not specified by ANSI) if the answer
966 ;;; were as follows:
967 (let ((x (make-string-output-stream)))
968 (let ((value (bug222-b t x)))
969 ;; not specified by ANSI
970 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
971 (assert (= value 3)))
972 ;; specified.
973 (assert (char= (char (get-output-stream-string x) 0) #\1)))
975 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
976 ;;; for invalid initargs where it should:
977 (defclass class234 () ())
978 (defclass subclass234 (class234) ())
979 (defvar *bug234* 0)
980 (defun bug-234 ()
981 (reinitialize-instance (make-instance 'class234) :dummy 0))
982 (defun subbug-234 ()
983 (reinitialize-instance (make-instance 'subclass234) :dummy 0))
984 (assert-error (bug-234) program-error)
985 (defmethod shared-initialize :after ((i class234) slots &key dummy)
986 (declare (ignore dummy))
987 (incf *bug234*))
988 (assert (typep (subbug-234) 'subclass234))
989 (assert (= *bug234*
990 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
993 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
994 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
995 (defclass class234-b1 () ())
996 (defclass class234-b2 (class234-b1) ())
997 (defvar *bug234-b* 0)
998 (defun bug234-b ()
999 (make-instance 'class234-b2))
1000 (compile 'bug234-b)
1001 (bug234-b)
1002 (assert (= *bug234-b* 0))
1003 (defmethod initialize-instance :before ((x class234-b1) &rest args)
1004 (declare (ignore args))
1005 (incf *bug234-b*))
1006 (bug234-b)
1007 (assert (= *bug234-b* 1))
1009 ;;; we should be able to make classes with uninterned names:
1010 (defclass #:class-with-uninterned-name () ())
1012 ;;; SLOT-MISSING should be called when there are missing slots.
1013 (defclass class-with-all-slots-missing () ())
1014 (defmethod slot-missing (class (o class-with-all-slots-missing)
1015 slot-name op
1016 &optional new-value)
1017 (declare (ignore new-value))
1018 (values op 1 2 3))
1020 (with-test (:name :slot-value-missing)
1021 (assert (equal (multiple-value-list
1022 (slot-value (make-instance 'class-with-all-slots-missing) 'foo))
1023 '(slot-value)))
1024 (assert (equal (multiple-value-list
1025 (funcall (lambda (x) (slot-value x 'bar))
1026 (make-instance 'class-with-all-slots-missing)))
1027 '(slot-value))))
1029 (with-test (:name :slot-boundp-missing)
1030 (assert (equal (multiple-value-list
1031 (slot-boundp (make-instance 'class-with-all-slots-missing) 'foo))
1032 '(t)))
1033 (assert (equal (multiple-value-list
1034 (funcall (lambda (x) (slot-boundp x 'bar))
1035 (make-instance 'class-with-all-slots-missing)))
1036 '(t))))
1038 (with-test (:name :slot-setf-missing)
1039 (assert (equal (multiple-value-list
1040 (setf (slot-value (make-instance 'class-with-all-slots-missing) 'foo) 10))
1041 '(10)))
1042 (assert (equal (multiple-value-list
1043 (funcall (lambda (x) (setf (slot-value x 'bar) 20))
1044 (make-instance 'class-with-all-slots-missing)))
1045 '(20))))
1047 (macrolet ((try (which)
1048 `(assert (eq ((lambda (x)
1049 (declare (,which sb-pcl::set-slot-value))
1050 (setf (slot-value x 'b) 'baz))
1051 (make-instance 'class-with-all-slots-missing))
1052 ;; SLOT-MISSING's value is specified to be ignored; we
1053 ;; return NEW-VALUE.
1054 'baz))))
1055 (try inline)
1056 (try notinline))
1058 ;;; we should be able to specialize on anything that names a class.
1059 (defclass name-for-class () ())
1060 (defmethod something-that-specializes ((x name-for-class)) 1)
1061 (setf (find-class 'other-name-for-class) (find-class 'name-for-class))
1062 (defmethod something-that-specializes ((x other-name-for-class)) 2)
1063 (assert (= (something-that-specializes (make-instance 'name-for-class)) 2))
1064 (assert (= (something-that-specializes (make-instance 'other-name-for-class))
1067 ;;; more forward referenced classes stuff
1068 (defclass frc-1 (frc-2) ())
1069 (assert (subtypep 'frc-1 (find-class 'frc-2)))
1070 (assert (subtypep (find-class 'frc-1) 'frc-2))
1071 (assert (not (subtypep (find-class 'frc-2) 'frc-1)))
1072 (defclass frc-2 (frc-3) ((a :initarg :a)))
1073 (assert (subtypep 'frc-1 (find-class 'frc-3)))
1074 (defclass frc-3 () ())
1075 (assert (typep (make-instance 'frc-1 :a 2) (find-class 'frc-1)))
1076 (assert (typep (make-instance 'frc-2 :a 3) (find-class 'frc-2)))
1078 ;;; check that we can define classes with two slots of different names
1079 ;;; (even if it STYLE-WARNs).
1080 (defclass odd-name-class ()
1081 ((name :initarg :name)
1082 (cl-user::name :initarg :name2)))
1083 (handler-bind ((style-warning #'muffle-warning))
1084 (let ((x (make-instance 'odd-name-class :name 1 :name2 2)))
1085 (assert (= (slot-value x 'name) 1))
1086 (assert (= (slot-value x 'cl-user::name) 2))))
1088 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
1089 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
1090 (defstruct allocatable-structure a)
1091 (assert (typep (allocate-instance (find-class 'allocatable-structure))
1092 'allocatable-structure))
1094 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
1095 ;;; amazingly, calls to CPL would work a couple of times, and then
1096 ;;; start returning NIL. A fix was found (relating to the
1097 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
1098 (defgeneric cpl (x)
1099 (:method-combination list)
1100 (:method list ((x broadcast-stream)) 'broadcast-stream)
1101 (:method list ((x integer)) 'integer)
1102 (:method list ((x number)) 'number)
1103 (:method list ((x stream)) 'stream)
1104 (:method list ((x structure-object)) 'structure-object))
1105 (assert (equal (cpl 0) '(integer number)))
1106 (assert (equal (cpl 0) '(integer number)))
1107 (assert (equal (cpl 0) '(integer number)))
1108 (assert (equal (cpl 0) '(integer number)))
1109 (assert (equal (cpl 0) '(integer number)))
1110 (assert (equal (cpl (make-broadcast-stream))
1111 '(broadcast-stream stream structure-object)))
1112 (assert (equal (cpl (make-broadcast-stream))
1113 '(broadcast-stream stream structure-object)))
1114 (assert (equal (cpl (make-broadcast-stream))
1115 '(broadcast-stream stream structure-object)))
1117 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
1118 ;;; parameters shouldn't affect the arguments to the next method for a
1119 ;;; no-argument call to CALL-NEXT-METHOD
1120 (defgeneric cnm-assignment (x)
1121 (:method (x) x)
1122 (:method ((x integer)) (setq x 3)
1123 (list x (call-next-method) (call-next-method x))))
1124 (assert (equal (cnm-assignment 1) '(3 1 3)))
1126 ;;; lp#1734771: Even for assignment to parameters (implcitly or
1127 ;;; explicitly) specialized to T, or using EQL specializers.
1128 (defgeneric bug-1734771 (x y)
1129 (:method (x y) (list x y))
1130 (:method ((x integer) y)
1131 (incf y)
1132 (call-next-method))
1133 (:method ((x symbol) (y t))
1134 (incf y)
1135 (call-next-method))
1136 (:method (x (y (eql nil)))
1137 (setf y t)
1138 (call-next-method)))
1139 (with-test (:name (:cnm-assignment :bug-1734771 1))
1140 (assert (equal (bug-1734771 2 3) '(2 3))))
1141 (with-test (:name (:cnm-assignment :bug-1734771 2))
1142 (assert (equal (bug-1734771 t 3) '(t 3))))
1143 (with-test (:name (:cnm-assignment :bug-1734771 3))
1144 (assert (equal (bug-1734771 #\c nil) '(#\c nil))))
1146 (defgeneric bug-1734771-2 (x &optional y)
1147 (:method (x &optional (y nil y-p)) (list x y y-p))
1148 (:method ((x integer) &optional (y 0))
1149 (incf y)
1150 (call-next-method))
1151 (:method ((x symbol) &optional (y 0))
1152 (call-next-method)))
1153 (with-test (:name (:cnm-assignment :bug-1734771 4))
1154 (assert (equal (bug-1734771-2 2) '(2 nil nil))))
1155 (with-test (:name (:cnm-assignment :bug-1734771 5))
1156 (assert (equal (bug-1734771-2 2 0) '(2 0 t))))
1157 (with-test (:name (:cnm-assignment :bug-1734771 6))
1158 (assert (equal (bug-1734771-2 t) '(t nil nil))))
1160 ;;; Bug reported by Istvan Marko 2003-07-09
1161 (let ((class-name (gentemp)))
1162 (loop for i from 1 to 9
1163 for slot-name = (intern (format nil "X~D" i))
1164 for initarg-name = (intern (format nil "X~D" i) :keyword)
1165 collect `(,slot-name :initarg ,initarg-name) into slot-descs
1166 append `(,initarg-name (list 0)) into default-initargs
1167 finally (eval `(defclass ,class-name ()
1168 (,@slot-descs)
1169 (:default-initargs ,@default-initargs))))
1170 (let ((f (checked-compile `(lambda () (make-instance ',class-name)))))
1171 (assert (typep (funcall f) class-name))))
1173 ;;; bug 262: DEFMETHOD failed on a generic function without a lambda
1174 ;;; list
1175 (ensure-generic-function 'bug262)
1176 (defmethod bug262 (x y)
1177 (list x y))
1178 (assert (equal (bug262 1 2) '(1 2)))
1180 ;;; salex on #lisp 2003-10-13 reported that type declarations inside
1181 ;;; WITH-SLOTS are too hairy to be checked
1182 (defvar *x*)
1183 (with-test (:name (with-slots declare :note :hairy))
1184 (flet ((ensure-no-notes (form)
1185 ;; FIXME: it would be better to check specifically for the "type
1186 ;; is too hairy" note
1187 (checked-compile `(lambda () ,form) :allow-notes nil)))
1188 (ensure-no-notes '(with-slots (a) *x*
1189 (declare (integer a))
1191 (ensure-no-notes '(with-slots (a) *x*
1192 (declare (integer a))
1193 (declare (notinline slot-value))
1194 a))))
1196 ;;; from CLHS 7.6.5.1
1197 (defclass character-class () ((char :initarg :char)))
1198 (defclass picture-class () ((glyph :initarg :glyph)))
1199 (defclass character-picture-class (character-class picture-class) ())
1201 (defmethod width ((c character-class) &key font) font)
1202 (defmethod width ((p picture-class) &key pixel-size) pixel-size)
1204 (assert-error
1205 (width (make-instance 'character-class :char #\Q)
1206 :font 'baskerville :pixel-size 10)
1207 program-error)
1208 (assert-error
1209 (width (make-instance 'picture-class :glyph #\Q)
1210 :font 'baskerville :pixel-size 10)
1211 program-error)
1212 (assert (eq (width (make-instance 'character-picture-class :char #\Q)
1213 :font 'baskerville :pixel-size 10)
1214 'baskerville))
1216 ;;; class redefinition shouldn't give any warnings, in the usual case
1217 (defclass about-to-be-redefined () ((some-slot :accessor some-slot)))
1218 (handler-bind ((warning #'error))
1219 (defclass about-to-be-redefined () ((some-slot :accessor some-slot))))
1221 ;;; attempts to add accessorish methods to generic functions with more
1222 ;;; complex lambda lists should fail
1223 (defgeneric accessoroid (object &key &allow-other-keys))
1224 (assert-error
1225 (defclass accessoroid-class () ((slot :accessor accessoroid)))
1226 program-error)
1228 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1229 (defclass shared-slot-and-redefinition ()
1230 ((size :initarg :size :initform 1 :allocation :class)))
1231 (let ((i (make-instance 'shared-slot-and-redefinition)))
1232 (defclass shared-slot-and-redefinition ()
1233 ((size :initarg :size :initform 2 :allocation :class)))
1234 (assert (= (slot-value i 'size) 1)))
1236 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1237 (defclass superclass-born-to-be-obsoleted () (a))
1238 (defclass subclass-born-to-be-obsoleted (superclass-born-to-be-obsoleted) ())
1239 (defparameter *born-to-be-obsoleted*
1240 (make-instance 'subclass-born-to-be-obsoleted))
1241 (defparameter *born-to-be-obsoleted-obsoleted* nil)
1242 (defmethod update-instance-for-redefined-class
1243 ((o subclass-born-to-be-obsoleted) a d pl &key)
1244 (setf *born-to-be-obsoleted-obsoleted* t))
1245 (make-instances-obsolete 'superclass-born-to-be-obsoleted)
1246 (slot-boundp *born-to-be-obsoleted* 'a)
1247 (assert *born-to-be-obsoleted-obsoleted*)
1249 ;;; additional test suggested by Bruno Haible sbcl-devel 2004-04-21
1250 (defclass super-super-obsoleted () (a))
1251 (defclass super-obsoleted-1 (super-super-obsoleted) ())
1252 (defclass super-obsoleted-2 (super-super-obsoleted) ())
1253 (defclass obsoleted (super-obsoleted-1 super-obsoleted-2) ())
1254 (defparameter *obsoleted* (make-instance 'obsoleted))
1255 (defparameter *obsoleted-counter* 0)
1256 (defmethod update-instance-for-redefined-class ((o obsoleted) a d pl &key)
1257 (incf *obsoleted-counter*))
1258 (make-instances-obsolete 'super-super-obsoleted)
1259 (slot-boundp *obsoleted* 'a)
1260 (assert (= *obsoleted-counter* 1))
1262 ;;; yet another MAKE-INSTANCES-OBSOLETE test, this time from Nikodemus
1263 ;;; Siivola. Not all methods for accessing slots are created equal...
1264 (defclass yet-another-obsoletion-super () ((obs :accessor obs-of :initform 0)))
1265 (defclass yet-another-obsoletion-sub (yet-another-obsoletion-super) ())
1266 (defmethod shared-initialize :after ((i yet-another-obsoletion-super)
1267 slots &rest init)
1268 (declare (ignore init))
1269 (incf (obs-of i)))
1271 (defvar *yao-super* (make-instance 'yet-another-obsoletion-super))
1272 (defvar *yao-sub* (make-instance 'yet-another-obsoletion-sub))
1274 (assert (= (obs-of *yao-super*) 1))
1275 (assert (= (obs-of *yao-sub*) 1))
1276 (make-instances-obsolete 'yet-another-obsoletion-super)
1277 (assert (= (obs-of *yao-sub*) 2))
1278 (assert (= (obs-of *yao-super*) 2))
1279 (make-instances-obsolete 'yet-another-obsoletion-super)
1280 (assert (= (obs-of *yao-super*) 3))
1281 (assert (= (obs-of *yao-sub*) 3))
1282 (assert (= (slot-value *yao-super* 'obs) 3))
1283 (assert (= (slot-value *yao-sub* 'obs) 3))
1285 ;;; one more MIO test: variable slot names
1286 (defclass mio () ((x :initform 42)))
1287 (defvar *mio-slot* 'x)
1288 (defparameter *mio-counter* 0)
1289 (defmethod update-instance-for-redefined-class ((instance mio) new old plist &key)
1290 (incf *mio-counter*))
1292 (let ((x (make-instance 'mio)))
1293 (make-instances-obsolete 'mio)
1294 (slot-value x *mio-slot*))
1296 (let ((x (make-instance 'mio)))
1297 (make-instances-obsolete 'mio)
1298 (setf (slot-value x *mio-slot*) 13))
1300 (let ((x (make-instance 'mio)))
1301 (make-instances-obsolete 'mio)
1302 (slot-boundp x *mio-slot*))
1304 (let ((x (make-instance 'mio)))
1305 (make-instances-obsolete 'mio)
1306 (slot-makunbound x *mio-slot*))
1308 (assert (= 4 *mio-counter*))
1310 ;;; :class -> :instance slot allocation transfers of inherited slots,
1311 ;;; reported by Bruno Haible
1312 (with-test (:name (defclass :redefinition-class->instance-allocation))
1313 (let (i)
1314 (defclass super-with-magic-slot ()
1315 ((magic :initarg :size :initform 1 :allocation :class)))
1316 (defclass sub-of-super-with-magic-slot (super-with-magic-slot) ())
1317 (setq i (make-instance 'sub-of-super-with-magic-slot))
1318 (defclass super-with-magic-slot ()
1319 ((magic :initarg :size :initform 2)))
1320 (assert (= 1 (slot-value i 'magic)))))
1322 ;;; MAKE-INSTANCES-OBSOLETE return values
1323 (with-test (:name (make-instances-obsolete :return-values) )
1324 (defclass one-more-to-obsolete () ())
1325 (assert (eq 'one-more-to-obsolete
1326 (make-instances-obsolete 'one-more-to-obsolete)))
1327 (assert (eq (find-class 'one-more-to-obsolete)
1328 (make-instances-obsolete (find-class 'one-more-to-obsolete)))))
1330 ;;; Sensible error instead of a BUG. Reported by Thomas Burdick.
1331 (with-test (:name (defclass :slot-with-duplicate-accessors))
1332 (assert-error (defclass slot-with-duplicate-accessors ()
1333 ((slot :writer get-slot :reader get-slot)))
1334 (and error (not sb-int:bug))))
1336 ;;; BUG 321: errors in parsing DEFINE-METHOD-COMBINATION arguments
1337 ;;; lambda lists.
1339 (define-method-combination w-args ()
1340 ((method-list *))
1341 (:arguments arg1 arg2 &aux (extra :extra))
1342 `(progn ,arg1 ,arg2 ,extra
1343 ,@(mapcar (lambda (method) `(call-method ,method)) method-list)))
1344 (defgeneric mc-test-w-args (p1 p2 s)
1345 (:method-combination w-args)
1346 (:method ((p1 number) (p2 t) s)
1347 (vector-push-extend (list 'number p1 p2) s))
1348 (:method ((p1 string) (p2 t) s)
1349 (vector-push-extend (list 'string p1 p2) s))
1350 (:method ((p1 t) (p2 t) s) (vector-push-extend (list t p1 p2) s)))
1351 (let ((v (make-array 0 :adjustable t :fill-pointer t)))
1352 (assert (= (mc-test-w-args 1 2 v) 1))
1353 (assert (equal (aref v 0) '(number 1 2)))
1354 (assert (equal (aref v 1) '(t 1 2))))
1356 ;;; BUG 276: declarations and mutation.
1357 (defmethod fee ((x fixnum))
1358 (setq x (/ x 2))
1360 (assert (= (fee 1) 1/2))
1361 (defmethod fum ((x fixnum))
1362 (setf x (/ x 2))
1364 (assert (= (fum 3) 3/2))
1365 (defmethod fii ((x fixnum))
1366 (declare (special x))
1367 (setf x (/ x 2))
1369 (assert (= (fii 1) 1/2))
1370 (defvar *faa*)
1371 (defmethod faa ((*faa* string-stream))
1372 (setq *faa* (make-broadcast-stream *faa*))
1373 (write-line "Break, you sucker!" *faa*)
1374 'ok)
1375 (assert (eq 'ok (faa (make-string-output-stream))))
1376 (defmethod fex ((x fixnum) (y fixnum))
1377 (multiple-value-setq (x y) (values (/ x y) (/ y x)))
1378 (list x y))
1379 (assert (equal (fex 5 3) '(5/3 3/5)))
1381 ;;; Bug reported by Zach Beane; incorrect return of (function
1382 ;;; ',fun-name) in defgeneric
1383 (with-test (:name (defgeneric :return type))
1384 (flet ((test (form)
1385 (let ((fun (checked-compile form)))
1386 (assert (typep (funcall fun) 'generic-function)))))
1387 (test '(lambda () (flet ((nonsense () nil))
1388 (declare (ignorable #'nonsense))
1389 (defgeneric nonsense ()))))
1390 (test '(lambda () (flet ((nonsense-2 () nil))
1391 (declare (ignorable #'nonsense-2))
1392 (defgeneric nonsense-2 ()
1393 (:method () t)))))))
1395 ;;; bug reported by Bruno Haible: (setf find-class) using a
1396 ;;; forward-referenced class
1397 (defclass fr-sub (fr-super) ())
1398 (setf (find-class 'fr-alt) (find-class 'fr-super))
1399 (assert (eq (find-class 'fr-alt) (find-class 'fr-super)))
1402 ;;; ANSI Figure 4-8: all defined classes. Check that we can define
1403 ;;; methods on all of these.
1404 (progn
1405 (defgeneric method-for-defined-classes (x))
1406 (dolist (c '(arithmetic-error
1407 generic-function simple-error array hash-table
1408 simple-type-error
1409 bit-vector integer simple-warning
1410 broadcast-stream list standard-class
1411 built-in-class logical-pathname standard-generic-function
1412 cell-error method standard-method
1413 character method-combination standard-object
1414 class null storage-condition
1415 complex number stream
1416 concatenated-stream package stream-error
1417 condition package-error string
1418 cons parse-error string-stream
1419 control-error pathname structure-class
1420 division-by-zero print-not-readable structure-object
1421 echo-stream program-error style-warning
1422 end-of-file random-state symbol
1423 error ratio synonym-stream
1424 file-error rational t
1425 file-stream reader-error two-way-stream
1426 float readtable type-error
1427 floating-point-inexact real unbound-slot
1428 floating-point-invalid-operation restart unbound-variable
1429 floating-point-overflow sequence undefined-function
1430 floating-point-underflow serious-condition vector
1431 function simple-condition warning))
1432 (eval `(defmethod method-for-defined-classes ((x ,c)) (princ x))))
1433 (assert (string= (with-output-to-string (*standard-output*)
1434 (method-for-defined-classes #\3))
1435 "3")))
1439 ;;; When class definition does not complete due to a bad accessor
1440 ;;; name, do not cause an error when a new accessor name is provided
1441 ;;; during class redefinition
1443 (defun existing-name (object)
1444 (list object))
1446 (assert-error (defclass redefinition-of-accessor-class ()
1447 ((slot :accessor existing-name))))
1449 (defclass redefinition-of-accessor-class ()
1450 ((slot :accessor new-name)))
1454 (load "package-ctor-bug.lisp")
1455 (assert (= (package-ctor-bug:test) 3))
1456 (delete-package "PACKAGE-CTOR-BUG")
1457 (load "package-ctor-bug.lisp")
1458 (assert (= (package-ctor-bug:test) 3))
1460 (with-test (:name (defmethod (setf find-class) integer))
1461 (handler-bind ((warning #'muffle-warning))
1462 (mapcar #'eval
1464 (deftype defined-type () 'integer)
1465 (assert-error
1466 (defmethod method-on-defined-type ((x defined-type)) x))
1467 (deftype defined-type-and-class () 'integer)
1468 (setf (find-class 'defined-type-and-class) (find-class 'integer))
1469 (defmethod method-on-defined-type-and-class
1470 ((x defined-type-and-class))
1471 (1+ x))
1472 (assert (= (method-on-defined-type-and-class 3) 4))))))
1474 ;; bug 281
1475 (let (#+nil ; no more sb-pcl::*max-emf-precomputation-methods* as of
1476 ; sbcl-1.0.41.x
1477 (sb-pcl::*max-emf-precomputation-methods* 0))
1478 (handler-bind ((warning #'muffle-warning)) ; "invalid qualifiers"
1479 (eval '(defgeneric bug-281 (x)
1480 (:method-combination +)
1481 (:method ((x symbol)) 1)
1482 (:method + ((x number)) x))))
1483 (assert (= 1 (funcall 'bug-281 1)))
1484 (assert (= 4.2 (funcall 'bug-281 4.2)))
1485 (multiple-value-bind (val err) (ignore-errors (funcall 'bug-281 'symbol))
1486 (assert (not val))
1487 (assert (typep err 'error))))
1489 ;;; RESTART-CASE and CALL-METHOD
1491 ;;; from Bruno Haible
1493 (defun rc-cm/prompt-for-new-values ()
1494 (format *debug-io* "~&New values: ")
1495 (finish-output *debug-io*)
1496 (list (read *debug-io*)))
1498 (defun rc-cm/add-method-restarts (form method)
1499 (let ((block (gensym))
1500 (tag (gensym)))
1501 `(block ,block
1502 (tagbody
1503 ,tag
1504 (return-from ,block
1505 (restart-case ,form
1506 (method-redo ()
1507 :report (lambda (stream)
1508 (format stream "Try calling ~S again." ,method))
1509 (go ,tag))
1510 (method-return (l)
1511 :report (lambda (stream)
1512 (format stream "Specify return values for ~S call."
1513 ,method))
1514 :interactive (lambda () (rc-cm/prompt-for-new-values))
1515 (return-from ,block (values-list l)))))))))
1517 (defun rc-cm/convert-effective-method (efm)
1518 (if (consp efm)
1519 (if (eq (car efm) 'call-method)
1520 (let ((method-list (third efm)))
1521 (if (or (typep (first method-list) 'method) (rest method-list))
1522 ;; Reduce the case of multiple methods to a single one.
1523 ;; Make the call to the next-method explicit.
1524 (rc-cm/convert-effective-method
1525 `(call-method ,(second efm)
1526 ((make-method
1527 (call-method ,(first method-list) ,(rest method-list))))))
1528 ;; Now the case of at most one method.
1529 (if (typep (second efm) 'method)
1530 ;; Wrap the method call in a RESTART-CASE.
1531 (rc-cm/add-method-restarts
1532 (cons (rc-cm/convert-effective-method (car efm))
1533 (rc-cm/convert-effective-method (cdr efm)))
1534 (second efm))
1535 ;; Normal recursive processing.
1536 (cons (rc-cm/convert-effective-method (car efm))
1537 (rc-cm/convert-effective-method (cdr efm))))))
1538 (cons (rc-cm/convert-effective-method (car efm))
1539 (rc-cm/convert-effective-method (cdr efm))))
1540 efm))
1542 (define-method-combination standard-with-restarts ()
1543 ((around (:around))
1544 (before (:before))
1545 (primary () :required t)
1546 (after (:after)))
1547 (flet ((call-methods-sequentially (methods)
1548 (mapcar #'(lambda (method)
1549 `(call-method ,method))
1550 methods)))
1551 (let ((form (if (or before after (rest primary))
1552 `(multiple-value-prog1
1553 (progn
1554 ,@(call-methods-sequentially before)
1555 (call-method ,(first primary) ,(rest primary)))
1556 ,@(call-methods-sequentially (reverse after)))
1557 `(call-method ,(first primary)))))
1558 (when around
1559 (setq form
1560 `(call-method ,(first around)
1561 (,@(rest around) (make-method ,form)))))
1562 (rc-cm/convert-effective-method form))))
1564 (defgeneric rc-cm/testgf16 (x)
1565 (:method-combination standard-with-restarts))
1566 (defclass rc-cm/testclass16a () ())
1567 (defclass rc-cm/testclass16b (rc-cm/testclass16a) ())
1568 (defclass rc-cm/testclass16c (rc-cm/testclass16a) ())
1569 (defclass rc-cm/testclass16d (rc-cm/testclass16b rc-cm/testclass16c) ())
1570 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16a))
1571 (list 'a
1572 (not (null (find-restart 'method-redo)))
1573 (not (null (find-restart 'method-return)))))
1574 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16b))
1575 (cons 'b (call-next-method)))
1576 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16c))
1577 (cons 'c (call-next-method)))
1578 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16d))
1579 (cons 'd (call-next-method)))
1580 (assert (equal (rc-cm/testgf16 (make-instance 'rc-cm/testclass16d))
1581 '(d b c a t t)))
1583 ;;; test case from Gerd Moellmann
1584 (define-method-combination r-c/c-m-1 ()
1585 ((primary () :required t))
1586 `(restart-case (call-method ,(first primary))))
1588 (defgeneric r-c/c-m-1-gf ()
1589 (:method-combination r-c/c-m-1)
1590 (:method () nil))
1592 (assert (null (r-c/c-m-1-gf)))
1594 (handler-bind ((warning #'error))
1595 (eval '(defclass class-for-ctor/class-slot ()
1596 ((class-slot :initarg :class-slot :allocation :class))))
1597 (eval '(let ((c1 (make-instance 'class-for-ctor/class-slot))
1598 (c2 (make-instance 'class-for-ctor/class-slot :class-slot 1)))
1599 (assert (equal (list (slot-value c1 'class-slot)
1600 (slot-value c2 'class-slot))
1601 (list 1 1))))))
1603 ;;; tests of ctors on anonymous classes
1604 (defparameter *unnamed* (defclass ctor-unnamed-literal-class () ()))
1605 (setf (class-name *unnamed*) nil)
1606 (setf (find-class 'ctor-unnamed-literal-class) nil)
1607 (defparameter *unnamed2* (defclass ctor-unnamed-literal-class2 () ()))
1608 (defun ctor-unnamed-literal-class ()
1609 (make-instance '#.*unnamed*))
1610 (compile 'ctor-unnamed-literal-class)
1611 (defun ctor-unnamed-literal-class2 ()
1612 (make-instance '#.(find-class 'ctor-unnamed-literal-class2)))
1613 (compile 'ctor-unnamed-literal-class2)
1614 (defun ctor-unnamed-literal-class2/symbol ()
1615 (make-instance 'ctor-unnamed-literal-class2))
1616 (compile 'ctor-unnamed-literal-class2/symbol)
1617 (setf (class-name *unnamed2*) nil)
1618 (setf (find-class 'ctor-unnamed-literal-class2) nil)
1619 (with-test (:name (:ctor :unnamed-before))
1620 (assert (typep (ctor-unnamed-literal-class) *unnamed*)))
1621 (with-test (:name (:ctor :unnamed-after))
1622 (assert (typep (ctor-unnamed-literal-class2) *unnamed2*)))
1623 (with-test (:name (:ctor :unnamed-after/symbol))
1624 (assert-error (ctor-unnamed-literal-class2/symbol)))
1626 ;;; classes with slot types shouldn't break if the types don't name
1627 ;;; classes (bug #391)
1628 (defclass slot-type-superclass () ((slot :type fixnum)))
1629 (defclass slot-type-subclass (slot-type-superclass)
1630 ((slot :type (integer 1 5))))
1631 (let ((instance (make-instance 'slot-type-subclass)))
1632 (setf (slot-value instance 'slot) 3))
1634 ;;; ctors where there's a non-standard SHARED-INITIALIZE method and an
1635 ;;; initarg which isn't self-evaluating (kpreid on #lisp 2006-01-29)
1636 (defclass kpreid-enode ()
1637 ((slot :initarg not-a-keyword)))
1638 (defmethod shared-initialize ((o kpreid-enode) slots &key &allow-other-keys)
1639 (call-next-method))
1640 (defun make-kpreid-enode ()
1641 (make-instance 'kpreid-enode 'not-a-keyword 3))
1642 (with-test (:name (:ctor :non-keyword-initarg))
1643 (let ((x (make-kpreid-enode))
1644 (y (make-kpreid-enode)))
1645 (= (slot-value x 'slot) (slot-value y 'slot))))
1647 ;;; defining a class hierarchy shouldn't lead to spurious classoid
1648 ;;; errors on TYPEP questions (reported by Tim Moore on #lisp
1649 ;;; 2006-03-10)
1650 (defclass backwards-2 (backwards-1) (a b))
1651 (defclass backwards-3 (backwards-2) ())
1652 (defun typep-backwards-3 (x)
1653 (typep x 'backwards-3))
1654 (defclass backwards-1 () (a b))
1655 (assert (not (typep-backwards-3 1)))
1656 (assert (not (typep-backwards-3 (make-instance 'backwards-2))))
1657 (assert (typep-backwards-3 (make-instance 'backwards-3)))
1659 (defgeneric remove-method-1 (x)
1660 (:method ((x integer)) (1+ x)))
1661 (defgeneric remove-method-2 (x)
1662 (:method ((x integer)) (1- x)))
1663 (assert (eq #'remove-method-1
1664 (remove-method #'remove-method-1
1665 (find-method #'remove-method-2
1667 (list (find-class 'integer))))))
1668 (assert (= (remove-method-1 3) 4))
1669 (assert (= (remove-method-2 3) 2))
1671 ;;; ANSI doesn't require these restarts, but now that we have them we
1672 ;;; better test them too.
1673 (defclass slot-unbound-restart-test () ((x)))
1674 (let ((test (make-instance 'slot-unbound-restart-test)))
1675 (assert (not (slot-boundp test 'x)))
1676 (assert (= 42 (handler-bind ((unbound-slot (lambda (c) (use-value 42 c))))
1677 (slot-value test 'x))))
1678 (assert (not (slot-boundp test 'x)))
1679 (assert (= 13 (handler-bind ((unbound-slot (lambda (c) (store-value 13 c))))
1680 (slot-value test 'x))))
1681 (assert (= 13 (slot-value test 'x))))
1682 ;;; Using class instances as specializers, reported by Pascal Costanza, ref CLHS 7.6.2
1683 (defclass class-as-specializer-test ()
1685 (eval `(defmethod class-as-specializer-test1 ((x ,(find-class 'class-as-specializer-test)))
1686 'foo))
1687 (assert (eq 'foo (class-as-specializer-test1 (make-instance 'class-as-specializer-test))))
1688 (funcall (checked-compile `(lambda ()
1689 (defmethod class-as-specializer-test2 ((x ,(find-class 'class-as-specializer-test)))
1690 'bar))))
1691 (assert (eq 'bar (class-as-specializer-test2 (make-instance 'class-as-specializer-test))))
1693 ;;; CHANGE-CLASS and tricky allocation.
1694 (defclass foo-to-be-changed ()
1695 ((a :allocation :class :initform 1)))
1696 (defclass bar-to-be-changed (foo-to-be-changed) ())
1697 (defvar *bar-to-be-changed* (make-instance 'bar-to-be-changed))
1698 (defclass baz-to-be-changed ()
1699 ((a :allocation :instance :initform 2)))
1700 (change-class *bar-to-be-changed* 'baz-to-be-changed)
1701 (assert (= (slot-value *bar-to-be-changed* 'a) 1))
1703 ;;; proper name and class redefinition
1704 (defvar *to-be-renamed1* (defclass to-be-renamed1 () ()))
1705 (defvar *to-be-renamed2* (defclass to-be-renamed2 () ()))
1706 (setf (find-class 'to-be-renamed1) (find-class 'to-be-renamed2))
1707 (defvar *renamed1* (defclass to-be-renamed1 () ()))
1708 (assert (not (eq *to-be-renamed1* *to-be-renamed2*)))
1709 (assert (not (eq *to-be-renamed1* *renamed1*)))
1710 (assert (not (eq *to-be-renamed2* *renamed1*)))
1712 ;;; CLASS-NAME (and various other standardized generic functions) have
1713 ;;; their effective methods precomputed; in the process of rearranging
1714 ;;; (SETF FIND-CLASS) and FINALIZE-INHERITANCE, this broke.
1715 (defclass class-with-odd-class-name-method ()
1716 ((a :accessor class-name)))
1718 ;;; another case where precomputing (this time on PRINT-OBJECT) and
1719 ;;; lazily-finalized classes caused problems. (report from James Y
1720 ;;; Knight sbcl-devel 20-07-2006)
1722 (defclass base-print-object () ())
1723 ;;; this has the side-effect of finalizing BASE-PRINT-OBJECT, and
1724 ;;; additionally the second specializer (STREAM) changes the cache
1725 ;;; structure to require two keys, not just one.
1726 ;; warning is "Specializing on the second argument to PRINT-OBJECT ..."
1727 (handler-bind ((warning #'muffle-warning))
1728 (defmethod print-object ((o base-print-object) (s stream))
1729 nil))
1730 ;;; unfinalized as yet
1731 (defclass sub-print-object (base-print-object) ())
1732 ;;; the accessor causes an eager finalization
1733 (defclass subsub-print-object (sub-print-object)
1734 ((a :accessor a)))
1736 ;;; triggers a discriminating function (and so cache) recomputation.
1737 ;;; The method on BASE-PRINT-OBJECT will cause the system to attempt
1738 ;;; to fill the cache for all subclasses of BASE-PRINT-OBJECT which
1739 ;;; have valid wrappers; however, in the course of doing so, the
1740 ;;; SUB-PRINT-OBJECT class gets finalized, which invalidates the
1741 ;;; SUBSUB-PRINT-OBJECT wrapper; if an invalid wrapper gets into a
1742 ;;; cache with more than one key, then failure ensues.
1743 (reinitialize-instance #'print-object)
1745 ;;; bug in long-form method combination: if there's an applicable
1746 ;;; method not part of any method group, we need to call
1747 ;;; INVALID-METHOD-ERROR. (MC27 test case from Bruno Haible)
1748 (define-method-combination mc27 ()
1749 ((normal ())
1750 (ignored (:ignore :unused)))
1751 `(list 'result
1752 ,@(mapcar #'(lambda (method) `(call-method ,method)) normal)))
1753 (defgeneric test-mc27 (x)
1754 (:method-combination mc27)
1755 (:method :ignore ((x number)) (declare (notinline /)) (/ 0)))
1756 (handler-bind ((style-warning #'muffle-warning))
1757 (assert-error (test-mc27 7)))
1759 (define-method-combination mc27prime ()
1760 ((normal ())
1761 (ignored (:ignore)))
1762 `(list 'result ,@(mapcar (lambda (m) `(call-method ,m)) normal)))
1763 (defgeneric test-mc27prime (x)
1764 (:method-combination mc27prime)
1765 (:method :ignore ((x number)) (declare (notinline /)) (/ 0)))
1766 (handler-bind ((style-warning #'muffle-warning))
1767 (assert (equal '(result) (test-mc27prime 3))))
1768 (assert-error (test-mc27 t) sb-pcl::no-applicable-method-error) ; still no-applicable-method
1770 ;;; more invalid wrappers. This time for a long-standing bug in the
1771 ;;; compiler's expansion for TYPEP on various class-like things, with
1772 ;;; user-visible consequences.
1773 (defclass obsolete-again () ())
1774 (defvar *obsolete-again* (make-instance 'obsolete-again))
1775 (defvar *obsolete-again-hash* (sxhash *obsolete-again*))
1776 (make-instances-obsolete (find-class 'obsolete-again))
1777 (assert (not (streamp *obsolete-again*)))
1778 (make-instances-obsolete (find-class 'obsolete-again))
1779 (assert (= (sxhash *obsolete-again*) *obsolete-again-hash*))
1780 (compile (defun is-a-structure-object-p (x) (typep x 'structure-object)))
1781 (make-instances-obsolete (find-class 'obsolete-again))
1782 (assert (not (is-a-structure-object-p *obsolete-again*)))
1784 ;;; overeager optimization of slot-valuish things
1785 (defclass listoid ()
1786 ((caroid :initarg :caroid)
1787 (cdroid :initarg :cdroid :initform nil)))
1788 (defmethod lengthoid ((x listoid))
1789 (let ((result 0))
1790 (loop until (null x)
1791 do (incf result) (setq x (slot-value x 'cdroid)))
1792 result))
1793 (with-test (:name ((:setq :method-parameter) slot-value))
1794 (assert (= (lengthoid (make-instance 'listoid)) 1))
1795 (assert (= (lengthoid
1796 (make-instance 'listoid :cdroid
1797 (make-instance 'listoid :cdroid
1798 (make-instance 'listoid))))
1799 3)))
1803 ;;;; Tests for argument parsing in fast-method-functions.
1805 (defvar *foo* 0)
1807 (eval-when (:compile-toplevel :load-toplevel :execute)
1808 (setf (symbol-value 'a) 'invalid))
1810 (defmacro test1 (lambda-list values args &key declarations cnm)
1811 `(progn
1812 (fmakunbound 'll-method)
1813 (fmakunbound 'll-function)
1814 (defmethod ll-method ,lambda-list
1815 ,@declarations
1816 ,@(when cnm
1817 `((when nil (call-next-method))))
1818 (list ,@values))
1819 (defun ll-function ,lambda-list
1820 ,@declarations
1821 (list ,@values))
1822 (dotimes (i 2)
1823 (assert (equal (ll-method ,@args)
1824 (ll-function ,@args))))))
1826 (defmacro test (&rest args)
1827 `(progn
1828 (test1 ,@args :cnm nil)
1829 (test1 ,@args :cnm t)))
1831 ;; Just plain arguments
1833 (test (a) (a) (1))
1834 (test (a b c d e f g h i) (a b c d e f g h i) (1 2 3 4 5 6 7 8 9))
1836 (test (*foo*) (*foo* (symbol-value '*foo*)) (1))
1838 (test (a) (a (symbol-value 'a)) (1)
1839 :declarations ((declare (special a))))
1841 ;; Optionals
1843 (test (a &optional b c) (a b c) (1))
1844 (test (a &optional b c) (a b c) (1 2))
1845 (test (a &optional b c) (a b c) (1 2 3))
1847 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1))
1848 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2))
1849 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2 3))
1851 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) ())
1852 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) (1))
1854 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) ())
1855 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) (1))
1857 (test (&optional a) (a (symbol-value 'a)) ()
1858 :declarations ((declare (special a))))
1859 (test (&optional a) (a (symbol-value 'a)) (1)
1860 :declarations ((declare (special a))))
1862 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) ()
1863 :declarations ((declare (special a))))
1864 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) (1)
1865 :declarations ((declare (special a))))
1867 (defparameter *count* 0)
1869 (test (&optional (a (incf *count*)) (b (incf *count*)))
1870 (a b *count* (setf *count* 0))
1873 ;; Keywords with some &RESTs thrown in
1875 (dolist (args '((1)
1876 (1 :b 2)
1877 (1 :c 3)
1878 (1 :b 2 :c 3)
1879 (1 :c 3 :b 2)
1880 (1 :c 3 :c 1 :b 2 :b 4)))
1881 (eval `(test (a &key b c) (a b c) ,args))
1882 (eval `(test (a &key (b 'b b-p) (c 'c c-p))
1883 (a b c b-p c-p)
1884 ,args))
1885 (eval `(test (a &rest rest &key (b 'b b-p) (c 'c c-p))
1886 (a b c b-p c-p rest)
1887 ,args))
1888 (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1889 (a b c b-p c-p *foo* (symbol-value '*foo*))
1890 ,args))
1891 (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1892 (a b c b-p c-p *foo* (symbol-value '*foo*))
1893 ,args
1894 :declarations ((declare (special b-p))))))
1896 (dolist (args '(()
1897 (:*foo* 1)
1898 (:*foo* 1 :*foo* 2)))
1899 (eval `(test (&key *foo*) (*foo* (symbol-value '*foo*)) ,args))
1900 (eval `(test (&key (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p)
1901 ,args))
1902 (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1903 ,args))
1904 (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1905 ,args
1906 :declarations ((declare (special a))))))
1908 (defparameter *count* 0)
1910 (test (&key (a (incf *count*)) (b (incf *count*)))
1911 (a b *count* (setf *count* 0))
1914 (test (&key a b &allow-other-keys) (a b) (:a 1 :b 2 :c 3))
1916 (defmethod clim-style-lambda-list-test (a b &optional c d &key x y)
1917 (list a b c d x y))
1919 (clim-style-lambda-list-test 1 2)
1921 (setf *count* 0)
1923 (test (&aux (a (incf *count*)) (b (incf *count*)))
1924 (a b *count* (setf *count* 0))
1927 ;;;; long-form method combination with &rest in :arguments
1928 ;;;; (this had a bug what with fixed in 1.0.4.something)
1929 (define-method-combination long-form-with-&rest ()
1930 ((methods *))
1931 (:arguments x &rest others)
1932 `(progn
1933 ,@(mapcar (lambda (method)
1934 `(call-method ,method))
1935 methods)
1936 (list ,x (length ,others))))
1938 (defgeneric test-long-form-with-&rest (x &rest others)
1939 (:method-combination long-form-with-&rest))
1941 (defmethod test-long-form-with-&rest (x &rest others)
1942 others
1943 nil)
1945 (with-test (:name (define-method-combination :long-form-with-&rest))
1946 (assert (equal '(:foo 13)
1947 (apply #'test-long-form-with-&rest :foo (make-list 13)))))
1949 ;;;; slot-missing for non-standard classes on SLOT-VALUE
1950 ;;;;
1951 ;;;; FIXME: This is arguably not right, actually: CLHS seems to say
1952 ;;;; we should just signal an error at least for built-in classes, but
1953 ;;;; for a while we were hitting NO-APPLICABLE-METHOD, which is definitely
1954 ;;;; wrong -- so test this for now at least.
1956 (defvar *magic-symbol* (gensym "MAGIC"))
1958 (set *magic-symbol* 42)
1960 (defmethod slot-missing (class instance (slot-name (eql *magic-symbol*)) op
1961 &optional new)
1962 (if (eq 'setf op)
1963 (setf (symbol-value *magic-symbol*) new)
1964 (symbol-value *magic-symbol*)))
1966 (with-test (:name (slot-missing :non-standard-classes))
1967 (assert (eql 42 (slot-value (cons t t) *magic-symbol*)))
1968 (assert (eql 13 (setf (slot-value 123 *magic-symbol*) 13)))
1969 (assert (eql 13 (slot-value 'foobar *magic-symbol*))))
1971 ;;;; Built-in structure and condition layouts should have NIL in
1972 ;;;; LAYOUT-FOR-STD-CLASS-P, and classes should have T.
1974 (with-test (:name (sb-pcl::layout-for-std-class-p :builtin))
1975 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'warning))))
1976 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'hash-table))))
1977 (assert (eq t (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'standard-object)))))
1979 ;;;; bug 402: PCL used to warn about non-standard declarations
1980 (declaim (declaration bug-402-d))
1981 (defgeneric bug-402-gf (x))
1982 (with-test (:name (defmethod :non-standard-declaration :bug-402))
1983 (handler-bind ((warning #'error))
1984 (eval '(defmethod bug-402-gf (x)
1985 (declare (bug-402-d x))
1986 x))))
1988 ;;;; non-keyword :default-initargs + :before method on shared initialize
1989 ;;;; interacted badly with CTOR optimizations
1990 (defclass ctor-default-initarg-problem ()
1991 ((slot :initarg slotto))
1992 (:default-initargs slotto 123))
1993 (defmethod shared-initialize :before ((instance ctor-default-initarg-problem) slot-names &rest initargs)
1994 (format nil "~&Rock on: ~A~%" initargs))
1995 (defun provoke-ctor-default-initarg-problem ()
1996 (make-instance 'ctor-default-initarg-problem))
1997 (with-test (:name (make-instance :non-keyword-default-initargs
1998 shared-initialize :before))
1999 (handler-bind ((warning #'error))
2000 (assert (= 123 (slot-value (provoke-ctor-default-initarg-problem) 'slot)))))
2002 ;;;; discriminating net on streams used to generate code deletion notes on
2003 ;;;; first call
2004 (defgeneric stream-fd (stream direction))
2005 (defmethod stream-fd ((stream sb-sys:fd-stream) direction)
2006 (declare (ignore direction))
2007 (sb-sys:fd-stream-fd stream))
2008 (defmethod stream-fd ((stream synonym-stream) direction)
2009 (stream-fd (symbol-value (synonym-stream-symbol stream)) direction))
2010 (defmethod stream-fd ((stream two-way-stream) direction)
2011 (ecase direction
2012 (:input
2013 (stream-fd
2014 (two-way-stream-input-stream stream) direction))
2015 (:output
2016 (stream-fd
2017 (two-way-stream-output-stream stream) direction))))
2018 (with-test (:name (:discriminating-name :code-deletion-note))
2019 (handler-bind ((compiler-note #'error))
2020 (stream-fd sb-sys:*stdin* :output)
2021 (stream-fd sb-sys:*stdin* :output)))
2023 (with-test (:name :bug-380)
2024 (defclass bug-380 ()
2025 ((slot :accessor bug380-slot)))
2026 (fmakunbound 'foo-slot)
2027 (defgeneric foo-slot (x y z))
2028 (defclass foo ()
2029 ((slot :accessor foo-slot-value))))
2031 ;;; SET and (SETF SYMBOL-VALUE) used to confuse permuation vector
2032 ;;; optimizations
2033 (defclass fih ()
2034 ((x :initform :fih)))
2035 (defclass fah ()
2036 ((x :initform :fah)))
2037 (declaim (special *fih*))
2038 (defmethod fihfah ((*fih* fih))
2039 (set '*fih* (make-instance 'fah))
2040 (list (slot-value *fih* 'x)
2041 (eval '(slot-value *fih* 'x))))
2042 (defmethod fihfah ((fah fah))
2043 (declare (special fah))
2044 (set 'fah (make-instance 'fih))
2045 (list (slot-value fah 'x)
2046 (eval '(slot-value fah 'x))))
2047 (with-test (:name :set-of-a-method-specializer)
2048 (assert (equal '(:fah :fah) (fihfah (make-instance 'fih))))
2049 (assert (equal '(:fih :fih) (fihfah (make-instance 'fah)))))
2051 (defmethod no-implicit-declarations-for-local-specials ((faax double-float))
2052 (declare (special faax))
2053 (set 'faax (when (< faax 0) (- faax)))
2054 faax)
2055 (with-test (:name :no-implicit-declarations-for-local-specials)
2056 (assert (not (no-implicit-declarations-for-local-specials 1.0d0))))
2058 (defstruct bug-357-a
2059 slot1
2060 (slot2 t)
2061 (slot3 (coerce pi 'single-float) :type single-float))
2062 (defclass bug-357-b (bug-357-a)
2063 ((slot2 :initform 't2)
2064 (slot4 :initform -44)
2065 (slot5)
2066 (slot6 :initform t)
2067 (slot7 :initform (floor (* pi pi)))
2068 (slot8 :initform 88))
2069 (:metaclass structure-class))
2070 (defstruct (bug-357-c (:include bug-357-b (slot8 -88) (slot5 :ok)))
2071 slot9
2072 (slot10 t)
2073 (slot11 (floor (exp 3))))
2074 (with-test (:name :bug-357)
2075 (flet ((slots (x)
2076 (list (bug-357-c-slot1 x)
2077 (bug-357-c-slot2 x)
2078 (bug-357-c-slot3 x)
2079 (bug-357-c-slot4 x)
2080 (bug-357-c-slot5 x)
2081 (bug-357-c-slot6 x)
2082 (bug-357-c-slot7 x)
2083 (bug-357-c-slot8 x)
2084 (bug-357-c-slot9 x)
2085 (bug-357-c-slot10 x)
2086 (bug-357-c-slot11 x))))
2087 (let ((base (slots (make-bug-357-c))))
2088 (assert (equal base (slots (make-instance 'bug-357-c))))
2089 (assert (equal base '(nil t2 3.1415927 -44 :ok t 9 -88 nil t 20))))))
2091 (defclass class-slot-shared-initialize ()
2092 ((a :allocation :class :initform :ok)))
2093 (with-test (:name :class-slot-shared-initialize)
2094 (let ((x (make-instance 'class-slot-shared-initialize)))
2095 (assert (eq :ok (slot-value x 'a)))
2096 (slot-makunbound x 'a)
2097 (assert (not (slot-boundp x 'a)))
2098 (shared-initialize x '(a))
2099 (assert (slot-boundp x 'a))
2100 (assert (eq :ok (slot-value x 'a)))))
2102 (declaim (ftype (function (t t t) (values single-float &optional))
2103 i-dont-want-to-be-clobbered-1
2104 i-dont-want-to-be-clobbered-2))
2105 (handler-bind ((warning #'muffle-warning))
2106 (defgeneric i-dont-want-to-be-clobbered-1 (x y z))
2107 (defmethod i-dont-want-to-be-clobbered-2 ((x cons) y z)
2109 (defun i-cause-an-gf-info-update ()
2110 (i-dont-want-to-be-clobbered-2 t t t))
2111 (with-test (:name (defgeneric :should-clobber-ftype))
2112 ;; (because it doesn't check the argument or result types)
2113 (assert (equal '(function (t t t) *)
2114 (sb-kernel:type-specifier
2115 (sb-int:info :function
2116 :type 'i-dont-want-to-be-clobbered-1))))
2117 (assert (equal '(function (t t t) *)
2118 (sb-kernel:type-specifier
2119 (sb-int:info :function
2120 :type 'i-dont-want-to-be-clobbered-2))))
2121 (assert (eq :defined-method
2122 (sb-int:info :function
2123 :where-from 'i-dont-want-to-be-clobbered-1)))
2124 (assert (eq :defined-method
2125 (sb-int:info :function
2126 :where-from 'i-dont-want-to-be-clobbered-2))))
2128 (with-test (:name :bogus-parameter-specializer-name-error)
2129 (assert (eq :ok
2130 (handler-case
2131 (let ((*error-output* (make-broadcast-stream)))
2132 (eval `(defmethod #:fii ((x "a string")) 'string)))
2133 (sb-int:reference-condition (c)
2134 (when (member '(:ansi-cl :macro defmethod)
2135 (sb-int:reference-condition-references c)
2136 :test #'equal)
2137 :ok))))))
2139 (defclass remove-default-initargs-test ()
2140 ((x :initarg :x :initform 42)))
2141 (defclass remove-default-initatgs-test ()
2142 ((x :initarg :x :initform 42))
2143 (:default-initargs :x 0))
2144 (defclass remove-default-initargs-test ()
2145 ((x :initarg :x :initform 42)))
2146 (with-test (:name :remove-default-initargs)
2147 (assert (= 42 (slot-value (make-instance 'remove-default-initargs-test)
2148 'x))))
2150 ;; putting this inside WITH-TEST interferes with recognition of the class name
2151 ;; as a specializer
2152 (defclass bug-485019 ()
2153 ((array :initarg :array)))
2154 (with-test (:name :bug-485019)
2155 ;; there was a bug in WALK-SETQ, used in method body walking, in the
2156 ;; presence of declarations on symbol macros.
2157 (defmethod bug-485019 ((bug-485019 bug-485019))
2158 (with-slots (array) bug-485019
2159 (declare (type (or null simple-array) array))
2160 (setf array (make-array 4)))
2161 bug-485019)
2162 (funcall 'bug-485019 (make-instance 'bug-485019)))
2164 ;;; The compiler didn't propagate the declarared type before applying
2165 ;;; the transform for (SETF SLOT-VALUE), so the generic accessor was used.
2166 (defstruct foo-520366
2167 slot)
2168 (defun quux-520366 (cont)
2169 (funcall cont))
2170 (defun bar-520366 (foo-struct)
2171 (declare (type foo-520366 foo-struct))
2172 (with-slots (slot) foo-struct
2173 (tagbody
2174 (quux-520366 #'(lambda ()
2175 (setf slot :value)
2176 (go TAG)))
2177 TAG)))
2178 (with-test (:name :bug-520366)
2179 (let ((callees (find-named-callees #'bar-520366)))
2180 (assert (equal (list #'quux-520366) callees))))
2182 (defgeneric no-applicable-method/retry (x))
2183 (defmethod no-applicable-method/retry ((x string))
2184 "string")
2185 (with-test (:name :no-applicable-method/retry)
2186 (assert (equal "cons"
2187 (handler-bind ((sb-pcl::no-applicable-method-error
2188 (lambda (c)
2189 (declare (ignore c))
2190 (let ((r (find-restart 'sb-pcl::retry)))
2191 (when r
2192 (eval `(defmethod no-applicable-method/retry ((x cons))
2193 "cons"))
2194 (invoke-restart r))))))
2195 (no-applicable-method/retry (cons t t))))))
2197 (defgeneric no-primary-method/retry (x))
2198 (defmethod no-primary-method/retry :before (x) (assert x))
2199 (with-test (:name :no-primary-method/retry)
2200 (assert (equal "ok!"
2201 (handler-bind ((sb-pcl::no-primary-method-error
2202 (lambda (c)
2203 (declare (ignore c))
2204 (let ((r (find-restart 'sb-pcl::retry)))
2205 (when r
2206 (eval `(defmethod no-primary-method/retry (x)
2207 "ok!"))
2208 (invoke-restart r))))))
2209 (no-primary-method/retry (cons t t))))))
2211 ;;; test that a cacheing strategy for make-instance initargs checking
2212 ;;; can handle class redefinitions
2214 (defun cacheing-initargs-redefinitions-make-instances
2215 (&optional (initarg :slot))
2216 (declare (notinline make-instance))
2217 (make-instance 'cacheing-initargs-redefinitions-check)
2218 (make-instance 'cacheing-initargs-redefinitions-check initarg 3))
2220 (defclass cacheing-initargs-redefinitions-check ()
2221 ((slot :initarg :slot)))
2223 (with-test (:name (make-instance :initargs-checking-before-redefinition))
2224 (make-instance 'cacheing-initargs-redefinitions-check)
2225 (make-instance 'cacheing-initargs-redefinitions-check :slot 3)
2226 (cacheing-initargs-redefinitions-make-instances :slot)
2227 (assert-error (cacheing-initargs-redefinitions-make-instances :slot2)))
2229 (defclass cacheing-initargs-redefinitions-check ()
2230 ((slot :initarg :slot2)))
2232 (with-test (:name (make-instance :initargs-checking-after-redefinition))
2233 (make-instance 'cacheing-initargs-redefinitions-check)
2234 (make-instance 'cacheing-initargs-redefinitions-check :slot2 3)
2235 (cacheing-initargs-redefinitions-make-instances :slot2)
2236 (assert-error (cacheing-initargs-redefinitions-make-instances :slot)))
2238 (defmethod initialize-instance :after
2239 ((class cacheing-initargs-redefinitions-check) &key slot)
2240 (declare (ignore slot))
2241 nil)
2243 (with-test (:name (make-instance :initargs-checking-new-method-initargs))
2244 (make-instance 'cacheing-initargs-redefinitions-check)
2245 (make-instance 'cacheing-initargs-redefinitions-check :slot2 3)
2246 (cacheing-initargs-redefinitions-make-instances :slot2)
2247 (let ((thing (cacheing-initargs-redefinitions-make-instances :slot)))
2248 (assert (not (slot-boundp thing 'slot)))))
2251 ;;; defmethod tests
2253 (with-test (:name (defmethod :specializer-builtin-class-alias :bug-618387))
2254 (let ((alias (gensym)))
2255 (setf (find-class alias) (find-class 'symbol))
2256 (eval `(defmethod lp-618387 ((s ,alias))
2257 (symbol-name s)))
2258 (assert (equal "FOO" (funcall 'lp-618387 :foo)))))
2260 (with-test (:name (defmethod :pcl-spurious-ignore-warnings))
2261 (defgeneric no-spurious-ignore-warnings (req &key key))
2262 (handler-bind ((warning (lambda (x) (error "~A" x))))
2263 (eval
2264 '(defmethod no-spurious-ignore-warnings ((req number) &key key)
2265 (declare (ignore key))
2266 (check-type req integer))))
2267 (defgeneric should-get-an-ignore-warning (req &key key))
2268 (let ((warnings 0))
2269 (handler-bind ((warning (lambda (c) (setq warnings 1) (muffle-warning c))))
2270 (eval '(defmethod should-get-an-ignore-warning ((req integer) &key key)
2271 (check-type req integer))))
2272 (assert (= warnings 1))))
2274 (handler-bind ((style-warning #'muffle-warning))
2275 (eval '(defgeneric generic-function-pretty-arglist-optional-and-key (req &optional opt &key key)
2276 (:method (req &optional opt &key key)
2277 (list req opt key)))))
2278 (with-test (:name :generic-function-pretty-arglist-optional-and-key)
2279 (handler-bind ((warning #'error))
2280 ;; Used to signal a style-warning
2281 (assert (equal '(req &optional opt &key key)
2282 (sb-pcl::generic-function-pretty-arglist
2283 #'generic-function-pretty-arglist-optional-and-key)))))
2285 (with-test (:name :bug-894202)
2286 (assert (eq :good
2287 (handler-case
2288 (let ((name (gensym "FOO"))
2289 (decl (gensym "BAR")))
2290 (eval `(defgeneric ,name ()
2291 (declare (,decl)))))
2292 (warning ()
2293 :good)))))
2295 (with-test (:name :bug-898331)
2296 (handler-bind ((warning #'error))
2297 (eval `(defgeneric bug-898331 (request type remaining-segment-requests all-requests)))
2298 (eval `(defmethod bug-898331 ((request cons) (type (eql :cancel))
2299 remaining-segment-requests
2300 all-segment-requests)
2301 (declare (ignore all-segment-requests))
2302 (check-type request t)))))
2304 (with-test (:name (defmethod :bug-1001799))
2305 ;; compilation of the defmethod used to cause infinite recursion
2306 (let ((pax (gensym "PAX"))
2307 (pnr (gensym "PNR"))
2308 (sup (gensym "SUP"))
2309 (frob (gensym "FROB"))
2310 (sb-ext:*evaluator-mode* :compile))
2311 (eval
2312 `(progn
2313 (declaim (optimize (speed 1) (space 1) (safety 3) (debug 3) (compilation-speed 1)))
2314 (defclass ,pax (,sup)
2315 ((,pnr :type (or null ,pnr))))
2316 (defclass ,pnr (,sup)
2317 ((,pax :type (or null ,pax))))
2318 (defclass ,sup ()
2320 (defmethod ,frob ((pnr ,pnr))
2321 (slot-value pnr ',pax))
2322 (declaim (optimize (safety 1) (debug 1)))))))
2324 (defclass bug-1099708 () ((slot-1099708 :initarg :slot-1099708)))
2325 (defun make-1099708-1 ()
2326 (make-instance 'bug-1099708 :slot-1099708 '#1= (1 2 . #1#)))
2327 (defun make-1099708-2 ()
2328 (make-instance 'bug-1099708 :slot-1099708 '#2= (1 2 . #2#)))
2329 (with-test (:name :bug-1099708)
2330 ;; caused infinite equal testing in function name lookup
2331 (assert (not (eql (slot-value (make-1099708-1) 'slot-1099708)
2332 (slot-value (make-1099708-2) 'slot-1099708)))))
2334 (defclass bug-1099708b-list ()
2335 ((slot-1099708b-list :initarg :slot-1099708b-list)))
2336 (defun make-1099708b-list-1 ()
2337 (make-instance 'bug-1099708b-list :slot-1099708b-list '(some value)))
2338 (defun make-1099708b-list-2 ()
2339 (make-instance 'bug-1099708b-list :slot-1099708b-list '(some value)))
2340 (with-test (:name :bug-1099708b-list)
2341 (assert (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list)
2342 (slot-value (make-1099708b-list-1) 'slot-1099708b-list)))
2343 (assert (eql (slot-value (make-1099708b-list-2) 'slot-1099708b-list)
2344 (slot-value (make-1099708b-list-2) 'slot-1099708b-list)))
2345 (assert (not (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list)
2346 (slot-value (make-1099708b-list-2) 'slot-1099708b-list)))))
2348 (defclass bug-1099708b-string ()
2349 ((slot-1099708b-string :initarg :slot-1099708b-string)))
2350 (defun make-1099708b-string-1 ()
2351 (make-instance 'bug-1099708b-string :slot-1099708b-string "string"))
2352 (defun make-1099708b-string-2 ()
2353 (make-instance 'bug-1099708b-string :slot-1099708b-string "string"))
2354 (with-test (:name :bug-1099708b-string)
2355 (assert (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string)
2356 (slot-value (make-1099708b-string-1) 'slot-1099708b-string)))
2357 (assert (eql (slot-value (make-1099708b-string-2) 'slot-1099708b-string)
2358 (slot-value (make-1099708b-string-2) 'slot-1099708b-string)))
2359 (assert (not (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string)
2360 (slot-value (make-1099708b-string-2) 'slot-1099708b-string)))))
2362 (defclass bug-1099708b-bitvector ()
2363 ((slot-1099708b-bitvector :initarg :slot-1099708b-bitvector)))
2364 (defun make-1099708b-bitvector-1 ()
2365 (make-instance 'bug-1099708b-bitvector :slot-1099708b-bitvector #*1011))
2366 (defun make-1099708b-bitvector-2 ()
2367 (make-instance 'bug-1099708b-bitvector :slot-1099708b-bitvector #*1011))
2368 (with-test (:name :bug-1099708b-bitvector)
2369 (assert (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)
2370 (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)))
2371 (assert (eql (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)
2372 (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)))
2373 (assert (not (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)
2374 (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)))))
2376 (defclass bug-1099708b-pathname ()
2377 ((slot-1099708b-pathname :initarg :slot-1099708b-pathname)))
2378 #+nil ; after change 58602640ed, I don't see how to make this assert something useful
2379 (with-test (:name :bug-1099708b-pathname)
2380 (defun make-1099708b-pathname-1 ()
2381 (make-instance 'bug-1099708b-pathname :slot-1099708b-pathname #p"pn"))
2382 (defun make-1099708b-pathname-2 ()
2383 (make-instance 'bug-1099708b-pathname :slot-1099708b-pathname #p"pn"))
2384 (assert (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)
2385 (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)))
2386 (assert (eql (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)
2387 (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)))
2388 (assert (not (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)
2389 (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)))))
2391 (defclass bug-1099708c-list ()
2392 ((slot-1099708c-list :initarg :slot-1099708c-list)))
2393 (progn
2394 (defun make-1099708c-list-1 ()
2395 (make-instance 'bug-1099708c-list :slot-1099708c-list #1='(some value)))
2396 (defun make-1099708c-list-2 ()
2397 (make-instance 'bug-1099708c-list :slot-1099708c-list #1#)))
2398 (with-test (:name :bug-1099708c-list)
2399 (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list)
2400 (slot-value (make-1099708c-list-1) 'slot-1099708c-list)))
2401 (assert (eql (slot-value (make-1099708c-list-2) 'slot-1099708c-list)
2402 (slot-value (make-1099708c-list-2) 'slot-1099708c-list)))
2403 (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list)
2404 (slot-value (make-1099708c-list-2) 'slot-1099708c-list))))
2406 ;;; bug-1179858
2408 ;;; Define a class and force the "fallback" constructor generator to be
2409 ;;; used by having a HAIRY-AROUND-OR-NONSTANDARD-PRIMARY-METHOD-P on
2410 ;;; SHARED-INITIALIZE.
2411 (defclass bug-1179858 ()
2412 ((foo :initarg :foo :reader bug-1179858-foo))
2413 (:default-initargs :foo (error "Should not be evaluated")))
2414 (defmethod shared-initialize :around ((instance bug-1179858) (slot-names t) &key)
2415 (call-next-method))
2417 (with-test (:name (make-instance :fallback-generator-initarg-handling :bug-1179858))
2418 ;; Now compile a lambda containing MAKE-INSTANCE to exercise the
2419 ;; fallback constructor generator. Call the resulting compiled
2420 ;; function to trigger the bug.
2421 (checked-compile-and-assert ()
2422 '(lambda () (make-instance 'bug-1179858 :foo t))
2423 (() nil :test (constantly t))))
2425 ;;; Other brokenness, found while investigating: fallback-generator
2426 ;;; handling of non-keyword initialization arguments
2427 (defclass bug-1179858b ()
2428 ((foo :initarg foo :reader bug-1179858b-foo))
2429 (:default-initargs foo 14))
2430 (defmethod shared-initialize :around ((instance bug-1179858b) (slot-names t) &key)
2431 (call-next-method))
2433 (with-test (:name (make-instance :fallback-generator-non-keyword-initarg :bug-1179858))
2434 (flet ((foo= (n i) (= (bug-1179858b-foo i) n)))
2435 (assert
2436 (foo= 14 (funcall (checked-compile '(lambda () (make-instance 'bug-1179858b))))))
2437 (assert
2438 (foo= 15 (funcall (checked-compile '(lambda () (make-instance 'bug-1179858b 'foo 15))))))))
2440 (with-test (:name (:cpl-violation-setup :bug-309076))
2441 (assert-error
2442 (progn
2443 (defclass bug-309076-broken-class (standard-class) ()
2444 (:metaclass sb-mop:funcallable-standard-class))
2445 (sb-mop:finalize-inheritance (find-class 'bug-309076-broken-class)))))
2447 (defclass bug-309076-class (standard-class) ())
2448 (with-test (:name (:cpl-violation-irrelevant-class :bug-309076))
2449 (defmethod sb-mop:validate-superclass ((x bug-309076-class) (y standard-class)) t)
2450 (assert (typep (make-instance 'bug-309076-class) 'bug-309076-class)))
2452 (eval-when (:compile-toplevel :load-toplevel :execute)
2453 (require 'sb-cltl2)
2454 (defmethod b ()))
2456 (defmacro macro ()
2457 (let ((a 20))
2458 (declare (special a))
2459 (checked-compile-and-assert ()
2460 (sb-mop:make-method-lambda
2462 (find-method #'b () ())
2463 '(lambda () (declare (special a)) a)
2464 nil)
2465 (('(1) ()) 20))))
2467 (with-test (:name :make-method-lambda-leakage)
2468 ;; lambda list of X leaks into the invocation of make-method-lambda
2469 ;; during code-walking performed by make-method-lambda invoked by
2470 ;; DEFMETHOD
2471 (sb-cltl2:macroexpand-all '(defmethod x (a) (macro))))
2473 (with-test (:name (defmethod :undefined-function :bug-503095))
2474 (flet ((test-load (file)
2475 (handler-bind
2476 (((or warning error) #'error))
2477 (load file))))
2478 (multiple-value-bind (fasl warnings errorsp)
2479 (compile-file "bug-503095.lisp" :print nil :verbose nil)
2480 (unwind-protect
2481 (progn (assert (and fasl (not warnings) (not errorsp)))
2482 (test-load fasl))
2483 (and fasl (delete-file fasl))))
2484 (test-load "bug-503095-2.lisp")))
2486 (defclass a-633911 ()
2487 ((x-633911 :initform nil
2488 :accessor x-633911)))
2489 (defclass b-633911 ()
2490 ((x-633911 :initform nil
2491 :accessor x-633911)))
2492 (with-test (:name :accessor-and-plain-method)
2493 (defmethod x-633911 ((b a-633911)) 10)
2494 (assert (= (x-633911 (make-instance 'a-633911)) 10)))
2496 (with-test (:name (built-in-class :subclass error))
2497 (flet ((mapper (c)
2498 (when (and (class-name c) (typep c 'built-in-class))
2499 (assert-error (eval `(defclass ,(gensym) (,(class-name c))
2500 ()))))))
2501 (sb-pcl::map-all-classes #'mapper)))
2503 (defclass slot-value-using-class-a () ())
2504 (defclass slot-value-using-class-b () (x))
2506 (with-test (:name :svuc-with-bad-slotd)
2507 (let* ((a (make-instance 'slot-value-using-class-a))
2508 (b (make-instance 'slot-value-using-class-b))
2509 (slotd (car (sb-mop:class-slots (class-of b)))))
2510 (assert-error (sb-mop:slot-value-using-class (class-of a) a slotd))
2511 (assert-error (setf (sb-mop:slot-value-using-class (class-of a) a slotd) t))))
2513 ;; A test that the *FGENS* cache works.
2514 ;; This is not a test in the CLOS problem domain, but in the implementation
2515 ;; of discriminating functions.
2516 (defgeneric f (x y) (:method (x y) (+ x y)))
2517 (defgeneric g (x y) (:method (x y) (* x y)))
2518 (with-test (:name :fgens-sharing)
2519 (let ((count0 (hash-table-count sb-pcl::*fgens*))
2520 (foo (f 1 2))
2521 (count1 (hash-table-count sb-pcl::*fgens*))
2522 (bar (g 1 2))
2523 (count2 (hash-table-count sb-pcl::*fgens*)))
2524 (declare (ignore foo bar))
2525 (assert (= count0 count1 count2))))
2528 ;;; Classes shouldn't be their own direct or indirect superclasses or
2529 ;;; metaclasses.
2531 (with-test (:name (sb-mop:ensure-class :class-is-direct-superclass
2532 :bug-1418883))
2533 (assert-error
2534 (defclass class-with-self-as-superclass (class-with-self-as-superclass) ())))
2536 (with-test (:name (sb-mop:ensure-class :superclass-cycle :bug-1418883))
2537 ;; These have a superclass cycle from the beginning.
2538 (defclass class-with-superclass-cycle1 (class-with-superclass-cycle2) ())
2539 (assert-error
2540 (defclass class-with-superclass-cycle2 (class-with-superclass-cycle1) ())))
2542 (with-test (:name (sb-mop:ensure-class :self-metaclass))
2543 ;; These have a metaclass cycle from the beginning.
2544 (assert-error
2545 (defclass class-with-self-as-metaclass () ()
2546 (:metaclass class-with-self-as-metaclass))))
2548 (with-test (:name (sb-pcl::update-class :class-becomes-direct-superclass
2549 :bug-1418883))
2550 (defclass class-with-eventual-self-as-superclass () ())
2551 ;; Update class to introduce superclass.
2552 (assert-error
2553 (defclass class-with-eventual-self-as-superclass
2554 (class-with-eventual-self-as-superclass) ())))
2556 (with-test (:name (sb-pcl::update-class :superclasses-become-cyclic
2557 :bug-1418883))
2558 ;; Nothing wrong with these.
2559 (defclass class-with-eventual-superclass-cycle1 () ())
2560 (defclass class-with-eventual-superclass-cycle2
2561 (class-with-eventual-superclass-cycle1) ())
2562 ;; Update first class to introduce the superclass cycle.
2563 (assert-error
2564 (defclass class-with-eventual-superclass-cycle1
2565 (class-with-eventual-superclass-cycle2) ())))
2567 (with-test (:name (sb-pcl::update-class :becomes-self-metaclass))
2568 (defclass class-with-eventual-self-as-metaclass () ())
2569 ;; Try to update metaclass to self.
2570 (assert-error
2571 (defclass class-with-eventual-self-as-metaclass () ()
2572 (:metaclass class-with-eventual-self-as-metaclass))))
2574 (with-test (:name :function-keywords)
2575 (defgeneric function-keywords-test (&key))
2576 (assert (equal (function-keywords
2577 (defmethod function-keywords-test (&key a b)
2578 (declare (ignore a b))))
2579 '(:a :b))))
2581 (with-test (:name :superclass-finalization)
2582 (let* ((class1 (gensym "CLASS1-"))
2583 (class2 (gensym "CLASS2-")))
2584 (eval `(defclass ,class1 () ()))
2585 (eval `(defclass ,class2 (,class1) ()))
2586 (let ((instance (make-instance class2)))
2587 (sb-mop:finalize-inheritance (find-class class1))
2588 (assert (not (sb-kernel:layout-invalid (sb-kernel:layout-of instance)))))))
2590 (with-test (:name (allocate-instance :on symbol))
2591 (let ((class (gensym "CLASS-")))
2592 (eval `(defclass ,class () ()))
2593 (checked-compile-and-assert ()
2594 `(lambda () (allocate-instance ',class))
2595 (() (condition 'sb-pcl::no-applicable-method-error)))))
2597 (defclass unbound-slot-after-allocation=class ()
2598 ((abc :allocation :class)
2599 (d :accessor unbound-slot-after-allocation=class)))
2601 (with-test (:name :unbound-slot-after-allocation=class)
2602 (assert-error (unbound-slot-after-allocation=class
2603 (make-instance 'unbound-slot-after-allocation=class))
2604 unbound-slot))
2606 (with-test (:name :layouf-of-nil)
2607 (assert (eq (sb-kernel:layout-of nil) (sb-kernel:find-layout 'null))))
2609 (with-test (:name (defmethod :on-classless-type))
2610 (handler-bind ((timeout (lambda (condition)
2611 (declare (ignore condition))
2612 (error "Timeout"))))
2613 (sb-ext:with-timeout 1
2614 (checked-compile-and-assert (:allow-warnings t)
2615 `(lambda ()
2616 (defmethod foo ((bar keyword))))
2617 (() (condition 'sb-pcl:class-not-found-error))))))