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