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