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