4484eb72f3c803c92f7a21e99dee50e118d6a753
[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
1232 (defgeneric r-c/c-m-1-gf ()
1233   (:method-combination r-c/c-m-1)
1234   (:method () nil))
1235
1236 (assert (null (r-c/c-m-1-gf)))
1237
1238 (handler-bind ((warning #'error))
1239   (eval '(defclass class-for-ctor/class-slot ()
1240           ((class-slot :initarg :class-slot :allocation :class))))
1241   (eval '(let ((c1 (make-instance 'class-for-ctor/class-slot))
1242                (c2 (make-instance 'class-for-ctor/class-slot :class-slot 1)))
1243           (assert (equal (list (slot-value c1 'class-slot)
1244                                (slot-value c2 'class-slot))
1245                    (list 1 1))))))
1246 \f
1247 ;;; tests of ctors on anonymous classes
1248 (defparameter *unnamed* (defclass ctor-unnamed-literal-class () ()))
1249 (setf (class-name *unnamed*) nil)
1250 (setf (find-class 'ctor-unnamed-literal-class) nil)
1251 (defparameter *unnamed2* (defclass ctor-unnamed-literal-class2 () ()))
1252 (defun ctor-unnamed-literal-class ()
1253   (make-instance '#.*unnamed*))
1254 (compile 'ctor-unnamed-literal-class)
1255 (defun ctor-unnamed-literal-class2 ()
1256   (make-instance '#.(find-class 'ctor-unnamed-literal-class2)))
1257 (compile 'ctor-unnamed-literal-class2)
1258 (defun ctor-unnamed-literal-class2/symbol ()
1259   (make-instance 'ctor-unnamed-literal-class2))
1260 (compile 'ctor-unnamed-literal-class2/symbol)
1261 (setf (class-name *unnamed2*) nil)
1262 (setf (find-class 'ctor-unnamed-literal-class2) nil)
1263 (with-test (:name (:ctor :unnamed-before))
1264   (assert (typep (ctor-unnamed-literal-class) *unnamed*)))
1265 (with-test (:name (:ctor :unnamed-after))
1266   (assert (typep (ctor-unnamed-literal-class2) *unnamed2*)))
1267 (with-test (:name (:ctor :unnamed-after/symbol))
1268   (assert (raises-error? (ctor-unnamed-literal-class2/symbol))))
1269 \f
1270 ;;; classes with slot types shouldn't break if the types don't name
1271 ;;; classes (bug #391)
1272 (defclass slot-type-superclass () ((slot :type fixnum)))
1273 (defclass slot-type-subclass (slot-type-superclass)
1274   ((slot :type (integer 1 5))))
1275 (let ((instance (make-instance 'slot-type-subclass)))
1276   (setf (slot-value instance 'slot) 3))
1277 \f
1278 ;;; ctors where there's a non-standard SHARED-INITIALIZE method and an
1279 ;;; initarg which isn't self-evaluating (kpreid on #lisp 2006-01-29)
1280 (defclass kpreid-enode ()
1281   ((slot :initarg not-a-keyword)))
1282 (defmethod shared-initialize ((o kpreid-enode) slots &key &allow-other-keys)
1283   (call-next-method))
1284 (defun make-kpreid-enode ()
1285   (make-instance 'kpreid-enode 'not-a-keyword 3))
1286 (with-test (:name (:ctor :non-keyword-initarg))
1287   (let ((x (make-kpreid-enode))
1288         (y (make-kpreid-enode)))
1289     (= (slot-value x 'slot) (slot-value y 'slot))))
1290 \f
1291 ;;; defining a class hierarchy shouldn't lead to spurious classoid
1292 ;;; errors on TYPEP questions (reported by Tim Moore on #lisp
1293 ;;; 2006-03-10)
1294 (defclass backwards-2 (backwards-1) (a b))
1295 (defclass backwards-3 (backwards-2) ())
1296 (defun typep-backwards-3 (x)
1297   (typep x 'backwards-3))
1298 (defclass backwards-1 () (a b))
1299 (assert (not (typep-backwards-3 1)))
1300 (assert (not (typep-backwards-3 (make-instance 'backwards-2))))
1301 (assert (typep-backwards-3 (make-instance 'backwards-3)))
1302 \f
1303 (defgeneric remove-method-1 (x)
1304   (:method ((x integer)) (1+ x)))
1305 (defgeneric remove-method-2 (x)
1306   (:method ((x integer)) (1- x)))
1307 (assert (eq #'remove-method-1
1308             (remove-method #'remove-method-1
1309                            (find-method #'remove-method-2
1310                                         nil
1311                                         (list (find-class 'integer))))))
1312 (assert (= (remove-method-1 3) 4))
1313 (assert (= (remove-method-2 3) 2))
1314
1315 ;;; ANSI doesn't require these restarts, but now that we have them we
1316 ;;; better test them too.
1317 (defclass slot-unbound-restart-test () ((x)))
1318 (let ((test (make-instance 'slot-unbound-restart-test)))
1319   (assert (not (slot-boundp test 'x)))
1320   (assert (= 42 (handler-bind ((unbound-slot (lambda (c) (use-value 42 c))))
1321                   (slot-value test 'x))))
1322   (assert (not (slot-boundp test 'x)))
1323   (assert (= 13 (handler-bind ((unbound-slot (lambda (c) (store-value 13 c))))
1324                   (slot-value test 'x))))
1325   (assert (= 13 (slot-value test 'x))))
1326
1327 ;;; Using class instances as specializers, reported by Pascal Costanza, ref CLHS 7.6.2
1328 (defclass class-as-specializer-test ()
1329    ())
1330 (eval `(defmethod class-as-specializer-test1 ((x ,(find-class 'class-as-specializer-test)))
1331           'foo))
1332 (assert (eq 'foo (class-as-specializer-test1 (make-instance 'class-as-specializer-test))))
1333 (funcall (compile nil `(lambda ()
1334                          (defmethod class-as-specializer-test2 ((x ,(find-class 'class-as-specializer-test)))
1335                            'bar))))
1336 (assert (eq 'bar (class-as-specializer-test2 (make-instance 'class-as-specializer-test))))
1337 \f
1338 ;;; CHANGE-CLASS and tricky allocation.
1339 (defclass foo-to-be-changed ()
1340   ((a :allocation :class :initform 1)))
1341 (defclass bar-to-be-changed (foo-to-be-changed) ())
1342 (defvar *bar-to-be-changed* (make-instance 'bar-to-be-changed))
1343 (defclass baz-to-be-changed ()
1344   ((a :allocation :instance :initform 2)))
1345 (change-class *bar-to-be-changed* 'baz-to-be-changed)
1346 (assert (= (slot-value *bar-to-be-changed* 'a) 1))
1347 \f
1348 ;;; proper name and class redefinition
1349 (defvar *to-be-renamed1* (defclass to-be-renamed1 () ()))
1350 (defvar *to-be-renamed2* (defclass to-be-renamed2 () ()))
1351 (setf (find-class 'to-be-renamed1) (find-class 'to-be-renamed2))
1352 (defvar *renamed1* (defclass to-be-renamed1 () ()))
1353 (assert (not (eq *to-be-renamed1* *to-be-renamed2*)))
1354 (assert (not (eq *to-be-renamed1* *renamed1*)))
1355 (assert (not (eq *to-be-renamed2* *renamed1*)))
1356 \f
1357 ;;; CLASS-NAME (and various other standardized generic functions) have
1358 ;;; their effective methods precomputed; in the process of rearranging
1359 ;;; (SETF FIND-CLASS) and FINALIZE-INHERITANCE, this broke.
1360 (defclass class-with-odd-class-name-method ()
1361   ((a :accessor class-name)))
1362 \f
1363 ;;; another case where precomputing (this time on PRINT-OBJECT) and
1364 ;;; lazily-finalized classes caused problems.  (report from James Y
1365 ;;; Knight sbcl-devel 20-07-2006)
1366
1367 (defclass base-print-object () ())
1368 ;;; this has the side-effect of finalizing BASE-PRINT-OBJECT, and
1369 ;;; additionally the second specializer (STREAM) changes the cache
1370 ;;; structure to require two keys, not just one.
1371 (defmethod print-object ((o base-print-object) (s stream))
1372   nil)
1373
1374 ;;; unfinalized as yet
1375 (defclass sub-print-object (base-print-object) ())
1376 ;;; the accessor causes an eager finalization
1377 (defclass subsub-print-object (sub-print-object)
1378   ((a :accessor a)))
1379
1380 ;;; triggers a discriminating function (and so cache) recomputation.
1381 ;;; The method on BASE-PRINT-OBJECT will cause the system to attempt
1382 ;;; to fill the cache for all subclasses of BASE-PRINT-OBJECT which
1383 ;;; have valid wrappers; however, in the course of doing so, the
1384 ;;; SUB-PRINT-OBJECT class gets finalized, which invalidates the
1385 ;;; SUBSUB-PRINT-OBJECT wrapper; if an invalid wrapper gets into a
1386 ;;; cache with more than one key, then failure ensues.
1387 (reinitialize-instance #'print-object)
1388 \f
1389 ;;; bug in long-form method combination: if there's an applicable
1390 ;;; method not part of any method group, we need to call
1391 ;;; INVALID-METHOD-ERROR.  (MC27 test case from Bruno Haible)
1392 (define-method-combination mc27 ()
1393   ((normal ())
1394    (ignored (:ignore :unused)))
1395   `(list 'result
1396     ,@(mapcar #'(lambda (method) `(call-method ,method)) normal)))
1397 (defgeneric test-mc27 (x)
1398   (:method-combination mc27)
1399   (:method :ignore ((x number)) (/ 0)))
1400 (assert (raises-error? (test-mc27 7)))
1401
1402 (define-method-combination mc27prime ()
1403   ((normal ())
1404    (ignored (:ignore)))
1405   `(list 'result ,@(mapcar (lambda (m) `(call-method ,m)) normal)))
1406 (defgeneric test-mc27prime (x)
1407   (:method-combination mc27prime)
1408   (:method :ignore ((x number)) (/ 0)))
1409 (assert (equal '(result) (test-mc27prime 3)))
1410 (assert (raises-error? (test-mc27 t))) ; still no-applicable-method
1411 \f
1412 ;;; more invalid wrappers.  This time for a long-standing bug in the
1413 ;;; compiler's expansion for TYPEP on various class-like things, with
1414 ;;; user-visible consequences.
1415 (defclass obsolete-again () ())
1416 (defvar *obsolete-again* (make-instance 'obsolete-again))
1417 (defvar *obsolete-again-hash* (sxhash *obsolete-again*))
1418 (make-instances-obsolete (find-class 'obsolete-again))
1419 (assert (not (streamp *obsolete-again*)))
1420 (make-instances-obsolete (find-class 'obsolete-again))
1421 (assert (= (sxhash *obsolete-again*) *obsolete-again-hash*))
1422 (compile (defun is-a-structure-object-p (x) (typep x 'structure-object)))
1423 (make-instances-obsolete (find-class 'obsolete-again))
1424 (assert (not (is-a-structure-object-p *obsolete-again*)))
1425 \f
1426 ;;; overeager optimization of slot-valuish things
1427 (defclass listoid ()
1428   ((caroid :initarg :caroid)
1429    (cdroid :initarg :cdroid :initform nil)))
1430 (defmethod lengthoid ((x listoid))
1431   (let ((result 0))
1432     (loop until (null x)
1433           do (incf result) (setq x (slot-value x 'cdroid)))
1434     result))
1435 (with-test (:name ((:setq :method-parameter) slot-value))
1436   (assert (= (lengthoid (make-instance 'listoid)) 1))
1437   (assert (= (lengthoid
1438               (make-instance 'listoid :cdroid
1439                              (make-instance 'listoid :cdroid
1440                                             (make-instance 'listoid))))
1441              3)))
1442
1443 \f
1444
1445 ;;;; Tests for argument parsing in fast-method-functions.
1446
1447 (defvar *foo* 0)
1448
1449 (eval-when (:compile-toplevel :load-toplevel :execute)
1450   (setf (symbol-value 'a) 'invalid))
1451
1452 (defmacro test1 (lambda-list values args &key declarations cnm)
1453   `(progn
1454      (fmakunbound 'll-method)
1455      (fmakunbound 'll-function)
1456      (defmethod ll-method ,lambda-list
1457        ,@declarations
1458        ,@(when cnm
1459            `((when nil (call-next-method))))
1460        (list ,@values))
1461      (defun ll-function ,lambda-list
1462        ,@declarations
1463        (list ,@values))
1464      (dotimes (i 2)
1465        (assert (equal (ll-method ,@args)
1466                       (ll-function ,@args))))))
1467
1468 (defmacro test (&rest args)
1469   `(progn
1470      (test1 ,@args :cnm nil)
1471      (test1 ,@args :cnm t)))
1472
1473 ;; Just plain arguments
1474
1475 (test (a) (a) (1))
1476 (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))
1477
1478 (test (*foo*) (*foo* (symbol-value '*foo*)) (1))
1479
1480 (test (a) (a (symbol-value 'a)) (1)
1481       :declarations ((declare (special a))))
1482
1483 ;; Optionals
1484
1485 (test (a &optional b c) (a b c) (1))
1486 (test (a &optional b c) (a b c) (1 2))
1487 (test (a &optional b c) (a b c) (1 2 3))
1488
1489 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1))
1490 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2))
1491 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2 3))
1492
1493 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) ())
1494 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) (1))
1495
1496 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) ())
1497 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) (1))
1498
1499 (test (&optional a) (a (symbol-value 'a)) ()
1500       :declarations ((declare (special a))))
1501 (test (&optional a) (a (symbol-value 'a)) (1)
1502       :declarations ((declare (special a))))
1503
1504 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) ()
1505       :declarations ((declare (special a))))
1506 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) (1)
1507       :declarations ((declare (special a))))
1508
1509 (defparameter *count* 0)
1510
1511 (test (&optional (a (incf *count*)) (b (incf *count*)))
1512       (a b *count* (setf *count* 0))
1513       ())
1514
1515 ;; Keywords with some &RESTs thrown in
1516
1517 (dolist (args '((1)
1518                 (1 :b 2)
1519                 (1 :c 3)
1520                 (1 :b 2 :c 3)
1521                 (1 :c 3 :b 2)
1522                 (1 :c 3 :c 1 :b 2 :b 4)))
1523   (eval `(test (a &key b c) (a b c) ,args))
1524   (eval `(test (a &key (b 'b b-p) (c 'c c-p))
1525                (a b c b-p c-p)
1526                ,args))
1527   (eval `(test (a &rest rest &key (b 'b b-p) (c 'c c-p))
1528                (a b c b-p c-p rest)
1529                ,args))
1530   (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1531                (a b c b-p c-p *foo* (symbol-value '*foo*))
1532                ,args))
1533   (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1534                (a b c b-p c-p *foo* (symbol-value '*foo*))
1535                ,args
1536                :declarations ((declare (special b-p))))))
1537
1538 (dolist (args '(()
1539                 (:*foo* 1)
1540                 (:*foo* 1 :*foo* 2)))
1541   (eval `(test (&key *foo*) (*foo* (symbol-value '*foo*)) ,args))
1542   (eval `(test (&key (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p)
1543                ,args))
1544   (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1545                ,args))
1546   (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1547                ,args
1548                :declarations ((declare (special a))))))
1549
1550 (defparameter *count* 0)
1551
1552 (test (&key (a (incf *count*)) (b (incf *count*)))
1553       (a b *count* (setf *count* 0))
1554       ())
1555
1556 (test (&key a b &allow-other-keys) (a b) (:a 1 :b 2 :c 3))
1557
1558 (defmethod clim-style-lambda-list-test (a b &optional c d &key x y)
1559   (list a b c d x y))
1560
1561 (clim-style-lambda-list-test 1 2)
1562
1563 (setf *count* 0)
1564
1565 (test (&aux (a (incf *count*)) (b (incf *count*)))
1566       (a b *count* (setf *count* 0))
1567       ())
1568
1569 ;;;; long-form method combination with &rest in :arguments
1570 ;;;; (this had a bug what with fixed in 1.0.4.something)
1571 (define-method-combination long-form-with-&rest ()
1572   ((methods *))
1573   (:arguments x &rest others)
1574   `(progn
1575      ,@(mapcar (lambda (method)
1576                  `(call-method ,method))
1577                methods)
1578      (list ,x (length ,others))))
1579
1580 (defgeneric test-long-form-with-&rest (x &rest others)
1581   (:method-combination long-form-with-&rest))
1582
1583 (defmethod test-long-form-with-&rest (x &rest others)
1584   nil)
1585
1586 (assert (equal '(:foo 13)
1587                (apply #'test-long-form-with-&rest :foo (make-list 13))))
1588
1589 ;;;; slot-missing for non-standard classes on SLOT-VALUE
1590 ;;;;
1591 ;;;; FIXME: This is arguably not right, actually: CLHS seems to say
1592 ;;;; we should just signal an error at least for built-in classes, but
1593 ;;;; for a while we were hitting NO-APPLICABLE-METHOD, which is definitely
1594 ;;;; wrong -- so test this for now at least.
1595
1596 (defvar *magic-symbol* (gensym "MAGIC"))
1597
1598 (set *magic-symbol* 42)
1599
1600 (defmethod slot-missing (class instance (slot-name (eql *magic-symbol*)) op
1601                          &optional new)
1602   (if (eq 'setf op)
1603       (setf (symbol-value *magic-symbol*)  new)
1604       (symbol-value *magic-symbol*)))
1605
1606 (assert (eql 42 (slot-value (cons t t) *magic-symbol*)))
1607 (assert (eql 13 (setf (slot-value 123 *magic-symbol*) 13)))
1608 (assert (eql 13 (slot-value 'foobar *magic-symbol*)))
1609
1610 ;;;; Built-in structure and condition layouts should have NIL in
1611 ;;;; LAYOUT-FOR-STD-CLASS-P, and classes should have T.
1612
1613 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'warning))))
1614 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'hash-table))))
1615 (assert (eq t (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'standard-object))))
1616
1617 ;;;; bug 402: PCL used to warn about non-standard declarations
1618 (declaim (declaration bug-402-d))
1619 (defgeneric bug-402-gf (x))
1620 (with-test (:name :bug-402)
1621   (handler-bind ((warning #'error))
1622     (eval '(defmethod bug-402-gf (x)
1623             (declare (bug-402-d x))
1624             x))))
1625
1626 ;;;; non-keyword :default-initargs + :before method on shared initialize
1627 ;;;; interacted badly with CTOR optimizations
1628 (defclass ctor-default-initarg-problem ()
1629   ((slot :initarg slotto))
1630   (:default-initargs slotto 123))
1631 (defmethod shared-initialize :before ((instance ctor-default-initarg-problem) slot-names &rest initargs)
1632   (format t "~&Rock on: ~A~%" initargs))
1633 (defun provoke-ctor-default-initarg-problem ()
1634   (make-instance 'ctor-default-initarg-problem))
1635 (handler-bind ((warning #'error))
1636   (assert (= 123 (slot-value (provoke-ctor-default-initarg-problem) 'slot))))
1637
1638 ;;;; discriminating net on streams used to generate code deletion notes on
1639 ;;;; first call
1640 (defgeneric stream-fd (stream direction))
1641 (defmethod stream-fd ((stream sb-sys:fd-stream) direction)
1642   (declare (ignore direction))
1643   (sb-sys:fd-stream-fd stream))
1644 (defmethod stream-fd ((stream synonym-stream) direction)
1645   (stream-fd (symbol-value (synonym-stream-symbol stream)) direction))
1646 (defmethod stream-fd ((stream two-way-stream) direction)
1647   (ecase direction
1648     (:input
1649      (stream-fd
1650       (two-way-stream-input-stream stream) direction))
1651     (:output
1652      (stream-fd
1653       (two-way-stream-output-stream stream) direction))))
1654 (with-test (:name (:discriminating-name :code-deletion-note))
1655   (handler-bind ((compiler-note #'error))
1656     (stream-fd sb-sys:*stdin* :output)
1657     (stream-fd sb-sys:*stdin* :output)))
1658
1659 (with-test (:name :bug-380)
1660   (defclass bug-380 ()
1661     ((slot :accessor bug380-slot)))
1662   (fmakunbound 'foo-slot)
1663   (defgeneric foo-slot (x y z))
1664   (defclass foo ()
1665     ((slot :accessor foo-slot-value))))
1666
1667 ;;; SET and (SETF SYMBOL-VALUE) used to confuse permuation vector
1668 ;;; optimizations
1669 (defclass fih ()
1670   ((x :initform :fih)))
1671 (defclass fah ()
1672   ((x :initform :fah)))
1673 (declaim (special *fih*))
1674 (defmethod fihfah ((*fih* fih))
1675   (set '*fih* (make-instance 'fah))
1676   (list (slot-value *fih* 'x)
1677         (eval '(slot-value *fih* 'x))))
1678 (defmethod fihfah ((fah fah))
1679   (declare (special fah))
1680   (set 'fah (make-instance 'fih))
1681   (list (slot-value fah 'x)
1682         (eval '(slot-value fah 'x))))
1683 (with-test (:name :set-of-a-method-specializer)
1684   (assert (equal '(:fah :fah) (fihfah (make-instance 'fih))))
1685   (assert (equal '(:fih :fih) (fihfah (make-instance 'fah)))))
1686
1687 (defmethod no-implicit-declarations-for-local-specials ((faax double-float))
1688   (declare (special faax))
1689   (set 'faax (when (< faax 0) (- faax)))
1690   faax)
1691 (with-test (:name :no-implicit-declarations-for-local-specials)
1692   (assert (not (no-implicit-declarations-for-local-specials 1.0d0))))
1693
1694 (defstruct bug-357-a
1695   slot1
1696   (slot2 t)
1697   (slot3 (coerce pi 'single-float) :type single-float))
1698 (defclass bug-357-b (bug-357-a)
1699   ((slot2 :initform 't2)
1700    (slot4 :initform -44)
1701    (slot5)
1702    (slot6 :initform t)
1703    (slot7 :initform (floor (* pi pi)))
1704    (slot8 :initform 88))
1705   (:metaclass structure-class))
1706 (defstruct (bug-357-c (:include bug-357-b (slot8 -88) (slot5 :ok)))
1707   slot9
1708   (slot10 t)
1709   (slot11 (floor (exp 3))))
1710 (with-test (:name :bug-357)
1711   (flet ((slots (x)
1712            (list (bug-357-c-slot1 x)
1713                  (bug-357-c-slot2 x)
1714                  (bug-357-c-slot3 x)
1715                  (bug-357-c-slot4 x)
1716                  (bug-357-c-slot5 x)
1717                  (bug-357-c-slot6 x)
1718                  (bug-357-c-slot7 x)
1719                  (bug-357-c-slot8 x)
1720                  (bug-357-c-slot9 x)
1721                  (bug-357-c-slot10 x)
1722                  (bug-357-c-slot11 x))))
1723     (let ((base (slots (make-bug-357-c))))
1724       (assert (equal base (slots (make-instance 'bug-357-c))))
1725       (assert (equal base '(nil t2 3.1415927 -44 :ok t 9 -88 nil t 20))))))
1726
1727 (defclass class-slot-shared-initialize ()
1728   ((a :allocation :class :initform :ok)))
1729 (with-test (:name :class-slot-shared-initialize)
1730   (let ((x (make-instance 'class-slot-shared-initialize)))
1731     (assert (eq :ok (slot-value x 'a)))
1732     (slot-makunbound x 'a)
1733     (assert (not (slot-boundp x 'a)))
1734     (shared-initialize x '(a))
1735     (assert (slot-boundp x 'a))
1736     (assert (eq :ok (slot-value x 'a)))))
1737
1738 (declaim (ftype (function (t t t) (values single-float &optional))
1739                 i-dont-want-to-be-clobbered-1
1740                 i-dont-want-to-be-clobbered-2))
1741 (defgeneric i-dont-want-to-be-clobbered-1 (t t t))
1742 (defmethod i-dont-want-to-be-clobbered-2 ((x cons) y z)
1743   y)
1744 (defun i-cause-an-gf-info-update ()
1745   (i-dont-want-to-be-clobbered-2 t t t))
1746 (with-test (:name :defgeneric-should-clobber-ftype)
1747   ;; (because it doesn't check the argument or result types)
1748   (assert (equal '(function (t t t) *)
1749                  (sb-kernel:type-specifier
1750                   (sb-int:info :function
1751                                :type 'i-dont-want-to-be-clobbered-1))))
1752   (assert (equal '(function (t t t) *)
1753                  (sb-kernel:type-specifier
1754                   (sb-int:info :function
1755                                :type 'i-dont-want-to-be-clobbered-2))))
1756   (assert (eq :defined-method
1757               (sb-int:info :function
1758                            :where-from 'i-dont-want-to-be-clobbered-1)))
1759   (assert (eq :defined-method
1760               (sb-int:info :function
1761                            :where-from 'i-dont-want-to-be-clobbered-2))))
1762
1763 (with-test (:name :bogus-parameter-specializer-name-error)
1764   (assert (eq :ok
1765               (handler-case
1766                   (eval `(defmethod #:fii ((x "a string")) 'string))
1767                 (sb-int:reference-condition (c)
1768                   (when (member '(:ansi-cl :macro defmethod)
1769                                 (sb-int:reference-condition-references c)
1770                                 :test #'equal)
1771                     :ok))))))
1772
1773 (defclass remove-default-initargs-test ()
1774   ((x :initarg :x :initform 42)))
1775 (defclass remove-default-initatgs-test ()
1776   ((x :initarg :x :initform 42))
1777   (:default-initargs :x 0))
1778 (defclass remove-default-initargs-test ()
1779   ((x :initarg :x :initform 42)))
1780 (with-test (:name :remove-default-initargs)
1781   (assert (= 42 (slot-value (make-instance 'remove-default-initargs-test)
1782                             'x))))
1783 \f
1784 (with-test (:name :bug-485019)
1785   ;; there was a bug in WALK-SETQ, used in method body walking, in the
1786   ;; presence of declarations on symbol macros.
1787   (defclass bug-485019 ()
1788     ((array :initarg :array)))
1789   (defmethod bug-485019 ((bug-485019 bug-485019))
1790     (with-slots (array) bug-485019
1791       (declare (type (or null simple-array) array))
1792       (setf array (make-array 4)))
1793     bug-485019)
1794   (bug-485019 (make-instance 'bug-485019)))
1795
1796 ;;; The compiler didn't propagate the declarared type before applying
1797 ;;; the transform for (SETF SLOT-VALUE), so the generic accessor was used.
1798 (defstruct foo-520366
1799   slot)
1800 (defun quux-520366 (cont)
1801   (funcall cont))
1802 (defun bar-520366 (foo-struct)
1803   (declare (type foo-520366 foo-struct))
1804   (with-slots (slot) foo-struct
1805     (tagbody
1806        (quux-520366 #'(lambda ()
1807                         (setf slot :value)
1808                         (go TAG)))
1809      TAG)))
1810 (with-test (:name :bug-520366)
1811   (let ((callees (find-named-callees #'bar-520366)))
1812     (assert (equal (list #'quux-520366) callees))))
1813
1814 (defgeneric no-applicable-method/retry (x))
1815 (defmethod no-applicable-method/retry ((x string))
1816   "string")
1817 (with-test (:name :no-applicable-method/retry)
1818   (assert (equal "cons"
1819                  (handler-bind ((error
1820                                  (lambda (c)
1821                                    (declare (ignore c))
1822                                    (let ((r (find-restart 'sb-pcl::retry)))
1823                                      (when r
1824                                        (eval `(defmethod no-applicable-method/retry ((x cons))
1825                                                 "cons"))
1826                                        (invoke-restart r))))))
1827                    (no-applicable-method/retry (cons t t))))))
1828
1829 (defgeneric no-primary-method/retry (x))
1830 (defmethod no-primary-method/retry :before (x) (assert x))
1831 (with-test (:name :no-primary-method/retry)
1832   (assert (equal "ok!"
1833                  (handler-bind ((error
1834                                  (lambda (c)
1835                                    (declare (ignore c))
1836                                    (let ((r (find-restart 'sb-pcl::retry)))
1837                                      (when r
1838                                        (eval `(defmethod no-primary-method/retry (x)
1839                                                 "ok!"))
1840                                        (invoke-restart r))))))
1841                    (no-primary-method/retry (cons t t))))))
1842 \f
1843 ;;; test that a cacheing strategy for make-instance initargs checking
1844 ;;; can handle class redefinitions
1845 (defclass cacheing-initargs-redefinitions-check ()
1846   ((slot :initarg :slot)))
1847 (defun cacheing-initargs-redefinitions-check-fun (&optional (initarg :slot))
1848   (declare (notinline make-instance))
1849   (make-instance 'cacheing-initargs-redefinitions-check)
1850   (make-instance 'cacheing-initargs-redefinitions-check initarg 3))
1851 (with-test (:name :make-instance-initargs)
1852   (make-instance 'cacheing-initargs-redefinitions-check)
1853   (make-instance 'cacheing-initargs-redefinitions-check :slot 3)
1854   (cacheing-initargs-redefinitions-check-fun :slot)
1855   (assert (raises-error? (cacheing-initargs-redefinitions-check-fun :slot2))))
1856 (defclass cacheing-initargs-redefinitions-check ()
1857   ((slot :initarg :slot2)))
1858 (with-test (:name :make-instance-redefined-initargs)
1859   (make-instance 'cacheing-initargs-redefinitions-check)
1860   (make-instance 'cacheing-initargs-redefinitions-check :slot2 3)
1861   (cacheing-initargs-redefinitions-check-fun :slot2)
1862   (assert (raises-error? (cacheing-initargs-redefinitions-check-fun :slot))))
1863 (defmethod initialize-instance :after ((class cacheing-initargs-redefinitions-check) &key slot)
1864   nil)
1865 (with-test (:name :make-instance-new-method-initargs)
1866   (make-instance 'cacheing-initargs-redefinitions-check)
1867   (make-instance 'cacheing-initargs-redefinitions-check :slot2 3)
1868   (cacheing-initargs-redefinitions-check-fun :slot2)
1869   (let ((thing (cacheing-initargs-redefinitions-check-fun :slot)))
1870     (assert (not (slot-boundp thing 'slot)))))
1871
1872 (with-test (:name :defmethod-specializer-builtin-class-alias)
1873   (let ((alias (gensym)))
1874     (setf (find-class alias) (find-class 'symbol))
1875     (eval `(defmethod lp-618387 ((s ,alias))
1876              (symbol-name s)))
1877     (assert (equal "FOO" (funcall 'lp-618387 :foo)))))
1878
1879 (with-test (:name :pcl-spurious-ignore-warnings)
1880   (defgeneric no-spurious-ignore-warnings (req &key key))
1881   (handler-bind ((warning (lambda (x) (error "~A" x))))
1882     (eval
1883      '(defmethod no-spurious-ignore-warnings ((req number) &key key)
1884        (declare (ignore key))
1885        (check-type req integer))))
1886   (defgeneric should-get-an-ignore-warning (req &key key))
1887   (let ((warnings 0))
1888     (handler-bind ((warning (lambda (c) (setq warnings 1) (muffle-warning c))))
1889       (eval '(defmethod should-get-an-ignore-warning ((req integer) &key key)
1890               (check-type req integer))))
1891     (assert (= warnings 1))))
1892
1893 (defgeneric generic-function-pretty-arglist-optional-and-key (req &optional opt &key key)
1894   (:method (req &optional opt &key key)
1895     (list req opt key)))
1896
1897 (with-test (:name :generic-function-pretty-arglist-optional-and-key)
1898   (handler-bind ((warning #'error))
1899     ;; Used to signal a style-warning
1900     (assert (equal '(req &optional opt &key key)
1901                    (sb-pcl::generic-function-pretty-arglist
1902                     #'generic-function-pretty-arglist-optional-and-key)))))
1903
1904 (with-test (:name :bug-894202)
1905   (assert (eq :good
1906               (handler-case
1907                   (let ((name (gensym "FOO"))
1908                         (decl (gensym "BAR")))
1909                     (eval `(defgeneric ,name ()
1910                              (declare (,decl)))))
1911                 (warning ()
1912                   :good)))))
1913
1914 (with-test (:name :bug-898331)
1915   (handler-bind ((warning #'error))
1916     (eval `(defgeneric bug-898331 (request type remaining-segment-requests all-requests)))
1917     (eval `(defmethod bug-898331 ((request cons) (type (eql :cancel))
1918                                   remaining-segment-requests
1919                                   all-segment-requests)
1920              (declare (ignore all-segment-requests))
1921              (check-type request t)))))
1922
1923 (with-test (:name :bug-1001799)
1924   ;; compilation of the defmethod used to cause infinite recursion
1925   (let ((pax (gensym "PAX"))
1926         (pnr (gensym "PNR"))
1927         (sup (gensym "SUP"))
1928         (frob (gensym "FROB"))
1929         (sb-ext:*evaluator-mode* :compile))
1930     (eval
1931      `(progn
1932         (declaim (optimize (speed 1) (space 1) (safety 3) (debug 3) (compilation-speed 1)))
1933         (defclass ,pax (,sup)
1934           ((,pnr :type (or null ,pnr))))
1935         (defclass ,pnr (,sup)
1936           ((,pax :type (or null ,pax))))
1937         (defclass ,sup ()
1938           ())
1939         (defmethod ,frob ((pnr ,pnr))
1940           (slot-value pnr ',pax))))))
1941
1942 (with-test (:name :bug-1099708)
1943   (defclass bug-1099708 ()
1944     ((slot-1099708 :initarg :slot-1099708)))
1945   ;; caused infinite equal testing in function name lookup
1946   (eval
1947    '(progn
1948      (defun make-1099708-1 ()
1949        (make-instance 'bug-1099708 :slot-1099708 '#1= (1 2 . #1#)))
1950      (defun make-1099708-2 ()
1951        (make-instance 'bug-1099708 :slot-1099708 '#2= (1 2 . #2#)))))
1952   (assert (not (eql (slot-value (make-1099708-1) 'slot-1099708)
1953                     (slot-value (make-1099708-2) 'slot-1099708)))))
1954
1955 (with-test (:name :bug-1099708b-list)
1956   (defclass bug-1099708b-list ()
1957     ((slot-1099708b-list :initarg :slot-1099708b-list)))
1958   (eval
1959    '(progn
1960      (defun make-1099708b-list-1 ()
1961        (make-instance 'bug-1099708b-list :slot-1099708b-list '(some value)))
1962      (defun make-1099708b-list-2 ()
1963        (make-instance 'bug-1099708b-list :slot-1099708b-list '(some value)))))
1964   (assert (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list)
1965                (slot-value (make-1099708b-list-1) 'slot-1099708b-list)))
1966   (assert (eql (slot-value (make-1099708b-list-2) 'slot-1099708b-list)
1967                (slot-value (make-1099708b-list-2) 'slot-1099708b-list)))
1968   (assert (not (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list)
1969                     (slot-value (make-1099708b-list-2) 'slot-1099708b-list)))))
1970
1971 (with-test (:name :bug-1099708b-string)
1972   (defclass bug-1099708b-string ()
1973     ((slot-1099708b-string :initarg :slot-1099708b-string)))
1974   (eval
1975    '(progn
1976      (defun make-1099708b-string-1 ()
1977        (make-instance 'bug-1099708b-string :slot-1099708b-string "string"))
1978      (defun make-1099708b-string-2 ()
1979        (make-instance 'bug-1099708b-string :slot-1099708b-string "string"))))
1980   (assert (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string)
1981                (slot-value (make-1099708b-string-1) 'slot-1099708b-string)))
1982   (assert (eql (slot-value (make-1099708b-string-2) 'slot-1099708b-string)
1983                (slot-value (make-1099708b-string-2) 'slot-1099708b-string)))
1984   (assert (not (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string)
1985                     (slot-value (make-1099708b-string-2) 'slot-1099708b-string)))))
1986
1987 (with-test (:name :bug-1099708b-bitvector)
1988   (defclass bug-1099708b-bitvector ()
1989     ((slot-1099708b-bitvector :initarg :slot-1099708b-bitvector)))
1990   (eval
1991    '(progn
1992      (defun make-1099708b-bitvector-1 ()
1993        (make-instance 'bug-1099708b-bitvector :slot-1099708b-bitvector #*1011))
1994      (defun make-1099708b-bitvector-2 ()
1995        (make-instance 'bug-1099708b-bitvector :slot-1099708b-bitvector #*1011))))
1996   (assert (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)
1997                (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)))
1998   (assert (eql (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)
1999                (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)))
2000   (assert (not (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)
2001                     (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)))))
2002
2003 (with-test (:name :bug-1099708b-pathname)
2004   (defclass bug-1099708b-pathname ()
2005     ((slot-1099708b-pathname :initarg :slot-1099708b-pathname)))
2006   (eval
2007    '(progn
2008      (defun make-1099708b-pathname-1 ()
2009        (make-instance 'bug-1099708b-pathname :slot-1099708b-pathname #p"pn"))
2010      (defun make-1099708b-pathname-2 ()
2011        (make-instance 'bug-1099708b-pathname :slot-1099708b-pathname #p"pn"))))
2012   (assert (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)
2013                (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)))
2014   (assert (eql (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)
2015                (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)))
2016   (assert (not (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)
2017                     (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)))))
2018
2019 (with-test (:name :bug-1099708c-list)
2020   (defclass bug-1099708c-list ()
2021     ((slot-1099708c-list :initarg :slot-1099708c-list)))
2022   (eval
2023    '(progn
2024      (defun make-1099708c-list-1 ()
2025        (make-instance 'bug-1099708c-list :slot-1099708c-list #1='(some value)))
2026      (defun make-1099708c-list-2 ()
2027        (make-instance 'bug-1099708c-list :slot-1099708c-list #1#))))
2028   (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list)
2029                (slot-value (make-1099708c-list-1) 'slot-1099708c-list)))
2030   (assert (eql (slot-value (make-1099708c-list-2) 'slot-1099708c-list)
2031                (slot-value (make-1099708c-list-2) 'slot-1099708c-list)))
2032   (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list)
2033                (slot-value (make-1099708c-list-2) 'slot-1099708c-list))))
2034
2035 ;;; bug-1179858
2036
2037 ;;; Define a class and force the "fallback" constructor generator to be
2038 ;;; used by having a HAIRY-AROUND-OR-NONSTANDARD-PRIMARY-METHOD-P on
2039 ;;; SHARED-INITIALIZE.
2040 (defclass bug-1179858 ()
2041   ((foo :initarg :foo :reader bug-1179858-foo))
2042   (:default-initargs :foo (error "Should not be evaluated")))
2043 (defmethod shared-initialize :around ((instance bug-1179858) (slot-names t) &key)
2044   (call-next-method))
2045
2046 (with-test (:name (make-instance :fallback-generator-initarg-handling
2047                    :bug-1179858))
2048   ;; Now compile a lambda containing MAKE-INSTANCE to exercise the
2049   ;; fallback constructor generator. Call the resulting compiled
2050   ;; function to trigger the bug.
2051   (funcall (compile nil '(lambda () (make-instance 'bug-1179858 :foo t)))))
2052
2053 ;;; Other brokenness, found while investigating: fallback-generator
2054 ;;; handling of non-keyword initialization arguments
2055 (defclass bug-1179858b ()
2056   ((foo :initarg foo :reader bug-1179858b-foo))
2057   (:default-initargs foo 14))
2058 (defmethod shared-initialize :around ((instance bug-1179858b) (slot-names t) &key)
2059   (call-next-method))
2060
2061 (with-test (:name (make-instance :fallback-generator-non-keyword-initarg
2062                    :bug-1179858))
2063   (flet ((foo= (n i) (= (bug-1179858b-foo i) n)))
2064     (assert
2065      (foo= 14 (funcall (compile nil '(lambda () (make-instance 'bug-1179858b))))))
2066     (assert
2067      (foo= 15 (funcall (compile nil '(lambda () (make-instance 'bug-1179858b 'foo 15))))))))
2068
2069 (with-test (:name (cpl-violation-setup :bug-309076))
2070   (assert (raises-error?
2071            (progn
2072              (defclass bug-309076-broken-class (standard-class) ()
2073                (:metaclass sb-mop:funcallable-standard-class))
2074              (sb-mop:finalize-inheritance (find-class 'bug-309076-broken-class))))))
2075
2076 (with-test (:name (cpl-violation-irrelevant-class :bug-309076))
2077   (defclass bug-309076-class (standard-class) ())
2078   (defmethod sb-mop:validate-superclass ((x bug-309076-class) (y standard-class)) t)
2079   (assert (typep (make-instance 'bug-309076-class) 'bug-309076-class)))
2080
2081 ;;;; success