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