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