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