tests: CHECKED-COMPILE[-AND-ASSERT] in {clos[-1],ctor}.[im]pure.lisp
[sbcl.git] / tests / clos.impure.lisp
blob232cb0b83f4f9a2fa0104a14a2c3c7746c228be0
1 ;;;; miscellaneous side-effectful tests of CLOS
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 #+interpreter (sb-ext:exit :code 104)
16 (load "compiler-test-util.lisp")
17 (defpackage "CLOS-IMPURE"
18 (:use "CL" "SB-EXT" "ASSERTOID" "TEST-UTIL" "COMPILER-TEST-UTIL"))
19 (in-package "CLOS-IMPURE")
21 ;;; It should be possible to do DEFGENERIC and DEFMETHOD referring to
22 ;;; structure types defined earlier in the file.
23 (defstruct struct-a x y)
24 (defstruct struct-b x y z)
25 (defmethod wiggle ((a struct-a))
26 (+ (struct-a-x a)
27 (struct-a-y a)))
28 (defgeneric jiggle (arg))
29 (defmethod jiggle ((a struct-a))
30 (- (struct-a-x a)
31 (struct-a-y a)))
32 (defmethod jiggle ((b struct-b))
33 (- (struct-b-x b)
34 (struct-b-y b)
35 (struct-b-z b)))
37 (with-test (:name (defmethod defstruct :same-file))
38 (assert (= (wiggle (make-struct-a :x 6 :y 5))
39 (jiggle (make-struct-b :x 19 :y 6 :z 2)))))
41 ;;; Compiling DEFGENERIC should prevent "undefined function" style
42 ;;; warnings from code within the same file.
43 (defgeneric gf-defined-in-this-file (x y))
44 (defun function-using-gf-defined-in-this-file (x y n)
45 (unless (minusp n)
46 (gf-defined-in-this-file x y)))
48 ;;; Until Martin Atzmueller ported Pierre Mai's CMU CL fixes in
49 ;;; sbcl-0.6.12.25, the implementation of NO-APPLICABLE-METHOD was
50 ;;; broken in such a way that the code here would signal an error.
51 (defgeneric zut-n-a-m (a b c))
52 (defmethod no-applicable-method ((zut-n-a-m (eql #'zut-n-a-m)) &rest args)
53 (declare (ignore args))
54 :no-applicable-method)
55 (with-test (:name no-applicable-method)
56 (assert (eq :no-applicable-method (zut-n-a-m 1 2 3))))
58 ;;; bug reported and fixed by Alexey Dejneka sbcl-devel 2001-09-10:
59 ;;; This DEFGENERIC shouldn't cause an error.
60 (defgeneric ad-gf (a) (:method :around (x) x))
62 ;;; DEFGENERIC and DEFMETHOD shouldn't accept &REST when it's not
63 ;;; followed by a variable:
64 ;;; e.g. (DEFMETHOD FOO ((X T) &REST) NIL) should signal an error.
65 ;;; Of course most of these are totally redundant
66 ;;; now that there is only one function to parse all lambda lists.
67 (eval-when (:load-toplevel :compile-toplevel :execute)
68 (defmacro expect-error (form)
69 `(multiple-value-bind (res condition)
70 (let ((*error-output* (make-broadcast-stream)))
71 (ignore-errors (eval ',form))) ; delay until *error-output* is bound
72 (declare (ignore res))
73 (typep condition 'error))))
74 (assert (expect-error (defmethod foo0 ((x t) &rest) nil)))
75 (assert (expect-error (defgeneric foo1 (x &rest))))
76 (assert (expect-error (defgeneric foo2 (x a &rest))))
77 (defgeneric foo3 (x &rest y))
78 (defmethod foo3 ((x t) &rest y) y nil)
79 (defgeneric foo4 (x &rest z &key y))
80 (defmethod foo4 ((x t) &rest z &key y) z y nil)
81 (assert (expect-error (defgeneric foo5 (x &rest))))
82 (assert (expect-error (defmethod foo6 (x &rest))))
84 ;;; legal method specializers
85 (defclass bug-525916-1 () ())
86 (defclass bug-525916-2 () ())
87 (with-test (:name (defmethod :specializer-syntax :bug-525916))
88 (assert (expect-error (defmethod invalid ((arg)) arg)))
89 (assert (expect-error (defmethod invalid (nil) 1)))
90 (assert (expect-error (defmethod invalid ((arg . bug-525916-1)) arg)))
91 (assert (expect-error (defmethod invalid ((arg bug-525916-1 bug-525916-2)) arg))))
93 ;;; more lambda-list checking
94 ;;;
95 ;;; DEFGENERIC lambda lists are subject to various limitations, as per
96 ;;; section 3.4.2 of the ANSI spec. Since Alexey Dejneka's patch for
97 ;;; bug 191-b ca. sbcl-0.7.22, these limitations should be enforced.
98 (flet ((test-case (operator lambda-list
99 &optional expect-failure-p expect-warnings-p &rest messages)
100 (multiple-value-bind
101 (fun failure-p warnings style-warnings notes errors)
102 (checked-compile `(lambda () (,operator ,(gensym) ,lambda-list))
103 :allow-failure expect-failure-p
104 :allow-warnings expect-warnings-p
105 :allow-style-warnings expect-warnings-p)
106 (declare (ignore fun notes))
107 (let ((warnings (append style-warnings warnings)))
108 (when expect-failure-p (assert failure-p))
109 (when expect-warnings-p (assert warnings))
110 (assert (= (length messages) (+ (length warnings) (length errors))))
111 (loop for message in messages
112 for error in (append warnings errors)
113 do (assert (search message (princ-to-string error))))))))
115 (with-test (:name (defgeneric :lambda-list))
116 (mapc
117 (lambda (spec) (apply #'test-case 'defgeneric spec))
118 '(;; basic sanity
119 (("a" #p"b")
120 t nil "Required argument is not a symbol: \"a\"")
121 (())
122 ((x))
123 ;; repeated names and keywords
124 ((x x)
125 t nil "The variable X occurs more than once")
126 ((x &rest x)
127 t nil "The variable X occurs more than once")
128 ((&optional x x)
129 t nil "The variable X occurs more than once")
130 ((&key x ((:y x)))
131 t nil "The variable X occurs more than once")
132 ((&key x ((:x y)))
133 t nil "The keyword :X occurs more than once")
134 ((&key ((:x a)) ((:x b)))
135 t nil "The keyword :X occurs more than once")
136 ;; illegal variable names
137 ((nil)
138 t nil "NIL cannot be used")
139 ((:pi)
140 t nil ":PI is a keyword and cannot be used")
141 ((pi)
142 t nil "COMMON-LISP:PI names a defined constant")
143 ;; forbidden default or supplied-p for &OPTIONAL or &KEY arguments
144 ((x &optional (y 0))
145 t nil "Invalid &OPTIONAL argument specifier (Y 0)")
146 ((x &optional y))
147 ((x y &key (z :z z-p))
148 t nil "Invalid &KEY argument specifier (Z :Z Z-P)")
149 ((x y &key z))
150 ((x &optional (y 0) &key z)
152 "&OPTIONAL and &KEY found in the same lambda list"
153 "Invalid &OPTIONAL argument specifier (Y 0)")
154 ((x &optional y &key z)
155 nil t "&OPTIONAL and &KEY found in the same lambda list")
156 ((x &optional y &key (z :z))
158 "&OPTIONAL and &KEY found in the same lambda list"
159 "Invalid &KEY argument specifier (Z :Z)")
160 ((&optional &key (k :k k-p))
161 t nil "Invalid &KEY argument specifier (K :K K-P)")
162 ((&optional &key k))
163 ;; forbidden &AUX
164 ((x y z &optional a &aux g h)
165 t nil "&AUX is not allowed in a generic function lambda list")
166 ((x y z &optional a))
167 ((x &aux)
168 t nil "&AUX is not allowed in a generic function lambda list")
169 ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations
170 ;; on required arguments
171 (((arg t))
172 t nil "Required argument is not a symbol: (ARG T)"))))
174 (with-test (:name (defmethod :lambda-list))
175 (mapc
176 (lambda (spec) (apply #'test-case 'defmethod spec))
177 '(;; Invalid specialized required argument
178 (((x t t))
179 t nil "arg is not a non-NIL symbol or a list of two elements: (X T T)")
180 ;; Repeated names and keywords
181 (((x t) (x t))
182 t nil "The variable X occurs more than once")
183 (((x t) &rest x)
184 t nil "The variable X occurs more than once")
185 ((&optional x x)
186 t nil "The variable X occurs more than once")
187 ((&key x ((:y x)))
188 t nil "The variable X occurs more than once")
189 ((&key x ((:x y)))
190 t nil "The keyword :X occurs more than once")
191 ;; Illegal variable names
192 (((nil t))
193 t nil "NIL cannot be used")
194 (((:pi t))
195 t nil ":PI is a keyword and cannot be used")
196 (((pi t))
197 t nil "COMMON-LISP:PI names a defined constant")))))
200 ;;; Explicit :metaclass option with structure-class and
201 ;;; standard-class.
203 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
204 (defclass structure-class-foo2 (structure-class-foo1)
205 () (:metaclass cl:structure-class))
206 (with-test (:name (defclass :metaclass cl:structure-class))
207 (assert (typep (class-of (make-instance 'structure-class-foo1))
208 'structure-class))
209 (assert (typep (make-instance 'structure-class-foo1) 'structure-class-foo1))
210 (assert (typep (class-of (make-instance 'structure-class-foo2))
211 'structure-class))
212 (assert (typep (make-instance 'structure-class-foo2) 'structure-class-foo2)))
214 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
215 (defclass standard-class-foo2 (standard-class-foo1)
216 () (:metaclass cl:standard-class))
217 (with-test (:name (defclass :metaclass cl:standard-class))
218 (assert (typep (make-instance 'standard-class-foo1) 'standard-class-foo1))
219 (assert (typep (make-instance 'standard-class-foo2) 'standard-class-foo2)))
221 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
222 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
223 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
224 ;;; DEFGENERIC-containing files does the right thing instead of
225 ;;; randomly slicing your generic functions. (APD made this work
226 ;;; in sbcl-0.7.0.2.)
227 (defgeneric born-to-be-redefined (x)
228 (:method ((x integer))
229 'integer))
230 (defmethod born-to-be-redefined ((x real))
231 'real)
232 (assert (eq (born-to-be-redefined 1) 'integer))
233 (defgeneric born-to-be-redefined (x))
234 (assert (eq (born-to-be-redefined 1) 'real)) ; failed until sbcl-0.7.0.2
235 (defgeneric born-to-be-redefined (x)
236 (:method ((x integer))
237 'integer))
238 (defmethod born-to-be-redefined ((x integer))
239 'int)
240 (assert (eq (born-to-be-redefined 1) 'int))
241 (defgeneric born-to-be-redefined (x))
242 (assert (eq (born-to-be-redefined 1) 'int))
244 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
245 ;;; preventing forward-references and also CHANGE-CLASS (which
246 ;;; forward-references used interally) from working properly.
248 ;;; One symptom was reported by Brian Spilsbury (sbcl-devel
249 ;;; 2002-04-08), and another on IRC by Dan Barlow simultaneously.
250 ;;; Better check that it doesn't happen again.
252 ;;; Some more CHANGE-CLASS testing (which only applies to the now
253 ;;; ANSI-compliant version) thanks to Espen Johnsen)
255 ;; First, the forward references:
256 (defclass forward-ref-a (forward-ref-b) ())
257 (defclass forward-ref-b () ())
258 ;; (a couple more complicated examples found by Paul Dietz' test
259 ;; suite):
260 (defclass forward-ref-c1 (forward-ref-c2) ())
261 (defclass forward-ref-c2 (forward-ref-c3) ())
263 (defclass forward-ref-d1 (forward-ref-d2 forward-ref-d3)
265 (defclass forward-ref-d2 (forward-ref-d4 forward-ref-d5)
268 ;; Then CHANGE-CLASS
269 (defun change-class-test-case (spec)
270 (destructuring-bind (from-class from-initargs
271 to-class to-initargs
272 expected-slots)
273 spec
274 (let ((from (typecase from-class
275 (symbol
276 (apply #'make-instance from-class from-initargs))
277 ((cons (eql :class) (cons symbol))
278 (find-class (second from-class))))))
279 (flet ((change ()
280 (apply #'change-class from to-class to-initargs))
281 ;; These local functions make ASSERT produce better error
282 ;; messages.
283 (slot-value-equal (instance name value expected)
284 (declare (ignore instance name))
285 (equal value expected))
286 (slot-not-bound (instance name)
287 (not (slot-boundp instance name))))
288 (case expected-slots
289 (error
290 (assert-error (change)))
291 (type-error
292 (assert-error (change) type-error))
294 (let ((to (change)))
295 (loop :for (name value) :in expected-slots
296 :do (case value
297 (:unbound
298 (assert (slot-not-bound to name)))
300 (assert
301 (slot-value-equal
302 to name (slot-value to name) value))))))))))))
304 (defclass change-class.smoke.1 ()
305 ((foo :initarg :foo)))
306 (defclass change-class.smoke.2 (change-class.smoke.1) ())
307 (defclass change-class.smoke.3 (change-class.smoke.1)
308 ((foo :initarg :foo)))
309 (defclass change-class.smoke.4 ()
310 ((foo :initarg :foo) (bar :initarg :bar)))
311 (defclass change-class.smoke.5 ()
312 ((a :initarg :a) (b :initarg :b) (c :initarg :c)))
313 (defclass change-class.smoke.6 () ())
315 (with-test (:name (change-class :smoke))
316 (mapc
317 #'change-class-test-case
318 '(;; Test unbound slots.
319 (change-class.smoke.1 () change-class.smoke.1 ()
320 ((foo :unbound)))
321 (change-class.smoke.1 () change-class.smoke.2 ()
322 ((foo :unbound)))
323 (change-class.smoke.1 () change-class.smoke.3 ()
324 ((foo :unbound)))
325 (change-class.smoke.1 () change-class.smoke.4 ()
326 ((foo :unbound) (bar :unbound)))
327 (change-class.smoke.4 (:bar 1) change-class.smoke.1 ()
328 ((foo :unbound)))
330 ;; Bound slots are retained.
331 (change-class.smoke.1 (:foo :baz) change-class.smoke.1 ()
332 ((foo :baz)))
333 (change-class.smoke.1 (:foo :baz) change-class.smoke.2 ()
334 ((foo :baz)))
335 (change-class.smoke.1 (:foo :baz) change-class.smoke.3 ()
336 ((foo :baz)))
337 (change-class.smoke.1 (:foo :baz) change-class.smoke.4 ()
338 ((foo :baz) (bar :unbound)))
339 (change-class.smoke.4 (:foo :baz) change-class.smoke.1 ()
340 ((foo :baz)))
342 ;; Original test.
343 (change-class.smoke.5 (:a 1 :b 2 :c 3) change-class.smoke.5 ()
344 ((a 1) (b 2) (c 3)))
346 ;; Original test by Espen Johnsen
347 (change-class.smoke.1 (:foo 1) change-class.smoke.4 (:bar 2)
348 ((foo 1) (bar 2)))
350 ;; Cannot change objects into metaobjects.
351 (change-class.smoke.6 () class ()
352 error)
353 (change-class.smoke.6 () generic-function ()
354 error)
355 (change-class.smoke.6 () method ()
356 error)
357 (change-class.smoke.6 () slot-definition ()
358 error))))
360 ;; Test for type-checking
362 (locally (declare (optimize (safety 3))) ; force slot type-checking
363 (defclass change-class.type-check.1 ()
364 ((foo :initarg :foo :type real)))
365 (defclass change-class.type-check.2 ()
366 ((foo :initarg :foo :type integer))))
368 (with-test (:name (change-class :type-check))
369 (mapc
370 #'change-class-test-case
371 '(;; These are allowed.
372 (change-class.type-check.1 () change-class.type-check.2 ()
373 ((foo :unbound)))
374 (change-class.type-check.1 (:foo 1) change-class.type-check.2 ()
375 ((foo 1)))
376 (change-class.type-check.1 (:foo 1.0) change-class.type-check.2 (:foo 2)
377 ((foo 2)))
379 ;; These are not allowed.
380 (change-class.type-check.1 () change-class.type-check.2 (:foo 1.0)
381 type-error)
382 (change-class.type-check.1 (:foo 1.0) change-class.type-check.2 ()
383 type-error)))
385 ;; Type-mismatches should be recoverable via USE-VALUE restart.
386 (let* ((from (make-instance 'change-class.type-check.1 :foo 1.0))
387 (to (handler-bind ((type-error (lambda (condition)
388 (declare (ignore condition))
389 (use-value 3))))
390 (change-class from 'change-class.type-check.2))))
391 (assert (equal (slot-value to 'foo) 3))))
393 ;; Test interaction with initforms and -args
395 (defclass change-class.initforms.1 ()
397 (defclass change-class.initforms.2 ()
398 ((foo :initarg :foo)))
399 (defclass change-class.initforms.3 ()
400 ((foo :initarg :foo :initform :bar)))
401 (defclass change-class.initforms.4 ()
402 ((foo :initarg :foo))
403 (:default-initargs
404 :foo :bar))
406 (with-test (:name (change-class :initforms))
407 (mapc
408 #'change-class-test-case
409 '(;; Initialization of added slot.
410 (change-class.initforms.1 () change-class.initforms.3 ()
411 ((foo :bar)))
412 (change-class.initforms.1 () change-class.initforms.3 (:foo :fez)
413 ((foo :fez)))
414 (change-class.initforms.1 () change-class.initforms.4 ()
415 ((foo :unbound))) ; default initargs are not used
416 (change-class.initforms.1 () change-class.initforms.4 (:foo :fez)
417 ((foo :fez)))
419 ;; Unbound slot remains unbound.
420 (change-class.initforms.2 () change-class.initforms.3 ()
421 ((foo :unbound)))
422 (change-class.initforms.2 () change-class.initforms.3 (:foo :fez)
423 ((foo :fez)))
424 (change-class.initforms.2 () change-class.initforms.4 ()
425 ((foo :unbound)))
426 (change-class.initforms.2 () change-class.initforms.4 (:foo :fez)
427 ((foo :fez)))
429 ;; Value is retained.
430 (change-class.initforms.2 (:foo :baz) change-class.initforms.3 ()
431 ((foo :baz)))
432 (change-class.initforms.2 (:foo :baz) change-class.initforms.3 (:foo :fez)
433 ((foo :fez)))
434 (change-class.initforms.2 (:foo :baz) change-class.initforms.4 ()
435 ((foo :baz)))
436 (change-class.initforms.2 (:foo :baz) change-class.initforms.4 (:foo :fez)
437 ((foo :fez))))))
439 ;; Test for FORWARD-REFERENCED-CLASS
441 (defclass change-class.forward-referenced.1 () ())
442 ;; CHANGE-CLASS.FORWARD-REFERENCED.2 is only needed to create the
443 ;; FORWARD-REFERENCED-CLASS CHANGE-CLASS.FORWARD-REFERENCED.3.
444 (defclass change-class.forward-referenced.2 (change-class.forward-referenced.3) ())
446 (with-test (:name (change-class sb-pcl:forward-referenced-class))
447 (mapc
448 #'change-class-test-case
449 '(;; Changing instances of "ordinary classes" to classes which are
450 ;; instances of FORWARD-REFERENCED-CLASS is not allowed.
451 (change-class.forward-referenced.1 () change-class.forward-referenced.3 ()
452 error)
454 ;; Changing instances of FORWARD-REFERENCED-CLASS into
455 ;; non-CLASSes and in particular non-CLASS metaobjects is not
456 ;; allowed.
457 ((:class change-class.forward-referenced.3) () change-class.forward-referenced.1 ()
458 error)
459 ((:class change-class.forward-referenced.3) () generic-function ()
460 error)
461 ((:class change-class.forward-referenced.3) () method ()
462 error)
463 ((:class change-class.forward-referenced.3) () slot-definition ()
464 error)
466 ;; Changing instances of FORWARD-REFERENCED-CLASS into CLASS is
467 ;; allowed but destructive. Therefore has to be final test case.
468 ((:class change-class.forward-referenced.3) () standard-class ()
469 ()))))
471 ;; Test for FUNCALLABLE-STANDARD-CLASS
473 (defclass change-class.funcallable.1 () ())
474 (defclass change-class.funcallable.2 () ()
475 (:metaclass sb-mop:funcallable-standard-class))
476 (defclass change-class.funcallable.3 () ()
477 (:metaclass sb-mop:funcallable-standard-class))
479 (with-test (:name (change-class sb-mop:funcallable-standard-class))
480 (mapc
481 #'change-class-test-case
482 '(;; Cannot change STANDARD-OBJECT into FUNCALLABLE-STANDARD-OBJECT
483 ;; and vice-versa.
484 (change-class.funcallable.1 () change-class.funcallable.2 ()
485 error)
486 (change-class.funcallable.2 () change-class.funcallable.1 ()
487 error)
488 ;; FUNCALLABLE-STANDARD-OBJECTs should work.
489 (change-class.funcallable.2 () change-class.funcallable.2 ()
491 (change-class.funcallable.2 () change-class.funcallable.3 ()
492 ()))))
494 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
495 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
496 (defgeneric bug180 (x)
497 (:method-combination list :most-specific-last))
498 (defmethod bug180 list ((x number))
499 'number)
500 (defmethod bug180 list ((x fixnum))
501 'fixnum)
502 (assert (equal (bug180 14) '(number fixnum)))
504 ;;; printing a structure class should not loop indefinitely (or cause
505 ;;; a stack overflow):
506 (defclass test-printing-structure-class ()
507 ((slot :initarg :slot))
508 (:metaclass structure-class))
509 (with-test (:name :test-printing-structure-class)
510 (write-to-string (make-instance 'test-printing-structure-class :slot 2)))
512 ;;; structure-classes should behave nicely when subclassed
513 (defclass super-structure ()
514 ((a :initarg :a :accessor a-accessor)
515 (b :initform 2 :reader b-reader))
516 (:metaclass structure-class))
517 (defclass sub-structure (super-structure)
518 ((c :initarg :c :writer c-writer :accessor c-accessor))
519 (:metaclass structure-class))
520 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
521 (assert (= (a-accessor foo) 1))
522 (assert (= (b-reader foo) 2))
523 (assert (= (c-accessor foo) 3))
524 (setf (a-accessor foo) 4)
525 (c-writer 5 foo)
526 (assert (= (a-accessor foo) 4))
527 (assert (= (c-accessor foo) 5)))
529 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
530 ;;; encoding of effective method functions for slot accessors as
531 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
532 ;;; functions to get broken in special ways even though ordinary
533 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
534 ;;; for that possibility. Now we have a few tests:
535 (defclass fish ()
536 ((fin :reader ffin :writer ffin!)
537 (tail :reader ftail :writer ftail!)))
538 (defvar *fish* (make-instance 'fish))
539 (ffin! 'triangular-fin *fish*)
540 (defclass cod (fish) ())
541 (defvar *cod* (make-instance 'cod))
542 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
543 (defmethod ffin! (new-fin (cod cod))
544 ; (format t "~&about to set ~S fin to ~S~%" cod new-fin)
545 (vector-push-extend '(cod) *clos-dispatch-side-fx*)
546 (prog1
547 (call-next-method)
548 ; (format t "~&done setting ~S fin to ~S~%" cod new-fin)
550 (defmethod ffin! :before (new-fin (cod cod))
551 (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
552 ;(format t "~&exploring the CLOS dispatch zoo with COD fins~%")
554 (ffin! 'almost-triang-fin *cod*)
555 (assert (eq (ffin *cod*) 'almost-triang-fin))
556 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
558 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
559 ;;; ignored its options; Gerd Moellmann found and fixed the problem
560 ;;; for cmucl (cmucl-imp 2002-06-18).
561 (define-method-combination test-mc (x)
562 ;; X above being a method-group-specifier
563 ((primary () :required t))
565 `(call-method ,(first primary)))
567 (defgeneric gf (obj)
568 (:method-combination test-mc 1))
570 (defmethod gf (obj)
571 obj)
573 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
574 ;;; some others were of the wrong type:
575 (macrolet ((assert-program-error (form)
576 `(multiple-value-bind (value error)
577 (ignore-errors (let ((*error-output* (make-broadcast-stream)))
578 (eval ',form)))
579 (unless (and (null value) (typep error 'program-error))
580 (error "~S failed: ~S, ~S" ',form value error)))))
581 (assert-program-error (defclass foo001 () (a b a)))
582 (assert-program-error (defclass foo002 ()
583 (a b)
584 (:default-initargs x 'a x 'b)))
585 (assert-program-error (defclass foo003 ()
586 ((a :allocation :class :allocation :class))))
587 (assert-program-error (defclass foo004 ()
588 ((a :silly t))))
589 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
590 ;; Moellmann in sbcl-0.7.8.x:
591 (assert-program-error (progn
592 (defmethod odd-key-args-checking (&key (key 42)) key)
593 (odd-key-args-checking 3)))
594 (assert (= (odd-key-args-checking) 42))
595 (assert (eq (odd-key-args-checking :key t) t))
596 ;; yet some more, fixed in sbcl-0.7.9.xx
597 (assert-program-error (defclass foo005 ()
598 (:metaclass sb-pcl::funcallable-standard-class)
599 (:metaclass 1)))
600 (assert-program-error (defclass foo006 ()
601 ((a :reader (setf a)))))
602 (assert-program-error (defclass foo007 ()
603 ((a :initarg 1))))
604 (assert-program-error (defclass foo008 ()
605 (a :initarg :a)
606 (:default-initargs :a 1)
607 (:default-initargs :a 2)))
608 ;; and also BUG 47d, fixed in sbcl-0.8alpha.0.26
609 (assert-program-error (defgeneric if (x)))
610 ;; DEFCLASS should detect an error if slot names aren't suitable as
611 ;; variable names:
612 (assert-program-error (defclass foo009 ()
613 ((:a :initarg :a))))
614 (assert-program-error (defclass foo010 ()
615 (("a" :initarg :a))))
616 (assert-program-error (defclass foo011 ()
617 ((#1a() :initarg :a))))
618 (assert-program-error (defclass foo012 ()
619 ((t :initarg :t))))
620 (assert-program-error (defclass foo013 () ("a")))
621 ;; specialized lambda lists have certain restrictions on ordering,
622 ;; repeating keywords, and the like:
623 (assert-program-error (defmethod foo014 ((foo t) &rest) nil))
624 (assert-program-error (defmethod foo015 ((foo t) &rest x y) nil))
625 (assert-program-error (defmethod foo016 ((foo t) &allow-other-keys) nil))
626 (assert-program-error (defmethod foo017 ((foo t)
627 &optional x &optional y) nil))
628 (assert-program-error (defmethod foo018 ((foo t) &rest x &rest y) nil))
629 (assert-program-error (defmethod foo019 ((foo t) &rest x &optional y) nil))
630 (assert-program-error (defmethod foo020 ((foo t) &key x &optional y) nil))
631 (assert-program-error (defmethod foo021 ((foo t) &key x &rest y) nil)))
633 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
634 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
635 ;;; (thanks to Gerd Moellmann)
636 (with-test (:name (documentation :argument-precedence-order))
637 (defun foo022 ()
638 "Documentation"
640 (let ((answer (documentation 'foo022 'function)))
641 (assert (stringp answer))
642 (defmethod documentation ((x (eql 'foo022)) y) "WRONG")
643 (assert (string= (documentation 'foo022 'function) answer))))
645 ;;; only certain declarations are permitted in DEFGENERIC
646 (macrolet ((assert-program-error (form)
647 `(multiple-value-bind (value error)
648 (ignore-errors (let ((*error-output* (make-broadcast-stream)))
649 (eval ',form)))
650 (assert (null value))
651 (assert (typep error 'program-error)))))
652 (assert-program-error (defgeneric bogus-declaration (x)
653 (declare (special y))))
654 (assert-program-error (defgeneric bogus-declaration2 (x)
655 (declare (notinline concatenate)))))
656 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
657 ;;; method.
658 (defmethod no-next-method-test ((x integer)) (call-next-method))
659 (assert-error (no-next-method-test 1) sb-pcl::no-next-method-error)
660 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
661 (declare (ignore args))
662 'success)
663 (assert (eq (no-next-method-test 1) 'success))
664 (assert-error (no-next-method-test 'foo) sb-pcl::no-applicable-method-error)
666 ;;; regression test for bug 176, following a fix that seems
667 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
668 ;;; Moellmann, merged in sbcl-0.7.9.12).
669 (dotimes (i 10)
670 (let ((lastname (intern (format nil "C176-~D" (1- i))))
671 (name (intern (format nil "C176-~D" i))))
672 (eval `(defclass ,name
673 (,@(if (= i 0) nil (list lastname)))
674 ()))
675 (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
676 (declare (ignore any))))))
677 (defclass b176 () (aslot-176))
678 (defclass c176-0 (b176) ())
679 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
681 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
682 ;;; primary methods:
683 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
684 ((around (:around))
685 (primary (dmc-test-mc) :order order :required t))
686 (let ((form (if (rest primary)
687 `(and ,@(mapcar #'(lambda (method)
688 `(call-method ,method))
689 primary))
690 `(call-method ,(first primary)))))
691 (if around
692 `(call-method ,(first around)
693 (,@(rest around)
694 (make-method ,form)))
695 form)))
697 (defgeneric dmc-test-mc (&key k)
698 (:method-combination dmc-test-mc))
700 (defmethod dmc-test-mc dmc-test-mc (&key k)
703 (dmc-test-mc :k 1)
704 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
705 ;;; the NAME argument, not some random method object. So:
706 (assert (eq (define-method-combination dmc-test-return-foo)
707 'dmc-test-return-foo))
708 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
709 'dmc-test-return-bar))
710 (assert (eq (define-method-combination dmc-test-return
711 (&optional (order :most-specific-first))
712 ((around (:around))
713 (primary (dmc-test-return) :order order :required t))
714 (let ((form (if (rest primary)
715 `(and ,@(mapcar #'(lambda (method)
716 `(call-method ,method))
717 primary))
718 `(call-method ,(first primary)))))
719 (if around
720 `(call-method ,(first around)
721 (,@(rest around)
722 (make-method ,form)))
723 form)))
724 'dmc-test-return))
726 ;;; DEFINE-METHOD-COMBINATION should, according to the description in 7.7,
727 ;;; allow you to do everything in the body forms yourself if you specify
728 ;;; exactly one method group whose qualifier-pattern is *
730 ;;; The specific language is:
731 ;;; "The use of method group specifiers provides a convenient syntax to select
732 ;;; methods, to divide them among the possible roles, and to perform the
733 ;;; necessary error checking. It is possible to perform further filtering of
734 ;;; methods in the body forms by using normal list-processing operations and
735 ;;; the functions method-qualifiers and invalid-method-error. It is permissible
736 ;;; to use setq on the variables named in the method group specifiers and to
737 ;;; bind additional variables. It is also possible to bypass the method group
738 ;;; specifier mechanism and do everything in the body forms. This is
739 ;;; accomplished by writing a single method group with * as its only
740 ;;; qualifier-pattern; the variable is then bound to a list of all of the
741 ;;; applicable methods, in most-specific-first order."
742 (define-method-combination wam-test-method-combination-a ()
743 ((all-methods *))
744 (do ((methods all-methods (rest methods))
745 (primary nil)
746 (around nil))
747 ((null methods)
748 (let ((primary (nreverse primary))
749 (around (nreverse around)))
750 (if primary
751 (let ((form (if (rest primary)
752 `(call-method ,(first primary) ,(rest primary))
753 `(call-method ,(first primary)))))
754 (if around
755 `(call-method ,(first around) (,@(rest around)
756 (make-method ,form)))
757 form))
758 `(make-method (error "No primary methods")))))
759 (let* ((method (first methods))
760 (qualifier (first (method-qualifiers method))))
761 (cond
762 ((equal :around qualifier)
763 (push method around))
764 ((null qualifier)
765 (push method primary))))))
767 (defgeneric wam-test-mc-a (val)
768 (:method-combination wam-test-method-combination-a))
769 (assert-error (wam-test-mc-a 13))
770 (defmethod wam-test-mc-a ((val number))
771 (+ val (if (next-method-p) (call-next-method) 0)))
772 (assert (= (wam-test-mc-a 13) 13))
773 (defmethod wam-test-mc-a :around ((val number))
774 (+ val (if (next-method-p) (call-next-method) 0)))
775 (assert (= (wam-test-mc-a 13) 26))
777 ;;; DEFINE-METHOD-COMBINATION
778 ;;; When two methods are in the same method group and have the same
779 ;;; specializers, their sort order within the group may be ambiguous. Therefore,
780 ;;; we should throw an error when we have two methods in the same group with
781 ;;; the same specializers /as long as/ we have more than one method group
782 ;;; or our single method group qualifier-pattern is not *. This resolves the
783 ;;; apparent conflict with the above 'It is also possible to bypass' language.
785 ;;; The language specifying this behavior is:
786 ;;; "Note that two methods with identical specializers, but with different
787 ;;; qualifiers, are not ordered by the algorithm described in Step 2 of the
788 ;;; method selection and combination process described in Section 7.6.6
789 ;;; (Method Selection and Combination). Normally the two methods play different
790 ;;; roles in the effective method because they have different qualifiers, and
791 ;;; no matter how they are ordered in the result of Step 2, the effective
792 ;;; method is the same. If the two methods play the same role and their order
793 ;;; matters, an error is signaled. This happens as part of the qualifier
794 ;;; pattern matching in define-method-combination."
796 ;;; Note that the spec pretty much equates 'method group' and 'role'.
797 ;; First we ensure that it fails correctly when there is more than one
798 ;; method group
799 (define-method-combination wam-test-method-combination-b ()
800 ((around (:around))
801 (primary * :required t))
802 (let ((form (if (rest primary)
803 `(call-method ,(first primary) ,(rest primary))
804 `(call-method ,(first primary)))))
805 (if around
806 `(call-method ,(first around) (,@(rest around)
807 (make-method ,form)))
808 form)))
810 (defgeneric wam-test-mc-b (val)
811 (:method-combination wam-test-method-combination-b))
812 (defmethod wam-test-mc-b ((val number))
813 (+ val (if (next-method-p) (call-next-method) 0)))
814 (assert (= (wam-test-mc-b 13) 13))
815 (defmethod wam-test-mc-b :around ((val number))
816 (+ val (if (next-method-p) (call-next-method) 0)))
817 (assert (= (wam-test-mc-b 13) 26))
818 (defmethod wam-test-mc-b :somethingelse ((val number))
819 (+ val (if (next-method-p) (call-next-method) 0)))
820 (let ((*error-output* (make-broadcast-stream)))
821 (assert-error (wam-test-mc-b 13)))
823 ;;; now, ensure that it fails with a single group with a qualifier-pattern
824 ;;; that is not *
825 (define-method-combination wam-test-method-combination-c ()
826 ((methods listp :required t))
827 (if (rest methods)
828 `(call-method ,(first methods) ,(rest methods))
829 `(call-method ,(first methods))))
831 (defgeneric wam-test-mc-c (val)
832 (:method-combination wam-test-method-combination-c))
833 (assert-error (wam-test-mc-c 13))
834 (defmethod wam-test-mc-c :foo ((val number))
835 (+ val (if (next-method-p) (call-next-method) 0)))
836 (assert (= (wam-test-mc-c 13) 13))
837 (defmethod wam-test-mc-c :bar ((val number))
838 (+ val (if (next-method-p) (call-next-method) 0)))
839 (let ((*error-output* (make-broadcast-stream)))
840 (assert-error (wam-test-mc-c 13)))
842 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
843 ;;; given:
844 (defmethod incompatible-ll-test-1 (x) x)
845 (assert-error (defmethod incompatible-ll-test-1 (x y) y))
846 (assert-error (defmethod incompatible-ll-test-1 (x &rest y) y))
847 ;;; Sneakily using a bit of MOPness to check some consistency
848 (assert (= (length
849 (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
851 (defmethod incompatible-ll-test-2 (x &key bar) bar)
852 (assert-error (defmethod incompatible-ll-test-2 (x) x))
853 (defmethod incompatible-ll-test-2 (x &rest y) y)
854 (assert (= (length
855 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
856 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
857 (assert (= (length
858 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
860 ;;; Per Christophe, this is an illegal method call because of 7.6.5
861 (handler-bind ((style-warning #'muffle-warning))
862 (eval '(assert-error (incompatible-ll-test-2 t 1 2))))
864 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
866 (defmethod incompatible-ll-test-3 ((x integer)) x)
867 (remove-method #'incompatible-ll-test-3
868 (find-method #'incompatible-ll-test-3
870 (list (find-class 'integer))))
871 (assert-error (defmethod incompatible-ll-test-3 (x y) (list x y)))
874 ;;; Attempting to instantiate classes with forward references in their
875 ;;; CPL should signal errors (FIXME: of what type?)
876 (defclass never-finished-class (this-one-unfinished-too) ())
877 (multiple-value-bind (result error)
878 (ignore-errors (make-instance 'never-finished-class))
879 (assert (null result))
880 (assert (typep error 'error)))
881 (multiple-value-bind (result error)
882 (ignore-errors (make-instance 'this-one-unfinished-too))
883 (assert (null result))
884 (assert (typep error 'error)))
886 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
887 ;;; weren't for a while in sbcl-0.7.9.xx)
888 (defclass superclass-with-slot ()
889 ((a :allocation :class)))
890 (defclass subclass-for-class-allocation (superclass-with-slot) ())
891 (make-instance 'subclass-for-class-allocation)
893 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
894 ;;; resulting in failure in the following:
895 (defmethod call-next-method-lexical-args ((x integer))
897 (defmethod call-next-method-lexical-args :around ((x integer))
898 (let ((x (1+ x)))
899 (declare (ignorable x))
900 (call-next-method)))
901 (assert (= (call-next-method-lexical-args 3) 3))
903 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
904 ;;; until 0.7.9.5x
905 (defvar *d-m-c-args-test* nil)
906 (define-method-combination progn-with-lock ()
907 ((methods ()))
908 (:arguments object)
909 `(unwind-protect
910 (progn (lock (object-lock ,object))
911 ,@(mapcar #'(lambda (method)
912 `(call-method ,method))
913 methods))
914 (unlock (object-lock ,object))))
915 (defun object-lock (obj)
916 (push "object-lock" *d-m-c-args-test*)
917 obj)
918 (defun unlock (obj)
919 (push "unlock" *d-m-c-args-test*)
920 obj)
921 (defun lock (obj)
922 (push "lock" *d-m-c-args-test*)
923 obj)
924 (defgeneric d-m-c-args-test (x)
925 (:method-combination progn-with-lock))
926 (defmethod d-m-c-args-test ((x symbol))
927 (push "primary" *d-m-c-args-test*))
928 (defmethod d-m-c-args-test ((x number))
929 (error "foo"))
930 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
931 (assert (equal *d-m-c-args-test*
932 '("unlock" "object-lock" "primary" "lock" "object-lock")))
933 (setf *d-m-c-args-test* nil)
934 (ignore-errors (d-m-c-args-test 1))
935 (assert (equal *d-m-c-args-test*
936 '("unlock" "object-lock" "lock" "object-lock")))
938 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
939 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
940 ;;; doesn't, but it does well enough to compile the following without
941 ;;; error (the problems remain in asking for a complete macroexpansion
942 ;;; of an arbitrary form).
943 (defgeneric bug222 (z))
944 (symbol-macrolet ((x 1))
945 (defmethod bug222 (z)
946 (macrolet ((frob (form) `(progn ,form ,x)))
947 (frob (princ-to-string x)))))
948 (assert (= (bug222 t) 1))
950 ;;; also, a test case to guard against bogus environment hacking:
952 (eval-when (:compile-toplevel :load-toplevel :execute)
953 (set 'bug222-b 3))
954 (defgeneric bug222-b (z stream))
955 ;;; this should at the least compile:
956 (let ((bug222-b 1))
957 (defmethod bug222-b (z stream)
958 (macrolet ((frob (form)
959 ;; This macro's expander should see the global value of BUG222-B.
960 ;; But forcing it do to so by explicit call of SYMBOL-VALUE
961 ;; would defeat the purpose of the test. So ignore the warning.
962 (declare (muffle-conditions warning))
963 `(progn ,form ,bug222-b)))
964 (frob (format stream "~D~%" bug222-b)))))
965 ;;; and it would be nice (though not specified by ANSI) if the answer
966 ;;; were as follows:
967 (let ((x (make-string-output-stream)))
968 (let ((value (bug222-b t x)))
969 ;; not specified by ANSI
970 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
971 (assert (= value 3)))
972 ;; specified.
973 (assert (char= (char (get-output-stream-string x) 0) #\1)))
975 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
976 ;;; for invalid initargs where it should:
977 (defclass class234 () ())
978 (defclass subclass234 (class234) ())
979 (defvar *bug234* 0)
980 (defun bug-234 ()
981 (reinitialize-instance (make-instance 'class234) :dummy 0))
982 (defun subbug-234 ()
983 (reinitialize-instance (make-instance 'subclass234) :dummy 0))
984 (assert-error (bug-234) program-error)
985 (defmethod shared-initialize :after ((i class234) slots &key dummy)
986 (declare (ignore dummy))
987 (incf *bug234*))
988 (assert (typep (subbug-234) 'subclass234))
989 (assert (= *bug234*
990 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
993 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
994 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
995 (defclass class234-b1 () ())
996 (defclass class234-b2 (class234-b1) ())
997 (defvar *bug234-b* 0)
998 (defun bug234-b ()
999 (make-instance 'class234-b2))
1000 (compile 'bug234-b)
1001 (bug234-b)
1002 (assert (= *bug234-b* 0))
1003 (defmethod initialize-instance :before ((x class234-b1) &rest args)
1004 (declare (ignore args))
1005 (incf *bug234-b*))
1006 (bug234-b)
1007 (assert (= *bug234-b* 1))
1009 ;;; we should be able to make classes with uninterned names:
1010 (defclass #:class-with-uninterned-name () ())
1012 ;;; SLOT-MISSING should be called when there are missing slots.
1013 (defclass class-with-all-slots-missing () ())
1014 (defmethod slot-missing (class (o class-with-all-slots-missing)
1015 slot-name op
1016 &optional new-value)
1017 (declare (ignore new-value))
1018 (values op 1 2 3))
1020 (with-test (:name :slot-value-missing)
1021 (assert (equal (multiple-value-list
1022 (slot-value (make-instance 'class-with-all-slots-missing) 'foo))
1023 '(slot-value)))
1024 (assert (equal (multiple-value-list
1025 (funcall (lambda (x) (slot-value x 'bar))
1026 (make-instance 'class-with-all-slots-missing)))
1027 '(slot-value))))
1029 (with-test (:name :slot-boundp-missing)
1030 (assert (equal (multiple-value-list
1031 (slot-boundp (make-instance 'class-with-all-slots-missing) 'foo))
1032 '(t)))
1033 (assert (equal (multiple-value-list
1034 (funcall (lambda (x) (slot-boundp x 'bar))
1035 (make-instance 'class-with-all-slots-missing)))
1036 '(t))))
1038 (with-test (:name :slot-setf-missing)
1039 (assert (equal (multiple-value-list
1040 (setf (slot-value (make-instance 'class-with-all-slots-missing) 'foo) 10))
1041 '(10)))
1042 (assert (equal (multiple-value-list
1043 (funcall (lambda (x) (setf (slot-value x 'bar) 20))
1044 (make-instance 'class-with-all-slots-missing)))
1045 '(20))))
1047 (macrolet ((try (which)
1048 `(assert (eq ((lambda (x)
1049 (declare (,which sb-pcl::set-slot-value))
1050 (setf (slot-value x 'b) 'baz))
1051 (make-instance 'class-with-all-slots-missing))
1052 ;; SLOT-MISSING's value is specified to be ignored; we
1053 ;; return NEW-VALUE.
1054 'baz))))
1055 (try inline)
1056 (try notinline))
1058 ;;; we should be able to specialize on anything that names a class.
1059 (defclass name-for-class () ())
1060 (defmethod something-that-specializes ((x name-for-class)) 1)
1061 (setf (find-class 'other-name-for-class) (find-class 'name-for-class))
1062 (defmethod something-that-specializes ((x other-name-for-class)) 2)
1063 (assert (= (something-that-specializes (make-instance 'name-for-class)) 2))
1064 (assert (= (something-that-specializes (make-instance 'other-name-for-class))
1067 ;;; more forward referenced classes stuff
1068 (defclass frc-1 (frc-2) ())
1069 (assert (subtypep 'frc-1 (find-class 'frc-2)))
1070 (assert (subtypep (find-class 'frc-1) 'frc-2))
1071 (assert (not (subtypep (find-class 'frc-2) 'frc-1)))
1072 (defclass frc-2 (frc-3) ((a :initarg :a)))
1073 (assert (subtypep 'frc-1 (find-class 'frc-3)))
1074 (defclass frc-3 () ())
1075 (assert (typep (make-instance 'frc-1 :a 2) (find-class 'frc-1)))
1076 (assert (typep (make-instance 'frc-2 :a 3) (find-class 'frc-2)))
1078 ;;; check that we can define classes with two slots of different names
1079 ;;; (even if it STYLE-WARNs).
1080 (defclass odd-name-class ()
1081 ((name :initarg :name)
1082 (cl-user::name :initarg :name2)))
1083 (handler-bind ((style-warning #'muffle-warning))
1084 (let ((x (make-instance 'odd-name-class :name 1 :name2 2)))
1085 (assert (= (slot-value x 'name) 1))
1086 (assert (= (slot-value x 'cl-user::name) 2))))
1088 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
1089 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
1090 (defstruct allocatable-structure a)
1091 (assert (typep (allocate-instance (find-class 'allocatable-structure))
1092 'allocatable-structure))
1094 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
1095 ;;; amazingly, calls to CPL would work a couple of times, and then
1096 ;;; start returning NIL. A fix was found (relating to the
1097 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
1098 (defgeneric cpl (x)
1099 (:method-combination list)
1100 (:method list ((x broadcast-stream)) 'broadcast-stream)
1101 (:method list ((x integer)) 'integer)
1102 (:method list ((x number)) 'number)
1103 (:method list ((x stream)) 'stream)
1104 (:method list ((x structure-object)) 'structure-object))
1105 (assert (equal (cpl 0) '(integer number)))
1106 (assert (equal (cpl 0) '(integer number)))
1107 (assert (equal (cpl 0) '(integer number)))
1108 (assert (equal (cpl 0) '(integer number)))
1109 (assert (equal (cpl 0) '(integer number)))
1110 (assert (equal (cpl (make-broadcast-stream))
1111 '(broadcast-stream stream structure-object)))
1112 (assert (equal (cpl (make-broadcast-stream))
1113 '(broadcast-stream stream structure-object)))
1114 (assert (equal (cpl (make-broadcast-stream))
1115 '(broadcast-stream stream structure-object)))
1117 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
1118 ;;; parameters shouldn't affect the arguments to the next method for a
1119 ;;; no-argument call to CALL-NEXT-METHOD
1120 (defgeneric cnm-assignment (x)
1121 (:method (x) x)
1122 (:method ((x integer)) (setq x 3)
1123 (list x (call-next-method) (call-next-method x))))
1124 (assert (equal (cnm-assignment 1) '(3 1 3)))
1126 ;;; Bug reported by Istvan Marko 2003-07-09
1127 (let ((class-name (gentemp)))
1128 (loop for i from 1 to 9
1129 for slot-name = (intern (format nil "X~D" i))
1130 for initarg-name = (intern (format nil "X~D" i) :keyword)
1131 collect `(,slot-name :initarg ,initarg-name) into slot-descs
1132 append `(,initarg-name (list 0)) into default-initargs
1133 finally (eval `(defclass ,class-name ()
1134 (,@slot-descs)
1135 (:default-initargs ,@default-initargs))))
1136 (let ((f (checked-compile `(lambda () (make-instance ',class-name)))))
1137 (assert (typep (funcall f) class-name))))
1139 ;;; bug 262: DEFMETHOD failed on a generic function without a lambda
1140 ;;; list
1141 (ensure-generic-function 'bug262)
1142 (defmethod bug262 (x y)
1143 (list x y))
1144 (assert (equal (bug262 1 2) '(1 2)))
1146 ;;; salex on #lisp 2003-10-13 reported that type declarations inside
1147 ;;; WITH-SLOTS are too hairy to be checked
1148 (defvar *x*)
1149 (with-test (:name (with-slots declare :note :hairy))
1150 (flet ((ensure-no-notes (form)
1151 ;; FIXME: it would be better to check specifically for the "type
1152 ;; is too hairy" note
1153 (checked-compile `(lambda () ,form) :allow-notes nil)))
1154 (ensure-no-notes '(with-slots (a) *x*
1155 (declare (integer a))
1157 (ensure-no-notes '(with-slots (a) *x*
1158 (declare (integer a))
1159 (declare (notinline slot-value))
1160 a))))
1162 ;;; from CLHS 7.6.5.1
1163 (defclass character-class () ((char :initarg :char)))
1164 (defclass picture-class () ((glyph :initarg :glyph)))
1165 (defclass character-picture-class (character-class picture-class) ())
1167 (defmethod width ((c character-class) &key font) font)
1168 (defmethod width ((p picture-class) &key pixel-size) pixel-size)
1170 (assert-error
1171 (width (make-instance 'character-class :char #\Q)
1172 :font 'baskerville :pixel-size 10)
1173 program-error)
1174 (assert-error
1175 (width (make-instance 'picture-class :glyph #\Q)
1176 :font 'baskerville :pixel-size 10)
1177 program-error)
1178 (assert (eq (width (make-instance 'character-picture-class :char #\Q)
1179 :font 'baskerville :pixel-size 10)
1180 'baskerville))
1182 ;;; class redefinition shouldn't give any warnings, in the usual case
1183 (defclass about-to-be-redefined () ((some-slot :accessor some-slot)))
1184 (handler-bind ((warning #'error))
1185 (defclass about-to-be-redefined () ((some-slot :accessor some-slot))))
1187 ;;; attempts to add accessorish methods to generic functions with more
1188 ;;; complex lambda lists should fail
1189 (defgeneric accessoroid (object &key &allow-other-keys))
1190 (assert-error
1191 (defclass accessoroid-class () ((slot :accessor accessoroid)))
1192 program-error)
1194 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1195 (defclass shared-slot-and-redefinition ()
1196 ((size :initarg :size :initform 1 :allocation :class)))
1197 (let ((i (make-instance 'shared-slot-and-redefinition)))
1198 (defclass shared-slot-and-redefinition ()
1199 ((size :initarg :size :initform 2 :allocation :class)))
1200 (assert (= (slot-value i 'size) 1)))
1202 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1203 (defclass superclass-born-to-be-obsoleted () (a))
1204 (defclass subclass-born-to-be-obsoleted (superclass-born-to-be-obsoleted) ())
1205 (defparameter *born-to-be-obsoleted*
1206 (make-instance 'subclass-born-to-be-obsoleted))
1207 (defparameter *born-to-be-obsoleted-obsoleted* nil)
1208 (defmethod update-instance-for-redefined-class
1209 ((o subclass-born-to-be-obsoleted) a d pl &key)
1210 (setf *born-to-be-obsoleted-obsoleted* t))
1211 (make-instances-obsolete 'superclass-born-to-be-obsoleted)
1212 (slot-boundp *born-to-be-obsoleted* 'a)
1213 (assert *born-to-be-obsoleted-obsoleted*)
1215 ;;; additional test suggested by Bruno Haible sbcl-devel 2004-04-21
1216 (defclass super-super-obsoleted () (a))
1217 (defclass super-obsoleted-1 (super-super-obsoleted) ())
1218 (defclass super-obsoleted-2 (super-super-obsoleted) ())
1219 (defclass obsoleted (super-obsoleted-1 super-obsoleted-2) ())
1220 (defparameter *obsoleted* (make-instance 'obsoleted))
1221 (defparameter *obsoleted-counter* 0)
1222 (defmethod update-instance-for-redefined-class ((o obsoleted) a d pl &key)
1223 (incf *obsoleted-counter*))
1224 (make-instances-obsolete 'super-super-obsoleted)
1225 (slot-boundp *obsoleted* 'a)
1226 (assert (= *obsoleted-counter* 1))
1228 ;;; yet another MAKE-INSTANCES-OBSOLETE test, this time from Nikodemus
1229 ;;; Siivola. Not all methods for accessing slots are created equal...
1230 (defclass yet-another-obsoletion-super () ((obs :accessor obs-of :initform 0)))
1231 (defclass yet-another-obsoletion-sub (yet-another-obsoletion-super) ())
1232 (defmethod shared-initialize :after ((i yet-another-obsoletion-super)
1233 slots &rest init)
1234 (declare (ignore init))
1235 (incf (obs-of i)))
1237 (defvar *yao-super* (make-instance 'yet-another-obsoletion-super))
1238 (defvar *yao-sub* (make-instance 'yet-another-obsoletion-sub))
1240 (assert (= (obs-of *yao-super*) 1))
1241 (assert (= (obs-of *yao-sub*) 1))
1242 (make-instances-obsolete 'yet-another-obsoletion-super)
1243 (assert (= (obs-of *yao-sub*) 2))
1244 (assert (= (obs-of *yao-super*) 2))
1245 (make-instances-obsolete 'yet-another-obsoletion-super)
1246 (assert (= (obs-of *yao-super*) 3))
1247 (assert (= (obs-of *yao-sub*) 3))
1248 (assert (= (slot-value *yao-super* 'obs) 3))
1249 (assert (= (slot-value *yao-sub* 'obs) 3))
1251 ;;; one more MIO test: variable slot names
1252 (defclass mio () ((x :initform 42)))
1253 (defvar *mio-slot* 'x)
1254 (defparameter *mio-counter* 0)
1255 (defmethod update-instance-for-redefined-class ((instance mio) new old plist &key)
1256 (incf *mio-counter*))
1258 (let ((x (make-instance 'mio)))
1259 (make-instances-obsolete 'mio)
1260 (slot-value x *mio-slot*))
1262 (let ((x (make-instance 'mio)))
1263 (make-instances-obsolete 'mio)
1264 (setf (slot-value x *mio-slot*) 13))
1266 (let ((x (make-instance 'mio)))
1267 (make-instances-obsolete 'mio)
1268 (slot-boundp x *mio-slot*))
1270 (let ((x (make-instance 'mio)))
1271 (make-instances-obsolete 'mio)
1272 (slot-makunbound x *mio-slot*))
1274 (assert (= 4 *mio-counter*))
1276 ;;; :class -> :instance slot allocation transfers of inherited slots,
1277 ;;; reported by Bruno Haible
1278 (with-test (:name (defclass :redefinition-class->instance-allocation))
1279 (let (i)
1280 (defclass super-with-magic-slot ()
1281 ((magic :initarg :size :initform 1 :allocation :class)))
1282 (defclass sub-of-super-with-magic-slot (super-with-magic-slot) ())
1283 (setq i (make-instance 'sub-of-super-with-magic-slot))
1284 (defclass super-with-magic-slot ()
1285 ((magic :initarg :size :initform 2)))
1286 (assert (= 1 (slot-value i 'magic)))))
1288 ;;; MAKE-INSTANCES-OBSOLETE return values
1289 (with-test (:name (make-instances-obsolete :return-values) )
1290 (defclass one-more-to-obsolete () ())
1291 (assert (eq 'one-more-to-obsolete
1292 (make-instances-obsolete 'one-more-to-obsolete)))
1293 (assert (eq (find-class 'one-more-to-obsolete)
1294 (make-instances-obsolete (find-class 'one-more-to-obsolete)))))
1296 ;;; Sensible error instead of a BUG. Reported by Thomas Burdick.
1297 (with-test (:name (defclass :slot-with-duplicate-accessors))
1298 (assert-error (defclass slot-with-duplicate-accessors ()
1299 ((slot :writer get-slot :reader get-slot)))
1300 (and error (not sb-int:bug))))
1302 ;;; BUG 321: errors in parsing DEFINE-METHOD-COMBINATION arguments
1303 ;;; lambda lists.
1305 (define-method-combination w-args ()
1306 ((method-list *))
1307 (:arguments arg1 arg2 &aux (extra :extra))
1308 `(progn ,arg1 ,arg2 ,extra
1309 ,@(mapcar (lambda (method) `(call-method ,method)) method-list)))
1310 (defgeneric mc-test-w-args (p1 p2 s)
1311 (:method-combination w-args)
1312 (:method ((p1 number) (p2 t) s)
1313 (vector-push-extend (list 'number p1 p2) s))
1314 (:method ((p1 string) (p2 t) s)
1315 (vector-push-extend (list 'string p1 p2) s))
1316 (:method ((p1 t) (p2 t) s) (vector-push-extend (list t p1 p2) s)))
1317 (let ((v (make-array 0 :adjustable t :fill-pointer t)))
1318 (assert (= (mc-test-w-args 1 2 v) 1))
1319 (assert (equal (aref v 0) '(number 1 2)))
1320 (assert (equal (aref v 1) '(t 1 2))))
1322 ;;; BUG 276: declarations and mutation.
1323 (defmethod fee ((x fixnum))
1324 (setq x (/ x 2))
1326 (assert (= (fee 1) 1/2))
1327 (defmethod fum ((x fixnum))
1328 (setf x (/ x 2))
1330 (assert (= (fum 3) 3/2))
1331 (defmethod fii ((x fixnum))
1332 (declare (special x))
1333 (setf x (/ x 2))
1335 (assert (= (fii 1) 1/2))
1336 (defvar *faa*)
1337 (defmethod faa ((*faa* string-stream))
1338 (setq *faa* (make-broadcast-stream *faa*))
1339 (write-line "Break, you sucker!" *faa*)
1340 'ok)
1341 (assert (eq 'ok (faa (make-string-output-stream))))
1342 (defmethod fex ((x fixnum) (y fixnum))
1343 (multiple-value-setq (x y) (values (/ x y) (/ y x)))
1344 (list x y))
1345 (assert (equal (fex 5 3) '(5/3 3/5)))
1347 ;;; Bug reported by Zach Beane; incorrect return of (function
1348 ;;; ',fun-name) in defgeneric
1349 (with-test (:name (defgeneric :return type))
1350 (flet ((test (form)
1351 (let ((fun (checked-compile form)))
1352 (assert (typep (funcall fun) 'generic-function)))))
1353 (test '(lambda () (flet ((nonsense () nil))
1354 (declare (ignorable #'nonsense))
1355 (defgeneric nonsense ()))))
1356 (test '(lambda () (flet ((nonsense-2 () nil))
1357 (declare (ignorable #'nonsense-2))
1358 (defgeneric nonsense-2 ()
1359 (:method () t)))))))
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 (checked-compile `(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) sb-pcl::no-applicable-method-error) ; 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 ((sb-pcl::no-applicable-method-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 ((sb-pcl::no-primary-method-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 (checked-compile-and-assert ()
2388 '(lambda () (make-instance 'bug-1179858 :foo t))
2389 (() nil :test (constantly t))))
2391 ;;; Other brokenness, found while investigating: fallback-generator
2392 ;;; handling of non-keyword initialization arguments
2393 (defclass bug-1179858b ()
2394 ((foo :initarg foo :reader bug-1179858b-foo))
2395 (:default-initargs foo 14))
2396 (defmethod shared-initialize :around ((instance bug-1179858b) (slot-names t) &key)
2397 (call-next-method))
2399 (with-test (:name (make-instance :fallback-generator-non-keyword-initarg :bug-1179858))
2400 (flet ((foo= (n i) (= (bug-1179858b-foo i) n)))
2401 (assert
2402 (foo= 14 (funcall (checked-compile '(lambda () (make-instance 'bug-1179858b))))))
2403 (assert
2404 (foo= 15 (funcall (checked-compile '(lambda () (make-instance 'bug-1179858b 'foo 15))))))))
2406 (with-test (:name (:cpl-violation-setup :bug-309076))
2407 (assert-error
2408 (progn
2409 (defclass bug-309076-broken-class (standard-class) ()
2410 (:metaclass sb-mop:funcallable-standard-class))
2411 (sb-mop:finalize-inheritance (find-class 'bug-309076-broken-class)))))
2413 (defclass bug-309076-class (standard-class) ())
2414 (with-test (:name (:cpl-violation-irrelevant-class :bug-309076))
2415 (defmethod sb-mop:validate-superclass ((x bug-309076-class) (y standard-class)) t)
2416 (assert (typep (make-instance 'bug-309076-class) 'bug-309076-class)))
2418 (eval-when (:compile-toplevel :load-toplevel :execute)
2419 (require 'sb-cltl2)
2420 (defmethod b ()))
2422 (defmacro macro ()
2423 (let ((a 20))
2424 (declare (special a))
2425 (checked-compile-and-assert ()
2426 (sb-mop:make-method-lambda
2428 (find-method #'b () ())
2429 '(lambda () (declare (special a)) a)
2430 nil)
2431 (('(1) ()) 20))))
2433 (with-test (:name :make-method-lambda-leakage)
2434 ;; lambda list of X leaks into the invocation of make-method-lambda
2435 ;; during code-walking performed by make-method-lambda invoked by
2436 ;; DEFMETHOD
2437 (sb-cltl2:macroexpand-all '(defmethod x (a) (macro))))
2439 (with-test (:name (defmethod :undefined-function :bug-503095))
2440 (flet ((test-load (file)
2441 (handler-bind
2442 (((or warning error) #'error))
2443 (load file))))
2444 (multiple-value-bind (fasl warnings errorsp)
2445 (compile-file "bug-503095.lisp" :print nil :verbose nil)
2446 (unwind-protect
2447 (progn (assert (and fasl (not warnings) (not errorsp)))
2448 (test-load fasl))
2449 (and fasl (delete-file fasl))))
2450 (test-load "bug-503095-2.lisp")))
2452 (defclass a-633911 ()
2453 ((x-633911 :initform nil
2454 :accessor x-633911)))
2455 (defclass b-633911 ()
2456 ((x-633911 :initform nil
2457 :accessor x-633911)))
2458 (with-test (:name :accessor-and-plain-method)
2459 (defmethod x-633911 ((b a-633911)) 10)
2460 (assert (= (x-633911 (make-instance 'a-633911)) 10)))
2462 (with-test (:name (built-in-class :subclass error))
2463 (flet ((mapper (c)
2464 (when (and (class-name c) (typep c 'built-in-class))
2465 (assert-error (eval `(defclass ,(gensym) (,(class-name c))
2466 ()))))))
2467 (sb-pcl::map-all-classes #'mapper)))
2469 (defclass slot-value-using-class-a () ())
2470 (defclass slot-value-using-class-b () (x))
2472 (with-test (:name :svuc-with-bad-slotd)
2473 (let* ((a (make-instance 'slot-value-using-class-a))
2474 (b (make-instance 'slot-value-using-class-b))
2475 (slotd (car (sb-mop:class-slots (class-of b)))))
2476 (assert-error (sb-mop:slot-value-using-class (class-of a) a slotd))
2477 (assert-error (setf (sb-mop:slot-value-using-class (class-of a) a slotd) t))))
2479 ;; A test that the *FGENS* cache works.
2480 ;; This is not a test in the CLOS problem domain, but in the implementation
2481 ;; of discriminating functions.
2482 (defgeneric f (x y) (:method (x y) (+ x y)))
2483 (defgeneric g (x y) (:method (x y) (* x y)))
2484 (with-test (:name :fgens-sharing)
2485 (let ((count0 (hash-table-count sb-pcl::*fgens*))
2486 (foo (f 1 2))
2487 (count1 (hash-table-count sb-pcl::*fgens*))
2488 (bar (g 1 2))
2489 (count2 (hash-table-count sb-pcl::*fgens*)))
2490 (declare (ignore foo bar))
2491 (assert (= count0 count1 count2))))
2494 ;;; Classes shouldn't be their own direct or indirect superclasses or
2495 ;;; metaclasses.
2497 (with-test (:name (sb-mop:ensure-class :class-is-direct-superclass
2498 :bug-1418883))
2499 (assert-error
2500 (defclass class-with-self-as-superclass (class-with-self-as-superclass) ())))
2502 (with-test (:name (sb-mop:ensure-class :superclass-cycle :bug-1418883))
2503 ;; These have a superclass cycle from the beginning.
2504 (defclass class-with-superclass-cycle1 (class-with-superclass-cycle2) ())
2505 (assert-error
2506 (defclass class-with-superclass-cycle2 (class-with-superclass-cycle1) ())))
2508 (with-test (:name (sb-mop:ensure-class :self-metaclass))
2509 ;; These have a metaclass cycle from the beginning.
2510 (assert-error
2511 (defclass class-with-self-as-metaclass () ()
2512 (:metaclass class-with-self-as-metaclass))))
2514 (with-test (:name (sb-pcl::update-class :class-becomes-direct-superclass
2515 :bug-1418883))
2516 (defclass class-with-eventual-self-as-superclass () ())
2517 ;; Update class to introduce superclass.
2518 (assert-error
2519 (defclass class-with-eventual-self-as-superclass
2520 (class-with-eventual-self-as-superclass) ())))
2522 (with-test (:name (sb-pcl::update-class :superclasses-become-cyclic
2523 :bug-1418883))
2524 ;; Nothing wrong with these.
2525 (defclass class-with-eventual-superclass-cycle1 () ())
2526 (defclass class-with-eventual-superclass-cycle2
2527 (class-with-eventual-superclass-cycle1) ())
2528 ;; Update first class to introduce the superclass cycle.
2529 (assert-error
2530 (defclass class-with-eventual-superclass-cycle1
2531 (class-with-eventual-superclass-cycle2) ())))
2533 (with-test (:name (sb-pcl::update-class :becomes-self-metaclass))
2534 (defclass class-with-eventual-self-as-metaclass () ())
2535 ;; Try to update metaclass to self.
2536 (assert-error
2537 (defclass class-with-eventual-self-as-metaclass () ()
2538 (:metaclass class-with-eventual-self-as-metaclass))))
2540 (with-test (:name :function-keywords)
2541 (defgeneric function-keywords-test (&key))
2542 (assert (equal (function-keywords
2543 (defmethod function-keywords-test (&key a b)
2544 (declare (ignore a b))))
2545 '(:a :b))))
2547 (with-test (:name :superclass-finalization)
2548 (let* ((class1 (gensym "CLASS1-"))
2549 (class2 (gensym "CLASS2-")))
2550 (eval `(defclass ,class1 () ()))
2551 (eval `(defclass ,class2 (,class1) ()))
2552 (let ((instance (make-instance class2)))
2553 (sb-mop:finalize-inheritance (find-class class1))
2554 (assert (not (sb-kernel:layout-invalid (sb-kernel:layout-of instance)))))))
2556 (with-test (:name (allocate-instance :on symbol))
2557 (let ((class (gensym "CLASS-")))
2558 (eval `(defclass ,class () ()))
2559 (checked-compile-and-assert ()
2560 `(lambda () (allocate-instance ',class))
2561 (() (condition sb-pcl::no-applicable-method-error)))))
2563 (defclass unbound-slot-after-allocation=class ()
2564 ((abc :allocation :class)
2565 (d :accessor unbound-slot-after-allocation=class)))
2567 (with-test (:name :unbound-slot-after-allocation=class)
2568 (assert-error (unbound-slot-after-allocation=class
2569 (make-instance 'unbound-slot-after-allocation=class))
2570 unbound-slot))
2572 (with-test (:name :layouf-of-nil)
2573 (assert (eq (sb-kernel:layout-of nil) (sb-kernel:find-layout 'null))))
2575 (with-test (:name (defmethod :on-classless-type))
2576 (handler-bind ((timeout (lambda (condition)
2577 (declare (ignore condition))
2578 (error "Timeout"))))
2579 (sb-ext:with-timeout 0.1
2580 (checked-compile-and-assert (:allow-warnings t)
2581 `(lambda ()
2582 (defmethod foo ((bar keyword))))
2583 (() (condition sb-pcl:class-not-found-error))))))