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