1.0.37.57: better DEFMETHOD pretty-printing
[sbcl/pkhuong.git] / tests / clos.impure.lisp
blob91eab018bc1ab08d224c89e4fd4295ec259f58ef
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 (load "compiler-test-util.lisp")
15 (defpackage "CLOS-IMPURE"
16 (:use "CL" "ASSERTOID" "TEST-UTIL" "COMPILER-TEST-UTIL"))
17 (in-package "CLOS-IMPURE")
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))))
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)))
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)
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))
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))))
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))))
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))))
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))
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))
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))
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))
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.
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) ())
192 (defclass forward-ref-d1 (forward-ref-d2 forward-ref-d3) ())
193 (defclass forward-ref-d2 (forward-ref-d4 forward-ref-d5) ())
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)))
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))))
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)))
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)))
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))
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)))
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*))
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)))
291 (defgeneric gf (obj)
292 (:method-combination test-mc 1))
294 (defmethod gf (obj)
295 obj)
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)))
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 (let ((answer (documentation '+ 'function)))
360 (assert (stringp answer))
361 (defmethod documentation ((x (eql '+)) y) "WRONG")
362 (assert (string= (documentation '+ 'function) answer)))
364 ;;; only certain declarations are permitted in DEFGENERIC
365 (macrolet ((assert-program-error (form)
366 `(multiple-value-bind (value error)
367 (ignore-errors ,form)
368 (assert (null value))
369 (assert (typep error 'program-error)))))
370 (assert-program-error (defgeneric bogus-declaration (x)
371 (declare (special y))))
372 (assert-program-error (defgeneric bogus-declaration2 (x)
373 (declare (notinline concatenate)))))
374 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
375 ;;; method.
376 (defmethod no-next-method-test ((x integer)) (call-next-method))
377 (assert (null (ignore-errors (no-next-method-test 1))))
378 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
379 'success)
380 (assert (eq (no-next-method-test 1) 'success))
381 (assert (null (ignore-errors (no-next-method-test 'foo))))
383 ;;; regression test for bug 176, following a fix that seems
384 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
385 ;;; Moellmann, merged in sbcl-0.7.9.12).
386 (dotimes (i 10)
387 (let ((lastname (intern (format nil "C176-~D" (1- i))))
388 (name (intern (format nil "C176-~D" i))))
389 (eval `(defclass ,name
390 (,@(if (= i 0) nil (list lastname)))
391 ()))
392 (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
393 (declare (ignore any))))))
394 (defclass b176 () (aslot-176))
395 (defclass c176-0 (b176) ())
396 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
398 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
399 ;;; primary methods:
400 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
401 ((around (:around))
402 (primary (dmc-test-mc) :order order :required t))
403 (let ((form (if (rest primary)
404 `(and ,@(mapcar #'(lambda (method)
405 `(call-method ,method))
406 primary))
407 `(call-method ,(first primary)))))
408 (if around
409 `(call-method ,(first around)
410 (,@(rest around)
411 (make-method ,form)))
412 form)))
414 (defgeneric dmc-test-mc (&key k)
415 (:method-combination dmc-test-mc))
417 (defmethod dmc-test-mc dmc-test-mc (&key k)
420 (dmc-test-mc :k 1)
421 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
422 ;;; the NAME argument, not some random method object. So:
423 (assert (eq (define-method-combination dmc-test-return-foo)
424 'dmc-test-return-foo))
425 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
426 'dmc-test-return-bar))
427 (assert (eq (define-method-combination dmc-test-return
428 (&optional (order :most-specific-first))
429 ((around (:around))
430 (primary (dmc-test-return) :order order :required t))
431 (let ((form (if (rest primary)
432 `(and ,@(mapcar #'(lambda (method)
433 `(call-method ,method))
434 primary))
435 `(call-method ,(first primary)))))
436 (if around
437 `(call-method ,(first around)
438 (,@(rest around)
439 (make-method ,form)))
440 form)))
441 'dmc-test-return))
443 ;;; DEFINE-METHOD-COMBINATION should, according to the description in 7.7,
444 ;;; allow you to do everything in the body forms yourself if you specify
445 ;;; exactly one method group whose qualifier-pattern is *
447 ;;; The specific language is:
448 ;;; "The use of method group specifiers provides a convenient syntax to select
449 ;;; methods, to divide them among the possible roles, and to perform the
450 ;;; necessary error checking. It is possible to perform further filtering of
451 ;;; methods in the body forms by using normal list-processing operations and
452 ;;; the functions method-qualifiers and invalid-method-error. It is permissible
453 ;;; to use setq on the variables named in the method group specifiers and to
454 ;;; bind additional variables. It is also possible to bypass the method group
455 ;;; specifier mechanism and do everything in the body forms. This is
456 ;;; accomplished by writing a single method group with * as its only
457 ;;; qualifier-pattern; the variable is then bound to a list of all of the
458 ;;; applicable methods, in most-specific-first order."
459 (define-method-combination wam-test-method-combination-a ()
460 ((all-methods *))
461 (do ((methods all-methods (rest methods))
462 (primary nil)
463 (around nil))
464 ((null methods)
465 (let ((primary (nreverse primary))
466 (around (nreverse around)))
467 (if primary
468 (let ((form (if (rest primary)
469 `(call-method ,(first primary) ,(rest primary))
470 `(call-method ,(first primary)))))
471 (if around
472 `(call-method ,(first around) (,@(rest around)
473 (make-method ,form)))
474 form))
475 `(make-method (error "No primary methods")))))
476 (let* ((method (first methods))
477 (qualifier (first (method-qualifiers method))))
478 (cond
479 ((equal :around qualifier)
480 (push method around))
481 ((null qualifier)
482 (push method primary))))))
484 (defgeneric wam-test-mc-a (val)
485 (:method-combination wam-test-method-combination-a))
486 (assert (raises-error? (wam-test-mc-a 13)))
487 (defmethod wam-test-mc-a ((val number))
488 (+ val (if (next-method-p) (call-next-method) 0)))
489 (assert (= (wam-test-mc-a 13) 13))
490 (defmethod wam-test-mc-a :around ((val number))
491 (+ val (if (next-method-p) (call-next-method) 0)))
492 (assert (= (wam-test-mc-a 13) 26))
494 ;;; DEFINE-METHOD-COMBINATION
495 ;;; When two methods are in the same method group and have the same
496 ;;; specializers, their sort order within the group may be ambiguous. Therefore,
497 ;;; we should throw an error when we have two methods in the same group with
498 ;;; the same specializers /as long as/ we have more than one method group
499 ;;; or our single method group qualifier-pattern is not *. This resolves the
500 ;;; apparent conflict with the above 'It is also possible to bypass' language.
502 ;;; The language specifying this behavior is:
503 ;;; "Note that two methods with identical specializers, but with different
504 ;;; qualifiers, are not ordered by the algorithm described in Step 2 of the
505 ;;; method selection and combination process described in Section 7.6.6
506 ;;; (Method Selection and Combination). Normally the two methods play different
507 ;;; roles in the effective method because they have different qualifiers, and
508 ;;; no matter how they are ordered in the result of Step 2, the effective
509 ;;; method is the same. If the two methods play the same role and their order
510 ;;; matters, an error is signaled. This happens as part of the qualifier
511 ;;; pattern matching in define-method-combination."
513 ;;; Note that the spec pretty much equates 'method group' and 'role'.
514 ;; First we ensure that it fails correctly when there is more than one
515 ;; method group
516 (define-method-combination wam-test-method-combination-b ()
517 ((around (:around))
518 (primary * :required t))
519 (let ((form (if (rest primary)
520 `(call-method ,(first primary) ,(rest primary))
521 `(call-method ,(first primary)))))
522 (if around
523 `(call-method ,(first around) (,@(rest around)
524 (make-method ,form)))
525 form)))
527 (defgeneric wam-test-mc-b (val)
528 (:method-combination wam-test-method-combination-b))
529 (defmethod wam-test-mc-b ((val number))
530 (+ val (if (next-method-p) (call-next-method) 0)))
531 (assert (= (wam-test-mc-b 13) 13))
532 (defmethod wam-test-mc-b :around ((val number))
533 (+ val (if (next-method-p) (call-next-method) 0)))
534 (assert (= (wam-test-mc-b 13) 26))
535 (defmethod wam-test-mc-b :somethingelse ((val number))
536 (+ val (if (next-method-p) (call-next-method) 0)))
537 (assert (raises-error? (wam-test-mc-b 13)))
539 ;;; now, ensure that it fails with a single group with a qualifier-pattern
540 ;;; that is not *
541 (define-method-combination wam-test-method-combination-c ()
542 ((methods listp :required t))
543 (if (rest methods)
544 `(call-method ,(first methods) ,(rest methods))
545 `(call-method ,(first methods))))
547 (defgeneric wam-test-mc-c (val)
548 (:method-combination wam-test-method-combination-c))
549 (assert (raises-error? (wam-test-mc-c 13)))
550 (defmethod wam-test-mc-c :foo ((val number))
551 (+ val (if (next-method-p) (call-next-method) 0)))
552 (assert (= (wam-test-mc-c 13) 13))
553 (defmethod wam-test-mc-c :bar ((val number))
554 (+ val (if (next-method-p) (call-next-method) 0)))
555 (assert (raises-error? (wam-test-mc-c 13)))
557 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
558 ;;; given:
559 (defmethod incompatible-ll-test-1 (x) x)
560 (assert (raises-error? (defmethod incompatible-ll-test-1 (x y) y)))
561 (assert (raises-error? (defmethod incompatible-ll-test-1 (x &rest y) y)))
562 ;;; Sneakily using a bit of MOPness to check some consistency
563 (assert (= (length
564 (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
566 (defmethod incompatible-ll-test-2 (x &key bar) bar)
567 (assert (raises-error? (defmethod incompatible-ll-test-2 (x) x)))
568 (defmethod incompatible-ll-test-2 (x &rest y) y)
569 (assert (= (length
570 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
571 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
572 (assert (= (length
573 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
575 ;;; Per Christophe, this is an illegal method call because of 7.6.5
576 (assert (raises-error? (incompatible-ll-test-2 t 1 2)))
578 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
580 (defmethod incompatible-ll-test-3 ((x integer)) x)
581 (remove-method #'incompatible-ll-test-3
582 (find-method #'incompatible-ll-test-3
584 (list (find-class 'integer))))
585 (assert (raises-error? (defmethod incompatible-ll-test-3 (x y) (list x y))))
588 ;;; Attempting to instantiate classes with forward references in their
589 ;;; CPL should signal errors (FIXME: of what type?)
590 (defclass never-finished-class (this-one-unfinished-too) ())
591 (multiple-value-bind (result error)
592 (ignore-errors (make-instance 'never-finished-class))
593 (assert (null result))
594 (assert (typep error 'error)))
595 (multiple-value-bind (result error)
596 (ignore-errors (make-instance 'this-one-unfinished-too))
597 (assert (null result))
598 (assert (typep error 'error)))
600 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
601 ;;; weren't for a while in sbcl-0.7.9.xx)
602 (defclass superclass-with-slot ()
603 ((a :allocation :class)))
604 (defclass subclass-for-class-allocation (superclass-with-slot) ())
605 (make-instance 'subclass-for-class-allocation)
607 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
608 ;;; resulting in failure in the following:
609 (defmethod call-next-method-lexical-args ((x integer))
611 (defmethod call-next-method-lexical-args :around ((x integer))
612 (let ((x (1+ x)))
613 (call-next-method)))
614 (assert (= (call-next-method-lexical-args 3) 3))
616 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
617 ;;; until 0.7.9.5x
618 (defvar *d-m-c-args-test* nil)
619 (define-method-combination progn-with-lock ()
620 ((methods ()))
621 (:arguments object)
622 `(unwind-protect
623 (progn (lock (object-lock ,object))
624 ,@(mapcar #'(lambda (method)
625 `(call-method ,method))
626 methods))
627 (unlock (object-lock ,object))))
628 (defun object-lock (obj)
629 (push "object-lock" *d-m-c-args-test*)
630 obj)
631 (defun unlock (obj)
632 (push "unlock" *d-m-c-args-test*)
633 obj)
634 (defun lock (obj)
635 (push "lock" *d-m-c-args-test*)
636 obj)
637 (defgeneric d-m-c-args-test (x)
638 (:method-combination progn-with-lock))
639 (defmethod d-m-c-args-test ((x symbol))
640 (push "primary" *d-m-c-args-test*))
641 (defmethod d-m-c-args-test ((x number))
642 (error "foo"))
643 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
644 (assert (equal *d-m-c-args-test*
645 '("unlock" "object-lock" "primary" "lock" "object-lock")))
646 (setf *d-m-c-args-test* nil)
647 (ignore-errors (d-m-c-args-test 1))
648 (assert (equal *d-m-c-args-test*
649 '("unlock" "object-lock" "lock" "object-lock")))
651 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
652 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
653 ;;; doesn't, but it does well enough to compile the following without
654 ;;; error (the problems remain in asking for a complete macroexpansion
655 ;;; of an arbitrary form).
656 (symbol-macrolet ((x 1))
657 (defmethod bug222 (z)
658 (macrolet ((frob (form) `(progn ,form ,x)))
659 (frob (print x)))))
660 (assert (= (bug222 t) 1))
662 ;;; also, a test case to guard against bogus environment hacking:
664 (eval-when (:compile-toplevel :load-toplevel :execute)
665 (setq bug222-b 3))
666 ;;; this should at the least compile:
667 (let ((bug222-b 1))
668 (defmethod bug222-b (z stream)
669 (macrolet ((frob (form) `(progn ,form ,bug222-b)))
670 (frob (format stream "~D~%" bug222-b)))))
671 ;;; and it would be nice (though not specified by ANSI) if the answer
672 ;;; were as follows:
673 (let ((x (make-string-output-stream)))
674 (let ((value (bug222-b t x)))
675 ;; not specified by ANSI
676 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
677 (assert (= value 3)))
678 ;; specified.
679 (assert (char= (char (get-output-stream-string x) 0) #\1)))
681 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
682 ;;; for invalid initargs where it should:
683 (defclass class234 () ())
684 (defclass subclass234 (class234) ())
685 (defvar *bug234* 0)
686 (defun bug-234 ()
687 (reinitialize-instance (make-instance 'class234) :dummy 0))
688 (defun subbug-234 ()
689 (reinitialize-instance (make-instance 'subclass234) :dummy 0))
690 (assert (raises-error? (bug-234) program-error))
691 (defmethod shared-initialize :after ((i class234) slots &key dummy)
692 (incf *bug234*))
693 (assert (typep (subbug-234) 'subclass234))
694 (assert (= *bug234*
695 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
698 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
699 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
700 (defclass class234-b1 () ())
701 (defclass class234-b2 (class234-b1) ())
702 (defvar *bug234-b* 0)
703 (defun bug234-b ()
704 (make-instance 'class234-b2))
705 (compile 'bug234-b)
706 (bug234-b)
707 (assert (= *bug234-b* 0))
708 (defmethod initialize-instance :before ((x class234-b1) &rest args)
709 (declare (ignore args))
710 (incf *bug234-b*))
711 (bug234-b)
712 (assert (= *bug234-b* 1))
714 ;;; we should be able to make classes with uninterned names:
715 (defclass #:class-with-uninterned-name () ())
717 ;;; SLOT-MISSING should be called when there are missing slots.
718 (defclass class-with-all-slots-missing () ())
719 (defmethod slot-missing (class (o class-with-all-slots-missing)
720 slot-name op
721 &optional new-value)
723 (assert (eq (slot-value (make-instance 'class-with-all-slots-missing) 'foo)
724 'slot-value))
725 (assert (eq (funcall (lambda (x) (slot-value x 'bar))
726 (make-instance 'class-with-all-slots-missing))
727 'slot-value))
728 (assert (eq (funcall (lambda (x) (setf (slot-value x 'baz) 'baz))
729 (make-instance 'class-with-all-slots-missing))
730 ;; SLOT-MISSING's value is specified to be ignored; we
731 ;; return NEW-VALUE.
732 'baz))
734 ;;; we should be able to specialize on anything that names a class.
735 (defclass name-for-class () ())
736 (defmethod something-that-specializes ((x name-for-class)) 1)
737 (setf (find-class 'other-name-for-class) (find-class 'name-for-class))
738 (defmethod something-that-specializes ((x other-name-for-class)) 2)
739 (assert (= (something-that-specializes (make-instance 'name-for-class)) 2))
740 (assert (= (something-that-specializes (make-instance 'other-name-for-class))
743 ;;; more forward referenced classes stuff
744 (defclass frc-1 (frc-2) ())
745 (assert (subtypep 'frc-1 (find-class 'frc-2)))
746 (assert (subtypep (find-class 'frc-1) 'frc-2))
747 (assert (not (subtypep (find-class 'frc-2) 'frc-1)))
748 (defclass frc-2 (frc-3) ((a :initarg :a)))
749 (assert (subtypep 'frc-1 (find-class 'frc-3)))
750 (defclass frc-3 () ())
751 (assert (typep (make-instance 'frc-1 :a 2) (find-class 'frc-1)))
752 (assert (typep (make-instance 'frc-2 :a 3) (find-class 'frc-2)))
754 ;;; check that we can define classes with two slots of different names
755 ;;; (even if it STYLE-WARNs).
756 (defclass odd-name-class ()
757 ((name :initarg :name)
758 (cl-user::name :initarg :name2)))
759 (let ((x (make-instance 'odd-name-class :name 1 :name2 2)))
760 (assert (= (slot-value x 'name) 1))
761 (assert (= (slot-value x 'cl-user::name) 2)))
763 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
764 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
765 (defstruct allocatable-structure a)
766 (assert (typep (allocate-instance (find-class 'allocatable-structure))
767 'allocatable-structure))
769 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
770 ;;; amazingly, calls to CPL would work a couple of times, and then
771 ;;; start returning NIL. A fix was found (relating to the
772 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
773 (defgeneric cpl (x)
774 (:method-combination list)
775 (:method list ((x broadcast-stream)) 'broadcast-stream)
776 (:method list ((x integer)) 'integer)
777 (:method list ((x number)) 'number)
778 (:method list ((x stream)) 'stream)
779 (:method list ((x structure-object)) 'structure-object))
780 (assert (equal (cpl 0) '(integer number)))
781 (assert (equal (cpl 0) '(integer number)))
782 (assert (equal (cpl 0) '(integer number)))
783 (assert (equal (cpl 0) '(integer number)))
784 (assert (equal (cpl 0) '(integer number)))
785 (assert (equal (cpl (make-broadcast-stream))
786 '(broadcast-stream stream structure-object)))
787 (assert (equal (cpl (make-broadcast-stream))
788 '(broadcast-stream stream structure-object)))
789 (assert (equal (cpl (make-broadcast-stream))
790 '(broadcast-stream stream structure-object)))
792 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
793 ;;; parameters shouldn't affect the arguments to the next method for a
794 ;;; no-argument call to CALL-NEXT-METHOD
795 (defgeneric cnm-assignment (x)
796 (:method (x) x)
797 (:method ((x integer)) (setq x 3)
798 (list x (call-next-method) (call-next-method x))))
799 (assert (equal (cnm-assignment 1) '(3 1 3)))
801 ;;; Bug reported by Istvan Marko 2003-07-09
802 (let ((class-name (gentemp)))
803 (loop for i from 1 to 9
804 for slot-name = (intern (format nil "X~D" i))
805 for initarg-name = (intern (format nil "X~D" i) :keyword)
806 collect `(,slot-name :initarg ,initarg-name) into slot-descs
807 append `(,initarg-name (list 0)) into default-initargs
808 finally (eval `(defclass ,class-name ()
809 (,@slot-descs)
810 (:default-initargs ,@default-initargs))))
811 (let ((f (compile nil `(lambda () (make-instance ',class-name)))))
812 (assert (typep (funcall f) class-name))))
814 ;;; bug 262: DEFMETHOD failed on a generic function without a lambda
815 ;;; list
816 (ensure-generic-function 'bug262)
817 (defmethod bug262 (x y)
818 (list x y))
819 (assert (equal (bug262 1 2) '(1 2)))
821 ;;; salex on #lisp 2003-10-13 reported that type declarations inside
822 ;;; WITH-SLOTS are too hairy to be checked
823 (defun ensure-no-notes (form)
824 (handler-case (compile nil `(lambda () ,form))
825 (sb-ext:compiler-note (c)
826 ;; FIXME: it would be better to check specifically for the "type
827 ;; is too hairy" note
828 (error c))))
829 (defvar *x*)
830 (ensure-no-notes '(with-slots (a) *x*
831 (declare (integer a))
833 (ensure-no-notes '(with-slots (a) *x*
834 (declare (integer a))
835 (declare (notinline slot-value))
838 ;;; from CLHS 7.6.5.1
839 (defclass character-class () ((char :initarg :char)))
840 (defclass picture-class () ((glyph :initarg :glyph)))
841 (defclass character-picture-class (character-class picture-class) ())
843 (defmethod width ((c character-class) &key font) font)
844 (defmethod width ((p picture-class) &key pixel-size) pixel-size)
846 (assert (raises-error?
847 (width (make-instance 'character-class :char #\Q)
848 :font 'baskerville :pixel-size 10)
849 program-error))
850 (assert (raises-error?
851 (width (make-instance 'picture-class :glyph #\Q)
852 :font 'baskerville :pixel-size 10)
853 program-error))
854 (assert (eq (width (make-instance 'character-picture-class :char #\Q)
855 :font 'baskerville :pixel-size 10)
856 'baskerville))
858 ;;; class redefinition shouldn't give any warnings, in the usual case
859 (defclass about-to-be-redefined () ((some-slot :accessor some-slot)))
860 (handler-bind ((warning #'error))
861 (defclass about-to-be-redefined () ((some-slot :accessor some-slot))))
863 ;;; attempts to add accessorish methods to generic functions with more
864 ;;; complex lambda lists should fail
865 (defgeneric accessoroid (object &key &allow-other-keys))
866 (assert (raises-error?
867 (defclass accessoroid-class () ((slot :accessor accessoroid)))
868 program-error))
870 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
871 (defclass shared-slot-and-redefinition ()
872 ((size :initarg :size :initform 1 :allocation :class)))
873 (let ((i (make-instance 'shared-slot-and-redefinition)))
874 (defclass shared-slot-and-redefinition ()
875 ((size :initarg :size :initform 2 :allocation :class)))
876 (assert (= (slot-value i 'size) 1)))
878 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
879 (defclass superclass-born-to-be-obsoleted () (a))
880 (defclass subclass-born-to-be-obsoleted (superclass-born-to-be-obsoleted) ())
881 (defparameter *born-to-be-obsoleted*
882 (make-instance 'subclass-born-to-be-obsoleted))
883 (defparameter *born-to-be-obsoleted-obsoleted* nil)
884 (defmethod update-instance-for-redefined-class
885 ((o subclass-born-to-be-obsoleted) a d pl &key)
886 (setf *born-to-be-obsoleted-obsoleted* t))
887 (make-instances-obsolete 'superclass-born-to-be-obsoleted)
888 (slot-boundp *born-to-be-obsoleted* 'a)
889 (assert *born-to-be-obsoleted-obsoleted*)
891 ;;; additional test suggested by Bruno Haible sbcl-devel 2004-04-21
892 (defclass super-super-obsoleted () (a))
893 (defclass super-obsoleted-1 (super-super-obsoleted) ())
894 (defclass super-obsoleted-2 (super-super-obsoleted) ())
895 (defclass obsoleted (super-obsoleted-1 super-obsoleted-2) ())
896 (defparameter *obsoleted* (make-instance 'obsoleted))
897 (defparameter *obsoleted-counter* 0)
898 (defmethod update-instance-for-redefined-class ((o obsoleted) a d pl &key)
899 (incf *obsoleted-counter*))
900 (make-instances-obsolete 'super-super-obsoleted)
901 (slot-boundp *obsoleted* 'a)
902 (assert (= *obsoleted-counter* 1))
904 ;;; yet another MAKE-INSTANCES-OBSOLETE test, this time from Nikodemus
905 ;;; Siivola. Not all methods for accessing slots are created equal...
906 (defclass yet-another-obsoletion-super () ((obs :accessor obs-of :initform 0)))
907 (defclass yet-another-obsoletion-sub (yet-another-obsoletion-super) ())
908 (defmethod shared-initialize :after ((i yet-another-obsoletion-super)
909 slots &rest init)
910 (incf (obs-of i)))
912 (defvar *yao-super* (make-instance 'yet-another-obsoletion-super))
913 (defvar *yao-sub* (make-instance 'yet-another-obsoletion-sub))
915 (assert (= (obs-of *yao-super*) 1))
916 (assert (= (obs-of *yao-sub*) 1))
917 (make-instances-obsolete 'yet-another-obsoletion-super)
918 (assert (= (obs-of *yao-sub*) 2))
919 (assert (= (obs-of *yao-super*) 2))
920 (make-instances-obsolete 'yet-another-obsoletion-super)
921 (assert (= (obs-of *yao-super*) 3))
922 (assert (= (obs-of *yao-sub*) 3))
923 (assert (= (slot-value *yao-super* 'obs) 3))
924 (assert (= (slot-value *yao-sub* 'obs) 3))
926 ;;; one more MIO test: variable slot names
927 (defclass mio () ((x :initform 42)))
928 (defvar *mio-slot* 'x)
929 (defparameter *mio-counter* 0)
930 (defmethod update-instance-for-redefined-class ((instance mio) new old plist &key)
931 (incf *mio-counter*))
933 (let ((x (make-instance 'mio)))
934 (make-instances-obsolete 'mio)
935 (slot-value x *mio-slot*))
937 (let ((x (make-instance 'mio)))
938 (make-instances-obsolete 'mio)
939 (setf (slot-value x *mio-slot*) 13))
941 (let ((x (make-instance 'mio)))
942 (make-instances-obsolete 'mio)
943 (slot-boundp x *mio-slot*))
945 (let ((x (make-instance 'mio)))
946 (make-instances-obsolete 'mio)
947 (slot-makunbound x *mio-slot*))
949 (assert (= 4 *mio-counter*))
951 ;;; shared -> local slot transfers of inherited slots, reported by
952 ;;; Bruno Haible
953 (let (i)
954 (defclass super-with-magic-slot ()
955 ((magic :initarg :size :initform 1 :allocation :class)))
956 (defclass sub-of-super-with-magic-slot (super-with-magic-slot) ())
957 (setq i (make-instance 'sub-of-super-with-magic-slot))
958 (defclass super-with-magic-slot ()
959 ((magic :initarg :size :initform 2)))
960 (assert (= 1 (slot-value i 'magic))))
962 ;;; MAKE-INSTANCES-OBSOLETE return values
963 (defclass one-more-to-obsolete () ())
964 (assert (eq 'one-more-to-obsolete
965 (make-instances-obsolete 'one-more-to-obsolete)))
966 (assert (eq (find-class 'one-more-to-obsolete)
967 (make-instances-obsolete (find-class 'one-more-to-obsolete))))
969 ;;; Sensible error instead of a BUG. Reported by Thomas Burdick.
970 (multiple-value-bind (value err)
971 (ignore-errors
972 (defclass slot-def-with-duplicate-accessors ()
973 ((slot :writer get-slot :reader get-slot))))
974 (assert (typep err 'error))
975 (assert (not (typep err 'sb-int:bug))))
977 ;;; BUG 321: errors in parsing DEFINE-METHOD-COMBINATION arguments
978 ;;; lambda lists.
980 (define-method-combination w-args ()
981 ((method-list *))
982 (:arguments arg1 arg2 &aux (extra :extra))
983 `(progn ,@(mapcar (lambda (method) `(call-method ,method)) method-list)))
984 (defgeneric mc-test-w-args (p1 p2 s)
985 (:method-combination w-args)
986 (:method ((p1 number) (p2 t) s)
987 (vector-push-extend (list 'number p1 p2) s))
988 (:method ((p1 string) (p2 t) s)
989 (vector-push-extend (list 'string p1 p2) s))
990 (:method ((p1 t) (p2 t) s) (vector-push-extend (list t p1 p2) s)))
991 (let ((v (make-array 0 :adjustable t :fill-pointer t)))
992 (assert (= (mc-test-w-args 1 2 v) 1))
993 (assert (equal (aref v 0) '(number 1 2)))
994 (assert (equal (aref v 1) '(t 1 2))))
996 ;;; BUG 276: declarations and mutation.
997 (defmethod fee ((x fixnum))
998 (setq x (/ x 2))
1000 (assert (= (fee 1) 1/2))
1001 (defmethod fum ((x fixnum))
1002 (setf x (/ x 2))
1004 (assert (= (fum 3) 3/2))
1005 (defmethod fii ((x fixnum))
1006 (declare (special x))
1007 (setf x (/ x 2))
1009 (assert (= (fii 1) 1/2))
1010 (defvar *faa*)
1011 (defmethod faa ((*faa* string-stream))
1012 (setq *faa* (make-broadcast-stream *faa*))
1013 (write-line "Break, you sucker!" *faa*)
1014 'ok)
1015 (assert (eq 'ok (faa (make-string-output-stream))))
1016 (defmethod fex ((x fixnum) (y fixnum))
1017 (multiple-value-setq (x y) (values (/ x y) (/ y x)))
1018 (list x y))
1019 (assert (equal (fex 5 3) '(5/3 3/5)))
1021 ;;; Bug reported by Zach Beane; incorrect return of (function
1022 ;;; ',fun-name) in defgeneric
1023 (assert
1024 (typep (funcall (compile nil
1025 '(lambda () (flet ((nonsense () nil))
1026 (defgeneric nonsense ())))))
1027 'generic-function))
1029 (assert
1030 (typep (funcall (compile nil
1031 '(lambda () (flet ((nonsense-2 () nil))
1032 (defgeneric nonsense-2 ()
1033 (:method () t))))))
1034 'generic-function))
1036 ;;; bug reported by Bruno Haible: (setf find-class) using a
1037 ;;; forward-referenced class
1038 (defclass fr-sub (fr-super) ())
1039 (setf (find-class 'fr-alt) (find-class 'fr-super))
1040 (assert (eq (find-class 'fr-alt) (find-class 'fr-super)))
1043 ;;; ANSI Figure 4-8: all defined classes. Check that we can define
1044 ;;; methods on all of these.
1045 (progn
1046 (defgeneric method-for-defined-classes (x))
1047 (dolist (c '(arithmetic-error
1048 generic-function simple-error array hash-table
1049 simple-type-error
1050 bit-vector integer simple-warning
1051 broadcast-stream list standard-class
1052 built-in-class logical-pathname standard-generic-function
1053 cell-error method standard-method
1054 character method-combination standard-object
1055 class null storage-condition
1056 complex number stream
1057 concatenated-stream package stream-error
1058 condition package-error string
1059 cons parse-error string-stream
1060 control-error pathname structure-class
1061 division-by-zero print-not-readable structure-object
1062 echo-stream program-error style-warning
1063 end-of-file random-state symbol
1064 error ratio synonym-stream
1065 file-error rational t
1066 file-stream reader-error two-way-stream
1067 float readtable type-error
1068 floating-point-inexact real unbound-slot
1069 floating-point-invalid-operation restart unbound-variable
1070 floating-point-overflow sequence undefined-function
1071 floating-point-underflow serious-condition vector
1072 function simple-condition warning))
1073 (eval `(defmethod method-for-defined-classes ((x ,c)) (princ x))))
1074 (assert (string= (with-output-to-string (*standard-output*)
1075 (method-for-defined-classes #\3))
1076 "3")))
1080 ;;; When class definition does not complete due to a bad accessor
1081 ;;; name, do not cause an error when a new accessor name is provided
1082 ;;; during class redefinition
1084 (defun existing-name (object)
1085 (list object))
1087 (assert (raises-error? (defclass redefinition-of-accessor-class ()
1088 ((slot :accessor existing-name)))))
1090 (defclass redefinition-of-accessor-class ()
1091 ((slot :accessor new-name)))
1095 (load "package-ctor-bug.lisp")
1096 (assert (= (package-ctor-bug:test) 3))
1097 (delete-package "PACKAGE-CTOR-BUG")
1098 (load "package-ctor-bug.lisp")
1099 (assert (= (package-ctor-bug:test) 3))
1101 (with-test (:name (:defmethod (setf find-class) integer))
1102 (mapcar #'eval
1104 (deftype defined-type () 'integer)
1105 (assert (raises-error?
1106 (defmethod method-on-defined-type ((x defined-type)) x)))
1107 (deftype defined-type-and-class () 'integer)
1108 (setf (find-class 'defined-type-and-class) (find-class 'integer))
1109 (defmethod method-on-defined-type-and-class
1110 ((x defined-type-and-class))
1111 (1+ x))
1112 (assert (= (method-on-defined-type-and-class 3) 4)))))
1114 ;; bug 281
1115 (let ((sb-pcl::*max-emf-precomputation-methods* 0))
1116 (eval '(defgeneric bug-281 (x)
1117 (:method-combination +)
1118 (:method ((x symbol)) 1)
1119 (:method + ((x number)) x)))
1120 (assert (= 1 (bug-281 1)))
1121 (assert (= 4.2 (bug-281 4.2)))
1122 (multiple-value-bind (val err) (ignore-errors (bug-281 'symbol))
1123 (assert (not val))
1124 (assert (typep err 'error))))
1126 ;;; RESTART-CASE and CALL-METHOD
1128 ;;; from Bruno Haible
1130 (defun rc-cm/prompt-for-new-values ()
1131 (format *debug-io* "~&New values: ")
1132 (finish-output *debug-io*)
1133 (list (read *debug-io*)))
1135 (defun rc-cm/add-method-restarts (form method)
1136 (let ((block (gensym))
1137 (tag (gensym)))
1138 `(block ,block
1139 (tagbody
1140 ,tag
1141 (return-from ,block
1142 (restart-case ,form
1143 (method-redo ()
1144 :report (lambda (stream)
1145 (format stream "Try calling ~S again." ,method))
1146 (go ,tag))
1147 (method-return (l)
1148 :report (lambda (stream)
1149 (format stream "Specify return values for ~S call."
1150 ,method))
1151 :interactive (lambda () (rc-cm/prompt-for-new-values))
1152 (return-from ,block (values-list l)))))))))
1154 (defun rc-cm/convert-effective-method (efm)
1155 (if (consp efm)
1156 (if (eq (car efm) 'call-method)
1157 (let ((method-list (third efm)))
1158 (if (or (typep (first method-list) 'method) (rest method-list))
1159 ;; Reduce the case of multiple methods to a single one.
1160 ;; Make the call to the next-method explicit.
1161 (rc-cm/convert-effective-method
1162 `(call-method ,(second efm)
1163 ((make-method
1164 (call-method ,(first method-list) ,(rest method-list))))))
1165 ;; Now the case of at most one method.
1166 (if (typep (second efm) 'method)
1167 ;; Wrap the method call in a RESTART-CASE.
1168 (rc-cm/add-method-restarts
1169 (cons (rc-cm/convert-effective-method (car efm))
1170 (rc-cm/convert-effective-method (cdr efm)))
1171 (second efm))
1172 ;; Normal recursive processing.
1173 (cons (rc-cm/convert-effective-method (car efm))
1174 (rc-cm/convert-effective-method (cdr efm))))))
1175 (cons (rc-cm/convert-effective-method (car efm))
1176 (rc-cm/convert-effective-method (cdr efm))))
1177 efm))
1179 (define-method-combination standard-with-restarts ()
1180 ((around (:around))
1181 (before (:before))
1182 (primary () :required t)
1183 (after (:after)))
1184 (flet ((call-methods-sequentially (methods)
1185 (mapcar #'(lambda (method)
1186 `(call-method ,method))
1187 methods)))
1188 (let ((form (if (or before after (rest primary))
1189 `(multiple-value-prog1
1190 (progn
1191 ,@(call-methods-sequentially before)
1192 (call-method ,(first primary) ,(rest primary)))
1193 ,@(call-methods-sequentially (reverse after)))
1194 `(call-method ,(first primary)))))
1195 (when around
1196 (setq form
1197 `(call-method ,(first around)
1198 (,@(rest around) (make-method ,form)))))
1199 (rc-cm/convert-effective-method form))))
1201 (defgeneric rc-cm/testgf16 (x)
1202 (:method-combination standard-with-restarts))
1203 (defclass rc-cm/testclass16a () ())
1204 (defclass rc-cm/testclass16b (rc-cm/testclass16a) ())
1205 (defclass rc-cm/testclass16c (rc-cm/testclass16a) ())
1206 (defclass rc-cm/testclass16d (rc-cm/testclass16b rc-cm/testclass16c) ())
1207 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16a))
1208 (list 'a
1209 (not (null (find-restart 'method-redo)))
1210 (not (null (find-restart 'method-return)))))
1211 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16b))
1212 (cons 'b (call-next-method)))
1213 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16c))
1214 (cons 'c (call-next-method)))
1215 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16d))
1216 (cons 'd (call-next-method)))
1217 (assert (equal (rc-cm/testgf16 (make-instance 'rc-cm/testclass16d))
1218 '(d b c a t t)))
1220 ;;; test case from Gerd Moellmann
1221 (define-method-combination r-c/c-m-1 ()
1222 ((primary () :required t))
1223 `(restart-case (call-method ,(first primary))
1224 ()))
1226 (defgeneric r-c/c-m-1-gf ()
1227 (:method-combination r-c/c-m-1)
1228 (:method () nil))
1230 (assert (null (r-c/c-m-1-gf)))
1232 (handler-bind ((warning #'error))
1233 (eval '(defclass class-for-ctor/class-slot ()
1234 ((class-slot :initarg :class-slot :allocation :class))))
1235 (eval '(let ((c1 (make-instance 'class-for-ctor/class-slot))
1236 (c2 (make-instance 'class-for-ctor/class-slot :class-slot 1)))
1237 (assert (equal (list (slot-value c1 'class-slot)
1238 (slot-value c2 'class-slot))
1239 (list 1 1))))))
1241 ;;; tests of ctors on anonymous classes
1242 (defparameter *unnamed* (defclass ctor-unnamed-literal-class () ()))
1243 (setf (class-name *unnamed*) nil)
1244 (setf (find-class 'ctor-unnamed-literal-class) nil)
1245 (defparameter *unnamed2* (defclass ctor-unnamed-literal-class2 () ()))
1246 (defun ctor-unnamed-literal-class ()
1247 (make-instance '#.*unnamed*))
1248 (compile 'ctor-unnamed-literal-class)
1249 (defun ctor-unnamed-literal-class2 ()
1250 (make-instance '#.(find-class 'ctor-unnamed-literal-class2)))
1251 (compile 'ctor-unnamed-literal-class2)
1252 (defun ctor-unnamed-literal-class2/symbol ()
1253 (make-instance 'ctor-unnamed-literal-class2))
1254 (compile 'ctor-unnamed-literal-class2/symbol)
1255 (setf (class-name *unnamed2*) nil)
1256 (setf (find-class 'ctor-unnamed-literal-class2) nil)
1257 (with-test (:name (:ctor :unnamed-before))
1258 (assert (typep (ctor-unnamed-literal-class) *unnamed*)))
1259 (with-test (:name (:ctor :unnamed-after))
1260 (assert (typep (ctor-unnamed-literal-class2) *unnamed2*)))
1261 (with-test (:name (:ctor :unnamed-after/symbol))
1262 (assert (raises-error? (ctor-unnamed-literal-class2/symbol))))
1264 ;;; classes with slot types shouldn't break if the types don't name
1265 ;;; classes (bug #391)
1266 (defclass slot-type-superclass () ((slot :type fixnum)))
1267 (defclass slot-type-subclass (slot-type-superclass)
1268 ((slot :type (integer 1 5))))
1269 (let ((instance (make-instance 'slot-type-subclass)))
1270 (setf (slot-value instance 'slot) 3))
1272 ;;; ctors where there's a non-standard SHARED-INITIALIZE method and an
1273 ;;; initarg which isn't self-evaluating (kpreid on #lisp 2006-01-29)
1274 (defclass kpreid-enode ()
1275 ((slot :initarg not-a-keyword)))
1276 (defmethod shared-initialize ((o kpreid-enode) slots &key &allow-other-keys)
1277 (call-next-method))
1278 (defun make-kpreid-enode ()
1279 (make-instance 'kpreid-enode 'not-a-keyword 3))
1280 (with-test (:name (:ctor :non-keyword-initarg))
1281 (let ((x (make-kpreid-enode))
1282 (y (make-kpreid-enode)))
1283 (= (slot-value x 'slot) (slot-value y 'slot))))
1285 ;;; defining a class hierarchy shouldn't lead to spurious classoid
1286 ;;; errors on TYPEP questions (reported by Tim Moore on #lisp
1287 ;;; 2006-03-10)
1288 (defclass backwards-2 (backwards-1) (a b))
1289 (defclass backwards-3 (backwards-2) ())
1290 (defun typep-backwards-3 (x)
1291 (typep x 'backwards-3))
1292 (defclass backwards-1 () (a b))
1293 (assert (not (typep-backwards-3 1)))
1294 (assert (not (typep-backwards-3 (make-instance 'backwards-2))))
1295 (assert (typep-backwards-3 (make-instance 'backwards-3)))
1297 (defgeneric remove-method-1 (x)
1298 (:method ((x integer)) (1+ x)))
1299 (defgeneric remove-method-2 (x)
1300 (:method ((x integer)) (1- x)))
1301 (assert (eq #'remove-method-1
1302 (remove-method #'remove-method-1
1303 (find-method #'remove-method-2
1305 (list (find-class 'integer))))))
1306 (assert (= (remove-method-1 3) 4))
1307 (assert (= (remove-method-2 3) 2))
1309 ;;; ANSI doesn't require these restarts, but now that we have them we
1310 ;;; better test them too.
1311 (defclass slot-unbound-restart-test () ((x)))
1312 (let ((test (make-instance 'slot-unbound-restart-test)))
1313 (assert (not (slot-boundp test 'x)))
1314 (assert (= 42 (handler-bind ((unbound-slot (lambda (c) (use-value 42 c))))
1315 (slot-value test 'x))))
1316 (assert (not (slot-boundp test 'x)))
1317 (assert (= 13 (handler-bind ((unbound-slot (lambda (c) (store-value 13 c))))
1318 (slot-value test 'x))))
1319 (assert (= 13 (slot-value test 'x))))
1321 ;;; Using class instances as specializers, reported by Pascal Costanza, ref CLHS 7.6.2
1322 (defclass class-as-specializer-test ()
1324 (eval `(defmethod class-as-specializer-test1 ((x ,(find-class 'class-as-specializer-test)))
1325 'foo))
1326 (assert (eq 'foo (class-as-specializer-test1 (make-instance 'class-as-specializer-test))))
1327 (funcall (compile nil `(lambda ()
1328 (defmethod class-as-specializer-test2 ((x ,(find-class 'class-as-specializer-test)))
1329 'bar))))
1330 (assert (eq 'bar (class-as-specializer-test2 (make-instance 'class-as-specializer-test))))
1332 ;;; CHANGE-CLASS and tricky allocation.
1333 (defclass foo-to-be-changed ()
1334 ((a :allocation :class :initform 1)))
1335 (defclass bar-to-be-changed (foo-to-be-changed) ())
1336 (defvar *bar-to-be-changed* (make-instance 'bar-to-be-changed))
1337 (defclass baz-to-be-changed ()
1338 ((a :allocation :instance :initform 2)))
1339 (change-class *bar-to-be-changed* 'baz-to-be-changed)
1340 (assert (= (slot-value *bar-to-be-changed* 'a) 1))
1342 ;;; proper name and class redefinition
1343 (defvar *to-be-renamed1* (defclass to-be-renamed1 () ()))
1344 (defvar *to-be-renamed2* (defclass to-be-renamed2 () ()))
1345 (setf (find-class 'to-be-renamed1) (find-class 'to-be-renamed2))
1346 (defvar *renamed1* (defclass to-be-renamed1 () ()))
1347 (assert (not (eq *to-be-renamed1* *to-be-renamed2*)))
1348 (assert (not (eq *to-be-renamed1* *renamed1*)))
1349 (assert (not (eq *to-be-renamed2* *renamed1*)))
1351 ;;; CLASS-NAME (and various other standardized generic functions) have
1352 ;;; their effective methods precomputed; in the process of rearranging
1353 ;;; (SETF FIND-CLASS) and FINALIZE-INHERITANCE, this broke.
1354 (defclass class-with-odd-class-name-method ()
1355 ((a :accessor class-name)))
1357 ;;; another case where precomputing (this time on PRINT-OBJECT) and
1358 ;;; lazily-finalized classes caused problems. (report from James Y
1359 ;;; Knight sbcl-devel 20-07-2006)
1361 (defclass base-print-object () ())
1362 ;;; this has the side-effect of finalizing BASE-PRINT-OBJECT, and
1363 ;;; additionally the second specializer (STREAM) changes the cache
1364 ;;; structure to require two keys, not just one.
1365 (defmethod print-object ((o base-print-object) (s stream))
1366 nil)
1368 ;;; unfinalized as yet
1369 (defclass sub-print-object (base-print-object) ())
1370 ;;; the accessor causes an eager finalization
1371 (defclass subsub-print-object (sub-print-object)
1372 ((a :accessor a)))
1374 ;;; triggers a discriminating function (and so cache) recomputation.
1375 ;;; The method on BASE-PRINT-OBJECT will cause the system to attempt
1376 ;;; to fill the cache for all subclasses of BASE-PRINT-OBJECT which
1377 ;;; have valid wrappers; however, in the course of doing so, the
1378 ;;; SUB-PRINT-OBJECT class gets finalized, which invalidates the
1379 ;;; SUBSUB-PRINT-OBJECT wrapper; if an invalid wrapper gets into a
1380 ;;; cache with more than one key, then failure ensues.
1381 (reinitialize-instance #'print-object)
1383 ;;; bug in long-form method combination: if there's an applicable
1384 ;;; method not part of any method group, we need to call
1385 ;;; INVALID-METHOD-ERROR. (MC27 test case from Bruno Haible)
1386 (define-method-combination mc27 ()
1387 ((normal ())
1388 (ignored (:ignore :unused)))
1389 `(list 'result
1390 ,@(mapcar #'(lambda (method) `(call-method ,method)) normal)))
1391 (defgeneric test-mc27 (x)
1392 (:method-combination mc27)
1393 (:method :ignore ((x number)) (/ 0)))
1394 (assert (raises-error? (test-mc27 7)))
1396 (define-method-combination mc27prime ()
1397 ((normal ())
1398 (ignored (:ignore)))
1399 `(list 'result ,@(mapcar (lambda (m) `(call-method ,m)) normal)))
1400 (defgeneric test-mc27prime (x)
1401 (:method-combination mc27prime)
1402 (:method :ignore ((x number)) (/ 0)))
1403 (assert (equal '(result) (test-mc27prime 3)))
1404 (assert (raises-error? (test-mc27 t))) ; still no-applicable-method
1406 ;;; more invalid wrappers. This time for a long-standing bug in the
1407 ;;; compiler's expansion for TYPEP on various class-like things, with
1408 ;;; user-visible consequences.
1409 (defclass obsolete-again () ())
1410 (defvar *obsolete-again* (make-instance 'obsolete-again))
1411 (defvar *obsolete-again-hash* (sxhash *obsolete-again*))
1412 (make-instances-obsolete (find-class 'obsolete-again))
1413 (assert (not (streamp *obsolete-again*)))
1414 (make-instances-obsolete (find-class 'obsolete-again))
1415 (assert (= (sxhash *obsolete-again*) *obsolete-again-hash*))
1416 (compile (defun is-a-structure-object-p (x) (typep x 'structure-object)))
1417 (make-instances-obsolete (find-class 'obsolete-again))
1418 (assert (not (is-a-structure-object-p *obsolete-again*)))
1420 ;;; overeager optimization of slot-valuish things
1421 (defclass listoid ()
1422 ((caroid :initarg :caroid)
1423 (cdroid :initarg :cdroid :initform nil)))
1424 (defmethod lengthoid ((x listoid))
1425 (let ((result 0))
1426 (loop until (null x)
1427 do (incf result) (setq x (slot-value x 'cdroid)))
1428 result))
1429 (with-test (:name ((:setq :method-parameter) slot-value))
1430 (assert (= (lengthoid (make-instance 'listoid)) 1))
1431 (assert (= (lengthoid
1432 (make-instance 'listoid :cdroid
1433 (make-instance 'listoid :cdroid
1434 (make-instance 'listoid))))
1435 3)))
1439 ;;;; Tests for argument parsing in fast-method-functions.
1441 (defvar *foo* 0)
1443 (eval-when (:compile-toplevel :load-toplevel :execute)
1444 (setf (symbol-value 'a) 'invalid))
1446 (defmacro test1 (lambda-list values args &key declarations cnm)
1447 `(progn
1448 (fmakunbound 'll-method)
1449 (fmakunbound 'll-function)
1450 (defmethod ll-method ,lambda-list
1451 ,@declarations
1452 ,@(when cnm
1453 `((when nil (call-next-method))))
1454 (list ,@values))
1455 (defun ll-function ,lambda-list
1456 ,@declarations
1457 (list ,@values))
1458 (dotimes (i 2)
1459 (assert (equal (ll-method ,@args)
1460 (ll-function ,@args))))))
1462 (defmacro test (&rest args)
1463 `(progn
1464 (test1 ,@args :cnm nil)
1465 (test1 ,@args :cnm t)))
1467 ;; Just plain arguments
1469 (test (a) (a) (1))
1470 (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))
1472 (test (*foo*) (*foo* (symbol-value '*foo*)) (1))
1474 (test (a) (a (symbol-value 'a)) (1)
1475 :declarations ((declare (special a))))
1477 ;; Optionals
1479 (test (a &optional b c) (a b c) (1))
1480 (test (a &optional b c) (a b c) (1 2))
1481 (test (a &optional b c) (a b c) (1 2 3))
1483 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1))
1484 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2))
1485 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2 3))
1487 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) ())
1488 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) (1))
1490 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) ())
1491 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) (1))
1493 (test (&optional a) (a (symbol-value 'a)) ()
1494 :declarations ((declare (special a))))
1495 (test (&optional a) (a (symbol-value 'a)) (1)
1496 :declarations ((declare (special a))))
1498 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) ()
1499 :declarations ((declare (special a))))
1500 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) (1)
1501 :declarations ((declare (special a))))
1503 (defparameter *count* 0)
1505 (test (&optional (a (incf *count*)) (b (incf *count*)))
1506 (a b *count* (setf *count* 0))
1509 ;; Keywords with some &RESTs thrown in
1511 (dolist (args '((1)
1512 (1 :b 2)
1513 (1 :c 3)
1514 (1 :b 2 :c 3)
1515 (1 :c 3 :b 2)
1516 (1 :c 3 :c 1 :b 2 :b 4)))
1517 (eval `(test (a &key b c) (a b c) ,args))
1518 (eval `(test (a &key (b 'b b-p) (c 'c c-p))
1519 (a b c b-p c-p)
1520 ,args))
1521 (eval `(test (a &rest rest &key (b 'b b-p) (c 'c c-p))
1522 (a b c b-p c-p rest)
1523 ,args))
1524 (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1525 (a b c b-p c-p *foo* (symbol-value '*foo*))
1526 ,args))
1527 (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1528 (a b c b-p c-p *foo* (symbol-value '*foo*))
1529 ,args
1530 :declarations ((declare (special b-p))))))
1532 (dolist (args '(()
1533 (:*foo* 1)
1534 (:*foo* 1 :*foo* 2)))
1535 (eval `(test (&key *foo*) (*foo* (symbol-value '*foo*)) ,args))
1536 (eval `(test (&key (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p)
1537 ,args))
1538 (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1539 ,args))
1540 (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1541 ,args
1542 :declarations ((declare (special a))))))
1544 (defparameter *count* 0)
1546 (test (&key (a (incf *count*)) (b (incf *count*)))
1547 (a b *count* (setf *count* 0))
1550 (test (&key a b &allow-other-keys) (a b) (:a 1 :b 2 :c 3))
1552 (defmethod clim-style-lambda-list-test (a b &optional c d &key x y)
1553 (list a b c d x y))
1555 (clim-style-lambda-list-test 1 2)
1557 (setf *count* 0)
1559 (test (&aux (a (incf *count*)) (b (incf *count*)))
1560 (a b *count* (setf *count* 0))
1563 ;;;; long-form method combination with &rest in :arguments
1564 ;;;; (this had a bug what with fixed in 1.0.4.something)
1565 (define-method-combination long-form-with-&rest ()
1566 ((methods *))
1567 (:arguments x &rest others)
1568 `(progn
1569 ,@(mapcar (lambda (method)
1570 `(call-method ,method))
1571 methods)
1572 (list ,x (length ,others))))
1574 (defgeneric test-long-form-with-&rest (x &rest others)
1575 (:method-combination long-form-with-&rest))
1577 (defmethod test-long-form-with-&rest (x &rest others)
1578 nil)
1580 (assert (equal '(:foo 13)
1581 (apply #'test-long-form-with-&rest :foo (make-list 13))))
1583 ;;;; slot-missing for non-standard classes on SLOT-VALUE
1584 ;;;;
1585 ;;;; FIXME: This is arguably not right, actually: CLHS seems to say
1586 ;;;; we should just signal an error at least for built-in classes, but
1587 ;;;; for a while we were hitting NO-APPLICABLE-METHOD, which is definitely
1588 ;;;; wrong -- so test this for now at least.
1590 (defvar *magic-symbol* (gensym "MAGIC"))
1592 (set *magic-symbol* 42)
1594 (defmethod slot-missing (class instance (slot-name (eql *magic-symbol*)) op
1595 &optional new)
1596 (if (eq 'setf op)
1597 (setf (symbol-value *magic-symbol*) new)
1598 (symbol-value *magic-symbol*)))
1600 (assert (eql 42 (slot-value (cons t t) *magic-symbol*)))
1601 (assert (eql 13 (setf (slot-value 123 *magic-symbol*) 13)))
1602 (assert (eql 13 (slot-value 'foobar *magic-symbol*)))
1604 ;;;; Built-in structure and condition layouts should have NIL in
1605 ;;;; LAYOUT-FOR-STD-CLASS-P, and classes should have T.
1607 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'warning))))
1608 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'hash-table))))
1609 (assert (eq t (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'standard-object))))
1611 ;;;; bug 402: PCL used to warn about non-standard declarations
1612 (declaim (declaration bug-402-d))
1613 (defgeneric bug-402-gf (x))
1614 (with-test (:name :bug-402)
1615 (handler-bind ((warning #'error))
1616 (eval '(defmethod bug-402-gf (x)
1617 (declare (bug-402-d x))
1618 x))))
1620 ;;;; non-keyword :default-initargs + :before method on shared initialize
1621 ;;;; interacted badly with CTOR optimizations
1622 (defclass ctor-default-initarg-problem ()
1623 ((slot :initarg slotto))
1624 (:default-initargs slotto 123))
1625 (defmethod shared-initialize :before ((instance ctor-default-initarg-problem) slot-names &rest initargs)
1626 (format t "~&Rock on: ~A~%" initargs))
1627 (defun provoke-ctor-default-initarg-problem ()
1628 (make-instance 'ctor-default-initarg-problem))
1629 (handler-bind ((warning #'error))
1630 (assert (= 123 (slot-value (provoke-ctor-default-initarg-problem) 'slot))))
1632 ;;;; discriminating net on streams used to generate code deletion notes on
1633 ;;;; first call
1634 (defgeneric stream-fd (stream direction))
1635 (defmethod stream-fd ((stream sb-sys:fd-stream) direction)
1636 (declare (ignore direction))
1637 (sb-sys:fd-stream-fd stream))
1638 (defmethod stream-fd ((stream synonym-stream) direction)
1639 (stream-fd (symbol-value (synonym-stream-symbol stream)) direction))
1640 (defmethod stream-fd ((stream two-way-stream) direction)
1641 (ecase direction
1642 (:input
1643 (stream-fd
1644 (two-way-stream-input-stream stream) direction))
1645 (:output
1646 (stream-fd
1647 (two-way-stream-output-stream stream) direction))))
1648 (with-test (:name (:discriminating-name :code-deletion-note))
1649 (handler-bind ((compiler-note #'error))
1650 (stream-fd sb-sys:*stdin* :output)
1651 (stream-fd sb-sys:*stdin* :output)))
1653 (with-test (:name :bug-380)
1654 (defclass bug-380 ()
1655 ((slot :accessor bug380-slot)))
1656 (fmakunbound 'foo-slot)
1657 (defgeneric foo-slot (x y z))
1658 (defclass foo ()
1659 ((slot :accessor foo-slot-value))))
1661 ;;; SET and (SETF SYMBOL-VALUE) used to confuse permuation vector
1662 ;;; optimizations
1663 (defclass fih ()
1664 ((x :initform :fih)))
1665 (defclass fah ()
1666 ((x :initform :fah)))
1667 (declaim (special *fih*))
1668 (defmethod fihfah ((*fih* fih))
1669 (set '*fih* (make-instance 'fah))
1670 (list (slot-value *fih* 'x)
1671 (eval '(slot-value *fih* 'x))))
1672 (defmethod fihfah ((fah fah))
1673 (declare (special fah))
1674 (set 'fah (make-instance 'fih))
1675 (list (slot-value fah 'x)
1676 (eval '(slot-value fah 'x))))
1677 (with-test (:name :set-of-a-method-specializer)
1678 (assert (equal '(:fah :fah) (fihfah (make-instance 'fih))))
1679 (assert (equal '(:fih :fih) (fihfah (make-instance 'fah)))))
1681 (defmethod no-implicit-declarations-for-local-specials ((faax double-float))
1682 (declare (special faax))
1683 (set 'faax (when (< faax 0) (- faax)))
1684 faax)
1685 (with-test (:name :no-implicit-declarations-for-local-specials)
1686 (assert (not (no-implicit-declarations-for-local-specials 1.0d0))))
1688 (defstruct bug-357-a
1689 slot1
1690 (slot2 t)
1691 (slot3 (coerce pi 'single-float) :type single-float))
1692 (defclass bug-357-b (bug-357-a)
1693 ((slot2 :initform 't2)
1694 (slot4 :initform -44)
1695 (slot5)
1696 (slot6 :initform t)
1697 (slot7 :initform (floor (* pi pi)))
1698 (slot8 :initform 88))
1699 (:metaclass structure-class))
1700 (defstruct (bug-357-c (:include bug-357-b (slot8 -88) (slot5 :ok)))
1701 slot9
1702 (slot10 t)
1703 (slot11 (floor (exp 3))))
1704 (with-test (:name :bug-357)
1705 (flet ((slots (x)
1706 (list (bug-357-c-slot1 x)
1707 (bug-357-c-slot2 x)
1708 (bug-357-c-slot3 x)
1709 (bug-357-c-slot4 x)
1710 (bug-357-c-slot5 x)
1711 (bug-357-c-slot6 x)
1712 (bug-357-c-slot7 x)
1713 (bug-357-c-slot8 x)
1714 (bug-357-c-slot9 x)
1715 (bug-357-c-slot10 x)
1716 (bug-357-c-slot11 x))))
1717 (let ((base (slots (make-bug-357-c))))
1718 (assert (equal base (slots (make-instance 'bug-357-c))))
1719 (assert (equal base '(nil t2 3.1415927 -44 :ok t 9 -88 nil t 20))))))
1721 (defclass class-slot-shared-initialize ()
1722 ((a :allocation :class :initform :ok)))
1723 (with-test (:name :class-slot-shared-initialize)
1724 (let ((x (make-instance 'class-slot-shared-initialize)))
1725 (assert (eq :ok (slot-value x 'a)))
1726 (slot-makunbound x 'a)
1727 (assert (not (slot-boundp x 'a)))
1728 (shared-initialize x '(a))
1729 (assert (slot-boundp x 'a))
1730 (assert (eq :ok (slot-value x 'a)))))
1732 (declaim (ftype (function (t t t) (values single-float &optional))
1733 i-dont-want-to-be-clobbered-1
1734 i-dont-want-to-be-clobbered-2))
1735 (defgeneric i-dont-want-to-be-clobbered-1 (t t t))
1736 (defmethod i-dont-want-to-be-clobbered-2 ((x cons) y z)
1738 (defun i-cause-an-gf-info-update ()
1739 (i-dont-want-to-be-clobbered-2 t t t))
1740 (with-test (:name :defgeneric-should-clobber-ftype)
1741 ;; (because it doesn't check the argument or result types)
1742 (assert (equal '(function (t t t) *)
1743 (sb-kernel:type-specifier
1744 (sb-int:info :function
1745 :type 'i-dont-want-to-be-clobbered-1))))
1746 (assert (equal '(function (t t t) *)
1747 (sb-kernel:type-specifier
1748 (sb-int:info :function
1749 :type 'i-dont-want-to-be-clobbered-2))))
1750 (assert (eq :defined-method
1751 (sb-int:info :function
1752 :where-from 'i-dont-want-to-be-clobbered-1)))
1753 (assert (eq :defined-method
1754 (sb-int:info :function
1755 :where-from 'i-dont-want-to-be-clobbered-2))))
1757 (with-test (:name :bogus-parameter-specializer-name-error)
1758 (assert (eq :ok
1759 (handler-case
1760 (eval `(defmethod #:fii ((x "a string")) 'string))
1761 (sb-int:reference-condition (c)
1762 (when (member '(:ansi-cl :macro defmethod)
1763 (sb-int:reference-condition-references c)
1764 :test #'equal)
1765 :ok))))))
1767 (defclass remove-default-initargs-test ()
1768 ((x :initarg :x :initform 42)))
1769 (defclass remove-default-initatgs-test ()
1770 ((x :initarg :x :initform 42))
1771 (:default-initargs :x 0))
1772 (defclass remove-default-initargs-test ()
1773 ((x :initarg :x :initform 42)))
1774 (with-test (:name :remove-default-initargs)
1775 (assert (= 42 (slot-value (make-instance 'remove-default-initargs-test)
1776 'x))))
1778 (with-test (:name :bug-485019)
1779 ;; there was a bug in WALK-SETQ, used in method body walking, in the
1780 ;; presence of declarations on symbol macros.
1781 (defclass bug-485019 ()
1782 ((array :initarg :array)))
1783 (defmethod bug-485019 ((bug-485019 bug-485019))
1784 (with-slots (array) bug-485019
1785 (declare (type (or null simple-array) array))
1786 (setf array (make-array 4)))
1787 bug-485019)
1788 (bug-485019 (make-instance 'bug-485019)))
1790 ;;; The compiler didn't propagate the declarared type before applying
1791 ;;; the transform for (SETF SLOT-VALUE), so the generic accessor was used.
1792 (defstruct foo-520366
1793 slot)
1794 (defun quux-520366 (cont)
1795 (funcall cont))
1796 (defun bar-520366 (foo-struct)
1797 (declare (type foo-520366 foo-struct))
1798 (with-slots (slot) foo-struct
1799 (tagbody
1800 (quux-520366 #'(lambda ()
1801 (setf slot :value)
1802 (go TAG)))
1803 TAG)))
1804 (with-test (:name :bug-520366)
1805 (let ((callees (find-named-callees #'bar-520366)))
1806 (assert (equal (list #'quux-520366) callees))))
1808 (defgeneric no-applicable-method/retry (x))
1809 (defmethod no-applicable-method/retry ((x string))
1810 "string")
1811 (with-test (:name :no-applicable-method/retry)
1812 (assert (equal "cons"
1813 (handler-bind ((error
1814 (lambda (c)
1815 (declare (ignore c))
1816 (let ((r (find-restart 'sb-pcl::retry)))
1817 (when r
1818 (eval `(defmethod no-applicable-method/retry ((x cons))
1819 "cons"))
1820 (invoke-restart r))))))
1821 (no-applicable-method/retry (cons t t))))))
1823 (defgeneric no-primary-method/retry (x))
1824 (defmethod no-primary-method/retry :before (x) (assert x))
1825 (with-test (:name :no-primary-method/retry)
1826 (assert (equal "ok!"
1827 (handler-bind ((error
1828 (lambda (c)
1829 (declare (ignore c))
1830 (let ((r (find-restart 'sb-pcl::retry)))
1831 (when r
1832 (eval `(defmethod no-primary-method/retry (x)
1833 "ok!"))
1834 (invoke-restart r))))))
1835 (no-primary-method/retry (cons t t))))))
1837 ;;;; success