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