1.0.35.20: More robust checking for DEFMETHOD argument specializers
[sbcl.git] / tests / clos.impure.lisp
1 ;;;; miscellaneous side-effectful tests of CLOS
2
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.
13
14 (defpackage "CLOS-IMPURE"
15   (:use "CL" "ASSERTOID" "TEST-UTIL"))
16 (in-package "CLOS-IMPURE")
17 \f
18 ;;; It should be possible to do DEFGENERIC and DEFMETHOD referring to
19 ;;; structure types defined earlier in the file.
20 (defstruct struct-a x y)
21 (defstruct struct-b x y z)
22 (defmethod wiggle ((a struct-a))
23   (+ (struct-a-x a)
24      (struct-a-y a)))
25 (defgeneric jiggle (arg))
26 (defmethod jiggle ((a struct-a))
27   (- (struct-a-x a)
28      (struct-a-y a)))
29 (defmethod jiggle ((b struct-b))
30   (- (struct-b-x b)
31      (struct-b-y b)
32      (struct-b-z b)))
33 (assert (= (wiggle (make-struct-a :x 6 :y 5))
34            (jiggle (make-struct-b :x 19 :y 6 :z 2))))
35
36 ;;; Compiling DEFGENERIC should prevent "undefined function" style
37 ;;; warnings from code within the same file.
38 (defgeneric gf-defined-in-this-file (x y))
39 (defun function-using-gf-defined-in-this-file (x y n)
40   (unless (minusp n)
41     (gf-defined-in-this-file x y)))
42
43 ;;; Until Martin Atzmueller ported Pierre Mai's CMU CL fixes in
44 ;;; sbcl-0.6.12.25, the implementation of NO-APPLICABLE-METHOD was
45 ;;; broken in such a way that the code here would signal an error.
46 (defgeneric zut-n-a-m (a b c))
47 (defmethod no-applicable-method ((zut-n-a-m (eql #'zut-n-a-m)) &rest args)
48   (format t "~&No applicable method for ZUT-N-A-M ~S, yet.~%" args))
49 (zut-n-a-m 1 2 3)
50
51 ;;; bug reported and fixed by Alexey Dejneka sbcl-devel 2001-09-10:
52 ;;; This DEFGENERIC shouldn't cause an error.
53 (defgeneric ad-gf (a) (:method :around (x) x))
54
55 ;;; DEFGENERIC and DEFMETHOD shouldn't accept &REST when it's not
56 ;;; followed by a variable:
57 ;;; e.g. (DEFMETHOD FOO ((X T) &REST) NIL) should signal an error.
58 (eval-when (:load-toplevel :compile-toplevel :execute)
59   (defmacro expect-error (&body body)
60     `(multiple-value-bind (res condition)
61       (ignore-errors (progn ,@body))
62       (declare (ignore res))
63       (typep condition 'error))))
64 (assert (expect-error (defmethod foo0 ((x t) &rest) nil)))
65 (assert (expect-error (defgeneric foo1 (x &rest))))
66 (assert (expect-error (defgeneric foo2 (x a &rest))))
67 (defgeneric foo3 (x &rest y))
68 (defmethod foo3 ((x t) &rest y) nil)
69 (defmethod foo4 ((x t) &rest z &key y) nil)
70 (defgeneric foo4 (x &rest z &key y))
71 (assert (expect-error (defgeneric foo5 (x &rest))))
72 (assert (expect-error (defmethod foo6 (x &rest))))
73
74 ;;; legal method specializers
75 (defclass bug-525916-1 () ())
76 (defclass bug-525916-2 () ())
77 (with-test (:name :bug-525916)
78 (assert (expect-error (defmethod invalid ((arg)) arg)))
79 (assert (expect-error (defmethod invalid (nil) 1)))
80 (assert (expect-error (defmethod invalid ((arg . bug-525916-1)) arg)))
81 (assert (expect-error (defmethod invalid ((arg bug-525916-1 bug-525916-2)) arg))))
82
83 ;;; more lambda-list checking
84 ;;;
85 ;;; DEFGENERIC lambda lists are subject to various limitations, as per
86 ;;; section 3.4.2 of the ANSI spec. Since Alexey Dejneka's patch for
87 ;;; bug 191-b ca. sbcl-0.7.22, these limitations should be enforced.
88 (labels ((coerce-to-boolean (x)
89            (if x t nil))
90          (%like-or-dislike (expr expected-failure-p)
91            (declare (type boolean expected-failure-p))
92            (format t "~&trying ~S~%" expr)
93            (multiple-value-bind (fun warnings-p failure-p)
94              (compile nil
95                       `(lambda ()
96                          ,expr))
97              (declare (ignore fun))
98              ;; In principle the constraint on WARNINGS-P below seems
99              ;; reasonable, but in practice we get warnings about
100              ;; undefined functions from the DEFGENERICs, apparently
101              ;; because the DECLAIMs which ordinarily prevent such
102              ;; warnings don't take effect because EVAL-WHEN
103              ;; (:COMPILE-TOPLEVEL) loses its magic when compiled
104              ;; within a LAMBDA. So maybe we can't test WARNINGS-P
105              ;; after all?
106              ;;(unless expected-failure-p
107              ;;  (assert (not warnings-p)))
108              (assert (eq (coerce-to-boolean failure-p) expected-failure-p))))
109          (like (expr)
110            (%like-or-dislike expr nil))
111          (dislike (expr)
112            (%like-or-dislike expr t)))
113   ;; basic sanity
114   (dislike '(defgeneric gf-for-ll-test-0 ("a" #p"b")))
115   (like    '(defgeneric gf-for-ll-test-1 ()))
116   (like    '(defgeneric gf-for-ll-test-2 (x)))
117   ;; forbidden default or supplied-p for &OPTIONAL or &KEY arguments
118   (dislike '(defgeneric gf-for-ll-test-3 (x &optional (y 0))))
119   (like    '(defgeneric gf-for-ll-test-4 (x &optional y)))
120   (dislike '(defgeneric gf-for-ll-test-5 (x y &key (z :z z-p))))
121   (like    '(defgeneric gf-for-ll-test-6 (x y &key z)))
122   (dislike '(defgeneric gf-for-ll-test-7 (x &optional (y 0) &key z)))
123   (like    '(defgeneric gf-for-ll-test-8 (x &optional y &key z)))
124   (dislike '(defgeneric gf-for-ll-test-9 (x &optional y &key (z :z))))
125   (like    '(defgeneric gf-for-ll-test-10 (x &optional y &key z)))
126   (dislike '(defgeneric gf-for-ll-test-11 (&optional &key (k :k k-p))))
127   (like    '(defgeneric gf-for-ll-test-12 (&optional &key k)))
128   ;; forbidden &AUX
129   (dislike '(defgeneric gf-for-ll-test-13 (x y z &optional a &aux g h)))
130   (like    '(defgeneric gf-for-ll-test-14 (x y z &optional a)))
131   (dislike '(defgeneric gf-for-ll-test-bare-aux-1 (x &aux)))
132   (like    '(defgeneric gf-for-ll-test-bare-aux-2 (x)))
133   ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations
134   ;; on required arguments
135   (dislike '(defgeneric gf-for-11-test-15 ((arg t))))
136   (like '(defgeneric gf-for-11-test-16 (arg))))
137
138 ;;; structure-class tests setup
139 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
140 (defclass structure-class-foo2 (structure-class-foo1)
141   () (:metaclass cl:structure-class))
142
143 ;;; standard-class tests setup
144 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
145 (defclass standard-class-foo2 (standard-class-foo1)
146   () (:metaclass cl:standard-class))
147
148 (assert (typep (class-of (make-instance 'structure-class-foo1))
149                'structure-class))
150 (assert (typep (make-instance 'structure-class-foo1) 'structure-class-foo1))
151 (assert (typep (make-instance 'standard-class-foo1) 'standard-class-foo1))
152
153 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
154 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
155 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
156 ;;; DEFGENERIC-containing files does the right thing instead of
157 ;;; randomly slicing your generic functions. (APD made this work
158 ;;; in sbcl-0.7.0.2.)
159 (defgeneric born-to-be-redefined (x)
160   (:method ((x integer))
161     'integer))
162 (defmethod born-to-be-redefined ((x real))
163   'real)
164 (assert (eq (born-to-be-redefined 1) 'integer))
165 (defgeneric born-to-be-redefined (x))
166 (assert (eq (born-to-be-redefined 1) 'real)) ; failed until sbcl-0.7.0.2
167 (defgeneric born-to-be-redefined (x)
168   (:method ((x integer))
169     'integer))
170 (defmethod born-to-be-redefined ((x integer))
171   'int)
172 (assert (eq (born-to-be-redefined 1) 'int))
173 (defgeneric born-to-be-redefined (x))
174 (assert (eq (born-to-be-redefined 1) 'int))
175 \f
176 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
177 ;;; preventing forward-references and also change-class (which
178 ;;; forward-references used interally) from working properly.  One
179 ;;; symptom was reported by Brian Spilsbury (sbcl-devel 2002-04-08),
180 ;;; and another on IRC by Dan Barlow simultaneously.  Better check
181 ;;; that it doesn't happen again.
182 ;;;
183 ;;; First, the forward references:
184 (defclass forward-ref-a (forward-ref-b) ())
185 (defclass forward-ref-b () ())
186 ;;; (a couple more complicated examples found by Paul Dietz' test
187 ;;; suite):
188 (defclass forward-ref-c1 (forward-ref-c2) ())
189 (defclass forward-ref-c2 (forward-ref-c3) ())
190
191 (defclass forward-ref-d1 (forward-ref-d2 forward-ref-d3) ())
192 (defclass forward-ref-d2 (forward-ref-d4 forward-ref-d5) ())
193
194 ;;; Then change-class
195 (defclass class-with-slots ()
196   ((a-slot :initarg :a-slot :accessor a-slot)
197    (b-slot :initarg :b-slot :accessor b-slot)
198    (c-slot :initarg :c-slot :accessor c-slot)))
199
200 (let ((foo (make-instance 'class-with-slots
201                           :a-slot 1
202                           :b-slot 2
203                           :c-slot 3)))
204   (let ((bar (change-class foo 'class-with-slots)))
205     (assert (= (a-slot bar) 1))
206     (assert (= (b-slot bar) 2))
207     (assert (= (c-slot bar) 3))))
208
209 ;;; some more CHANGE-CLASS testing, now that we have an ANSI-compliant
210 ;;; version (thanks to Espen Johnsen)
211 (defclass from-class ()
212   ((foo :initarg :foo :accessor foo)))
213 (defclass to-class ()
214   ((foo :initarg :foo :accessor foo)
215    (bar :initarg :bar :accessor bar)))
216 (let* ((from (make-instance 'from-class :foo 1))
217        (to (change-class from 'to-class :bar 2)))
218   (assert (= (foo to) 1))
219   (assert (= (bar to) 2)))
220
221 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
222 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
223 (defgeneric bug180 (x)
224   (:method-combination list :most-specific-last))
225 (defmethod bug180 list ((x number))
226   'number)
227 (defmethod bug180 list ((x fixnum))
228   'fixnum)
229 (assert (equal (bug180 14) '(number fixnum)))
230 \f
231 ;;; printing a structure class should not loop indefinitely (or cause
232 ;;; a stack overflow):
233 (defclass test-printing-structure-class ()
234   ((slot :initarg :slot))
235   (:metaclass structure-class))
236 (print (make-instance 'test-printing-structure-class :slot 2))
237
238 ;;; structure-classes should behave nicely when subclassed
239 (defclass super-structure ()
240   ((a :initarg :a :accessor a-accessor)
241    (b :initform 2 :reader b-reader))
242   (:metaclass structure-class))
243 (defclass sub-structure (super-structure)
244   ((c :initarg :c :writer c-writer :accessor c-accessor))
245   (:metaclass structure-class))
246 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
247   (assert (= (a-accessor foo) 1))
248   (assert (= (b-reader foo) 2))
249   (assert (= (c-accessor foo) 3))
250   (setf (a-accessor foo) 4)
251   (c-writer 5 foo)
252   (assert (= (a-accessor foo) 4))
253   (assert (= (c-accessor foo) 5)))
254 \f
255 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
256 ;;; encoding of effective method functions for slot accessors as
257 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
258 ;;; functions to get broken in special ways even though ordinary
259 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
260 ;;; for that possibility. Now we have a few tests:
261 (defclass fish ()
262   ((fin :reader ffin :writer ffin!)
263    (tail :reader ftail :writer ftail!)))
264 (defvar *fish* (make-instance 'fish))
265 (ffin! 'triangular-fin *fish*)
266 (defclass cod (fish) ())
267 (defvar *cod* (make-instance 'cod))
268 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
269 (defmethod ffin! (new-fin (cod cod))
270   (format t "~&about to set ~S fin to ~S~%" cod new-fin)
271   (vector-push-extend '(cod) *clos-dispatch-side-fx*)
272   (prog1
273       (call-next-method)
274     (format t "~&done setting ~S fin to ~S~%" cod new-fin)))
275 (defmethod ffin! :before (new-fin (cod cod))
276   (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
277   (format t "~&exploring the CLOS dispatch zoo with COD fins~%"))
278 (ffin! 'almost-triang-fin *cod*)
279 (assert (eq (ffin *cod*) 'almost-triang-fin))
280 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
281 \f
282 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
283 ;;; ignored its options; Gerd Moellmann found and fixed the problem
284 ;;; for cmucl (cmucl-imp 2002-06-18).
285 (define-method-combination test-mc (x)
286   ;; X above being a method-group-specifier
287   ((primary () :required t))
288   `(call-method ,(first primary)))
289
290 (defgeneric gf (obj)
291   (:method-combination test-mc 1))
292
293 (defmethod gf (obj)
294   obj)
295 \f
296 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
297 ;;; some others were of the wrong type:
298 (macrolet ((assert-program-error (form)
299              `(multiple-value-bind (value error)
300                   (ignore-errors ,form)
301                 (unless (and (null value) (typep error 'program-error))
302                   (error "~S failed: ~S, ~S" ',form value error)))))
303   (assert-program-error (defclass foo001 () (a b a)))
304   (assert-program-error (defclass foo002 ()
305                           (a b)
306                           (:default-initargs x 'a x 'b)))
307   (assert-program-error (defclass foo003 ()
308                           ((a :allocation :class :allocation :class))))
309   (assert-program-error (defclass foo004 ()
310                           ((a :silly t))))
311   ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
312   ;; Moellmann in sbcl-0.7.8.x:
313   (assert-program-error (progn
314                           (defmethod odd-key-args-checking (&key (key 42)) key)
315                           (odd-key-args-checking 3)))
316   (assert (= (odd-key-args-checking) 42))
317   (assert (eq (odd-key-args-checking :key t) t))
318   ;; yet some more, fixed in sbcl-0.7.9.xx
319   (assert-program-error (defclass foo005 ()
320                           (:metaclass sb-pcl::funcallable-standard-class)
321                           (:metaclass 1)))
322   (assert-program-error (defclass foo006 ()
323                           ((a :reader (setf a)))))
324   (assert-program-error (defclass foo007 ()
325                           ((a :initarg 1))))
326   (assert-program-error (defclass foo008 ()
327                           (a :initarg :a)
328                           (:default-initargs :a 1)
329                           (:default-initargs :a 2)))
330   ;; and also BUG 47d, fixed in sbcl-0.8alpha.0.26
331   (assert-program-error (defgeneric if (x)))
332   ;; DEFCLASS should detect an error if slot names aren't suitable as
333   ;; variable names:
334   (assert-program-error (defclass foo009 ()
335                           ((:a :initarg :a))))
336   (assert-program-error (defclass foo010 ()
337                           (("a" :initarg :a))))
338   (assert-program-error (defclass foo011 ()
339                           ((#1a() :initarg :a))))
340   (assert-program-error (defclass foo012 ()
341                           ((t :initarg :t))))
342   (assert-program-error (defclass foo013 () ("a")))
343   ;; specialized lambda lists have certain restrictions on ordering,
344   ;; repeating keywords, and the like:
345   (assert-program-error (defmethod foo014 ((foo t) &rest) nil))
346   (assert-program-error (defmethod foo015 ((foo t) &rest x y) nil))
347   (assert-program-error (defmethod foo016 ((foo t) &allow-other-keys) nil))
348   (assert-program-error (defmethod foo017 ((foo t)
349                                            &optional x &optional y) nil))
350   (assert-program-error (defmethod foo018 ((foo t) &rest x &rest y) nil))
351   (assert-program-error (defmethod foo019 ((foo t) &rest x &optional y) nil))
352   (assert-program-error (defmethod foo020 ((foo t) &key x &optional y) nil))
353   (assert-program-error (defmethod foo021 ((foo t) &key x &rest y) nil)))
354 \f
355 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
356 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
357 ;;; (thanks to Gerd Moellmann)
358 (let ((answer (documentation '+ 'function)))
359   (assert (stringp answer))
360   (defmethod documentation ((x (eql '+)) y) "WRONG")
361   (assert (string= (documentation '+ 'function) answer)))
362 \f
363 ;;; only certain declarations are permitted in DEFGENERIC
364 (macrolet ((assert-program-error (form)
365              `(multiple-value-bind (value error)
366                   (ignore-errors ,form)
367                 (assert (null value))
368                 (assert (typep error 'program-error)))))
369   (assert-program-error (defgeneric bogus-declaration (x)
370                           (declare (special y))))
371   (assert-program-error (defgeneric bogus-declaration2 (x)
372                           (declare (notinline concatenate)))))
373 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
374 ;;; method.
375 (defmethod no-next-method-test ((x integer)) (call-next-method))
376 (assert (null (ignore-errors (no-next-method-test 1))))
377 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
378   'success)
379 (assert (eq (no-next-method-test 1) 'success))
380 (assert (null (ignore-errors (no-next-method-test 'foo))))
381 \f
382 ;;; regression test for bug 176, following a fix that seems
383 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
384 ;;; Moellmann, merged in sbcl-0.7.9.12).
385 (dotimes (i 10)
386   (let ((lastname (intern (format nil "C176-~D" (1- i))))
387         (name (intern (format nil "C176-~D" i))))
388   (eval `(defclass ,name
389              (,@(if (= i 0) nil (list lastname)))
390            ()))
391   (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
392            (declare (ignore any))))))
393 (defclass b176 () (aslot-176))
394 (defclass c176-0 (b176) ())
395 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
396 \f
397 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
398 ;;; primary methods:
399 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
400   ((around (:around))
401    (primary (dmc-test-mc) :order order :required t))
402    (let ((form (if (rest primary)
403                    `(and ,@(mapcar #'(lambda (method)
404                                        `(call-method ,method))
405                                    primary))
406                    `(call-method ,(first primary)))))
407      (if around
408          `(call-method ,(first around)
409                        (,@(rest around)
410                         (make-method ,form)))
411          form)))
412
413 (defgeneric dmc-test-mc (&key k)
414   (:method-combination dmc-test-mc))
415
416 (defmethod dmc-test-mc dmc-test-mc (&key k)
417            k)
418
419 (dmc-test-mc :k 1)
420 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
421 ;;; the NAME argument, not some random method object. So:
422 (assert (eq (define-method-combination dmc-test-return-foo)
423             'dmc-test-return-foo))
424 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
425             'dmc-test-return-bar))
426 (assert (eq (define-method-combination dmc-test-return
427                 (&optional (order :most-specific-first))
428               ((around (:around))
429                (primary (dmc-test-return) :order order :required t))
430               (let ((form (if (rest primary)
431                               `(and ,@(mapcar #'(lambda (method)
432                                                   `(call-method ,method))
433                                               primary))
434                               `(call-method ,(first primary)))))
435                 (if around
436                     `(call-method ,(first around)
437                       (,@(rest around)
438                        (make-method ,form)))
439                     form)))
440             'dmc-test-return))
441 \f
442 ;;; DEFINE-METHOD-COMBINATION should, according to the description in 7.7,
443 ;;; allow you to do everything in the body forms yourself if you specify
444 ;;; exactly one method group whose qualifier-pattern is *
445 ;;;
446 ;;; The specific language is:
447 ;;; "The use of method group specifiers provides a convenient syntax to select
448 ;;; methods, to divide them among the possible roles, and to perform the
449 ;;; necessary error checking. It is possible to perform further filtering of
450 ;;; methods in the body forms by using normal list-processing operations and
451 ;;; the functions method-qualifiers and invalid-method-error. It is permissible
452 ;;; to use setq on the variables named in the method group specifiers and to
453 ;;; bind additional variables. It is also possible to bypass the method group
454 ;;; specifier mechanism and do everything in the body forms. This is
455 ;;; accomplished by writing a single method group with * as its only
456 ;;; qualifier-pattern; the variable is then bound to a list of all of the
457 ;;; applicable methods, in most-specific-first order."
458 (define-method-combination wam-test-method-combination-a ()
459   ((all-methods *))
460   (do ((methods all-methods (rest methods))
461        (primary nil)
462        (around nil))
463       ((null methods)
464        (let ((primary (nreverse primary))
465              (around (nreverse around)))
466          (if primary
467               (let ((form (if (rest primary)
468                              `(call-method ,(first primary) ,(rest primary))
469                              `(call-method ,(first primary)))))
470                 (if around
471                     `(call-method ,(first around) (,@(rest around)
472                                                    (make-method ,form)))
473                     form))
474               `(make-method (error "No primary methods")))))
475     (let* ((method (first methods))
476            (qualifier (first (method-qualifiers method))))
477       (cond
478         ((equal :around qualifier)
479          (push method around))
480         ((null qualifier)
481          (push method primary))))))
482
483 (defgeneric wam-test-mc-a (val)
484   (:method-combination wam-test-method-combination-a))
485 (assert (raises-error? (wam-test-mc-a 13)))
486 (defmethod wam-test-mc-a ((val number))
487   (+ val (if (next-method-p) (call-next-method) 0)))
488 (assert (= (wam-test-mc-a 13) 13))
489 (defmethod wam-test-mc-a :around ((val number))
490   (+ val (if (next-method-p) (call-next-method) 0)))
491 (assert (= (wam-test-mc-a 13) 26))
492
493 ;;; DEFINE-METHOD-COMBINATION
494 ;;; When two methods are in the same method group and have the same
495 ;;; specializers, their sort order within the group may be ambiguous. Therefore,
496 ;;; we should throw an error when we have two methods in the same group with
497 ;;; the same specializers /as long as/ we have more than one method group
498 ;;; or our single method group qualifier-pattern is not *. This resolves the
499 ;;; apparent conflict with the above 'It is also possible to bypass' language.
500 ;;;
501 ;;; The language specifying this behavior is:
502 ;;; "Note that two methods with identical specializers, but with different
503 ;;; qualifiers, are not ordered by the algorithm described in Step 2 of the
504 ;;; method selection and combination process described in Section 7.6.6
505 ;;; (Method Selection and Combination). Normally the two methods play different
506 ;;; roles in the effective method because they have different qualifiers, and
507 ;;; no matter how they are ordered in the result of Step 2, the effective
508 ;;; method is the same. If the two methods play the same role and their order
509 ;;; matters, an error is signaled. This happens as part of the qualifier
510 ;;; pattern matching in define-method-combination."
511 ;;;
512 ;;; Note that the spec pretty much equates 'method group' and 'role'.
513 ;; First we ensure that it fails correctly when there is more than one
514 ;; method group
515 (define-method-combination wam-test-method-combination-b ()
516   ((around (:around))
517    (primary * :required t))
518   (let ((form (if (rest primary)
519                   `(call-method ,(first primary) ,(rest primary))
520                   `(call-method ,(first primary)))))
521     (if around
522         `(call-method ,(first around) (,@(rest around)
523                                        (make-method ,form)))
524         form)))
525
526 (defgeneric wam-test-mc-b (val)
527   (:method-combination wam-test-method-combination-b))
528 (defmethod wam-test-mc-b ((val number))
529   (+ val (if (next-method-p) (call-next-method) 0)))
530 (assert (= (wam-test-mc-b 13) 13))
531 (defmethod wam-test-mc-b :around ((val number))
532   (+ val (if (next-method-p) (call-next-method) 0)))
533 (assert (= (wam-test-mc-b 13) 26))
534 (defmethod wam-test-mc-b :somethingelse ((val number))
535   (+ val (if (next-method-p) (call-next-method) 0)))
536 (assert (raises-error? (wam-test-mc-b 13)))
537
538 ;;; now, ensure that it fails with a single group with a qualifier-pattern
539 ;;; that is not *
540 (define-method-combination wam-test-method-combination-c ()
541   ((methods listp :required t))
542   (if (rest methods)
543       `(call-method ,(first methods) ,(rest methods))
544       `(call-method ,(first methods))))
545
546 (defgeneric wam-test-mc-c (val)
547   (:method-combination wam-test-method-combination-c))
548 (assert (raises-error? (wam-test-mc-c 13)))
549 (defmethod wam-test-mc-c :foo ((val number))
550   (+ val (if (next-method-p) (call-next-method) 0)))
551 (assert (= (wam-test-mc-c 13) 13))
552 (defmethod wam-test-mc-c :bar ((val number))
553   (+ val (if (next-method-p) (call-next-method) 0)))
554 (assert (raises-error? (wam-test-mc-c 13)))
555
556 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
557 ;;; given:
558 (defmethod incompatible-ll-test-1 (x) x)
559 (assert (raises-error? (defmethod incompatible-ll-test-1 (x y) y)))
560 (assert (raises-error? (defmethod incompatible-ll-test-1 (x &rest y) y)))
561 ;;; Sneakily using a bit of MOPness to check some consistency
562 (assert (= (length
563             (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
564
565 (defmethod incompatible-ll-test-2 (x &key bar) bar)
566 (assert (raises-error? (defmethod incompatible-ll-test-2 (x) x)))
567 (defmethod incompatible-ll-test-2 (x &rest y) y)
568 (assert (= (length
569             (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
570 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
571 (assert (= (length
572             (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
573
574 ;;; Per Christophe, this is an illegal method call because of 7.6.5
575 (assert (raises-error? (incompatible-ll-test-2 t 1 2)))
576
577 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
578
579 (defmethod incompatible-ll-test-3 ((x integer)) x)
580 (remove-method #'incompatible-ll-test-3
581                (find-method #'incompatible-ll-test-3
582                             nil
583                             (list (find-class 'integer))))
584 (assert (raises-error? (defmethod incompatible-ll-test-3 (x y) (list x y))))
585
586 \f
587 ;;; Attempting to instantiate classes with forward references in their
588 ;;; CPL should signal errors (FIXME: of what type?)
589 (defclass never-finished-class (this-one-unfinished-too) ())
590 (multiple-value-bind (result error)
591     (ignore-errors (make-instance 'never-finished-class))
592   (assert (null result))
593   (assert (typep error 'error)))
594 (multiple-value-bind (result error)
595     (ignore-errors (make-instance 'this-one-unfinished-too))
596   (assert (null result))
597   (assert (typep error 'error)))
598 \f
599 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
600 ;;; weren't for a while in sbcl-0.7.9.xx)
601 (defclass superclass-with-slot ()
602   ((a :allocation :class)))
603 (defclass subclass-for-class-allocation (superclass-with-slot) ())
604 (make-instance 'subclass-for-class-allocation)
605 \f
606 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
607 ;;; resulting in failure in the following:
608 (defmethod call-next-method-lexical-args ((x integer))
609   x)
610 (defmethod call-next-method-lexical-args :around ((x integer))
611   (let ((x (1+ x)))
612     (call-next-method)))
613 (assert (= (call-next-method-lexical-args 3) 3))
614 \f
615 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
616 ;;; until 0.7.9.5x
617 (defvar *d-m-c-args-test* nil)
618 (define-method-combination progn-with-lock ()
619   ((methods ()))
620   (:arguments object)
621   `(unwind-protect
622     (progn (lock (object-lock ,object))
623            ,@(mapcar #'(lambda (method)
624                          `(call-method ,method))
625                      methods))
626     (unlock (object-lock ,object))))
627 (defun object-lock (obj)
628   (push "object-lock" *d-m-c-args-test*)
629   obj)
630 (defun unlock (obj)
631   (push "unlock" *d-m-c-args-test*)
632   obj)
633 (defun lock (obj)
634   (push "lock" *d-m-c-args-test*)
635   obj)
636 (defgeneric d-m-c-args-test (x)
637   (:method-combination progn-with-lock))
638 (defmethod d-m-c-args-test ((x symbol))
639   (push "primary" *d-m-c-args-test*))
640 (defmethod d-m-c-args-test ((x number))
641   (error "foo"))
642 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
643 (assert (equal *d-m-c-args-test*
644                '("unlock" "object-lock" "primary" "lock" "object-lock")))
645 (setf *d-m-c-args-test* nil)
646 (ignore-errors (d-m-c-args-test 1))
647 (assert (equal *d-m-c-args-test*
648                '("unlock" "object-lock" "lock" "object-lock")))
649 \f
650 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
651 ;;; SYMBOL-MACROLET properly.  In fact, as of sbcl-0.7.10.20 it still
652 ;;; doesn't, but it does well enough to compile the following without
653 ;;; error (the problems remain in asking for a complete macroexpansion
654 ;;; of an arbitrary form).
655 (symbol-macrolet ((x 1))
656   (defmethod bug222 (z)
657     (macrolet ((frob (form) `(progn ,form ,x)))
658       (frob (print x)))))
659 (assert (= (bug222 t) 1))
660
661 ;;; also, a test case to guard against bogus environment hacking:
662
663 (eval-when (:compile-toplevel :load-toplevel :execute)
664   (setq bug222-b 3))
665 ;;; this should at the least compile:
666 (let ((bug222-b 1))
667   (defmethod bug222-b (z stream)
668     (macrolet ((frob (form) `(progn ,form ,bug222-b)))
669       (frob (format stream "~D~%" bug222-b)))))
670 ;;; and it would be nice (though not specified by ANSI) if the answer
671 ;;; were as follows:
672 (let ((x (make-string-output-stream)))
673   (let ((value (bug222-b t x)))
674     ;; not specified by ANSI
675     #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
676     (assert (= value 3)))
677   ;; specified.
678   (assert (char= (char (get-output-stream-string x) 0) #\1)))
679 \f
680 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
681 ;;; for invalid initargs where it should:
682 (defclass class234 () ())
683 (defclass subclass234 (class234) ())
684 (defvar *bug234* 0)
685 (defun bug-234 ()
686   (reinitialize-instance (make-instance 'class234) :dummy 0))
687 (defun subbug-234 ()
688   (reinitialize-instance (make-instance 'subclass234) :dummy 0))
689 (assert (raises-error? (bug-234) program-error))
690 (defmethod shared-initialize :after ((i class234) slots &key dummy)
691   (incf *bug234*))
692 (assert (typep (subbug-234) 'subclass234))
693 (assert (= *bug234*
694            ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
695            2))
696
697 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
698 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
699 (defclass class234-b1 () ())
700 (defclass class234-b2 (class234-b1) ())
701 (defvar *bug234-b* 0)
702 (defun bug234-b ()
703   (make-instance 'class234-b2))
704 (compile 'bug234-b)
705 (bug234-b)
706 (assert (= *bug234-b* 0))
707 (defmethod initialize-instance :before ((x class234-b1) &rest args)
708   (declare (ignore args))
709   (incf *bug234-b*))
710 (bug234-b)
711 (assert (= *bug234-b* 1))
712 \f
713 ;;; we should be able to make classes with uninterned names:
714 (defclass #:class-with-uninterned-name () ())
715 \f
716 ;;; SLOT-MISSING should be called when there are missing slots.
717 (defclass class-with-all-slots-missing () ())
718 (defmethod slot-missing (class (o class-with-all-slots-missing)
719                          slot-name op
720                          &optional new-value)
721   op)
722 (assert (eq (slot-value (make-instance 'class-with-all-slots-missing) 'foo)
723             'slot-value))
724 (assert (eq (funcall (lambda (x) (slot-value x 'bar))
725                      (make-instance 'class-with-all-slots-missing))
726             'slot-value))
727 (assert (eq (funcall (lambda (x) (setf (slot-value x 'baz) 'baz))
728                      (make-instance 'class-with-all-slots-missing))
729             ;; SLOT-MISSING's value is specified to be ignored; we
730             ;; return NEW-VALUE.
731             'baz))
732 \f
733 ;;; we should be able to specialize on anything that names a class.
734 (defclass name-for-class () ())
735 (defmethod something-that-specializes ((x name-for-class)) 1)
736 (setf (find-class 'other-name-for-class) (find-class 'name-for-class))
737 (defmethod something-that-specializes ((x other-name-for-class)) 2)
738 (assert (= (something-that-specializes (make-instance 'name-for-class)) 2))
739 (assert (= (something-that-specializes (make-instance 'other-name-for-class))
740            2))
741 \f
742 ;;; more forward referenced classes stuff
743 (defclass frc-1 (frc-2) ())
744 (assert (subtypep 'frc-1 (find-class 'frc-2)))
745 (assert (subtypep (find-class 'frc-1) 'frc-2))
746 (assert (not (subtypep (find-class 'frc-2) 'frc-1)))
747 (defclass frc-2 (frc-3) ((a :initarg :a)))
748 (assert (subtypep 'frc-1 (find-class 'frc-3)))
749 (defclass frc-3 () ())
750 (assert (typep (make-instance 'frc-1 :a 2) (find-class 'frc-1)))
751 (assert (typep (make-instance 'frc-2 :a 3) (find-class 'frc-2)))
752 \f
753 ;;; check that we can define classes with two slots of different names
754 ;;; (even if it STYLE-WARNs).
755 (defclass odd-name-class ()
756   ((name :initarg :name)
757    (cl-user::name :initarg :name2)))
758 (let ((x (make-instance 'odd-name-class :name 1 :name2 2)))
759   (assert (= (slot-value x 'name) 1))
760   (assert (= (slot-value x 'cl-user::name) 2)))
761 \f
762 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
763 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
764 (defstruct allocatable-structure a)
765 (assert (typep (allocate-instance (find-class 'allocatable-structure))
766                'allocatable-structure))
767 \f
768 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
769 ;;; amazingly, calls to CPL would work a couple of times, and then
770 ;;; start returning NIL.  A fix was found (relating to the
771 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
772 (defgeneric cpl (x)
773   (:method-combination list)
774   (:method list ((x broadcast-stream)) 'broadcast-stream)
775   (:method list ((x integer)) 'integer)
776   (:method list ((x number)) 'number)
777   (:method list ((x stream)) 'stream)
778   (:method list ((x structure-object)) 'structure-object))
779 (assert (equal (cpl 0) '(integer number)))
780 (assert (equal (cpl 0) '(integer number)))
781 (assert (equal (cpl 0) '(integer number)))
782 (assert (equal (cpl 0) '(integer number)))
783 (assert (equal (cpl 0) '(integer number)))
784 (assert (equal (cpl (make-broadcast-stream))
785                '(broadcast-stream stream structure-object)))
786 (assert (equal (cpl (make-broadcast-stream))
787                '(broadcast-stream stream structure-object)))
788 (assert (equal (cpl (make-broadcast-stream))
789                '(broadcast-stream stream structure-object)))
790 \f
791 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
792 ;;; parameters shouldn't affect the arguments to the next method for a
793 ;;; no-argument call to CALL-NEXT-METHOD
794 (defgeneric cnm-assignment (x)
795   (:method (x) x)
796   (:method ((x integer)) (setq x 3)
797            (list x (call-next-method) (call-next-method x))))
798 (assert (equal (cnm-assignment 1) '(3 1 3)))
799 \f
800 ;;; Bug reported by Istvan Marko 2003-07-09
801 (let ((class-name (gentemp)))
802   (loop for i from 1 to 9
803         for slot-name = (intern (format nil "X~D" i))
804         for initarg-name = (intern (format nil "X~D" i) :keyword)
805         collect `(,slot-name :initarg ,initarg-name) into slot-descs
806         append `(,initarg-name (list 0)) into default-initargs
807         finally (eval `(defclass ,class-name ()
808                          (,@slot-descs)
809                          (:default-initargs ,@default-initargs))))
810   (let ((f (compile nil `(lambda () (make-instance ',class-name)))))
811     (assert (typep (funcall f) class-name))))
812
813 ;;; bug 262: DEFMETHOD failed on a generic function without a lambda
814 ;;; list
815 (ensure-generic-function 'bug262)
816 (defmethod bug262 (x y)
817   (list x y))
818 (assert (equal (bug262 1 2) '(1 2)))
819
820 ;;; salex on #lisp 2003-10-13 reported that type declarations inside
821 ;;; WITH-SLOTS are too hairy to be checked
822 (defun ensure-no-notes (form)
823   (handler-case (compile nil `(lambda () ,form))
824     (sb-ext:compiler-note (c)
825       ;; FIXME: it would be better to check specifically for the "type
826       ;; is too hairy" note
827       (error c))))
828 (defvar *x*)
829 (ensure-no-notes '(with-slots (a) *x*
830                    (declare (integer a))
831                    a))
832 (ensure-no-notes '(with-slots (a) *x*
833                    (declare (integer a))
834                    (declare (notinline slot-value))
835                    a))
836
837 ;;; from CLHS 7.6.5.1
838 (defclass character-class () ((char :initarg :char)))
839 (defclass picture-class () ((glyph :initarg :glyph)))
840 (defclass character-picture-class (character-class picture-class) ())
841
842 (defmethod width ((c character-class) &key font) font)
843 (defmethod width ((p picture-class) &key pixel-size) pixel-size)
844
845 (assert (raises-error?
846          (width (make-instance 'character-class :char #\Q)
847                 :font 'baskerville :pixel-size 10)
848          program-error))
849 (assert (raises-error?
850          (width (make-instance 'picture-class :glyph #\Q)
851                 :font 'baskerville :pixel-size 10)
852          program-error))
853 (assert (eq (width (make-instance 'character-picture-class :char #\Q)
854                    :font 'baskerville :pixel-size 10)
855             'baskerville))
856
857 ;;; class redefinition shouldn't give any warnings, in the usual case
858 (defclass about-to-be-redefined () ((some-slot :accessor some-slot)))
859 (handler-bind ((warning #'error))
860   (defclass about-to-be-redefined () ((some-slot :accessor some-slot))))
861
862 ;;; attempts to add accessorish methods to generic functions with more
863 ;;; complex lambda lists should fail
864 (defgeneric accessoroid (object &key &allow-other-keys))
865 (assert (raises-error?
866          (defclass accessoroid-class () ((slot :accessor accessoroid)))
867          program-error))
868
869 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
870 (defclass shared-slot-and-redefinition ()
871   ((size :initarg :size :initform 1 :allocation :class)))
872 (let ((i (make-instance 'shared-slot-and-redefinition)))
873   (defclass shared-slot-and-redefinition ()
874     ((size :initarg :size :initform 2 :allocation :class)))
875   (assert (= (slot-value i 'size) 1)))
876
877 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
878 (defclass superclass-born-to-be-obsoleted () (a))
879 (defclass subclass-born-to-be-obsoleted (superclass-born-to-be-obsoleted) ())
880 (defparameter *born-to-be-obsoleted*
881   (make-instance 'subclass-born-to-be-obsoleted))
882 (defparameter *born-to-be-obsoleted-obsoleted* nil)
883 (defmethod update-instance-for-redefined-class
884     ((o subclass-born-to-be-obsoleted) a d pl &key)
885   (setf *born-to-be-obsoleted-obsoleted* t))
886 (make-instances-obsolete 'superclass-born-to-be-obsoleted)
887 (slot-boundp *born-to-be-obsoleted* 'a)
888 (assert *born-to-be-obsoleted-obsoleted*)
889
890 ;;; additional test suggested by Bruno Haible sbcl-devel 2004-04-21
891 (defclass super-super-obsoleted () (a))
892 (defclass super-obsoleted-1 (super-super-obsoleted) ())
893 (defclass super-obsoleted-2 (super-super-obsoleted) ())
894 (defclass obsoleted (super-obsoleted-1 super-obsoleted-2) ())
895 (defparameter *obsoleted* (make-instance 'obsoleted))
896 (defparameter *obsoleted-counter* 0)
897 (defmethod update-instance-for-redefined-class ((o obsoleted) a d pl &key)
898   (incf *obsoleted-counter*))
899 (make-instances-obsolete 'super-super-obsoleted)
900 (slot-boundp *obsoleted* 'a)
901 (assert (= *obsoleted-counter* 1))
902
903 ;;; yet another MAKE-INSTANCES-OBSOLETE test, this time from Nikodemus
904 ;;; Siivola.  Not all methods for accessing slots are created equal...
905 (defclass yet-another-obsoletion-super () ((obs :accessor obs-of :initform 0)))
906 (defclass yet-another-obsoletion-sub (yet-another-obsoletion-super) ())
907 (defmethod shared-initialize :after ((i yet-another-obsoletion-super)
908                                      slots &rest init)
909   (incf (obs-of i)))
910
911 (defvar *yao-super* (make-instance 'yet-another-obsoletion-super))
912 (defvar *yao-sub* (make-instance 'yet-another-obsoletion-sub))
913
914 (assert (= (obs-of *yao-super*) 1))
915 (assert (= (obs-of *yao-sub*) 1))
916 (make-instances-obsolete 'yet-another-obsoletion-super)
917 (assert (= (obs-of *yao-sub*) 2))
918 (assert (= (obs-of *yao-super*) 2))
919 (make-instances-obsolete 'yet-another-obsoletion-super)
920 (assert (= (obs-of *yao-super*) 3))
921 (assert (= (obs-of *yao-sub*) 3))
922 (assert (= (slot-value *yao-super* 'obs) 3))
923 (assert (= (slot-value *yao-sub* 'obs) 3))
924
925 ;;; one more MIO test: variable slot names
926 (defclass mio () ((x :initform 42)))
927 (defvar *mio-slot* 'x)
928 (defparameter *mio-counter* 0)
929 (defmethod update-instance-for-redefined-class ((instance mio) new old plist &key)
930   (incf *mio-counter*))
931
932 (let ((x (make-instance 'mio)))
933   (make-instances-obsolete 'mio)
934   (slot-value x *mio-slot*))
935
936 (let ((x (make-instance 'mio)))
937   (make-instances-obsolete 'mio)
938   (setf (slot-value x *mio-slot*) 13))
939
940 (let ((x (make-instance 'mio)))
941   (make-instances-obsolete 'mio)
942   (slot-boundp x *mio-slot*))
943
944 (let ((x (make-instance 'mio)))
945   (make-instances-obsolete 'mio)
946   (slot-makunbound x *mio-slot*))
947
948 (assert (= 4 *mio-counter*))
949
950 ;;; shared -> local slot transfers of inherited slots, reported by
951 ;;; Bruno Haible
952 (let (i)
953   (defclass super-with-magic-slot ()
954     ((magic :initarg :size :initform 1 :allocation :class)))
955   (defclass sub-of-super-with-magic-slot (super-with-magic-slot) ())
956   (setq i (make-instance 'sub-of-super-with-magic-slot))
957   (defclass super-with-magic-slot ()
958     ((magic :initarg :size :initform 2)))
959   (assert (= 1 (slot-value i 'magic))))
960
961 ;;; MAKE-INSTANCES-OBSOLETE return values
962 (defclass one-more-to-obsolete () ())
963 (assert (eq 'one-more-to-obsolete
964             (make-instances-obsolete 'one-more-to-obsolete)))
965 (assert (eq (find-class 'one-more-to-obsolete)
966             (make-instances-obsolete (find-class 'one-more-to-obsolete))))
967
968 ;;; Sensible error instead of a BUG. Reported by Thomas Burdick.
969 (multiple-value-bind (value err)
970     (ignore-errors
971       (defclass slot-def-with-duplicate-accessors ()
972         ((slot :writer get-slot :reader get-slot))))
973   (assert (typep err 'error))
974   (assert (not (typep err 'sb-int:bug))))
975
976 ;;; BUG 321: errors in parsing DEFINE-METHOD-COMBINATION arguments
977 ;;; lambda lists.
978
979 (define-method-combination w-args ()
980   ((method-list *))
981   (:arguments arg1 arg2 &aux (extra :extra))
982   `(progn ,@(mapcar (lambda (method) `(call-method ,method)) method-list)))
983 (defgeneric mc-test-w-args (p1 p2 s)
984   (:method-combination w-args)
985   (:method ((p1 number) (p2 t) s)
986     (vector-push-extend (list 'number p1 p2) s))
987   (:method ((p1 string) (p2 t) s)
988     (vector-push-extend (list 'string p1 p2) s))
989   (:method ((p1 t) (p2 t) s) (vector-push-extend (list t p1 p2) s)))
990 (let ((v (make-array 0 :adjustable t :fill-pointer t)))
991   (assert (= (mc-test-w-args 1 2 v) 1))
992   (assert (equal (aref v 0) '(number 1 2)))
993   (assert (equal (aref v 1) '(t 1 2))))
994
995 ;;; BUG 276: declarations and mutation.
996 (defmethod fee ((x fixnum))
997   (setq x (/ x 2))
998   x)
999 (assert (= (fee 1) 1/2))
1000 (defmethod fum ((x fixnum))
1001   (setf x (/ x 2))
1002   x)
1003 (assert (= (fum 3) 3/2))
1004 (defmethod fii ((x fixnum))
1005   (declare (special x))
1006   (setf x (/ x 2))
1007   x)
1008 (assert (= (fii 1) 1/2))
1009 (defvar *faa*)
1010 (defmethod faa ((*faa* string-stream))
1011   (setq *faa* (make-broadcast-stream *faa*))
1012   (write-line "Break, you sucker!" *faa*)
1013   'ok)
1014 (assert (eq 'ok (faa (make-string-output-stream))))
1015 (defmethod fex ((x fixnum) (y fixnum))
1016   (multiple-value-setq (x y) (values (/ x y) (/ y x)))
1017   (list x y))
1018 (assert (equal (fex 5 3) '(5/3 3/5)))
1019
1020 ;;; Bug reported by Zach Beane; incorrect return of (function
1021 ;;; ',fun-name) in defgeneric
1022 (assert
1023  (typep (funcall (compile nil
1024                           '(lambda () (flet ((nonsense () nil))
1025                                         (defgeneric nonsense ())))))
1026         'generic-function))
1027
1028 (assert
1029  (typep (funcall (compile nil
1030                           '(lambda () (flet ((nonsense-2 () nil))
1031                                         (defgeneric nonsense-2 ()
1032                                           (:method () t))))))
1033         'generic-function))
1034
1035 ;;; bug reported by Bruno Haible: (setf find-class) using a
1036 ;;; forward-referenced class
1037 (defclass fr-sub (fr-super) ())
1038 (setf (find-class 'fr-alt) (find-class 'fr-super))
1039 (assert (eq (find-class 'fr-alt) (find-class 'fr-super)))
1040
1041
1042 ;;; ANSI Figure 4-8: all defined classes.  Check that we can define
1043 ;;; methods on all of these.
1044 (progn
1045   (defgeneric method-for-defined-classes (x))
1046   (dolist (c '(arithmetic-error
1047                generic-function simple-error array hash-table
1048                simple-type-error
1049                bit-vector integer simple-warning
1050                broadcast-stream list standard-class
1051                built-in-class logical-pathname standard-generic-function
1052                cell-error method standard-method
1053                character method-combination standard-object
1054                class null storage-condition
1055                complex number stream
1056                concatenated-stream package stream-error
1057                condition package-error string
1058                cons parse-error string-stream
1059                control-error pathname structure-class
1060                division-by-zero print-not-readable structure-object
1061                echo-stream program-error style-warning
1062                end-of-file random-state symbol
1063                error ratio synonym-stream
1064                file-error rational t
1065                file-stream reader-error two-way-stream
1066                float readtable type-error
1067                floating-point-inexact real unbound-slot
1068                floating-point-invalid-operation restart unbound-variable
1069                floating-point-overflow sequence undefined-function
1070                floating-point-underflow serious-condition vector
1071                function simple-condition warning))
1072     (eval `(defmethod method-for-defined-classes ((x ,c)) (princ x))))
1073   (assert (string= (with-output-to-string (*standard-output*)
1074                      (method-for-defined-classes #\3))
1075                    "3")))
1076
1077
1078 \f
1079 ;;; When class definition does not complete due to a bad accessor
1080 ;;; name, do not cause an error when a new accessor name is provided
1081 ;;; during class redefinition
1082
1083 (defun existing-name (object)
1084   (list object))
1085
1086 (assert (raises-error? (defclass redefinition-of-accessor-class ()
1087                          ((slot :accessor existing-name)))))
1088
1089 (defclass redefinition-of-accessor-class ()
1090   ((slot :accessor new-name)))
1091
1092 \f
1093
1094 (load "package-ctor-bug.lisp")
1095 (assert (= (package-ctor-bug:test) 3))
1096 (delete-package "PACKAGE-CTOR-BUG")
1097 (load "package-ctor-bug.lisp")
1098 (assert (= (package-ctor-bug:test) 3))
1099
1100 (with-test (:name (:defmethod (setf find-class) integer))
1101   (mapcar #'eval
1102           '(
1103             (deftype defined-type () 'integer)
1104             (assert (raises-error?
1105                      (defmethod method-on-defined-type ((x defined-type)) x)))
1106             (deftype defined-type-and-class () 'integer)
1107             (setf (find-class 'defined-type-and-class) (find-class 'integer))
1108             (defmethod method-on-defined-type-and-class
1109                 ((x defined-type-and-class))
1110               (1+ x))
1111             (assert (= (method-on-defined-type-and-class 3) 4)))))
1112
1113 ;; bug 281
1114 (let ((sb-pcl::*max-emf-precomputation-methods* 0))
1115   (eval '(defgeneric bug-281 (x)
1116           (:method-combination +)
1117           (:method ((x symbol)) 1)
1118           (:method + ((x number)) x)))
1119   (assert (= 1 (bug-281 1)))
1120   (assert (= 4.2 (bug-281 4.2)))
1121   (multiple-value-bind (val err) (ignore-errors (bug-281 'symbol))
1122     (assert (not val))
1123     (assert (typep err 'error))))
1124 \f
1125 ;;; RESTART-CASE and CALL-METHOD
1126
1127 ;;; from Bruno Haible
1128
1129 (defun rc-cm/prompt-for-new-values ()
1130   (format *debug-io* "~&New values: ")
1131   (finish-output *debug-io*)
1132   (list (read *debug-io*)))
1133
1134 (defun rc-cm/add-method-restarts (form method)
1135   (let ((block (gensym))
1136         (tag (gensym)))
1137     `(block ,block
1138       (tagbody
1139          ,tag
1140          (return-from ,block
1141            (restart-case ,form
1142              (method-redo ()
1143                :report (lambda (stream)
1144                          (format stream "Try calling ~S again." ,method))
1145                (go ,tag))
1146              (method-return (l)
1147                :report (lambda (stream)
1148                          (format stream "Specify return values for ~S call."
1149                                  ,method))
1150                :interactive (lambda () (rc-cm/prompt-for-new-values))
1151                (return-from ,block (values-list l)))))))))
1152
1153 (defun rc-cm/convert-effective-method (efm)
1154   (if (consp efm)
1155       (if (eq (car efm) 'call-method)
1156           (let ((method-list (third efm)))
1157             (if (or (typep (first method-list) 'method) (rest method-list))
1158                 ;; Reduce the case of multiple methods to a single one.
1159                 ;; Make the call to the next-method explicit.
1160                 (rc-cm/convert-effective-method
1161                  `(call-method ,(second efm)
1162                    ((make-method
1163                      (call-method ,(first method-list) ,(rest method-list))))))
1164                 ;; Now the case of at most one method.
1165                 (if (typep (second efm) 'method)
1166                     ;; Wrap the method call in a RESTART-CASE.
1167                     (rc-cm/add-method-restarts
1168                      (cons (rc-cm/convert-effective-method (car efm))
1169                            (rc-cm/convert-effective-method (cdr efm)))
1170                      (second efm))
1171                     ;; Normal recursive processing.
1172                     (cons (rc-cm/convert-effective-method (car efm))
1173                           (rc-cm/convert-effective-method (cdr efm))))))
1174           (cons (rc-cm/convert-effective-method (car efm))
1175                 (rc-cm/convert-effective-method (cdr efm))))
1176       efm))
1177
1178 (define-method-combination standard-with-restarts ()
1179   ((around (:around))
1180    (before (:before))
1181    (primary () :required t)
1182    (after (:after)))
1183   (flet ((call-methods-sequentially (methods)
1184            (mapcar #'(lambda (method)
1185                        `(call-method ,method))
1186                    methods)))
1187     (let ((form (if (or before after (rest primary))
1188                     `(multiple-value-prog1
1189                        (progn
1190                          ,@(call-methods-sequentially before)
1191                          (call-method ,(first primary) ,(rest primary)))
1192                       ,@(call-methods-sequentially (reverse after)))
1193                     `(call-method ,(first primary)))))
1194       (when around
1195         (setq form
1196               `(call-method ,(first around)
1197                 (,@(rest around) (make-method ,form)))))
1198       (rc-cm/convert-effective-method form))))
1199
1200 (defgeneric rc-cm/testgf16 (x)
1201   (:method-combination standard-with-restarts))
1202 (defclass rc-cm/testclass16a () ())
1203 (defclass rc-cm/testclass16b (rc-cm/testclass16a) ())
1204 (defclass rc-cm/testclass16c (rc-cm/testclass16a) ())
1205 (defclass rc-cm/testclass16d (rc-cm/testclass16b rc-cm/testclass16c) ())
1206 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16a))
1207   (list 'a
1208         (not (null (find-restart 'method-redo)))
1209         (not (null (find-restart 'method-return)))))
1210 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16b))
1211   (cons 'b (call-next-method)))
1212 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16c))
1213   (cons 'c (call-next-method)))
1214 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16d))
1215   (cons 'd (call-next-method)))
1216 (assert (equal (rc-cm/testgf16 (make-instance 'rc-cm/testclass16d))
1217                '(d b c a t t)))
1218
1219 ;;; test case from Gerd Moellmann
1220 (define-method-combination r-c/c-m-1 ()
1221   ((primary () :required t))
1222   `(restart-case (call-method ,(first primary))
1223      ()))
1224
1225 (defgeneric r-c/c-m-1-gf ()
1226   (:method-combination r-c/c-m-1)
1227   (:method () nil))
1228
1229 (assert (null (r-c/c-m-1-gf)))
1230
1231 (handler-bind ((warning #'error))
1232   (eval '(defclass class-for-ctor/class-slot ()
1233           ((class-slot :initarg :class-slot :allocation :class))))
1234   (eval '(let ((c1 (make-instance 'class-for-ctor/class-slot))
1235                (c2 (make-instance 'class-for-ctor/class-slot :class-slot 1)))
1236           (assert (equal (list (slot-value c1 'class-slot)
1237                                (slot-value c2 'class-slot))
1238                    (list 1 1))))))
1239 \f
1240 ;;; tests of ctors on anonymous classes
1241 (defparameter *unnamed* (defclass ctor-unnamed-literal-class () ()))
1242 (setf (class-name *unnamed*) nil)
1243 (setf (find-class 'ctor-unnamed-literal-class) nil)
1244 (defparameter *unnamed2* (defclass ctor-unnamed-literal-class2 () ()))
1245 (defun ctor-unnamed-literal-class ()
1246   (make-instance '#.*unnamed*))
1247 (compile 'ctor-unnamed-literal-class)
1248 (defun ctor-unnamed-literal-class2 ()
1249   (make-instance '#.(find-class 'ctor-unnamed-literal-class2)))
1250 (compile 'ctor-unnamed-literal-class2)
1251 (defun ctor-unnamed-literal-class2/symbol ()
1252   (make-instance 'ctor-unnamed-literal-class2))
1253 (compile 'ctor-unnamed-literal-class2/symbol)
1254 (setf (class-name *unnamed2*) nil)
1255 (setf (find-class 'ctor-unnamed-literal-class2) nil)
1256 (with-test (:name (:ctor :unnamed-before))
1257   (assert (typep (ctor-unnamed-literal-class) *unnamed*)))
1258 (with-test (:name (:ctor :unnamed-after))
1259   (assert (typep (ctor-unnamed-literal-class2) *unnamed2*)))
1260 (with-test (:name (:ctor :unnamed-after/symbol))
1261   (assert (raises-error? (ctor-unnamed-literal-class2/symbol))))
1262 \f
1263 ;;; classes with slot types shouldn't break if the types don't name
1264 ;;; classes (bug #391)
1265 (defclass slot-type-superclass () ((slot :type fixnum)))
1266 (defclass slot-type-subclass (slot-type-superclass)
1267   ((slot :type (integer 1 5))))
1268 (let ((instance (make-instance 'slot-type-subclass)))
1269   (setf (slot-value instance 'slot) 3))
1270 \f
1271 ;;; ctors where there's a non-standard SHARED-INITIALIZE method and an
1272 ;;; initarg which isn't self-evaluating (kpreid on #lisp 2006-01-29)
1273 (defclass kpreid-enode ()
1274   ((slot :initarg not-a-keyword)))
1275 (defmethod shared-initialize ((o kpreid-enode) slots &key &allow-other-keys)
1276   (call-next-method))
1277 (defun make-kpreid-enode ()
1278   (make-instance 'kpreid-enode 'not-a-keyword 3))
1279 (with-test (:name (:ctor :non-keyword-initarg))
1280   (let ((x (make-kpreid-enode))
1281         (y (make-kpreid-enode)))
1282     (= (slot-value x 'slot) (slot-value y 'slot))))
1283 \f
1284 ;;; defining a class hierarchy shouldn't lead to spurious classoid
1285 ;;; errors on TYPEP questions (reported by Tim Moore on #lisp
1286 ;;; 2006-03-10)
1287 (defclass backwards-2 (backwards-1) (a b))
1288 (defclass backwards-3 (backwards-2) ())
1289 (defun typep-backwards-3 (x)
1290   (typep x 'backwards-3))
1291 (defclass backwards-1 () (a b))
1292 (assert (not (typep-backwards-3 1)))
1293 (assert (not (typep-backwards-3 (make-instance 'backwards-2))))
1294 (assert (typep-backwards-3 (make-instance 'backwards-3)))
1295 \f
1296 (defgeneric remove-method-1 (x)
1297   (:method ((x integer)) (1+ x)))
1298 (defgeneric remove-method-2 (x)
1299   (:method ((x integer)) (1- x)))
1300 (assert (eq #'remove-method-1
1301             (remove-method #'remove-method-1
1302                            (find-method #'remove-method-2
1303                                         nil
1304                                         (list (find-class 'integer))))))
1305 (assert (= (remove-method-1 3) 4))
1306 (assert (= (remove-method-2 3) 2))
1307
1308 ;;; ANSI doesn't require these restarts, but now that we have them we
1309 ;;; better test them too.
1310 (defclass slot-unbound-restart-test () ((x)))
1311 (let ((test (make-instance 'slot-unbound-restart-test)))
1312   (assert (not (slot-boundp test 'x)))
1313   (assert (= 42 (handler-bind ((unbound-slot (lambda (c) (use-value 42 c))))
1314                   (slot-value test 'x))))
1315   (assert (not (slot-boundp test 'x)))
1316   (assert (= 13 (handler-bind ((unbound-slot (lambda (c) (store-value 13 c))))
1317                   (slot-value test 'x))))
1318   (assert (= 13 (slot-value test 'x))))
1319
1320 ;;; Using class instances as specializers, reported by Pascal Costanza, ref CLHS 7.6.2
1321 (defclass class-as-specializer-test ()
1322    ())
1323 (eval `(defmethod class-as-specializer-test1 ((x ,(find-class 'class-as-specializer-test)))
1324           'foo))
1325 (assert (eq 'foo (class-as-specializer-test1 (make-instance 'class-as-specializer-test))))
1326 (funcall (compile nil `(lambda ()
1327                          (defmethod class-as-specializer-test2 ((x ,(find-class 'class-as-specializer-test)))
1328                            'bar))))
1329 (assert (eq 'bar (class-as-specializer-test2 (make-instance 'class-as-specializer-test))))
1330 \f
1331 ;;; CHANGE-CLASS and tricky allocation.
1332 (defclass foo-to-be-changed ()
1333   ((a :allocation :class :initform 1)))
1334 (defclass bar-to-be-changed (foo-to-be-changed) ())
1335 (defvar *bar-to-be-changed* (make-instance 'bar-to-be-changed))
1336 (defclass baz-to-be-changed ()
1337   ((a :allocation :instance :initform 2)))
1338 (change-class *bar-to-be-changed* 'baz-to-be-changed)
1339 (assert (= (slot-value *bar-to-be-changed* 'a) 1))
1340 \f
1341 ;;; proper name and class redefinition
1342 (defvar *to-be-renamed1* (defclass to-be-renamed1 () ()))
1343 (defvar *to-be-renamed2* (defclass to-be-renamed2 () ()))
1344 (setf (find-class 'to-be-renamed1) (find-class 'to-be-renamed2))
1345 (defvar *renamed1* (defclass to-be-renamed1 () ()))
1346 (assert (not (eq *to-be-renamed1* *to-be-renamed2*)))
1347 (assert (not (eq *to-be-renamed1* *renamed1*)))
1348 (assert (not (eq *to-be-renamed2* *renamed1*)))
1349 \f
1350 ;;; CLASS-NAME (and various other standardized generic functions) have
1351 ;;; their effective methods precomputed; in the process of rearranging
1352 ;;; (SETF FIND-CLASS) and FINALIZE-INHERITANCE, this broke.
1353 (defclass class-with-odd-class-name-method ()
1354   ((a :accessor class-name)))
1355 \f
1356 ;;; another case where precomputing (this time on PRINT-OBJECT) and
1357 ;;; lazily-finalized classes caused problems.  (report from James Y
1358 ;;; Knight sbcl-devel 20-07-2006)
1359
1360 (defclass base-print-object () ())
1361 ;;; this has the side-effect of finalizing BASE-PRINT-OBJECT, and
1362 ;;; additionally the second specializer (STREAM) changes the cache
1363 ;;; structure to require two keys, not just one.
1364 (defmethod print-object ((o base-print-object) (s stream))
1365   nil)
1366
1367 ;;; unfinalized as yet
1368 (defclass sub-print-object (base-print-object) ())
1369 ;;; the accessor causes an eager finalization
1370 (defclass subsub-print-object (sub-print-object)
1371   ((a :accessor a)))
1372
1373 ;;; triggers a discriminating function (and so cache) recomputation.
1374 ;;; The method on BASE-PRINT-OBJECT will cause the system to attempt
1375 ;;; to fill the cache for all subclasses of BASE-PRINT-OBJECT which
1376 ;;; have valid wrappers; however, in the course of doing so, the
1377 ;;; SUB-PRINT-OBJECT class gets finalized, which invalidates the
1378 ;;; SUBSUB-PRINT-OBJECT wrapper; if an invalid wrapper gets into a
1379 ;;; cache with more than one key, then failure ensues.
1380 (reinitialize-instance #'print-object)
1381 \f
1382 ;;; bug in long-form method combination: if there's an applicable
1383 ;;; method not part of any method group, we need to call
1384 ;;; INVALID-METHOD-ERROR.  (MC27 test case from Bruno Haible)
1385 (define-method-combination mc27 ()
1386   ((normal ())
1387    (ignored (:ignore :unused)))
1388   `(list 'result
1389     ,@(mapcar #'(lambda (method) `(call-method ,method)) normal)))
1390 (defgeneric test-mc27 (x)
1391   (:method-combination mc27)
1392   (:method :ignore ((x number)) (/ 0)))
1393 (assert (raises-error? (test-mc27 7)))
1394
1395 (define-method-combination mc27prime ()
1396   ((normal ())
1397    (ignored (:ignore)))
1398   `(list 'result ,@(mapcar (lambda (m) `(call-method ,m)) normal)))
1399 (defgeneric test-mc27prime (x)
1400   (:method-combination mc27prime)
1401   (:method :ignore ((x number)) (/ 0)))
1402 (assert (equal '(result) (test-mc27prime 3)))
1403 (assert (raises-error? (test-mc27 t))) ; still no-applicable-method
1404 \f
1405 ;;; more invalid wrappers.  This time for a long-standing bug in the
1406 ;;; compiler's expansion for TYPEP on various class-like things, with
1407 ;;; user-visible consequences.
1408 (defclass obsolete-again () ())
1409 (defvar *obsolete-again* (make-instance 'obsolete-again))
1410 (defvar *obsolete-again-hash* (sxhash *obsolete-again*))
1411 (make-instances-obsolete (find-class 'obsolete-again))
1412 (assert (not (streamp *obsolete-again*)))
1413 (make-instances-obsolete (find-class 'obsolete-again))
1414 (assert (= (sxhash *obsolete-again*) *obsolete-again-hash*))
1415 (compile (defun is-a-structure-object-p (x) (typep x 'structure-object)))
1416 (make-instances-obsolete (find-class 'obsolete-again))
1417 (assert (not (is-a-structure-object-p *obsolete-again*)))
1418 \f
1419 ;;; overeager optimization of slot-valuish things
1420 (defclass listoid ()
1421   ((caroid :initarg :caroid)
1422    (cdroid :initarg :cdroid :initform nil)))
1423 (defmethod lengthoid ((x listoid))
1424   (let ((result 0))
1425     (loop until (null x)
1426           do (incf result) (setq x (slot-value x 'cdroid)))
1427     result))
1428 (with-test (:name ((:setq :method-parameter) slot-value))
1429   (assert (= (lengthoid (make-instance 'listoid)) 1))
1430   (assert (= (lengthoid
1431               (make-instance 'listoid :cdroid
1432                              (make-instance 'listoid :cdroid
1433                                             (make-instance 'listoid))))
1434              3)))
1435
1436 \f
1437
1438 ;;;; Tests for argument parsing in fast-method-functions.
1439
1440 (defvar *foo* 0)
1441
1442 (eval-when (:compile-toplevel :load-toplevel :execute)
1443   (setf (symbol-value 'a) 'invalid))
1444
1445 (defmacro test1 (lambda-list values args &key declarations cnm)
1446   `(progn
1447      (fmakunbound 'll-method)
1448      (fmakunbound 'll-function)
1449      (defmethod ll-method ,lambda-list
1450        ,@declarations
1451        ,@(when cnm
1452            `((when nil (call-next-method))))
1453        (list ,@values))
1454      (defun ll-function ,lambda-list
1455        ,@declarations
1456        (list ,@values))
1457      (dotimes (i 2)
1458        (assert (equal (ll-method ,@args)
1459                       (ll-function ,@args))))))
1460
1461 (defmacro test (&rest args)
1462   `(progn
1463      (test1 ,@args :cnm nil)
1464      (test1 ,@args :cnm t)))
1465
1466 ;; Just plain arguments
1467
1468 (test (a) (a) (1))
1469 (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))
1470
1471 (test (*foo*) (*foo* (symbol-value '*foo*)) (1))
1472
1473 (test (a) (a (symbol-value 'a)) (1)
1474       :declarations ((declare (special a))))
1475
1476 ;; Optionals
1477
1478 (test (a &optional b c) (a b c) (1))
1479 (test (a &optional b c) (a b c) (1 2))
1480 (test (a &optional b c) (a b c) (1 2 3))
1481
1482 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1))
1483 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2))
1484 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2 3))
1485
1486 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) ())
1487 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) (1))
1488
1489 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) ())
1490 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) (1))
1491
1492 (test (&optional a) (a (symbol-value 'a)) ()
1493       :declarations ((declare (special a))))
1494 (test (&optional a) (a (symbol-value 'a)) (1)
1495       :declarations ((declare (special a))))
1496
1497 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) ()
1498       :declarations ((declare (special a))))
1499 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) (1)
1500       :declarations ((declare (special a))))
1501
1502 (defparameter *count* 0)
1503
1504 (test (&optional (a (incf *count*)) (b (incf *count*)))
1505       (a b *count* (setf *count* 0))
1506       ())
1507
1508 ;; Keywords with some &RESTs thrown in
1509
1510 (dolist (args '((1)
1511                 (1 :b 2)
1512                 (1 :c 3)
1513                 (1 :b 2 :c 3)
1514                 (1 :c 3 :b 2)
1515                 (1 :c 3 :c 1 :b 2 :b 4)))
1516   (eval `(test (a &key b c) (a b c) ,args))
1517   (eval `(test (a &key (b 'b b-p) (c 'c c-p))
1518                (a b c b-p c-p)
1519                ,args))
1520   (eval `(test (a &rest rest &key (b 'b b-p) (c 'c c-p))
1521                (a b c b-p c-p rest)
1522                ,args))
1523   (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1524                (a b c b-p c-p *foo* (symbol-value '*foo*))
1525                ,args))
1526   (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1527                (a b c b-p c-p *foo* (symbol-value '*foo*))
1528                ,args
1529                :declarations ((declare (special b-p))))))
1530
1531 (dolist (args '(()
1532                 (:*foo* 1)
1533                 (:*foo* 1 :*foo* 2)))
1534   (eval `(test (&key *foo*) (*foo* (symbol-value '*foo*)) ,args))
1535   (eval `(test (&key (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p)
1536                ,args))
1537   (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1538                ,args))
1539   (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1540                ,args
1541                :declarations ((declare (special a))))))
1542
1543 (defparameter *count* 0)
1544
1545 (test (&key (a (incf *count*)) (b (incf *count*)))
1546       (a b *count* (setf *count* 0))
1547       ())
1548
1549 (test (&key a b &allow-other-keys) (a b) (:a 1 :b 2 :c 3))
1550
1551 (defmethod clim-style-lambda-list-test (a b &optional c d &key x y)
1552   (list a b c d x y))
1553
1554 (clim-style-lambda-list-test 1 2)
1555
1556 (setf *count* 0)
1557
1558 (test (&aux (a (incf *count*)) (b (incf *count*)))
1559       (a b *count* (setf *count* 0))
1560       ())
1561
1562 ;;;; long-form method combination with &rest in :arguments
1563 ;;;; (this had a bug what with fixed in 1.0.4.something)
1564 (define-method-combination long-form-with-&rest ()
1565   ((methods *))
1566   (:arguments x &rest others)
1567   `(progn
1568      ,@(mapcar (lambda (method)
1569                  `(call-method ,method))
1570                methods)
1571      (list ,x (length ,others))))
1572
1573 (defgeneric test-long-form-with-&rest (x &rest others)
1574   (:method-combination long-form-with-&rest))
1575
1576 (defmethod test-long-form-with-&rest (x &rest others)
1577   nil)
1578
1579 (assert (equal '(:foo 13)
1580                (apply #'test-long-form-with-&rest :foo (make-list 13))))
1581
1582 ;;;; slot-missing for non-standard classes on SLOT-VALUE
1583 ;;;;
1584 ;;;; FIXME: This is arguably not right, actually: CLHS seems to say
1585 ;;;; we should just signal an error at least for built-in classes, but
1586 ;;;; for a while we were hitting NO-APPLICABLE-METHOD, which is definitely
1587 ;;;; wrong -- so test this for now at least.
1588
1589 (defvar *magic-symbol* (gensym "MAGIC"))
1590
1591 (set *magic-symbol* 42)
1592
1593 (defmethod slot-missing (class instance (slot-name (eql *magic-symbol*)) op
1594                          &optional new)
1595   (if (eq 'setf op)
1596       (setf (symbol-value *magic-symbol*)  new)
1597       (symbol-value *magic-symbol*)))
1598
1599 (assert (eql 42 (slot-value (cons t t) *magic-symbol*)))
1600 (assert (eql 13 (setf (slot-value 123 *magic-symbol*) 13)))
1601 (assert (eql 13 (slot-value 'foobar *magic-symbol*)))
1602
1603 ;;;; Built-in structure and condition layouts should have NIL in
1604 ;;;; LAYOUT-FOR-STD-CLASS-P, and classes should have T.
1605
1606 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'warning))))
1607 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'hash-table))))
1608 (assert (eq t (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'standard-object))))
1609
1610 ;;;; bug 402: PCL used to warn about non-standard declarations
1611 (declaim (declaration bug-402-d))
1612 (defgeneric bug-402-gf (x))
1613 (with-test (:name :bug-402)
1614   (handler-bind ((warning #'error))
1615     (eval '(defmethod bug-402-gf (x)
1616             (declare (bug-402-d x))
1617             x))))
1618
1619 ;;;; non-keyword :default-initargs + :before method on shared initialize
1620 ;;;; interacted badly with CTOR optimizations
1621 (defclass ctor-default-initarg-problem ()
1622   ((slot :initarg slotto))
1623   (:default-initargs slotto 123))
1624 (defmethod shared-initialize :before ((instance ctor-default-initarg-problem) slot-names &rest initargs)
1625   (format t "~&Rock on: ~A~%" initargs))
1626 (defun provoke-ctor-default-initarg-problem ()
1627   (make-instance 'ctor-default-initarg-problem))
1628 (handler-bind ((warning #'error))
1629   (assert (= 123 (slot-value (provoke-ctor-default-initarg-problem) 'slot))))
1630
1631 ;;;; discriminating net on streams used to generate code deletion notes on
1632 ;;;; first call
1633 (defgeneric stream-fd (stream direction))
1634 (defmethod stream-fd ((stream sb-sys:fd-stream) direction)
1635   (declare (ignore direction))
1636   (sb-sys:fd-stream-fd stream))
1637 (defmethod stream-fd ((stream synonym-stream) direction)
1638   (stream-fd (symbol-value (synonym-stream-symbol stream)) direction))
1639 (defmethod stream-fd ((stream two-way-stream) direction)
1640   (ecase direction
1641     (:input
1642      (stream-fd
1643       (two-way-stream-input-stream stream) direction))
1644     (:output
1645      (stream-fd
1646       (two-way-stream-output-stream stream) direction))))
1647 (with-test (:name (:discriminating-name :code-deletion-note))
1648   (handler-bind ((compiler-note #'error))
1649     (stream-fd sb-sys:*stdin* :output)
1650     (stream-fd sb-sys:*stdin* :output)))
1651
1652 (with-test (:name :bug-380)
1653   (defclass bug-380 ()
1654     ((slot :accessor bug380-slot)))
1655   (fmakunbound 'foo-slot)
1656   (defgeneric foo-slot (x y z))
1657   (defclass foo ()
1658     ((slot :accessor foo-slot-value))))
1659
1660 ;;; SET and (SETF SYMBOL-VALUE) used to confuse permuation vector
1661 ;;; optimizations
1662 (defclass fih ()
1663   ((x :initform :fih)))
1664 (defclass fah ()
1665   ((x :initform :fah)))
1666 (declaim (special *fih*))
1667 (defmethod fihfah ((*fih* fih))
1668   (set '*fih* (make-instance 'fah))
1669   (list (slot-value *fih* 'x)
1670         (eval '(slot-value *fih* 'x))))
1671 (defmethod fihfah ((fah fah))
1672   (declare (special fah))
1673   (set 'fah (make-instance 'fih))
1674   (list (slot-value fah 'x)
1675         (eval '(slot-value fah 'x))))
1676 (with-test (:name :set-of-a-method-specializer)
1677   (assert (equal '(:fah :fah) (fihfah (make-instance 'fih))))
1678   (assert (equal '(:fih :fih) (fihfah (make-instance 'fah)))))
1679
1680 (defmethod no-implicit-declarations-for-local-specials ((faax double-float))
1681   (declare (special faax))
1682   (set 'faax (when (< faax 0) (- faax)))
1683   faax)
1684 (with-test (:name :no-implicit-declarations-for-local-specials)
1685   (assert (not (no-implicit-declarations-for-local-specials 1.0d0))))
1686
1687 (defstruct bug-357-a
1688   slot1
1689   (slot2 t)
1690   (slot3 (coerce pi 'single-float) :type single-float))
1691 (defclass bug-357-b (bug-357-a)
1692   ((slot2 :initform 't2)
1693    (slot4 :initform -44)
1694    (slot5)
1695    (slot6 :initform t)
1696    (slot7 :initform (floor (* pi pi)))
1697    (slot8 :initform 88))
1698   (:metaclass structure-class))
1699 (defstruct (bug-357-c (:include bug-357-b (slot8 -88) (slot5 :ok)))
1700   slot9
1701   (slot10 t)
1702   (slot11 (floor (exp 3))))
1703 (with-test (:name :bug-357)
1704   (flet ((slots (x)
1705            (list (bug-357-c-slot1 x)
1706                  (bug-357-c-slot2 x)
1707                  (bug-357-c-slot3 x)
1708                  (bug-357-c-slot4 x)
1709                  (bug-357-c-slot5 x)
1710                  (bug-357-c-slot6 x)
1711                  (bug-357-c-slot7 x)
1712                  (bug-357-c-slot8 x)
1713                  (bug-357-c-slot9 x)
1714                  (bug-357-c-slot10 x)
1715                  (bug-357-c-slot11 x))))
1716     (let ((base (slots (make-bug-357-c))))
1717       (assert (equal base (slots (make-instance 'bug-357-c))))
1718       (assert (equal base '(nil t2 3.1415927 -44 :ok t 9 -88 nil t 20))))))
1719
1720 (defclass class-slot-shared-initialize ()
1721   ((a :allocation :class :initform :ok)))
1722 (with-test (:name :class-slot-shared-initialize)
1723   (let ((x (make-instance 'class-slot-shared-initialize)))
1724     (assert (eq :ok (slot-value x 'a)))
1725     (slot-makunbound x 'a)
1726     (assert (not (slot-boundp x 'a)))
1727     (shared-initialize x '(a))
1728     (assert (slot-boundp x 'a))
1729     (assert (eq :ok (slot-value x 'a)))))
1730
1731 (declaim (ftype (function (t t t) (values single-float &optional))
1732                 i-dont-want-to-be-clobbered-1
1733                 i-dont-want-to-be-clobbered-2))
1734 (defgeneric i-dont-want-to-be-clobbered-1 (t t t))
1735 (defmethod i-dont-want-to-be-clobbered-2 ((x cons) y z)
1736   y)
1737 (defun i-cause-an-gf-info-update ()
1738   (i-dont-want-to-be-clobbered-2 t t t))
1739 (with-test (:name :defgeneric-should-clobber-ftype)
1740   ;; (because it doesn't check the argument or result types)
1741   (assert (equal '(function (t t t) *)
1742                  (sb-kernel:type-specifier
1743                   (sb-int:info :function
1744                                :type 'i-dont-want-to-be-clobbered-1))))
1745   (assert (equal '(function (t t t) *)
1746                  (sb-kernel:type-specifier
1747                   (sb-int:info :function
1748                                :type 'i-dont-want-to-be-clobbered-2))))
1749   (assert (eq :defined-method
1750               (sb-int:info :function
1751                            :where-from 'i-dont-want-to-be-clobbered-1)))
1752   (assert (eq :defined-method
1753               (sb-int:info :function
1754                            :where-from 'i-dont-want-to-be-clobbered-2))))
1755
1756 (with-test (:name :bogus-parameter-specializer-name-error)
1757   (assert (eq :ok
1758               (handler-case
1759                   (eval `(defmethod #:fii ((x "a string")) 'string))
1760                 (sb-int:reference-condition (c)
1761                   (when (member '(:ansi-cl :macro defmethod)
1762                                 (sb-int:reference-condition-references c)
1763                                 :test #'equal)
1764                     :ok))))))
1765
1766 (defclass remove-default-initargs-test ()
1767   ((x :initarg :x :initform 42)))
1768 (defclass remove-default-initatgs-test ()
1769   ((x :initarg :x :initform 42))
1770   (:default-initargs :x 0))
1771 (defclass remove-default-initargs-test ()
1772   ((x :initarg :x :initform 42)))
1773 (with-test (:name :remove-default-initargs)
1774   (assert (= 42 (slot-value (make-instance 'remove-default-initargs-test)
1775                             'x))))
1776 \f
1777 (with-test (:name :bug-485019)
1778   ;; there was a bug in WALK-SETQ, used in method body walking, in the
1779   ;; presence of declarations on symbol macros.
1780   (defclass bug-485019 ()
1781     ((array :initarg :array)))
1782   (defmethod bug-485019 ((bug-485019 bug-485019))
1783     (with-slots (array) bug-485019
1784       (declare (type (or null simple-array) array))
1785       (setf array (make-array 4)))
1786     bug-485019)
1787   (bug-485019 (make-instance 'bug-485019)))
1788
1789 ;;;; success