Improvements in CHANGE-CLASS tests
[sbcl.git] / tests / clos.impure.lisp
blob83b459eb2b0d4953e923351c7f129d72d412e22f
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.
181 ;;; One symptom was reported by Brian Spilsbury (sbcl-devel
182 ;;; 2002-04-08), and another on IRC by Dan Barlow simultaneously.
183 ;;; Better check that it doesn't happen again.
185 ;;; Some more CHANGE-CLASS testing (which only applies to the now
186 ;;; ANSI-compliant version) thanks to Espen Johnsen)
188 ;; First, the forward references:
189 (defclass forward-ref-a (forward-ref-b) ())
190 (defclass forward-ref-b () ())
191 ;; (a couple more complicated examples found by Paul Dietz' test
192 ;; suite):
193 (defclass forward-ref-c1 (forward-ref-c2) ())
194 (defclass forward-ref-c2 (forward-ref-c3) ())
196 (defclass forward-ref-d1 (forward-ref-d2 forward-ref-d3)
198 (defclass forward-ref-d2 (forward-ref-d4 forward-ref-d5)
201 ;; Then CHANGE-CLASS
202 (defun change-class-test-case (spec)
203 (destructuring-bind (from-class from-initargs
204 to-class to-initargs
205 expected-slots)
206 spec
207 (let ((from (typecase from-class
208 (symbol
209 (apply #'make-instance from-class from-initargs))
210 ((cons (eql :class) (cons symbol))
211 (find-class (second from-class))))))
212 (flet ((change ()
213 (apply #'change-class from to-class to-initargs))
214 ;; These local functions make ASSERT produce better error
215 ;; messages.
216 (slot-value-equal (instance name value expected)
217 (declare (ignore instance name))
218 (equal value expected))
219 (slot-not-bound (instance name)
220 (not (slot-boundp instance name))))
221 (case expected-slots
222 (error
223 (assert-error (change)))
224 (type-error
225 (assert-error (change) type-error))
227 (let ((to (change)))
228 (loop :for (name value) :in expected-slots
229 :do (case value
230 (:unbound
231 (assert (slot-not-bound to name)))
233 (assert
234 (slot-value-equal
235 to name (slot-value to name) value))))))))))))
237 (defclass change-class.smoke.1 ()
238 ((foo :initarg :foo)))
239 (defclass change-class.smoke.2 (change-class.smoke.1) ())
240 (defclass change-class.smoke.3 (change-class.smoke.1)
241 ((foo :initarg :foo)))
242 (defclass change-class.smoke.4 ()
243 ((foo :initarg :foo) (bar :initarg :bar)))
244 (defclass change-class.smoke.5 ()
245 ((a :initarg :a) (b :initarg :b) (c :initarg :c)))
246 (defclass change-class.smoke.6 () ())
248 (with-test (:name (change-class :smoke))
249 (mapc
250 #'change-class-test-case
251 '(;; Test unbound slots.
252 (change-class.smoke.1 () change-class.smoke.1 ()
253 ((foo :unbound)))
254 (change-class.smoke.1 () change-class.smoke.2 ()
255 ((foo :unbound)))
256 (change-class.smoke.1 () change-class.smoke.3 ()
257 ((foo :unbound)))
258 (change-class.smoke.1 () change-class.smoke.4 ()
259 ((foo :unbound) (bar :unbound)))
260 (change-class.smoke.4 (:bar 1) change-class.smoke.1 ()
261 ((foo :unbound)))
263 ;; Bound slots are retained.
264 (change-class.smoke.1 (:foo :baz) change-class.smoke.1 ()
265 ((foo :baz)))
266 (change-class.smoke.1 (:foo :baz) change-class.smoke.2 ()
267 ((foo :baz)))
268 (change-class.smoke.1 (:foo :baz) change-class.smoke.3 ()
269 ((foo :baz)))
270 (change-class.smoke.1 (:foo :baz) change-class.smoke.4 ()
271 ((foo :baz) (bar :unbound)))
272 (change-class.smoke.4 (:foo :baz) change-class.smoke.1 ()
273 ((foo :baz)))
275 ;; Original test.
276 (change-class.smoke.5 (:a 1 :b 2 :c 3) change-class.smoke.5 ()
277 ((a 1) (b 2) (c 3)))
279 ;; Original test by Espen Johnsen
280 (change-class.smoke.1 (:foo 1) change-class.smoke.4 (:bar 2)
281 ((foo 1) (bar 2)))
283 ;; Cannot change objects into metaobjects.
284 (change-class.smoke.6 () class ()
285 error)
286 (change-class.smoke.6 () generic-function ()
287 error)
288 (change-class.smoke.6 () method ()
289 error)
290 (change-class.smoke.6 () slot-definition ()
291 error))))
293 ;; Test for type-checking
295 (locally (declare (optimize (safety 3))) ; force slot type-checking
296 (defclass change-class.type-check.1 ()
297 ((foo :initarg :foo :type real)))
298 (defclass change-class.type-check.2 ()
299 ((foo :initarg :foo :type integer))))
301 (with-test (:name (change-class :type-check))
302 (mapc
303 #'change-class-test-case
304 '(;; These are allowed.
305 (change-class.type-check.1 () change-class.type-check.2 ()
306 ((foo :unbound)))
307 (change-class.type-check.1 (:foo 1) change-class.type-check.2 ()
308 ((foo 1)))
309 (change-class.type-check.1 (:foo 1.0) change-class.type-check.2 (:foo 2)
310 ((foo 2)))
312 ;; These are not allowed.
313 (change-class.type-check.1 () change-class.type-check.2 (:foo 1.0)
314 type-error)
315 (change-class.type-check.1 (:foo 1.0) change-class.type-check.2 ()
316 type-error)))
318 ;; Type-mismatches should be recoverable via USE-VALUE restart.
319 (let* ((from (make-instance 'change-class.type-check.1 :foo 1.0))
320 (to (handler-bind ((type-error (lambda (condition)
321 (use-value 3))))
322 (change-class from 'change-class.type-check.2))))
323 (assert (equal (slot-value to 'foo) 3))))
325 ;; Test interaction with initforms and -args
327 (defclass change-class.initforms.1 ()
329 (defclass change-class.initforms.2 ()
330 ((foo :initarg :foo)))
331 (defclass change-class.initforms.3 ()
332 ((foo :initarg :foo :initform :bar)))
333 (defclass change-class.initforms.4 ()
334 ((foo :initarg :foo))
335 (:default-initargs
336 :foo :bar))
338 (with-test (:name (change-class :initforms))
339 (mapc
340 #'change-class-test-case
341 '(;; Initialization of added slot.
342 (change-class.initforms.1 () change-class.initforms.3 ()
343 ((foo :bar)))
344 (change-class.initforms.1 () change-class.initforms.3 (:foo :fez)
345 ((foo :fez)))
346 (change-class.initforms.1 () change-class.initforms.4 ()
347 ((foo :unbound))) ; default initargs are not used
348 (change-class.initforms.1 () change-class.initforms.4 (:foo :fez)
349 ((foo :fez)))
351 ;; Unbound slot remains unbound.
352 (change-class.initforms.2 () change-class.initforms.3 ()
353 ((foo :unbound)))
354 (change-class.initforms.2 () change-class.initforms.3 (:foo :fez)
355 ((foo :fez)))
356 (change-class.initforms.2 () change-class.initforms.4 ()
357 ((foo :unbound)))
358 (change-class.initforms.2 () change-class.initforms.4 (:foo :fez)
359 ((foo :fez)))
361 ;; Value is retained.
362 (change-class.initforms.2 (:foo :baz) change-class.initforms.3 ()
363 ((foo :baz)))
364 (change-class.initforms.2 (:foo :baz) change-class.initforms.3 (:foo :fez)
365 ((foo :fez)))
366 (change-class.initforms.2 (:foo :baz) change-class.initforms.4 ()
367 ((foo :baz)))
368 (change-class.initforms.2 (:foo :baz) change-class.initforms.4 (:foo :fez)
369 ((foo :fez))))))
371 ;; Test for FORWARD-REFERENCED-CLASS
373 (defclass change-class.forward-referenced.1 () ())
374 ;; CHANGE-CLASS.FORWARD-REFERENCED.2 is only needed to create the
375 ;; FORWARD-REFERENCED-CLASS CHANGE-CLASS.FORWARD-REFERENCED.3.
376 (defclass change-class.forward-referenced.2 (change-class.forward-referenced.3) ())
378 (with-test (:name (change-class sb-pcl:forward-referenced-class))
379 (mapc
380 #'change-class-test-case
381 '(;; Changing instances of "ordinary classes" to classes which are
382 ;; instances of FORWARD-REFERENCED-CLASS is not allowed.
383 (change-class.forward-referenced.1 () change-class.forward-referenced.3 ()
384 error)
386 ;; Changing instances of FORWARD-REFERENCED-CLASS into
387 ;; non-CLASSes and in particular non-CLASS metaobjects is not
388 ;; allowed.
389 ((:class change-class.forward-referenced.3) () change-class.forward-referenced.1 ()
390 error)
391 ((:class change-class.forward-referenced.3) () generic-function ()
392 error)
393 ((:class change-class.forward-referenced.3) () method ()
394 error)
395 ((:class change-class.forward-referenced.3) () slot-definition ()
396 error)
398 ;; Changing instances of FORWARD-REFERENCED-CLASS into CLASS is
399 ;; allowed but destructive. Therefore has to be final test case.
400 ((:class change-class.forward-referenced.3) () standard-class ()
401 ()))))
403 ;; Test for FUNCALLABLE-STANDARD-CLASS
405 (defclass change-class.funcallable.1 () ())
406 (defclass change-class.funcallable.2 () ()
407 (:metaclass sb-mop:funcallable-standard-class))
408 (defclass change-class.funcallable.3 () ()
409 (:metaclass sb-mop:funcallable-standard-class))
411 (with-test (:name (change-class sb-mop:funcallable-standard-class))
412 (mapc
413 #'change-class-test-case
414 '(;; Cannot change STANDARD-OBJECT into FUNCALLABLE-STANDARD-OBJECT
415 ;; and vice-versa.
416 (change-class.funcallable.1 () change-class.funcallable.2 ()
417 error)
418 (change-class.funcallable.2 () change-class.funcallable.1 ()
419 error)
420 ;; FUNCALLABLE-STANDARD-OBJECTs should work.
421 (change-class.funcallable.2 () change-class.funcallable.2 ()
423 (change-class.funcallable.2 () change-class.funcallable.3 ()
424 ()))))
426 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
427 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
428 (defgeneric bug180 (x)
429 (:method-combination list :most-specific-last))
430 (defmethod bug180 list ((x number))
431 'number)
432 (defmethod bug180 list ((x fixnum))
433 'fixnum)
434 (assert (equal (bug180 14) '(number fixnum)))
436 ;;; printing a structure class should not loop indefinitely (or cause
437 ;;; a stack overflow):
438 (defclass test-printing-structure-class ()
439 ((slot :initarg :slot))
440 (:metaclass structure-class))
441 (print (make-instance 'test-printing-structure-class :slot 2))
443 ;;; structure-classes should behave nicely when subclassed
444 (defclass super-structure ()
445 ((a :initarg :a :accessor a-accessor)
446 (b :initform 2 :reader b-reader))
447 (:metaclass structure-class))
448 (defclass sub-structure (super-structure)
449 ((c :initarg :c :writer c-writer :accessor c-accessor))
450 (:metaclass structure-class))
451 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
452 (assert (= (a-accessor foo) 1))
453 (assert (= (b-reader foo) 2))
454 (assert (= (c-accessor foo) 3))
455 (setf (a-accessor foo) 4)
456 (c-writer 5 foo)
457 (assert (= (a-accessor foo) 4))
458 (assert (= (c-accessor foo) 5)))
460 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
461 ;;; encoding of effective method functions for slot accessors as
462 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
463 ;;; functions to get broken in special ways even though ordinary
464 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
465 ;;; for that possibility. Now we have a few tests:
466 (defclass fish ()
467 ((fin :reader ffin :writer ffin!)
468 (tail :reader ftail :writer ftail!)))
469 (defvar *fish* (make-instance 'fish))
470 (ffin! 'triangular-fin *fish*)
471 (defclass cod (fish) ())
472 (defvar *cod* (make-instance 'cod))
473 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
474 (defmethod ffin! (new-fin (cod cod))
475 (format t "~&about to set ~S fin to ~S~%" cod new-fin)
476 (vector-push-extend '(cod) *clos-dispatch-side-fx*)
477 (prog1
478 (call-next-method)
479 (format t "~&done setting ~S fin to ~S~%" cod new-fin)))
480 (defmethod ffin! :before (new-fin (cod cod))
481 (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
482 (format t "~&exploring the CLOS dispatch zoo with COD fins~%"))
483 (ffin! 'almost-triang-fin *cod*)
484 (assert (eq (ffin *cod*) 'almost-triang-fin))
485 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
487 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
488 ;;; ignored its options; Gerd Moellmann found and fixed the problem
489 ;;; for cmucl (cmucl-imp 2002-06-18).
490 (define-method-combination test-mc (x)
491 ;; X above being a method-group-specifier
492 ((primary () :required t))
493 `(call-method ,(first primary)))
495 (defgeneric gf (obj)
496 (:method-combination test-mc 1))
498 (defmethod gf (obj)
499 obj)
501 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
502 ;;; some others were of the wrong type:
503 (macrolet ((assert-program-error (form)
504 `(multiple-value-bind (value error)
505 (ignore-errors ,form)
506 (unless (and (null value) (typep error 'program-error))
507 (error "~S failed: ~S, ~S" ',form value error)))))
508 (assert-program-error (defclass foo001 () (a b a)))
509 (assert-program-error (defclass foo002 ()
510 (a b)
511 (:default-initargs x 'a x 'b)))
512 (assert-program-error (defclass foo003 ()
513 ((a :allocation :class :allocation :class))))
514 (assert-program-error (defclass foo004 ()
515 ((a :silly t))))
516 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
517 ;; Moellmann in sbcl-0.7.8.x:
518 (assert-program-error (progn
519 (defmethod odd-key-args-checking (&key (key 42)) key)
520 (odd-key-args-checking 3)))
521 (assert (= (odd-key-args-checking) 42))
522 (assert (eq (odd-key-args-checking :key t) t))
523 ;; yet some more, fixed in sbcl-0.7.9.xx
524 (assert-program-error (defclass foo005 ()
525 (:metaclass sb-pcl::funcallable-standard-class)
526 (:metaclass 1)))
527 (assert-program-error (defclass foo006 ()
528 ((a :reader (setf a)))))
529 (assert-program-error (defclass foo007 ()
530 ((a :initarg 1))))
531 (assert-program-error (defclass foo008 ()
532 (a :initarg :a)
533 (:default-initargs :a 1)
534 (:default-initargs :a 2)))
535 ;; and also BUG 47d, fixed in sbcl-0.8alpha.0.26
536 (assert-program-error (defgeneric if (x)))
537 ;; DEFCLASS should detect an error if slot names aren't suitable as
538 ;; variable names:
539 (assert-program-error (defclass foo009 ()
540 ((:a :initarg :a))))
541 (assert-program-error (defclass foo010 ()
542 (("a" :initarg :a))))
543 (assert-program-error (defclass foo011 ()
544 ((#1a() :initarg :a))))
545 (assert-program-error (defclass foo012 ()
546 ((t :initarg :t))))
547 (assert-program-error (defclass foo013 () ("a")))
548 ;; specialized lambda lists have certain restrictions on ordering,
549 ;; repeating keywords, and the like:
550 (assert-program-error (defmethod foo014 ((foo t) &rest) nil))
551 (assert-program-error (defmethod foo015 ((foo t) &rest x y) nil))
552 (assert-program-error (defmethod foo016 ((foo t) &allow-other-keys) nil))
553 (assert-program-error (defmethod foo017 ((foo t)
554 &optional x &optional y) nil))
555 (assert-program-error (defmethod foo018 ((foo t) &rest x &rest y) nil))
556 (assert-program-error (defmethod foo019 ((foo t) &rest x &optional y) nil))
557 (assert-program-error (defmethod foo020 ((foo t) &key x &optional y) nil))
558 (assert-program-error (defmethod foo021 ((foo t) &key x &rest y) nil)))
560 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
561 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
562 ;;; (thanks to Gerd Moellmann)
563 (with-test (:name :documentation-argument-precedence-order)
564 (defun foo022 ()
565 "Documentation"
567 (let ((answer (documentation 'foo022 'function)))
568 (assert (stringp answer))
569 (defmethod documentation ((x (eql 'foo022)) y) "WRONG")
570 (assert (string= (documentation 'foo022 'function) answer))))
572 ;;; only certain declarations are permitted in DEFGENERIC
573 (macrolet ((assert-program-error (form)
574 `(multiple-value-bind (value error)
575 (ignore-errors ,form)
576 (assert (null value))
577 (assert (typep error 'program-error)))))
578 (assert-program-error (defgeneric bogus-declaration (x)
579 (declare (special y))))
580 (assert-program-error (defgeneric bogus-declaration2 (x)
581 (declare (notinline concatenate)))))
582 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
583 ;;; method.
584 (defmethod no-next-method-test ((x integer)) (call-next-method))
585 (assert (null (ignore-errors (no-next-method-test 1))))
586 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
587 'success)
588 (assert (eq (no-next-method-test 1) 'success))
589 (assert (null (ignore-errors (no-next-method-test 'foo))))
591 ;;; regression test for bug 176, following a fix that seems
592 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
593 ;;; Moellmann, merged in sbcl-0.7.9.12).
594 (dotimes (i 10)
595 (let ((lastname (intern (format nil "C176-~D" (1- i))))
596 (name (intern (format nil "C176-~D" i))))
597 (eval `(defclass ,name
598 (,@(if (= i 0) nil (list lastname)))
599 ()))
600 (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
601 (declare (ignore any))))))
602 (defclass b176 () (aslot-176))
603 (defclass c176-0 (b176) ())
604 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
606 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
607 ;;; primary methods:
608 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
609 ((around (:around))
610 (primary (dmc-test-mc) :order order :required t))
611 (let ((form (if (rest primary)
612 `(and ,@(mapcar #'(lambda (method)
613 `(call-method ,method))
614 primary))
615 `(call-method ,(first primary)))))
616 (if around
617 `(call-method ,(first around)
618 (,@(rest around)
619 (make-method ,form)))
620 form)))
622 (defgeneric dmc-test-mc (&key k)
623 (:method-combination dmc-test-mc))
625 (defmethod dmc-test-mc dmc-test-mc (&key k)
628 (dmc-test-mc :k 1)
629 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
630 ;;; the NAME argument, not some random method object. So:
631 (assert (eq (define-method-combination dmc-test-return-foo)
632 'dmc-test-return-foo))
633 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
634 'dmc-test-return-bar))
635 (assert (eq (define-method-combination dmc-test-return
636 (&optional (order :most-specific-first))
637 ((around (:around))
638 (primary (dmc-test-return) :order order :required t))
639 (let ((form (if (rest primary)
640 `(and ,@(mapcar #'(lambda (method)
641 `(call-method ,method))
642 primary))
643 `(call-method ,(first primary)))))
644 (if around
645 `(call-method ,(first around)
646 (,@(rest around)
647 (make-method ,form)))
648 form)))
649 'dmc-test-return))
651 ;;; DEFINE-METHOD-COMBINATION should, according to the description in 7.7,
652 ;;; allow you to do everything in the body forms yourself if you specify
653 ;;; exactly one method group whose qualifier-pattern is *
655 ;;; The specific language is:
656 ;;; "The use of method group specifiers provides a convenient syntax to select
657 ;;; methods, to divide them among the possible roles, and to perform the
658 ;;; necessary error checking. It is possible to perform further filtering of
659 ;;; methods in the body forms by using normal list-processing operations and
660 ;;; the functions method-qualifiers and invalid-method-error. It is permissible
661 ;;; to use setq on the variables named in the method group specifiers and to
662 ;;; bind additional variables. It is also possible to bypass the method group
663 ;;; specifier mechanism and do everything in the body forms. This is
664 ;;; accomplished by writing a single method group with * as its only
665 ;;; qualifier-pattern; the variable is then bound to a list of all of the
666 ;;; applicable methods, in most-specific-first order."
667 (define-method-combination wam-test-method-combination-a ()
668 ((all-methods *))
669 (do ((methods all-methods (rest methods))
670 (primary nil)
671 (around nil))
672 ((null methods)
673 (let ((primary (nreverse primary))
674 (around (nreverse around)))
675 (if primary
676 (let ((form (if (rest primary)
677 `(call-method ,(first primary) ,(rest primary))
678 `(call-method ,(first primary)))))
679 (if around
680 `(call-method ,(first around) (,@(rest around)
681 (make-method ,form)))
682 form))
683 `(make-method (error "No primary methods")))))
684 (let* ((method (first methods))
685 (qualifier (first (method-qualifiers method))))
686 (cond
687 ((equal :around qualifier)
688 (push method around))
689 ((null qualifier)
690 (push method primary))))))
692 (defgeneric wam-test-mc-a (val)
693 (:method-combination wam-test-method-combination-a))
694 (assert-error (wam-test-mc-a 13))
695 (defmethod wam-test-mc-a ((val number))
696 (+ val (if (next-method-p) (call-next-method) 0)))
697 (assert (= (wam-test-mc-a 13) 13))
698 (defmethod wam-test-mc-a :around ((val number))
699 (+ val (if (next-method-p) (call-next-method) 0)))
700 (assert (= (wam-test-mc-a 13) 26))
702 ;;; DEFINE-METHOD-COMBINATION
703 ;;; When two methods are in the same method group and have the same
704 ;;; specializers, their sort order within the group may be ambiguous. Therefore,
705 ;;; we should throw an error when we have two methods in the same group with
706 ;;; the same specializers /as long as/ we have more than one method group
707 ;;; or our single method group qualifier-pattern is not *. This resolves the
708 ;;; apparent conflict with the above 'It is also possible to bypass' language.
710 ;;; The language specifying this behavior is:
711 ;;; "Note that two methods with identical specializers, but with different
712 ;;; qualifiers, are not ordered by the algorithm described in Step 2 of the
713 ;;; method selection and combination process described in Section 7.6.6
714 ;;; (Method Selection and Combination). Normally the two methods play different
715 ;;; roles in the effective method because they have different qualifiers, and
716 ;;; no matter how they are ordered in the result of Step 2, the effective
717 ;;; method is the same. If the two methods play the same role and their order
718 ;;; matters, an error is signaled. This happens as part of the qualifier
719 ;;; pattern matching in define-method-combination."
721 ;;; Note that the spec pretty much equates 'method group' and 'role'.
722 ;; First we ensure that it fails correctly when there is more than one
723 ;; method group
724 (define-method-combination wam-test-method-combination-b ()
725 ((around (:around))
726 (primary * :required t))
727 (let ((form (if (rest primary)
728 `(call-method ,(first primary) ,(rest primary))
729 `(call-method ,(first primary)))))
730 (if around
731 `(call-method ,(first around) (,@(rest around)
732 (make-method ,form)))
733 form)))
735 (defgeneric wam-test-mc-b (val)
736 (:method-combination wam-test-method-combination-b))
737 (defmethod wam-test-mc-b ((val number))
738 (+ val (if (next-method-p) (call-next-method) 0)))
739 (assert (= (wam-test-mc-b 13) 13))
740 (defmethod wam-test-mc-b :around ((val number))
741 (+ val (if (next-method-p) (call-next-method) 0)))
742 (assert (= (wam-test-mc-b 13) 26))
743 (defmethod wam-test-mc-b :somethingelse ((val number))
744 (+ val (if (next-method-p) (call-next-method) 0)))
745 (assert-error (wam-test-mc-b 13))
747 ;;; now, ensure that it fails with a single group with a qualifier-pattern
748 ;;; that is not *
749 (define-method-combination wam-test-method-combination-c ()
750 ((methods listp :required t))
751 (if (rest methods)
752 `(call-method ,(first methods) ,(rest methods))
753 `(call-method ,(first methods))))
755 (defgeneric wam-test-mc-c (val)
756 (:method-combination wam-test-method-combination-c))
757 (assert-error (wam-test-mc-c 13))
758 (defmethod wam-test-mc-c :foo ((val number))
759 (+ val (if (next-method-p) (call-next-method) 0)))
760 (assert (= (wam-test-mc-c 13) 13))
761 (defmethod wam-test-mc-c :bar ((val number))
762 (+ val (if (next-method-p) (call-next-method) 0)))
763 (assert-error (wam-test-mc-c 13))
765 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
766 ;;; given:
767 (defmethod incompatible-ll-test-1 (x) x)
768 (assert-error (defmethod incompatible-ll-test-1 (x y) y))
769 (assert-error (defmethod incompatible-ll-test-1 (x &rest y) y))
770 ;;; Sneakily using a bit of MOPness to check some consistency
771 (assert (= (length
772 (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
774 (defmethod incompatible-ll-test-2 (x &key bar) bar)
775 (assert-error (defmethod incompatible-ll-test-2 (x) x))
776 (defmethod incompatible-ll-test-2 (x &rest y) y)
777 (assert (= (length
778 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
779 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
780 (assert (= (length
781 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
783 ;;; Per Christophe, this is an illegal method call because of 7.6.5
784 (assert-error (incompatible-ll-test-2 t 1 2))
786 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
788 (defmethod incompatible-ll-test-3 ((x integer)) x)
789 (remove-method #'incompatible-ll-test-3
790 (find-method #'incompatible-ll-test-3
792 (list (find-class 'integer))))
793 (assert-error (defmethod incompatible-ll-test-3 (x y) (list x y)))
796 ;;; Attempting to instantiate classes with forward references in their
797 ;;; CPL should signal errors (FIXME: of what type?)
798 (defclass never-finished-class (this-one-unfinished-too) ())
799 (multiple-value-bind (result error)
800 (ignore-errors (make-instance 'never-finished-class))
801 (assert (null result))
802 (assert (typep error 'error)))
803 (multiple-value-bind (result error)
804 (ignore-errors (make-instance 'this-one-unfinished-too))
805 (assert (null result))
806 (assert (typep error 'error)))
808 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
809 ;;; weren't for a while in sbcl-0.7.9.xx)
810 (defclass superclass-with-slot ()
811 ((a :allocation :class)))
812 (defclass subclass-for-class-allocation (superclass-with-slot) ())
813 (make-instance 'subclass-for-class-allocation)
815 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
816 ;;; resulting in failure in the following:
817 (defmethod call-next-method-lexical-args ((x integer))
819 (defmethod call-next-method-lexical-args :around ((x integer))
820 (let ((x (1+ x)))
821 (call-next-method)))
822 (assert (= (call-next-method-lexical-args 3) 3))
824 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
825 ;;; until 0.7.9.5x
826 (defvar *d-m-c-args-test* nil)
827 (define-method-combination progn-with-lock ()
828 ((methods ()))
829 (:arguments object)
830 `(unwind-protect
831 (progn (lock (object-lock ,object))
832 ,@(mapcar #'(lambda (method)
833 `(call-method ,method))
834 methods))
835 (unlock (object-lock ,object))))
836 (defun object-lock (obj)
837 (push "object-lock" *d-m-c-args-test*)
838 obj)
839 (defun unlock (obj)
840 (push "unlock" *d-m-c-args-test*)
841 obj)
842 (defun lock (obj)
843 (push "lock" *d-m-c-args-test*)
844 obj)
845 (defgeneric d-m-c-args-test (x)
846 (:method-combination progn-with-lock))
847 (defmethod d-m-c-args-test ((x symbol))
848 (push "primary" *d-m-c-args-test*))
849 (defmethod d-m-c-args-test ((x number))
850 (error "foo"))
851 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
852 (assert (equal *d-m-c-args-test*
853 '("unlock" "object-lock" "primary" "lock" "object-lock")))
854 (setf *d-m-c-args-test* nil)
855 (ignore-errors (d-m-c-args-test 1))
856 (assert (equal *d-m-c-args-test*
857 '("unlock" "object-lock" "lock" "object-lock")))
859 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
860 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
861 ;;; doesn't, but it does well enough to compile the following without
862 ;;; error (the problems remain in asking for a complete macroexpansion
863 ;;; of an arbitrary form).
864 (symbol-macrolet ((x 1))
865 (defmethod bug222 (z)
866 (macrolet ((frob (form) `(progn ,form ,x)))
867 (frob (print x)))))
868 (assert (= (bug222 t) 1))
870 ;;; also, a test case to guard against bogus environment hacking:
872 (eval-when (:compile-toplevel :load-toplevel :execute)
873 (setq bug222-b 3))
874 ;;; this should at the least compile:
875 (let ((bug222-b 1))
876 (defmethod bug222-b (z stream)
877 (macrolet ((frob (form) `(progn ,form ,bug222-b)))
878 (frob (format stream "~D~%" bug222-b)))))
879 ;;; and it would be nice (though not specified by ANSI) if the answer
880 ;;; were as follows:
881 (let ((x (make-string-output-stream)))
882 (let ((value (bug222-b t x)))
883 ;; not specified by ANSI
884 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
885 (assert (= value 3)))
886 ;; specified.
887 (assert (char= (char (get-output-stream-string x) 0) #\1)))
889 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
890 ;;; for invalid initargs where it should:
891 (defclass class234 () ())
892 (defclass subclass234 (class234) ())
893 (defvar *bug234* 0)
894 (defun bug-234 ()
895 (reinitialize-instance (make-instance 'class234) :dummy 0))
896 (defun subbug-234 ()
897 (reinitialize-instance (make-instance 'subclass234) :dummy 0))
898 (assert-error (bug-234) program-error)
899 (defmethod shared-initialize :after ((i class234) slots &key dummy)
900 (incf *bug234*))
901 (assert (typep (subbug-234) 'subclass234))
902 (assert (= *bug234*
903 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
906 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
907 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
908 (defclass class234-b1 () ())
909 (defclass class234-b2 (class234-b1) ())
910 (defvar *bug234-b* 0)
911 (defun bug234-b ()
912 (make-instance 'class234-b2))
913 (compile 'bug234-b)
914 (bug234-b)
915 (assert (= *bug234-b* 0))
916 (defmethod initialize-instance :before ((x class234-b1) &rest args)
917 (declare (ignore args))
918 (incf *bug234-b*))
919 (bug234-b)
920 (assert (= *bug234-b* 1))
922 ;;; we should be able to make classes with uninterned names:
923 (defclass #:class-with-uninterned-name () ())
925 ;;; SLOT-MISSING should be called when there are missing slots.
926 (defclass class-with-all-slots-missing () ())
927 (defmethod slot-missing (class (o class-with-all-slots-missing)
928 slot-name op
929 &optional new-value)
931 (assert (eq (slot-value (make-instance 'class-with-all-slots-missing) 'foo)
932 'slot-value))
933 (assert (eq (funcall (lambda (x) (slot-value x 'bar))
934 (make-instance 'class-with-all-slots-missing))
935 'slot-value))
936 (assert (eq (funcall (lambda (x) (setf (slot-value x 'baz) 'baz))
937 (make-instance 'class-with-all-slots-missing))
938 ;; SLOT-MISSING's value is specified to be ignored; we
939 ;; return NEW-VALUE.
940 'baz))
942 ;;; we should be able to specialize on anything that names a class.
943 (defclass name-for-class () ())
944 (defmethod something-that-specializes ((x name-for-class)) 1)
945 (setf (find-class 'other-name-for-class) (find-class 'name-for-class))
946 (defmethod something-that-specializes ((x other-name-for-class)) 2)
947 (assert (= (something-that-specializes (make-instance 'name-for-class)) 2))
948 (assert (= (something-that-specializes (make-instance 'other-name-for-class))
951 ;;; more forward referenced classes stuff
952 (defclass frc-1 (frc-2) ())
953 (assert (subtypep 'frc-1 (find-class 'frc-2)))
954 (assert (subtypep (find-class 'frc-1) 'frc-2))
955 (assert (not (subtypep (find-class 'frc-2) 'frc-1)))
956 (defclass frc-2 (frc-3) ((a :initarg :a)))
957 (assert (subtypep 'frc-1 (find-class 'frc-3)))
958 (defclass frc-3 () ())
959 (assert (typep (make-instance 'frc-1 :a 2) (find-class 'frc-1)))
960 (assert (typep (make-instance 'frc-2 :a 3) (find-class 'frc-2)))
962 ;;; check that we can define classes with two slots of different names
963 ;;; (even if it STYLE-WARNs).
964 (defclass odd-name-class ()
965 ((name :initarg :name)
966 (cl-user::name :initarg :name2)))
967 (let ((x (make-instance 'odd-name-class :name 1 :name2 2)))
968 (assert (= (slot-value x 'name) 1))
969 (assert (= (slot-value x 'cl-user::name) 2)))
971 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
972 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
973 (defstruct allocatable-structure a)
974 (assert (typep (allocate-instance (find-class 'allocatable-structure))
975 'allocatable-structure))
977 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
978 ;;; amazingly, calls to CPL would work a couple of times, and then
979 ;;; start returning NIL. A fix was found (relating to the
980 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
981 (defgeneric cpl (x)
982 (:method-combination list)
983 (:method list ((x broadcast-stream)) 'broadcast-stream)
984 (:method list ((x integer)) 'integer)
985 (:method list ((x number)) 'number)
986 (:method list ((x stream)) 'stream)
987 (:method list ((x structure-object)) 'structure-object))
988 (assert (equal (cpl 0) '(integer number)))
989 (assert (equal (cpl 0) '(integer number)))
990 (assert (equal (cpl 0) '(integer number)))
991 (assert (equal (cpl 0) '(integer number)))
992 (assert (equal (cpl 0) '(integer number)))
993 (assert (equal (cpl (make-broadcast-stream))
994 '(broadcast-stream stream structure-object)))
995 (assert (equal (cpl (make-broadcast-stream))
996 '(broadcast-stream stream structure-object)))
997 (assert (equal (cpl (make-broadcast-stream))
998 '(broadcast-stream stream structure-object)))
1000 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
1001 ;;; parameters shouldn't affect the arguments to the next method for a
1002 ;;; no-argument call to CALL-NEXT-METHOD
1003 (defgeneric cnm-assignment (x)
1004 (:method (x) x)
1005 (:method ((x integer)) (setq x 3)
1006 (list x (call-next-method) (call-next-method x))))
1007 (assert (equal (cnm-assignment 1) '(3 1 3)))
1009 ;;; Bug reported by Istvan Marko 2003-07-09
1010 (let ((class-name (gentemp)))
1011 (loop for i from 1 to 9
1012 for slot-name = (intern (format nil "X~D" i))
1013 for initarg-name = (intern (format nil "X~D" i) :keyword)
1014 collect `(,slot-name :initarg ,initarg-name) into slot-descs
1015 append `(,initarg-name (list 0)) into default-initargs
1016 finally (eval `(defclass ,class-name ()
1017 (,@slot-descs)
1018 (:default-initargs ,@default-initargs))))
1019 (let ((f (compile nil `(lambda () (make-instance ',class-name)))))
1020 (assert (typep (funcall f) class-name))))
1022 ;;; bug 262: DEFMETHOD failed on a generic function without a lambda
1023 ;;; list
1024 (ensure-generic-function 'bug262)
1025 (defmethod bug262 (x y)
1026 (list x y))
1027 (assert (equal (bug262 1 2) '(1 2)))
1029 ;;; salex on #lisp 2003-10-13 reported that type declarations inside
1030 ;;; WITH-SLOTS are too hairy to be checked
1031 (defun ensure-no-notes (form)
1032 (handler-case (compile nil `(lambda () ,form))
1033 (sb-ext:compiler-note (c)
1034 ;; FIXME: it would be better to check specifically for the "type
1035 ;; is too hairy" note
1036 (error c))))
1037 (defvar *x*)
1038 (ensure-no-notes '(with-slots (a) *x*
1039 (declare (integer a))
1041 (ensure-no-notes '(with-slots (a) *x*
1042 (declare (integer a))
1043 (declare (notinline slot-value))
1046 ;;; from CLHS 7.6.5.1
1047 (defclass character-class () ((char :initarg :char)))
1048 (defclass picture-class () ((glyph :initarg :glyph)))
1049 (defclass character-picture-class (character-class picture-class) ())
1051 (defmethod width ((c character-class) &key font) font)
1052 (defmethod width ((p picture-class) &key pixel-size) pixel-size)
1054 (assert-error
1055 (width (make-instance 'character-class :char #\Q)
1056 :font 'baskerville :pixel-size 10)
1057 program-error)
1058 (assert-error
1059 (width (make-instance 'picture-class :glyph #\Q)
1060 :font 'baskerville :pixel-size 10)
1061 program-error)
1062 (assert (eq (width (make-instance 'character-picture-class :char #\Q)
1063 :font 'baskerville :pixel-size 10)
1064 'baskerville))
1066 ;;; class redefinition shouldn't give any warnings, in the usual case
1067 (defclass about-to-be-redefined () ((some-slot :accessor some-slot)))
1068 (handler-bind ((warning #'error))
1069 (defclass about-to-be-redefined () ((some-slot :accessor some-slot))))
1071 ;;; attempts to add accessorish methods to generic functions with more
1072 ;;; complex lambda lists should fail
1073 (defgeneric accessoroid (object &key &allow-other-keys))
1074 (assert-error
1075 (defclass accessoroid-class () ((slot :accessor accessoroid)))
1076 program-error)
1078 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1079 (defclass shared-slot-and-redefinition ()
1080 ((size :initarg :size :initform 1 :allocation :class)))
1081 (let ((i (make-instance 'shared-slot-and-redefinition)))
1082 (defclass shared-slot-and-redefinition ()
1083 ((size :initarg :size :initform 2 :allocation :class)))
1084 (assert (= (slot-value i 'size) 1)))
1086 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1087 (defclass superclass-born-to-be-obsoleted () (a))
1088 (defclass subclass-born-to-be-obsoleted (superclass-born-to-be-obsoleted) ())
1089 (defparameter *born-to-be-obsoleted*
1090 (make-instance 'subclass-born-to-be-obsoleted))
1091 (defparameter *born-to-be-obsoleted-obsoleted* nil)
1092 (defmethod update-instance-for-redefined-class
1093 ((o subclass-born-to-be-obsoleted) a d pl &key)
1094 (setf *born-to-be-obsoleted-obsoleted* t))
1095 (make-instances-obsolete 'superclass-born-to-be-obsoleted)
1096 (slot-boundp *born-to-be-obsoleted* 'a)
1097 (assert *born-to-be-obsoleted-obsoleted*)
1099 ;;; additional test suggested by Bruno Haible sbcl-devel 2004-04-21
1100 (defclass super-super-obsoleted () (a))
1101 (defclass super-obsoleted-1 (super-super-obsoleted) ())
1102 (defclass super-obsoleted-2 (super-super-obsoleted) ())
1103 (defclass obsoleted (super-obsoleted-1 super-obsoleted-2) ())
1104 (defparameter *obsoleted* (make-instance 'obsoleted))
1105 (defparameter *obsoleted-counter* 0)
1106 (defmethod update-instance-for-redefined-class ((o obsoleted) a d pl &key)
1107 (incf *obsoleted-counter*))
1108 (make-instances-obsolete 'super-super-obsoleted)
1109 (slot-boundp *obsoleted* 'a)
1110 (assert (= *obsoleted-counter* 1))
1112 ;;; yet another MAKE-INSTANCES-OBSOLETE test, this time from Nikodemus
1113 ;;; Siivola. Not all methods for accessing slots are created equal...
1114 (defclass yet-another-obsoletion-super () ((obs :accessor obs-of :initform 0)))
1115 (defclass yet-another-obsoletion-sub (yet-another-obsoletion-super) ())
1116 (defmethod shared-initialize :after ((i yet-another-obsoletion-super)
1117 slots &rest init)
1118 (incf (obs-of i)))
1120 (defvar *yao-super* (make-instance 'yet-another-obsoletion-super))
1121 (defvar *yao-sub* (make-instance 'yet-another-obsoletion-sub))
1123 (assert (= (obs-of *yao-super*) 1))
1124 (assert (= (obs-of *yao-sub*) 1))
1125 (make-instances-obsolete 'yet-another-obsoletion-super)
1126 (assert (= (obs-of *yao-sub*) 2))
1127 (assert (= (obs-of *yao-super*) 2))
1128 (make-instances-obsolete 'yet-another-obsoletion-super)
1129 (assert (= (obs-of *yao-super*) 3))
1130 (assert (= (obs-of *yao-sub*) 3))
1131 (assert (= (slot-value *yao-super* 'obs) 3))
1132 (assert (= (slot-value *yao-sub* 'obs) 3))
1134 ;;; one more MIO test: variable slot names
1135 (defclass mio () ((x :initform 42)))
1136 (defvar *mio-slot* 'x)
1137 (defparameter *mio-counter* 0)
1138 (defmethod update-instance-for-redefined-class ((instance mio) new old plist &key)
1139 (incf *mio-counter*))
1141 (let ((x (make-instance 'mio)))
1142 (make-instances-obsolete 'mio)
1143 (slot-value x *mio-slot*))
1145 (let ((x (make-instance 'mio)))
1146 (make-instances-obsolete 'mio)
1147 (setf (slot-value x *mio-slot*) 13))
1149 (let ((x (make-instance 'mio)))
1150 (make-instances-obsolete 'mio)
1151 (slot-boundp x *mio-slot*))
1153 (let ((x (make-instance 'mio)))
1154 (make-instances-obsolete 'mio)
1155 (slot-makunbound x *mio-slot*))
1157 (assert (= 4 *mio-counter*))
1159 ;;; shared -> local slot transfers of inherited slots, reported by
1160 ;;; Bruno Haible
1161 (let (i)
1162 (defclass super-with-magic-slot ()
1163 ((magic :initarg :size :initform 1 :allocation :class)))
1164 (defclass sub-of-super-with-magic-slot (super-with-magic-slot) ())
1165 (setq i (make-instance 'sub-of-super-with-magic-slot))
1166 (defclass super-with-magic-slot ()
1167 ((magic :initarg :size :initform 2)))
1168 (assert (= 1 (slot-value i 'magic))))
1170 ;;; MAKE-INSTANCES-OBSOLETE return values
1171 (defclass one-more-to-obsolete () ())
1172 (assert (eq 'one-more-to-obsolete
1173 (make-instances-obsolete 'one-more-to-obsolete)))
1174 (assert (eq (find-class 'one-more-to-obsolete)
1175 (make-instances-obsolete (find-class 'one-more-to-obsolete))))
1177 ;;; Sensible error instead of a BUG. Reported by Thomas Burdick.
1178 (multiple-value-bind (value err)
1179 (ignore-errors
1180 (defclass slot-def-with-duplicate-accessors ()
1181 ((slot :writer get-slot :reader get-slot))))
1182 (assert (typep err 'error))
1183 (assert (not (typep err 'sb-int:bug))))
1185 ;;; BUG 321: errors in parsing DEFINE-METHOD-COMBINATION arguments
1186 ;;; lambda lists.
1188 (define-method-combination w-args ()
1189 ((method-list *))
1190 (:arguments arg1 arg2 &aux (extra :extra))
1191 `(progn ,@(mapcar (lambda (method) `(call-method ,method)) method-list)))
1192 (defgeneric mc-test-w-args (p1 p2 s)
1193 (:method-combination w-args)
1194 (:method ((p1 number) (p2 t) s)
1195 (vector-push-extend (list 'number p1 p2) s))
1196 (:method ((p1 string) (p2 t) s)
1197 (vector-push-extend (list 'string p1 p2) s))
1198 (:method ((p1 t) (p2 t) s) (vector-push-extend (list t p1 p2) s)))
1199 (let ((v (make-array 0 :adjustable t :fill-pointer t)))
1200 (assert (= (mc-test-w-args 1 2 v) 1))
1201 (assert (equal (aref v 0) '(number 1 2)))
1202 (assert (equal (aref v 1) '(t 1 2))))
1204 ;;; BUG 276: declarations and mutation.
1205 (defmethod fee ((x fixnum))
1206 (setq x (/ x 2))
1208 (assert (= (fee 1) 1/2))
1209 (defmethod fum ((x fixnum))
1210 (setf x (/ x 2))
1212 (assert (= (fum 3) 3/2))
1213 (defmethod fii ((x fixnum))
1214 (declare (special x))
1215 (setf x (/ x 2))
1217 (assert (= (fii 1) 1/2))
1218 (defvar *faa*)
1219 (defmethod faa ((*faa* string-stream))
1220 (setq *faa* (make-broadcast-stream *faa*))
1221 (write-line "Break, you sucker!" *faa*)
1222 'ok)
1223 (assert (eq 'ok (faa (make-string-output-stream))))
1224 (defmethod fex ((x fixnum) (y fixnum))
1225 (multiple-value-setq (x y) (values (/ x y) (/ y x)))
1226 (list x y))
1227 (assert (equal (fex 5 3) '(5/3 3/5)))
1229 ;;; Bug reported by Zach Beane; incorrect return of (function
1230 ;;; ',fun-name) in defgeneric
1231 (assert
1232 (typep (funcall (compile nil
1233 '(lambda () (flet ((nonsense () nil))
1234 (defgeneric nonsense ())))))
1235 'generic-function))
1237 (assert
1238 (typep (funcall (compile nil
1239 '(lambda () (flet ((nonsense-2 () nil))
1240 (defgeneric nonsense-2 ()
1241 (:method () t))))))
1242 'generic-function))
1244 ;;; bug reported by Bruno Haible: (setf find-class) using a
1245 ;;; forward-referenced class
1246 (defclass fr-sub (fr-super) ())
1247 (setf (find-class 'fr-alt) (find-class 'fr-super))
1248 (assert (eq (find-class 'fr-alt) (find-class 'fr-super)))
1251 ;;; ANSI Figure 4-8: all defined classes. Check that we can define
1252 ;;; methods on all of these.
1253 (progn
1254 (defgeneric method-for-defined-classes (x))
1255 (dolist (c '(arithmetic-error
1256 generic-function simple-error array hash-table
1257 simple-type-error
1258 bit-vector integer simple-warning
1259 broadcast-stream list standard-class
1260 built-in-class logical-pathname standard-generic-function
1261 cell-error method standard-method
1262 character method-combination standard-object
1263 class null storage-condition
1264 complex number stream
1265 concatenated-stream package stream-error
1266 condition package-error string
1267 cons parse-error string-stream
1268 control-error pathname structure-class
1269 division-by-zero print-not-readable structure-object
1270 echo-stream program-error style-warning
1271 end-of-file random-state symbol
1272 error ratio synonym-stream
1273 file-error rational t
1274 file-stream reader-error two-way-stream
1275 float readtable type-error
1276 floating-point-inexact real unbound-slot
1277 floating-point-invalid-operation restart unbound-variable
1278 floating-point-overflow sequence undefined-function
1279 floating-point-underflow serious-condition vector
1280 function simple-condition warning))
1281 (eval `(defmethod method-for-defined-classes ((x ,c)) (princ x))))
1282 (assert (string= (with-output-to-string (*standard-output*)
1283 (method-for-defined-classes #\3))
1284 "3")))
1288 ;;; When class definition does not complete due to a bad accessor
1289 ;;; name, do not cause an error when a new accessor name is provided
1290 ;;; during class redefinition
1292 (defun existing-name (object)
1293 (list object))
1295 (assert-error (defclass redefinition-of-accessor-class ()
1296 ((slot :accessor existing-name))))
1298 (defclass redefinition-of-accessor-class ()
1299 ((slot :accessor new-name)))
1303 (load "package-ctor-bug.lisp")
1304 (assert (= (package-ctor-bug:test) 3))
1305 (delete-package "PACKAGE-CTOR-BUG")
1306 (load "package-ctor-bug.lisp")
1307 (assert (= (package-ctor-bug:test) 3))
1309 (with-test (:name (:defmethod (setf find-class) integer))
1310 (mapcar #'eval
1312 (deftype defined-type () 'integer)
1313 (assert-error
1314 (defmethod method-on-defined-type ((x defined-type)) x))
1315 (deftype defined-type-and-class () 'integer)
1316 (setf (find-class 'defined-type-and-class) (find-class 'integer))
1317 (defmethod method-on-defined-type-and-class
1318 ((x defined-type-and-class))
1319 (1+ x))
1320 (assert (= (method-on-defined-type-and-class 3) 4)))))
1322 ;; bug 281
1323 (let (#+nil ; no more sb-pcl::*max-emf-precomputation-methods* as of
1324 ; sbcl-1.0.41.x
1325 (sb-pcl::*max-emf-precomputation-methods* 0))
1326 (eval '(defgeneric bug-281 (x)
1327 (:method-combination +)
1328 (:method ((x symbol)) 1)
1329 (:method + ((x number)) x)))
1330 (assert (= 1 (bug-281 1)))
1331 (assert (= 4.2 (bug-281 4.2)))
1332 (multiple-value-bind (val err) (ignore-errors (bug-281 'symbol))
1333 (assert (not val))
1334 (assert (typep err 'error))))
1336 ;;; RESTART-CASE and CALL-METHOD
1338 ;;; from Bruno Haible
1340 (defun rc-cm/prompt-for-new-values ()
1341 (format *debug-io* "~&New values: ")
1342 (finish-output *debug-io*)
1343 (list (read *debug-io*)))
1345 (defun rc-cm/add-method-restarts (form method)
1346 (let ((block (gensym))
1347 (tag (gensym)))
1348 `(block ,block
1349 (tagbody
1350 ,tag
1351 (return-from ,block
1352 (restart-case ,form
1353 (method-redo ()
1354 :report (lambda (stream)
1355 (format stream "Try calling ~S again." ,method))
1356 (go ,tag))
1357 (method-return (l)
1358 :report (lambda (stream)
1359 (format stream "Specify return values for ~S call."
1360 ,method))
1361 :interactive (lambda () (rc-cm/prompt-for-new-values))
1362 (return-from ,block (values-list l)))))))))
1364 (defun rc-cm/convert-effective-method (efm)
1365 (if (consp efm)
1366 (if (eq (car efm) 'call-method)
1367 (let ((method-list (third efm)))
1368 (if (or (typep (first method-list) 'method) (rest method-list))
1369 ;; Reduce the case of multiple methods to a single one.
1370 ;; Make the call to the next-method explicit.
1371 (rc-cm/convert-effective-method
1372 `(call-method ,(second efm)
1373 ((make-method
1374 (call-method ,(first method-list) ,(rest method-list))))))
1375 ;; Now the case of at most one method.
1376 (if (typep (second efm) 'method)
1377 ;; Wrap the method call in a RESTART-CASE.
1378 (rc-cm/add-method-restarts
1379 (cons (rc-cm/convert-effective-method (car efm))
1380 (rc-cm/convert-effective-method (cdr efm)))
1381 (second efm))
1382 ;; Normal recursive processing.
1383 (cons (rc-cm/convert-effective-method (car efm))
1384 (rc-cm/convert-effective-method (cdr efm))))))
1385 (cons (rc-cm/convert-effective-method (car efm))
1386 (rc-cm/convert-effective-method (cdr efm))))
1387 efm))
1389 (define-method-combination standard-with-restarts ()
1390 ((around (:around))
1391 (before (:before))
1392 (primary () :required t)
1393 (after (:after)))
1394 (flet ((call-methods-sequentially (methods)
1395 (mapcar #'(lambda (method)
1396 `(call-method ,method))
1397 methods)))
1398 (let ((form (if (or before after (rest primary))
1399 `(multiple-value-prog1
1400 (progn
1401 ,@(call-methods-sequentially before)
1402 (call-method ,(first primary) ,(rest primary)))
1403 ,@(call-methods-sequentially (reverse after)))
1404 `(call-method ,(first primary)))))
1405 (when around
1406 (setq form
1407 `(call-method ,(first around)
1408 (,@(rest around) (make-method ,form)))))
1409 (rc-cm/convert-effective-method form))))
1411 (defgeneric rc-cm/testgf16 (x)
1412 (:method-combination standard-with-restarts))
1413 (defclass rc-cm/testclass16a () ())
1414 (defclass rc-cm/testclass16b (rc-cm/testclass16a) ())
1415 (defclass rc-cm/testclass16c (rc-cm/testclass16a) ())
1416 (defclass rc-cm/testclass16d (rc-cm/testclass16b rc-cm/testclass16c) ())
1417 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16a))
1418 (list 'a
1419 (not (null (find-restart 'method-redo)))
1420 (not (null (find-restart 'method-return)))))
1421 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16b))
1422 (cons 'b (call-next-method)))
1423 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16c))
1424 (cons 'c (call-next-method)))
1425 (defmethod rc-cm/testgf16 ((x rc-cm/testclass16d))
1426 (cons 'd (call-next-method)))
1427 (assert (equal (rc-cm/testgf16 (make-instance 'rc-cm/testclass16d))
1428 '(d b c a t t)))
1430 ;;; test case from Gerd Moellmann
1431 (define-method-combination r-c/c-m-1 ()
1432 ((primary () :required t))
1433 `(restart-case (call-method ,(first primary))))
1435 (defgeneric r-c/c-m-1-gf ()
1436 (:method-combination r-c/c-m-1)
1437 (:method () nil))
1439 (assert (null (r-c/c-m-1-gf)))
1441 (handler-bind ((warning #'error))
1442 (eval '(defclass class-for-ctor/class-slot ()
1443 ((class-slot :initarg :class-slot :allocation :class))))
1444 (eval '(let ((c1 (make-instance 'class-for-ctor/class-slot))
1445 (c2 (make-instance 'class-for-ctor/class-slot :class-slot 1)))
1446 (assert (equal (list (slot-value c1 'class-slot)
1447 (slot-value c2 'class-slot))
1448 (list 1 1))))))
1450 ;;; tests of ctors on anonymous classes
1451 (defparameter *unnamed* (defclass ctor-unnamed-literal-class () ()))
1452 (setf (class-name *unnamed*) nil)
1453 (setf (find-class 'ctor-unnamed-literal-class) nil)
1454 (defparameter *unnamed2* (defclass ctor-unnamed-literal-class2 () ()))
1455 (defun ctor-unnamed-literal-class ()
1456 (make-instance '#.*unnamed*))
1457 (compile 'ctor-unnamed-literal-class)
1458 (defun ctor-unnamed-literal-class2 ()
1459 (make-instance '#.(find-class 'ctor-unnamed-literal-class2)))
1460 (compile 'ctor-unnamed-literal-class2)
1461 (defun ctor-unnamed-literal-class2/symbol ()
1462 (make-instance 'ctor-unnamed-literal-class2))
1463 (compile 'ctor-unnamed-literal-class2/symbol)
1464 (setf (class-name *unnamed2*) nil)
1465 (setf (find-class 'ctor-unnamed-literal-class2) nil)
1466 (with-test (:name (:ctor :unnamed-before))
1467 (assert (typep (ctor-unnamed-literal-class) *unnamed*)))
1468 (with-test (:name (:ctor :unnamed-after))
1469 (assert (typep (ctor-unnamed-literal-class2) *unnamed2*)))
1470 (with-test (:name (:ctor :unnamed-after/symbol))
1471 (assert-error (ctor-unnamed-literal-class2/symbol)))
1473 ;;; classes with slot types shouldn't break if the types don't name
1474 ;;; classes (bug #391)
1475 (defclass slot-type-superclass () ((slot :type fixnum)))
1476 (defclass slot-type-subclass (slot-type-superclass)
1477 ((slot :type (integer 1 5))))
1478 (let ((instance (make-instance 'slot-type-subclass)))
1479 (setf (slot-value instance 'slot) 3))
1481 ;;; ctors where there's a non-standard SHARED-INITIALIZE method and an
1482 ;;; initarg which isn't self-evaluating (kpreid on #lisp 2006-01-29)
1483 (defclass kpreid-enode ()
1484 ((slot :initarg not-a-keyword)))
1485 (defmethod shared-initialize ((o kpreid-enode) slots &key &allow-other-keys)
1486 (call-next-method))
1487 (defun make-kpreid-enode ()
1488 (make-instance 'kpreid-enode 'not-a-keyword 3))
1489 (with-test (:name (:ctor :non-keyword-initarg))
1490 (let ((x (make-kpreid-enode))
1491 (y (make-kpreid-enode)))
1492 (= (slot-value x 'slot) (slot-value y 'slot))))
1494 ;;; defining a class hierarchy shouldn't lead to spurious classoid
1495 ;;; errors on TYPEP questions (reported by Tim Moore on #lisp
1496 ;;; 2006-03-10)
1497 (defclass backwards-2 (backwards-1) (a b))
1498 (defclass backwards-3 (backwards-2) ())
1499 (defun typep-backwards-3 (x)
1500 (typep x 'backwards-3))
1501 (defclass backwards-1 () (a b))
1502 (assert (not (typep-backwards-3 1)))
1503 (assert (not (typep-backwards-3 (make-instance 'backwards-2))))
1504 (assert (typep-backwards-3 (make-instance 'backwards-3)))
1506 (defgeneric remove-method-1 (x)
1507 (:method ((x integer)) (1+ x)))
1508 (defgeneric remove-method-2 (x)
1509 (:method ((x integer)) (1- x)))
1510 (assert (eq #'remove-method-1
1511 (remove-method #'remove-method-1
1512 (find-method #'remove-method-2
1514 (list (find-class 'integer))))))
1515 (assert (= (remove-method-1 3) 4))
1516 (assert (= (remove-method-2 3) 2))
1518 ;;; ANSI doesn't require these restarts, but now that we have them we
1519 ;;; better test them too.
1520 (defclass slot-unbound-restart-test () ((x)))
1521 (let ((test (make-instance 'slot-unbound-restart-test)))
1522 (assert (not (slot-boundp test 'x)))
1523 (assert (= 42 (handler-bind ((unbound-slot (lambda (c) (use-value 42 c))))
1524 (slot-value test 'x))))
1525 (assert (not (slot-boundp test 'x)))
1526 (assert (= 13 (handler-bind ((unbound-slot (lambda (c) (store-value 13 c))))
1527 (slot-value test 'x))))
1528 (assert (= 13 (slot-value test 'x))))
1530 ;;; Using class instances as specializers, reported by Pascal Costanza, ref CLHS 7.6.2
1531 (defclass class-as-specializer-test ()
1533 (eval `(defmethod class-as-specializer-test1 ((x ,(find-class 'class-as-specializer-test)))
1534 'foo))
1535 (assert (eq 'foo (class-as-specializer-test1 (make-instance 'class-as-specializer-test))))
1536 (funcall (compile nil `(lambda ()
1537 (defmethod class-as-specializer-test2 ((x ,(find-class 'class-as-specializer-test)))
1538 'bar))))
1539 (assert (eq 'bar (class-as-specializer-test2 (make-instance 'class-as-specializer-test))))
1541 ;;; CHANGE-CLASS and tricky allocation.
1542 (defclass foo-to-be-changed ()
1543 ((a :allocation :class :initform 1)))
1544 (defclass bar-to-be-changed (foo-to-be-changed) ())
1545 (defvar *bar-to-be-changed* (make-instance 'bar-to-be-changed))
1546 (defclass baz-to-be-changed ()
1547 ((a :allocation :instance :initform 2)))
1548 (change-class *bar-to-be-changed* 'baz-to-be-changed)
1549 (assert (= (slot-value *bar-to-be-changed* 'a) 1))
1551 ;;; proper name and class redefinition
1552 (defvar *to-be-renamed1* (defclass to-be-renamed1 () ()))
1553 (defvar *to-be-renamed2* (defclass to-be-renamed2 () ()))
1554 (setf (find-class 'to-be-renamed1) (find-class 'to-be-renamed2))
1555 (defvar *renamed1* (defclass to-be-renamed1 () ()))
1556 (assert (not (eq *to-be-renamed1* *to-be-renamed2*)))
1557 (assert (not (eq *to-be-renamed1* *renamed1*)))
1558 (assert (not (eq *to-be-renamed2* *renamed1*)))
1560 ;;; CLASS-NAME (and various other standardized generic functions) have
1561 ;;; their effective methods precomputed; in the process of rearranging
1562 ;;; (SETF FIND-CLASS) and FINALIZE-INHERITANCE, this broke.
1563 (defclass class-with-odd-class-name-method ()
1564 ((a :accessor class-name)))
1566 ;;; another case where precomputing (this time on PRINT-OBJECT) and
1567 ;;; lazily-finalized classes caused problems. (report from James Y
1568 ;;; Knight sbcl-devel 20-07-2006)
1570 (defclass base-print-object () ())
1571 ;;; this has the side-effect of finalizing BASE-PRINT-OBJECT, and
1572 ;;; additionally the second specializer (STREAM) changes the cache
1573 ;;; structure to require two keys, not just one.
1574 (defmethod print-object ((o base-print-object) (s stream))
1575 nil)
1577 ;;; unfinalized as yet
1578 (defclass sub-print-object (base-print-object) ())
1579 ;;; the accessor causes an eager finalization
1580 (defclass subsub-print-object (sub-print-object)
1581 ((a :accessor a)))
1583 ;;; triggers a discriminating function (and so cache) recomputation.
1584 ;;; The method on BASE-PRINT-OBJECT will cause the system to attempt
1585 ;;; to fill the cache for all subclasses of BASE-PRINT-OBJECT which
1586 ;;; have valid wrappers; however, in the course of doing so, the
1587 ;;; SUB-PRINT-OBJECT class gets finalized, which invalidates the
1588 ;;; SUBSUB-PRINT-OBJECT wrapper; if an invalid wrapper gets into a
1589 ;;; cache with more than one key, then failure ensues.
1590 (reinitialize-instance #'print-object)
1592 ;;; bug in long-form method combination: if there's an applicable
1593 ;;; method not part of any method group, we need to call
1594 ;;; INVALID-METHOD-ERROR. (MC27 test case from Bruno Haible)
1595 (define-method-combination mc27 ()
1596 ((normal ())
1597 (ignored (:ignore :unused)))
1598 `(list 'result
1599 ,@(mapcar #'(lambda (method) `(call-method ,method)) normal)))
1600 (defgeneric test-mc27 (x)
1601 (:method-combination mc27)
1602 (:method :ignore ((x number)) (/ 0)))
1603 (assert-error (test-mc27 7))
1605 (define-method-combination mc27prime ()
1606 ((normal ())
1607 (ignored (:ignore)))
1608 `(list 'result ,@(mapcar (lambda (m) `(call-method ,m)) normal)))
1609 (defgeneric test-mc27prime (x)
1610 (:method-combination mc27prime)
1611 (:method :ignore ((x number)) (/ 0)))
1612 (assert (equal '(result) (test-mc27prime 3)))
1613 (assert-error (test-mc27 t)) ; still no-applicable-method
1615 ;;; more invalid wrappers. This time for a long-standing bug in the
1616 ;;; compiler's expansion for TYPEP on various class-like things, with
1617 ;;; user-visible consequences.
1618 (defclass obsolete-again () ())
1619 (defvar *obsolete-again* (make-instance 'obsolete-again))
1620 (defvar *obsolete-again-hash* (sxhash *obsolete-again*))
1621 (make-instances-obsolete (find-class 'obsolete-again))
1622 (assert (not (streamp *obsolete-again*)))
1623 (make-instances-obsolete (find-class 'obsolete-again))
1624 (assert (= (sxhash *obsolete-again*) *obsolete-again-hash*))
1625 (compile (defun is-a-structure-object-p (x) (typep x 'structure-object)))
1626 (make-instances-obsolete (find-class 'obsolete-again))
1627 (assert (not (is-a-structure-object-p *obsolete-again*)))
1629 ;;; overeager optimization of slot-valuish things
1630 (defclass listoid ()
1631 ((caroid :initarg :caroid)
1632 (cdroid :initarg :cdroid :initform nil)))
1633 (defmethod lengthoid ((x listoid))
1634 (let ((result 0))
1635 (loop until (null x)
1636 do (incf result) (setq x (slot-value x 'cdroid)))
1637 result))
1638 (with-test (:name ((:setq :method-parameter) slot-value))
1639 (assert (= (lengthoid (make-instance 'listoid)) 1))
1640 (assert (= (lengthoid
1641 (make-instance 'listoid :cdroid
1642 (make-instance 'listoid :cdroid
1643 (make-instance 'listoid))))
1644 3)))
1648 ;;;; Tests for argument parsing in fast-method-functions.
1650 (defvar *foo* 0)
1652 (eval-when (:compile-toplevel :load-toplevel :execute)
1653 (setf (symbol-value 'a) 'invalid))
1655 (defmacro test1 (lambda-list values args &key declarations cnm)
1656 `(progn
1657 (fmakunbound 'll-method)
1658 (fmakunbound 'll-function)
1659 (defmethod ll-method ,lambda-list
1660 ,@declarations
1661 ,@(when cnm
1662 `((when nil (call-next-method))))
1663 (list ,@values))
1664 (defun ll-function ,lambda-list
1665 ,@declarations
1666 (list ,@values))
1667 (dotimes (i 2)
1668 (assert (equal (ll-method ,@args)
1669 (ll-function ,@args))))))
1671 (defmacro test (&rest args)
1672 `(progn
1673 (test1 ,@args :cnm nil)
1674 (test1 ,@args :cnm t)))
1676 ;; Just plain arguments
1678 (test (a) (a) (1))
1679 (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))
1681 (test (*foo*) (*foo* (symbol-value '*foo*)) (1))
1683 (test (a) (a (symbol-value 'a)) (1)
1684 :declarations ((declare (special a))))
1686 ;; Optionals
1688 (test (a &optional b c) (a b c) (1))
1689 (test (a &optional b c) (a b c) (1 2))
1690 (test (a &optional b c) (a b c) (1 2 3))
1692 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1))
1693 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2))
1694 (test (a &optional (b 'b b-p) (c 'c c-p)) (a b c b-p c-p) (1 2 3))
1696 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) ())
1697 (test (&optional *foo*) (*foo* (symbol-value '*foo*)) (1))
1699 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) ())
1700 (test (&optional (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p) (1))
1702 (test (&optional a) (a (symbol-value 'a)) ()
1703 :declarations ((declare (special a))))
1704 (test (&optional a) (a (symbol-value 'a)) (1)
1705 :declarations ((declare (special a))))
1707 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) ()
1708 :declarations ((declare (special a))))
1709 (test (&optional (a 'z a-p)) (a (symbol-value 'a) a-p) (1)
1710 :declarations ((declare (special a))))
1712 (defparameter *count* 0)
1714 (test (&optional (a (incf *count*)) (b (incf *count*)))
1715 (a b *count* (setf *count* 0))
1718 ;; Keywords with some &RESTs thrown in
1720 (dolist (args '((1)
1721 (1 :b 2)
1722 (1 :c 3)
1723 (1 :b 2 :c 3)
1724 (1 :c 3 :b 2)
1725 (1 :c 3 :c 1 :b 2 :b 4)))
1726 (eval `(test (a &key b c) (a b c) ,args))
1727 (eval `(test (a &key (b 'b b-p) (c 'c c-p))
1728 (a b c b-p c-p)
1729 ,args))
1730 (eval `(test (a &rest rest &key (b 'b b-p) (c 'c c-p))
1731 (a b c b-p c-p rest)
1732 ,args))
1733 (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1734 (a b c b-p c-p *foo* (symbol-value '*foo*))
1735 ,args))
1736 (eval `(test (a &rest *foo* &key (b 'b b-p) (c 'c c-p))
1737 (a b c b-p c-p *foo* (symbol-value '*foo*))
1738 ,args
1739 :declarations ((declare (special b-p))))))
1741 (dolist (args '(()
1742 (:*foo* 1)
1743 (:*foo* 1 :*foo* 2)))
1744 (eval `(test (&key *foo*) (*foo* (symbol-value '*foo*)) ,args))
1745 (eval `(test (&key (*foo* 'z foo-p)) (*foo* (symbol-value '*foo*) foo-p)
1746 ,args))
1747 (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1748 ,args))
1749 (eval `(test (&key ((:*foo* a) 'z foo-p)) (a (symbol-value 'a) foo-p)
1750 ,args
1751 :declarations ((declare (special a))))))
1753 (defparameter *count* 0)
1755 (test (&key (a (incf *count*)) (b (incf *count*)))
1756 (a b *count* (setf *count* 0))
1759 (test (&key a b &allow-other-keys) (a b) (:a 1 :b 2 :c 3))
1761 (defmethod clim-style-lambda-list-test (a b &optional c d &key x y)
1762 (list a b c d x y))
1764 (clim-style-lambda-list-test 1 2)
1766 (setf *count* 0)
1768 (test (&aux (a (incf *count*)) (b (incf *count*)))
1769 (a b *count* (setf *count* 0))
1772 ;;;; long-form method combination with &rest in :arguments
1773 ;;;; (this had a bug what with fixed in 1.0.4.something)
1774 (define-method-combination long-form-with-&rest ()
1775 ((methods *))
1776 (:arguments x &rest others)
1777 `(progn
1778 ,@(mapcar (lambda (method)
1779 `(call-method ,method))
1780 methods)
1781 (list ,x (length ,others))))
1783 (defgeneric test-long-form-with-&rest (x &rest others)
1784 (:method-combination long-form-with-&rest))
1786 (defmethod test-long-form-with-&rest (x &rest others)
1787 nil)
1789 (assert (equal '(:foo 13)
1790 (apply #'test-long-form-with-&rest :foo (make-list 13))))
1792 ;;;; slot-missing for non-standard classes on SLOT-VALUE
1793 ;;;;
1794 ;;;; FIXME: This is arguably not right, actually: CLHS seems to say
1795 ;;;; we should just signal an error at least for built-in classes, but
1796 ;;;; for a while we were hitting NO-APPLICABLE-METHOD, which is definitely
1797 ;;;; wrong -- so test this for now at least.
1799 (defvar *magic-symbol* (gensym "MAGIC"))
1801 (set *magic-symbol* 42)
1803 (defmethod slot-missing (class instance (slot-name (eql *magic-symbol*)) op
1804 &optional new)
1805 (if (eq 'setf op)
1806 (setf (symbol-value *magic-symbol*) new)
1807 (symbol-value *magic-symbol*)))
1809 (assert (eql 42 (slot-value (cons t t) *magic-symbol*)))
1810 (assert (eql 13 (setf (slot-value 123 *magic-symbol*) 13)))
1811 (assert (eql 13 (slot-value 'foobar *magic-symbol*)))
1813 ;;;; Built-in structure and condition layouts should have NIL in
1814 ;;;; LAYOUT-FOR-STD-CLASS-P, and classes should have T.
1816 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'warning))))
1817 (assert (not (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'hash-table))))
1818 (assert (eq t (sb-pcl::layout-for-std-class-p (sb-pcl::find-layout 'standard-object))))
1820 ;;;; bug 402: PCL used to warn about non-standard declarations
1821 (declaim (declaration bug-402-d))
1822 (defgeneric bug-402-gf (x))
1823 (with-test (:name :bug-402)
1824 (handler-bind ((warning #'error))
1825 (eval '(defmethod bug-402-gf (x)
1826 (declare (bug-402-d x))
1827 x))))
1829 ;;;; non-keyword :default-initargs + :before method on shared initialize
1830 ;;;; interacted badly with CTOR optimizations
1831 (defclass ctor-default-initarg-problem ()
1832 ((slot :initarg slotto))
1833 (:default-initargs slotto 123))
1834 (defmethod shared-initialize :before ((instance ctor-default-initarg-problem) slot-names &rest initargs)
1835 (format t "~&Rock on: ~A~%" initargs))
1836 (defun provoke-ctor-default-initarg-problem ()
1837 (make-instance 'ctor-default-initarg-problem))
1838 (handler-bind ((warning #'error))
1839 (assert (= 123 (slot-value (provoke-ctor-default-initarg-problem) 'slot))))
1841 ;;;; discriminating net on streams used to generate code deletion notes on
1842 ;;;; first call
1843 (defgeneric stream-fd (stream direction))
1844 (defmethod stream-fd ((stream sb-sys:fd-stream) direction)
1845 (declare (ignore direction))
1846 (sb-sys:fd-stream-fd stream))
1847 (defmethod stream-fd ((stream synonym-stream) direction)
1848 (stream-fd (symbol-value (synonym-stream-symbol stream)) direction))
1849 (defmethod stream-fd ((stream two-way-stream) direction)
1850 (ecase direction
1851 (:input
1852 (stream-fd
1853 (two-way-stream-input-stream stream) direction))
1854 (:output
1855 (stream-fd
1856 (two-way-stream-output-stream stream) direction))))
1857 (with-test (:name (:discriminating-name :code-deletion-note))
1858 (handler-bind ((compiler-note #'error))
1859 (stream-fd sb-sys:*stdin* :output)
1860 (stream-fd sb-sys:*stdin* :output)))
1862 (with-test (:name :bug-380)
1863 (defclass bug-380 ()
1864 ((slot :accessor bug380-slot)))
1865 (fmakunbound 'foo-slot)
1866 (defgeneric foo-slot (x y z))
1867 (defclass foo ()
1868 ((slot :accessor foo-slot-value))))
1870 ;;; SET and (SETF SYMBOL-VALUE) used to confuse permuation vector
1871 ;;; optimizations
1872 (defclass fih ()
1873 ((x :initform :fih)))
1874 (defclass fah ()
1875 ((x :initform :fah)))
1876 (declaim (special *fih*))
1877 (defmethod fihfah ((*fih* fih))
1878 (set '*fih* (make-instance 'fah))
1879 (list (slot-value *fih* 'x)
1880 (eval '(slot-value *fih* 'x))))
1881 (defmethod fihfah ((fah fah))
1882 (declare (special fah))
1883 (set 'fah (make-instance 'fih))
1884 (list (slot-value fah 'x)
1885 (eval '(slot-value fah 'x))))
1886 (with-test (:name :set-of-a-method-specializer)
1887 (assert (equal '(:fah :fah) (fihfah (make-instance 'fih))))
1888 (assert (equal '(:fih :fih) (fihfah (make-instance 'fah)))))
1890 (defmethod no-implicit-declarations-for-local-specials ((faax double-float))
1891 (declare (special faax))
1892 (set 'faax (when (< faax 0) (- faax)))
1893 faax)
1894 (with-test (:name :no-implicit-declarations-for-local-specials)
1895 (assert (not (no-implicit-declarations-for-local-specials 1.0d0))))
1897 (defstruct bug-357-a
1898 slot1
1899 (slot2 t)
1900 (slot3 (coerce pi 'single-float) :type single-float))
1901 (defclass bug-357-b (bug-357-a)
1902 ((slot2 :initform 't2)
1903 (slot4 :initform -44)
1904 (slot5)
1905 (slot6 :initform t)
1906 (slot7 :initform (floor (* pi pi)))
1907 (slot8 :initform 88))
1908 (:metaclass structure-class))
1909 (defstruct (bug-357-c (:include bug-357-b (slot8 -88) (slot5 :ok)))
1910 slot9
1911 (slot10 t)
1912 (slot11 (floor (exp 3))))
1913 (with-test (:name :bug-357)
1914 (flet ((slots (x)
1915 (list (bug-357-c-slot1 x)
1916 (bug-357-c-slot2 x)
1917 (bug-357-c-slot3 x)
1918 (bug-357-c-slot4 x)
1919 (bug-357-c-slot5 x)
1920 (bug-357-c-slot6 x)
1921 (bug-357-c-slot7 x)
1922 (bug-357-c-slot8 x)
1923 (bug-357-c-slot9 x)
1924 (bug-357-c-slot10 x)
1925 (bug-357-c-slot11 x))))
1926 (let ((base (slots (make-bug-357-c))))
1927 (assert (equal base (slots (make-instance 'bug-357-c))))
1928 (assert (equal base '(nil t2 3.1415927 -44 :ok t 9 -88 nil t 20))))))
1930 (defclass class-slot-shared-initialize ()
1931 ((a :allocation :class :initform :ok)))
1932 (with-test (:name :class-slot-shared-initialize)
1933 (let ((x (make-instance 'class-slot-shared-initialize)))
1934 (assert (eq :ok (slot-value x 'a)))
1935 (slot-makunbound x 'a)
1936 (assert (not (slot-boundp x 'a)))
1937 (shared-initialize x '(a))
1938 (assert (slot-boundp x 'a))
1939 (assert (eq :ok (slot-value x 'a)))))
1941 (declaim (ftype (function (t t t) (values single-float &optional))
1942 i-dont-want-to-be-clobbered-1
1943 i-dont-want-to-be-clobbered-2))
1944 (defgeneric i-dont-want-to-be-clobbered-1 (t t t))
1945 (defmethod i-dont-want-to-be-clobbered-2 ((x cons) y z)
1947 (defun i-cause-an-gf-info-update ()
1948 (i-dont-want-to-be-clobbered-2 t t t))
1949 (with-test (:name :defgeneric-should-clobber-ftype)
1950 ;; (because it doesn't check the argument or result types)
1951 (assert (equal '(function (t t t) *)
1952 (sb-kernel:type-specifier
1953 (sb-int:info :function
1954 :type 'i-dont-want-to-be-clobbered-1))))
1955 (assert (equal '(function (t t t) *)
1956 (sb-kernel:type-specifier
1957 (sb-int:info :function
1958 :type 'i-dont-want-to-be-clobbered-2))))
1959 (assert (eq :defined-method
1960 (sb-int:info :function
1961 :where-from 'i-dont-want-to-be-clobbered-1)))
1962 (assert (eq :defined-method
1963 (sb-int:info :function
1964 :where-from 'i-dont-want-to-be-clobbered-2))))
1966 (with-test (:name :bogus-parameter-specializer-name-error)
1967 (assert (eq :ok
1968 (handler-case
1969 (eval `(defmethod #:fii ((x "a string")) 'string))
1970 (sb-int:reference-condition (c)
1971 (when (member '(:ansi-cl :macro defmethod)
1972 (sb-int:reference-condition-references c)
1973 :test #'equal)
1974 :ok))))))
1976 (defclass remove-default-initargs-test ()
1977 ((x :initarg :x :initform 42)))
1978 (defclass remove-default-initatgs-test ()
1979 ((x :initarg :x :initform 42))
1980 (:default-initargs :x 0))
1981 (defclass remove-default-initargs-test ()
1982 ((x :initarg :x :initform 42)))
1983 (with-test (:name :remove-default-initargs)
1984 (assert (= 42 (slot-value (make-instance 'remove-default-initargs-test)
1985 'x))))
1987 (with-test (:name :bug-485019)
1988 ;; there was a bug in WALK-SETQ, used in method body walking, in the
1989 ;; presence of declarations on symbol macros.
1990 (defclass bug-485019 ()
1991 ((array :initarg :array)))
1992 (defmethod bug-485019 ((bug-485019 bug-485019))
1993 (with-slots (array) bug-485019
1994 (declare (type (or null simple-array) array))
1995 (setf array (make-array 4)))
1996 bug-485019)
1997 (bug-485019 (make-instance 'bug-485019)))
1999 ;;; The compiler didn't propagate the declarared type before applying
2000 ;;; the transform for (SETF SLOT-VALUE), so the generic accessor was used.
2001 (defstruct foo-520366
2002 slot)
2003 (defun quux-520366 (cont)
2004 (funcall cont))
2005 (defun bar-520366 (foo-struct)
2006 (declare (type foo-520366 foo-struct))
2007 (with-slots (slot) foo-struct
2008 (tagbody
2009 (quux-520366 #'(lambda ()
2010 (setf slot :value)
2011 (go TAG)))
2012 TAG)))
2013 (with-test (:name :bug-520366)
2014 (let ((callees (find-named-callees #'bar-520366)))
2015 (assert (equal (list #'quux-520366) callees))))
2017 (defgeneric no-applicable-method/retry (x))
2018 (defmethod no-applicable-method/retry ((x string))
2019 "string")
2020 (with-test (:name :no-applicable-method/retry)
2021 (assert (equal "cons"
2022 (handler-bind ((error
2023 (lambda (c)
2024 (declare (ignore c))
2025 (let ((r (find-restart 'sb-pcl::retry)))
2026 (when r
2027 (eval `(defmethod no-applicable-method/retry ((x cons))
2028 "cons"))
2029 (invoke-restart r))))))
2030 (no-applicable-method/retry (cons t t))))))
2032 (defgeneric no-primary-method/retry (x))
2033 (defmethod no-primary-method/retry :before (x) (assert x))
2034 (with-test (:name :no-primary-method/retry)
2035 (assert (equal "ok!"
2036 (handler-bind ((error
2037 (lambda (c)
2038 (declare (ignore c))
2039 (let ((r (find-restart 'sb-pcl::retry)))
2040 (when r
2041 (eval `(defmethod no-primary-method/retry (x)
2042 "ok!"))
2043 (invoke-restart r))))))
2044 (no-primary-method/retry (cons t t))))))
2046 ;;; test that a cacheing strategy for make-instance initargs checking
2047 ;;; can handle class redefinitions
2048 (defclass cacheing-initargs-redefinitions-check ()
2049 ((slot :initarg :slot)))
2050 (defun cacheing-initargs-redefinitions-check-fun (&optional (initarg :slot))
2051 (declare (notinline make-instance))
2052 (make-instance 'cacheing-initargs-redefinitions-check)
2053 (make-instance 'cacheing-initargs-redefinitions-check initarg 3))
2054 (with-test (:name :make-instance-initargs)
2055 (make-instance 'cacheing-initargs-redefinitions-check)
2056 (make-instance 'cacheing-initargs-redefinitions-check :slot 3)
2057 (cacheing-initargs-redefinitions-check-fun :slot)
2058 (assert-error (cacheing-initargs-redefinitions-check-fun :slot2)))
2059 (defclass cacheing-initargs-redefinitions-check ()
2060 ((slot :initarg :slot2)))
2061 (with-test (:name :make-instance-redefined-initargs)
2062 (make-instance 'cacheing-initargs-redefinitions-check)
2063 (make-instance 'cacheing-initargs-redefinitions-check :slot2 3)
2064 (cacheing-initargs-redefinitions-check-fun :slot2)
2065 (assert-error (cacheing-initargs-redefinitions-check-fun :slot)))
2066 (defmethod initialize-instance :after ((class cacheing-initargs-redefinitions-check) &key slot)
2067 nil)
2068 (with-test (:name :make-instance-new-method-initargs)
2069 (make-instance 'cacheing-initargs-redefinitions-check)
2070 (make-instance 'cacheing-initargs-redefinitions-check :slot2 3)
2071 (cacheing-initargs-redefinitions-check-fun :slot2)
2072 (let ((thing (cacheing-initargs-redefinitions-check-fun :slot)))
2073 (assert (not (slot-boundp thing 'slot)))))
2075 (with-test (:name :defmethod-specializer-builtin-class-alias)
2076 (let ((alias (gensym)))
2077 (setf (find-class alias) (find-class 'symbol))
2078 (eval `(defmethod lp-618387 ((s ,alias))
2079 (symbol-name s)))
2080 (assert (equal "FOO" (funcall 'lp-618387 :foo)))))
2082 (with-test (:name :pcl-spurious-ignore-warnings)
2083 (defgeneric no-spurious-ignore-warnings (req &key key))
2084 (handler-bind ((warning (lambda (x) (error "~A" x))))
2085 (eval
2086 '(defmethod no-spurious-ignore-warnings ((req number) &key key)
2087 (declare (ignore key))
2088 (check-type req integer))))
2089 (defgeneric should-get-an-ignore-warning (req &key key))
2090 (let ((warnings 0))
2091 (handler-bind ((warning (lambda (c) (setq warnings 1) (muffle-warning c))))
2092 (eval '(defmethod should-get-an-ignore-warning ((req integer) &key key)
2093 (check-type req integer))))
2094 (assert (= warnings 1))))
2096 (defgeneric generic-function-pretty-arglist-optional-and-key (req &optional opt &key key)
2097 (:method (req &optional opt &key key)
2098 (list req opt key)))
2100 (with-test (:name :generic-function-pretty-arglist-optional-and-key)
2101 (handler-bind ((warning #'error))
2102 ;; Used to signal a style-warning
2103 (assert (equal '(req &optional opt &key key)
2104 (sb-pcl::generic-function-pretty-arglist
2105 #'generic-function-pretty-arglist-optional-and-key)))))
2107 (with-test (:name :bug-894202)
2108 (assert (eq :good
2109 (handler-case
2110 (let ((name (gensym "FOO"))
2111 (decl (gensym "BAR")))
2112 (eval `(defgeneric ,name ()
2113 (declare (,decl)))))
2114 (warning ()
2115 :good)))))
2117 (with-test (:name :bug-898331)
2118 (handler-bind ((warning #'error))
2119 (eval `(defgeneric bug-898331 (request type remaining-segment-requests all-requests)))
2120 (eval `(defmethod bug-898331 ((request cons) (type (eql :cancel))
2121 remaining-segment-requests
2122 all-segment-requests)
2123 (declare (ignore all-segment-requests))
2124 (check-type request t)))))
2126 (with-test (:name :bug-1001799)
2127 ;; compilation of the defmethod used to cause infinite recursion
2128 (let ((pax (gensym "PAX"))
2129 (pnr (gensym "PNR"))
2130 (sup (gensym "SUP"))
2131 (frob (gensym "FROB"))
2132 (sb-ext:*evaluator-mode* :compile))
2133 (eval
2134 `(progn
2135 (declaim (optimize (speed 1) (space 1) (safety 3) (debug 3) (compilation-speed 1)))
2136 (defclass ,pax (,sup)
2137 ((,pnr :type (or null ,pnr))))
2138 (defclass ,pnr (,sup)
2139 ((,pax :type (or null ,pax))))
2140 (defclass ,sup ()
2142 (defmethod ,frob ((pnr ,pnr))
2143 (slot-value pnr ',pax))))))
2145 (with-test (:name :bug-1099708)
2146 (defclass bug-1099708 ()
2147 ((slot-1099708 :initarg :slot-1099708)))
2148 ;; caused infinite equal testing in function name lookup
2149 (eval
2150 '(progn
2151 (defun make-1099708-1 ()
2152 (make-instance 'bug-1099708 :slot-1099708 '#1= (1 2 . #1#)))
2153 (defun make-1099708-2 ()
2154 (make-instance 'bug-1099708 :slot-1099708 '#2= (1 2 . #2#)))))
2155 (assert (not (eql (slot-value (make-1099708-1) 'slot-1099708)
2156 (slot-value (make-1099708-2) 'slot-1099708)))))
2158 (with-test (:name :bug-1099708b-list)
2159 (defclass bug-1099708b-list ()
2160 ((slot-1099708b-list :initarg :slot-1099708b-list)))
2161 (eval
2162 '(progn
2163 (defun make-1099708b-list-1 ()
2164 (make-instance 'bug-1099708b-list :slot-1099708b-list '(some value)))
2165 (defun make-1099708b-list-2 ()
2166 (make-instance 'bug-1099708b-list :slot-1099708b-list '(some value)))))
2167 (assert (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list)
2168 (slot-value (make-1099708b-list-1) 'slot-1099708b-list)))
2169 (assert (eql (slot-value (make-1099708b-list-2) 'slot-1099708b-list)
2170 (slot-value (make-1099708b-list-2) 'slot-1099708b-list)))
2171 (assert (not (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list)
2172 (slot-value (make-1099708b-list-2) 'slot-1099708b-list)))))
2174 (with-test (:name :bug-1099708b-string)
2175 (defclass bug-1099708b-string ()
2176 ((slot-1099708b-string :initarg :slot-1099708b-string)))
2177 (eval
2178 '(progn
2179 (defun make-1099708b-string-1 ()
2180 (make-instance 'bug-1099708b-string :slot-1099708b-string "string"))
2181 (defun make-1099708b-string-2 ()
2182 (make-instance 'bug-1099708b-string :slot-1099708b-string "string"))))
2183 (assert (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string)
2184 (slot-value (make-1099708b-string-1) 'slot-1099708b-string)))
2185 (assert (eql (slot-value (make-1099708b-string-2) 'slot-1099708b-string)
2186 (slot-value (make-1099708b-string-2) 'slot-1099708b-string)))
2187 (assert (not (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string)
2188 (slot-value (make-1099708b-string-2) 'slot-1099708b-string)))))
2190 (with-test (:name :bug-1099708b-bitvector)
2191 (defclass bug-1099708b-bitvector ()
2192 ((slot-1099708b-bitvector :initarg :slot-1099708b-bitvector)))
2193 (eval
2194 '(progn
2195 (defun make-1099708b-bitvector-1 ()
2196 (make-instance 'bug-1099708b-bitvector :slot-1099708b-bitvector #*1011))
2197 (defun make-1099708b-bitvector-2 ()
2198 (make-instance 'bug-1099708b-bitvector :slot-1099708b-bitvector #*1011))))
2199 (assert (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)
2200 (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)))
2201 (assert (eql (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)
2202 (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)))
2203 (assert (not (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector)
2204 (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector)))))
2206 (with-test (:name :bug-1099708b-pathname)
2207 (defclass bug-1099708b-pathname ()
2208 ((slot-1099708b-pathname :initarg :slot-1099708b-pathname)))
2209 (eval
2210 '(progn
2211 (defun make-1099708b-pathname-1 ()
2212 (make-instance 'bug-1099708b-pathname :slot-1099708b-pathname #p"pn"))
2213 (defun make-1099708b-pathname-2 ()
2214 (make-instance 'bug-1099708b-pathname :slot-1099708b-pathname #p"pn"))))
2215 (assert (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)
2216 (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)))
2217 (assert (eql (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)
2218 (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)))
2219 (assert (not (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname)
2220 (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname)))))
2222 (with-test (:name :bug-1099708c-list)
2223 (defclass bug-1099708c-list ()
2224 ((slot-1099708c-list :initarg :slot-1099708c-list)))
2225 (eval
2226 '(progn
2227 (defun make-1099708c-list-1 ()
2228 (make-instance 'bug-1099708c-list :slot-1099708c-list #1='(some value)))
2229 (defun make-1099708c-list-2 ()
2230 (make-instance 'bug-1099708c-list :slot-1099708c-list #1#))))
2231 (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list)
2232 (slot-value (make-1099708c-list-1) 'slot-1099708c-list)))
2233 (assert (eql (slot-value (make-1099708c-list-2) 'slot-1099708c-list)
2234 (slot-value (make-1099708c-list-2) 'slot-1099708c-list)))
2235 (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list)
2236 (slot-value (make-1099708c-list-2) 'slot-1099708c-list))))
2238 ;;; bug-1179858
2240 ;;; Define a class and force the "fallback" constructor generator to be
2241 ;;; used by having a HAIRY-AROUND-OR-NONSTANDARD-PRIMARY-METHOD-P on
2242 ;;; SHARED-INITIALIZE.
2243 (defclass bug-1179858 ()
2244 ((foo :initarg :foo :reader bug-1179858-foo))
2245 (:default-initargs :foo (error "Should not be evaluated")))
2246 (defmethod shared-initialize :around ((instance bug-1179858) (slot-names t) &key)
2247 (call-next-method))
2249 (with-test (:name (:make-instance :fallback-generator-initarg-handling :bug-1179858))
2250 ;; Now compile a lambda containing MAKE-INSTANCE to exercise the
2251 ;; fallback constructor generator. Call the resulting compiled
2252 ;; function to trigger the bug.
2253 (funcall (compile nil '(lambda () (make-instance 'bug-1179858 :foo t)))))
2255 ;;; Other brokenness, found while investigating: fallback-generator
2256 ;;; handling of non-keyword initialization arguments
2257 (defclass bug-1179858b ()
2258 ((foo :initarg foo :reader bug-1179858b-foo))
2259 (:default-initargs foo 14))
2260 (defmethod shared-initialize :around ((instance bug-1179858b) (slot-names t) &key)
2261 (call-next-method))
2263 (with-test (:name (:make-instance :fallback-generator-non-keyword-initarg :bug-1179858))
2264 (flet ((foo= (n i) (= (bug-1179858b-foo i) n)))
2265 (assert
2266 (foo= 14 (funcall (compile nil '(lambda () (make-instance 'bug-1179858b))))))
2267 (assert
2268 (foo= 15 (funcall (compile nil '(lambda () (make-instance 'bug-1179858b 'foo 15))))))))
2270 (with-test (:name (:cpl-violation-setup :bug-309076))
2271 (assert-error
2272 (progn
2273 (defclass bug-309076-broken-class (standard-class) ()
2274 (:metaclass sb-mop:funcallable-standard-class))
2275 (sb-mop:finalize-inheritance (find-class 'bug-309076-broken-class)))))
2277 (with-test (:name (:cpl-violation-irrelevant-class :bug-309076))
2278 (defclass bug-309076-class (standard-class) ())
2279 (defmethod sb-mop:validate-superclass ((x bug-309076-class) (y standard-class)) t)
2280 (assert (typep (make-instance 'bug-309076-class) 'bug-309076-class)))
2283 (eval-when (:compile-toplevel :load-toplevel :execute)
2284 (require 'sb-cltl2)
2285 (defmethod b ()))
2287 (defmacro macro ()
2288 (let ((a 20))
2289 (declare (special a))
2290 (assert
2292 (funcall
2293 (compile nil
2294 (sb-mop:make-method-lambda
2296 (find-method #'b () ())
2297 '(lambda () (declare (special a)) a)
2298 nil))
2299 '(1) ())
2300 20))))
2302 (with-test (:name :make-method-lambda-leakage)
2303 ;; lambda list of X leaks into the invocation of make-method-lambda
2304 ;; during code-walking performed by make-method-lambda invoked by
2305 ;; DEFMETHOD
2306 (sb-cltl2:macroexpand-all '(defmethod x (a) (macro))))
2308 (with-test (:name (:defmethod-undefined-function :bug-503095))
2309 (flet ((test-load (file)
2310 (let (implicit-gf-warning)
2311 (handler-bind
2312 ((sb-ext:implicit-generic-function-warning
2313 (lambda (x)
2314 (setf implicit-gf-warning x)
2315 (muffle-warning x)))
2316 ((or warning error) #'error))
2317 (load file))
2318 (assert implicit-gf-warning))))
2319 (multiple-value-bind (fasl warnings errorsp) (compile-file "bug-503095.lisp")
2320 (unwind-protect
2321 (progn (assert (and fasl (not warnings) (not errorsp)))
2322 (test-load fasl))
2323 (and fasl (delete-file fasl))))
2324 (test-load "bug-503095-2.lisp")))
2326 (with-test (:name :accessor-and-plain-method)
2327 (defclass a-633911 ()
2328 ((x-633911 :initform nil
2329 :accessor x-633911)))
2331 (defmethod x-633911 ((b a-633911)) 10)
2333 (defclass b-633911 ()
2334 ((x-633911 :initform nil
2335 :accessor x-633911)))
2337 (assert (= (x-633911 (make-instance 'a-633911)) 10)))
2339 (with-test (:name (built-in-class :subclass error))
2340 (flet ((mapper (c)
2341 (when (and (class-name c) (typep c 'built-in-class))
2342 (assert-error (eval `(defclass ,(gensym) (,(class-name c))
2343 ()))))))
2344 (sb-pcl::map-all-classes #'mapper)))
2346 ;;;; success