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