00aa376d7b26128a9a7c9b6c525b4fbfbfeaba5b
[sbcl.git] / tests / defstruct.impure.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;;
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
11
12 (load "assertoid.lisp")
13 (load "compiler-test-util.lisp")
14 (use-package "ASSERTOID")
15 \f
16 ;;;; examples from, or close to, the Common Lisp DEFSTRUCT spec
17
18 ;;; Type mismatch of slot default init value isn't an error until the
19 ;;; default init value is actually used. (The justification is
20 ;;; somewhat bogus, but the requirement is clear.)
21 (defstruct person age (name 007 :type string)) ; not an error until 007 used
22 (make-person :name "James") ; not an error, 007 not used
23
24 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
25 (assert (raises-error? (make-person) type-error))
26 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
27 (assert (raises-error? (setf (person-name (make-person :name "Q")) 1)
28                        type-error))
29
30 ;;; An &AUX variable in a boa-constructor without a default value
31 ;;; means "do not initialize slot" and does not cause type error
32 (declaim (notinline opaque-identity))
33 (defun opaque-identity (x) x)
34
35 (defstruct (boa-saux (:constructor make-boa-saux (&aux a (b 3) (c))))
36     (a #\! :type (integer 1 2))
37     (b #\? :type (integer 3 4))
38     (c #\# :type (integer 5 6)))
39 (let ((s (make-boa-saux)))
40   (locally (declare (optimize (safety 3))
41                     (inline boa-saux-a))
42     (assert (raises-error? (opaque-identity (boa-saux-a s)) type-error)))
43   (setf (boa-saux-a s) 1)
44   (setf (boa-saux-c s) 5)
45   (assert (eql (boa-saux-a s) 1))
46   (assert (eql (boa-saux-b s) 3))
47   (assert (eql (boa-saux-c s) 5)))
48                                         ; these two checks should be
49                                         ; kept separated
50
51 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
52 (let ((s (make-boa-saux)))
53   (locally (declare (optimize (safety 0))
54                     (inline boa-saux-a))
55     (assert (eql (opaque-identity (boa-saux-a s)) 0)))
56   (setf (boa-saux-a s) 1)
57   (setf (boa-saux-c s) 5)
58   (assert (eql (boa-saux-a s) 1))
59   (assert (eql (boa-saux-b s) 3))
60   (assert (eql (boa-saux-c s) 5)))
61
62 (let ((s (make-boa-saux)))
63   (locally (declare (optimize (safety 3))
64                     (notinline boa-saux-a))
65     (assert (raises-error? (opaque-identity (boa-saux-a s)) type-error)))
66   (setf (boa-saux-a s) 1)
67   (setf (boa-saux-c s) 5)
68   (assert (eql (boa-saux-a s) 1))
69   (assert (eql (boa-saux-b s) 3))
70   (assert (eql (boa-saux-c s) 5)))
71
72 ;;; basic inheritance
73 (defstruct (astronaut (:include person)
74                       (:conc-name astro-))
75   helmet-size
76   (favorite-beverage 'tang))
77 (let ((x (make-astronaut :name "Buzz" :helmet-size 17.5)))
78   (assert (equal (person-name x) "Buzz"))
79   (assert (equal (astro-name x) "Buzz"))
80   (assert (eql (astro-favorite-beverage x) 'tang))
81   (assert (null (astro-age x))))
82 (defstruct (ancient-astronaut (:include person (age 77)))
83   helmet-size
84   (favorite-beverage 'tang))
85 (assert (eql (ancient-astronaut-age (make-ancient-astronaut :name "John")) 77))
86
87 ;;; interaction of :TYPE and :INCLUDE and :INITIAL-OFFSET
88 (defstruct (binop (:type list) :named (:initial-offset 2))
89   (operator '? :type symbol)
90   operand-1
91   operand-2)
92 (defstruct (annotated-binop (:type list)
93                             (:initial-offset 3)
94                             (:include binop))
95   commutative associative identity)
96 (assert (equal (make-annotated-binop :operator '*
97                                      :operand-1 'x
98                                      :operand-2 5
99                                      :commutative t
100                                      :associative t
101                                      :identity 1)
102                '(nil nil binop * x 5 nil nil nil t t 1)))
103
104 ;;; effect of :NAMED on :TYPE
105 (defstruct (named-binop (:type list) :named)
106   (operator '? :type symbol)
107   operand-1
108   operand-2)
109 (let ((named-binop (make-named-binop :operator '+ :operand-1 'x :operand-2 5)))
110   ;; The data representation is specified to look like this.
111   (assert (equal named-binop '(named-binop + x 5)))
112   ;; A meaningful NAMED-BINOP-P is defined.
113   (assert (named-binop-p named-binop))
114   (assert (named-binop-p (copy-list named-binop)))
115   (assert (not (named-binop-p (cons 11 named-binop))))
116   (assert (not (named-binop-p (find-package :cl)))))
117
118 ;;; example 1
119 (defstruct town
120   area
121   watertowers
122   (firetrucks 1 :type fixnum)
123   population
124   (elevation 5128 :read-only t))
125 (let ((town1 (make-town :area 0 :watertowers 0)))
126   (assert (town-p town1))
127   (assert (not (town-p 1)))
128   (assert (eql (town-area town1) 0))
129   (assert (eql (town-elevation town1) 5128))
130   (assert (null (town-population town1)))
131   (setf (town-population town1) 99)
132   (assert (eql (town-population town1) 99))
133   (let ((town2 (copy-town town1)))
134     (dolist (slot-accessor-name '(town-area
135                                   town-watertowers
136                                   town-firetrucks
137                                   town-population
138                                   town-elevation))
139       (assert (eql (funcall slot-accessor-name town1)
140                    (funcall slot-accessor-name town2))))
141     (assert (not (fboundp '(setf town-elevation)))))) ; 'cause it's :READ-ONLY
142
143 ;;; example 2
144 (defstruct (clown (:conc-name bozo-))
145   (nose-color 'red)
146   frizzy-hair-p
147   polkadots)
148 (let ((funny-clown (make-clown)))
149   (assert (eql (bozo-nose-color funny-clown) 'red)))
150 (defstruct (klown (:constructor make-up-klown)
151                   (:copier clone-klown)
152                   (:predicate is-a-bozo-p))
153   nose-color
154   frizzy-hair-p
155   polkadots)
156 (assert (is-a-bozo-p (make-up-klown)))
157 \f
158 ;;;; systematically testing variants of DEFSTRUCT:
159 ;;;;   * native, :TYPE LIST, and :TYPE VECTOR
160
161 ;;; FIXME: things to test:
162 ;;;   * Slot readers work.
163 ;;;   * Slot writers work.
164 ;;;   * Predicates work.
165
166 ;;; FIXME: things that would be nice to test systematically someday:
167 ;;;   * constructors (default, boa..)
168 ;;;   * copiers
169 ;;;   * no type checks when (> SPEED SAFETY)
170 ;;;   * Tests of inclusion would be good. (It's tested very lightly
171 ;;;     above, and then tested a fair amount by the system compiling
172 ;;;     itself.)
173
174 (defun string+ (&rest rest)
175   (apply #'concatenate 'string
176          (mapcar #'string rest)))
177 (defun symbol+ (&rest rest)
178   (values (intern (apply #'string+ rest))))
179
180 (defun accessor-name (conc-name slot-name)
181   (symbol+ conc-name slot-name))
182
183 ;;; Use the ordinary FDEFINITIONs of accessors (not inline expansions)
184 ;;; to read and write a structure slot.
185 (defun read-slot-notinline (conc-name slot-name instance)
186   (funcall (accessor-name conc-name slot-name) instance))
187 (defun write-slot-notinline (new-value conc-name slot-name instance)
188   (funcall (fdefinition `(setf ,(accessor-name conc-name slot-name)))
189            new-value instance))
190
191 ;;; Use inline expansions of slot accessors, if possible, to read and
192 ;;; write a structure slot.
193 (defun read-slot-inline (conc-name slot-name instance)
194   (funcall (compile nil
195                     `(lambda (instance)
196                        (,(accessor-name conc-name slot-name) instance)))
197            instance))
198 (defun write-slot-inline (new-value conc-name slot-name instance)
199   (funcall (compile nil
200                     `(lambda (new-value instance)
201                        (setf (,(accessor-name conc-name slot-name) instance)
202                              new-value)))
203            new-value
204            instance))
205
206 ;;; Read a structure slot, checking that the inline and out-of-line
207 ;;; accessors give the same result.
208 (defun read-slot (conc-name slot-name instance)
209   (let ((inline-value (read-slot-inline conc-name slot-name instance))
210         (notinline-value (read-slot-notinline conc-name slot-name instance)))
211     (assert (eql inline-value notinline-value))
212     inline-value))
213
214 ;;; Write a structure slot, using INLINEP argument to decide
215 ;;; on inlineness of accessor used.
216 (defun write-slot (new-value conc-name slot-name instance inlinep)
217   (if inlinep
218       (write-slot-inline new-value conc-name slot-name instance)
219       (write-slot-notinline new-value conc-name slot-name instance)))
220
221 ;;; bound during the tests so that we can get to it even if the
222 ;;; debugger is having a bad day
223 (defvar *instance*)
224
225 (declaim (optimize (debug 2)))
226
227 (defmacro test-variant (defstructname &key colontype boa-constructor-p)
228   `(progn
229
230      (format t "~&/beginning PROGN for COLONTYPE=~S~%" ',colontype)
231
232      (defstruct (,defstructname
233                   ,@(when colontype `((:type ,colontype)))
234                   ,@(when boa-constructor-p
235                           `((:constructor ,(symbol+ "CREATE-" defstructname)
236                              (id
237                               &optional
238                               (optional-test 2 optional-test-p)
239                               &key
240                               (home nil home-p)
241                               (no-home-comment "Home package CL not provided.")
242                               (comment (if home-p "" no-home-comment))
243                               (refcount (if optional-test-p optional-test nil))
244                               hash
245                               weight)))))
246
247        ;; some ordinary tagged slots
248        id
249        (home nil :type package :read-only t)
250        (comment "" :type simple-string)
251        ;; some raw slots
252        (weight 1.0 :type single-float)
253        (hash 1 :type (integer 1 #.(* 3 most-positive-fixnum)) :read-only t)
254        ;; more ordinary tagged slots
255        (refcount 0 :type (and unsigned-byte fixnum)))
256
257      (format t "~&/done with DEFSTRUCT~%")
258
259      (let* ((cn (string+ ',defstructname "-")) ; conc-name
260             (ctor (symbol-function ',(symbol+ (if boa-constructor-p
261                                                "CREATE-"
262                                                "MAKE-")
263                                              defstructname)))
264             (*instance* (funcall ctor
265                                  ,@(unless boa-constructor-p
266                                            `(:id)) "some id"
267                                  ,@(when boa-constructor-p
268                                          '(1))
269                                  :home (find-package :cl)
270                                  :hash (+ 14 most-positive-fixnum)
271                                  ,@(unless boa-constructor-p
272                                            `(:refcount 1)))))
273
274        ;; Check that ctor set up slot values correctly.
275        (format t "~&/checking constructed structure~%")
276        (assert (string= "some id" (read-slot cn "ID" *instance*)))
277        (assert (eql (find-package :cl) (read-slot cn "HOME" *instance*)))
278        (assert (string= "" (read-slot cn "COMMENT" *instance*)))
279        (assert (= 1.0 (read-slot cn "WEIGHT" *instance*)))
280        (assert (eql (+ 14 most-positive-fixnum)
281                     (read-slot cn "HASH" *instance*)))
282        (assert (= 1 (read-slot cn "REFCOUNT" *instance*)))
283
284        ;; There should be no writers for read-only slots.
285        (format t "~&/checking no read-only writers~%")
286        (assert (not (fboundp `(setf ,(symbol+ cn "HOME")))))
287        (assert (not (fboundp `(setf ,(symbol+ cn "HASH")))))
288        ;; (Read-only slot values are checked in the loop below.)
289
290        (dolist (inlinep '(t nil))
291          (format t "~&/doing INLINEP=~S~%" inlinep)
292          ;; Fiddle with writable slot values.
293          (let ((new-id (format nil "~S" (random 100)))
294                (new-comment (format nil "~X" (random 5555)))
295                (new-weight (random 10.0)))
296            (write-slot new-id cn "ID" *instance* inlinep)
297            (write-slot new-comment cn "COMMENT" *instance* inlinep)
298            (write-slot new-weight cn "WEIGHT" *instance* inlinep)
299            (assert (eql new-id (read-slot cn "ID" *instance*)))
300            (assert (eql new-comment (read-slot cn "COMMENT" *instance*)))
301            ;;(unless (eql new-weight (read-slot cn "WEIGHT" *instance*))
302            ;;  (error "WEIGHT mismatch: ~S vs. ~S"
303            ;;         new-weight (read-slot cn "WEIGHT" *instance*)))
304            (assert (eql new-weight (read-slot cn "WEIGHT" *instance*)))))
305        (format t "~&/done with INLINEP loop~%")
306
307        ;; :TYPE FOO objects don't go in the Lisp type system, so we
308        ;; can't test TYPEP stuff for them.
309        ;;
310        ;; FIXME: However, when they're named, they do define
311        ;; predicate functions, and we could test those.
312        ,@(unless colontype
313            `(;; Fiddle with predicate function.
314              (let ((pred-name (symbol+ ',defstructname "-P")))
315                (format t "~&/doing tests on PRED-NAME=~S~%" pred-name)
316                (assert (funcall pred-name *instance*))
317                (assert (not (funcall pred-name 14)))
318                (assert (not (funcall pred-name "test")))
319                (assert (not (funcall pred-name (make-hash-table))))
320                (let ((compiled-pred
321                       (compile nil `(lambda (x) (,pred-name x)))))
322                  (format t "~&/doing COMPILED-PRED tests~%")
323                  (assert (funcall compiled-pred *instance*))
324                  (assert (not (funcall compiled-pred 14)))
325                  (assert (not (funcall compiled-pred #()))))
326                ;; Fiddle with TYPEP.
327                (format t "~&/doing TYPEP tests, COLONTYPE=~S~%" ',colontype)
328                (assert (typep *instance* ',defstructname))
329                (assert (not (typep 0 ',defstructname)))
330                (assert (funcall (symbol+ "TYPEP") *instance* ',defstructname))
331                (assert (not (funcall (symbol+ "TYPEP") nil ',defstructname)))
332                (let* ((typename ',defstructname)
333                       (compiled-typep
334                        (compile nil `(lambda (x) (typep x ',typename)))))
335                  (assert (funcall compiled-typep *instance*))
336                  (assert (not (funcall compiled-typep nil))))))))
337
338      (format t "~&/done with PROGN for COLONTYPE=~S~%" ',colontype)))
339
340 (test-variant vanilla-struct)
341 (test-variant vector-struct :colontype vector)
342 (test-variant list-struct :colontype list)
343 (test-variant vanilla-struct :boa-constructor-p t)
344 (test-variant vector-struct :colontype vector :boa-constructor-p t)
345 (test-variant list-struct :colontype list :boa-constructor-p t)
346
347 \f
348 ;;;; testing raw slots harder
349 ;;;;
350 ;;;; The offsets of raw slots need to be rescaled during the punning
351 ;;;; process which is used to access them. That seems like a good
352 ;;;; place for errors to lurk, so we'll try hunting for them by
353 ;;;; verifying that all the raw slot data gets written successfully
354 ;;;; into the object, can be copied with the object, and can then be
355 ;;;; read back out (with none of it ending up bogusly outside the
356 ;;;; object, so that it couldn't be copied, or bogusly overwriting
357 ;;;; some other raw slot).
358
359 (defstruct manyraw
360   (a (expt 2 30) :type (unsigned-byte #.sb-vm:n-word-bits))
361   (b 0.1 :type single-float)
362   (c 0.2d0 :type double-float)
363   (d #c(0.3 0.3) :type (complex single-float))
364   unraw-slot-just-for-variety
365   (e #c(0.4d0 0.4d0) :type (complex double-float))
366   (aa (expt 2 30) :type (unsigned-byte #.sb-vm:n-word-bits))
367   (bb 0.1 :type single-float)
368   (cc 0.2d0 :type double-float)
369   (dd #c(0.3 0.3) :type (complex single-float))
370   (ee #c(0.4d0 0.4d0) :type (complex double-float)))
371
372 (defvar *manyraw* (make-manyraw))
373
374 (assert (eql (manyraw-a *manyraw*) (expt 2 30)))
375 (assert (eql (manyraw-b *manyraw*) 0.1))
376 (assert (eql (manyraw-c *manyraw*) 0.2d0))
377 (assert (eql (manyraw-d *manyraw*) #c(0.3 0.3)))
378 (assert (eql (manyraw-e *manyraw*) #c(0.4d0 0.4d0)))
379 (assert (eql (manyraw-aa *manyraw*) (expt 2 30)))
380 (assert (eql (manyraw-bb *manyraw*) 0.1))
381 (assert (eql (manyraw-cc *manyraw*) 0.2d0))
382 (assert (eql (manyraw-dd *manyraw*) #c(0.3 0.3)))
383 (assert (eql (manyraw-ee *manyraw*) #c(0.4d0 0.4d0)))
384
385 (setf (manyraw-aa *manyraw*) (expt 2 31)
386       (manyraw-bb *manyraw*) 0.11
387       (manyraw-cc *manyraw*) 0.22d0
388       (manyraw-dd *manyraw*) #c(0.33 0.33)
389       (manyraw-ee *manyraw*) #c(0.44d0 0.44d0))
390
391 (let ((copy (copy-manyraw *manyraw*)))
392   (assert (eql (manyraw-a copy) (expt 2 30)))
393   (assert (eql (manyraw-b copy) 0.1))
394   (assert (eql (manyraw-c copy) 0.2d0))
395   (assert (eql (manyraw-d copy) #c(0.3 0.3)))
396   (assert (eql (manyraw-e copy) #c(0.4d0 0.4d0)))
397   (assert (eql (manyraw-aa copy) (expt 2 31)))
398   (assert (eql (manyraw-bb copy) 0.11))
399   (assert (eql (manyraw-cc copy) 0.22d0))
400   (assert (eql (manyraw-dd copy) #c(0.33 0.33)))
401   (assert (eql (manyraw-ee copy) #c(0.44d0 0.44d0))))
402
403 \f
404 ;;;; Since GC treats raw slots specially now, let's try this with more objects
405 ;;;; and random values as a stress test.
406
407 (setf *manyraw* nil)
408
409 (defconstant +n-manyraw+ 10)
410 (defconstant +m-manyraw+ 1000)
411
412 (defun check-manyraws (manyraws)
413   (assert (eql (length manyraws) (* +n-manyraw+ +m-manyraw+)))
414   (loop
415       for m in (reverse manyraws)
416       for i from 0
417       do
418         ;; Compare the tagged reference values with raw reffer results.
419         (destructuring-bind (j a b c d e)
420             (manyraw-unraw-slot-just-for-variety m)
421           (assert (eql i j))
422           (assert (= (manyraw-a m) a))
423           (assert (= (manyraw-b m) b))
424           (assert (= (manyraw-c m) c))
425           (assert (= (manyraw-d m) d))
426           (assert (= (manyraw-e m) e)))
427         ;; Test the funny out-of-line OAOOM-style closures, too.
428         (mapcar (lambda (fn value)
429                   (assert (= (funcall fn m) value)))
430                 (list #'manyraw-a
431                       #'manyraw-b
432                       #'manyraw-c
433                       #'manyraw-d
434                       #'manyraw-e)
435                 (cdr (manyraw-unraw-slot-just-for-variety m)))))
436
437 (defstruct (manyraw-subclass (:include manyraw))
438   (stolperstein 0 :type (unsigned-byte 32)))
439
440 ;;; create lots of manyraw objects, triggering GC every now and then
441 (dotimes (y +n-manyraw+)
442   (dotimes (x +m-manyraw+)
443     (let ((a (random (expt 2 32)))
444           (b (random most-positive-single-float))
445           (c (random most-positive-double-float))
446           (d (complex
447               (random most-positive-single-float)
448               (random most-positive-single-float)))
449           (e (complex
450               (random most-positive-double-float)
451               (random most-positive-double-float))))
452       (push (funcall (if (zerop (mod x 3))
453                          #'make-manyraw-subclass
454                          #'make-manyraw)
455                      :unraw-slot-just-for-variety
456                      (list (+ x (* y +m-manyraw+)) a b c d e)
457                      :a a
458                      :b b
459                      :c c
460                      :d d
461                      :e e)
462             *manyraw*)))
463   (room)
464   (sb-ext:gc))
465 (with-test (:name :defstruct-raw-slot-gc)
466   (check-manyraws *manyraw*))
467
468 ;;; try a full GC, too
469 (sb-ext:gc :full t)
470 (with-test (:name (:defstruct-raw-slot-gc :full))
471   (check-manyraws *manyraw*))
472
473 ;;; fasl dumper and loader also have special handling of raw slots, so
474 ;;; dump all of them into a fasl
475 (defmethod make-load-form ((self manyraw) &optional env)
476   self env
477   :sb-just-dump-it-normally)
478 (with-open-file (s "tmp-defstruct.manyraw.lisp"
479                  :direction :output
480                  :if-exists :supersede)
481   (write-string "(defun dumped-manyraws () '#.*manyraw*)" s))
482 (compile-file "tmp-defstruct.manyraw.lisp")
483 (delete-file "tmp-defstruct.manyraw.lisp")
484
485 ;;; nuke the objects and try another GC just to be extra careful
486 (setf *manyraw* nil)
487 (sb-ext:gc :full t)
488
489 ;;; re-read the dumped structures and check them
490 (load "tmp-defstruct.manyraw.fasl")
491 (with-test (:name (:defstruct-raw-slot load))
492   (check-manyraws (dumped-manyraws)))
493
494 \f
495 ;;;; miscellaneous old bugs
496
497 (defstruct ya-struct)
498 (when (ignore-errors (or (ya-struct-p) 12))
499   (error "YA-STRUCT-P of no arguments should signal an error."))
500 (when (ignore-errors (or (ya-struct-p 'too 'many 'arguments) 12))
501   (error "YA-STRUCT-P of three arguments should signal an error."))
502
503 ;;; bug 210: Until sbcl-0.7.8.32 BOA constructors had SAFETY 0
504 ;;; declared inside on the theory that slot types were already
505 ;;; checked, which bogusly suppressed unbound-variable and other
506 ;;; checks within the evaluation of initforms.
507 (defvar *bug210*)
508 (defstruct (bug210a (:constructor bug210a ()))
509   (slot *bug210*))
510 (defstruct bug210b
511   (slot *bug210*))
512 ;;; Because of bug 210, this assertion used to fail.
513 (assert (typep (nth-value 1 (ignore-errors (bug210a))) 'unbound-variable))
514 ;;; Even with bug 210, these assertions succeeded.
515 (assert (typep (nth-value 1 (ignore-errors *bug210*)) 'unbound-variable))
516 (assert (typep (nth-value 1 (ignore-errors (make-bug210b))) 'unbound-variable))
517
518 ;;; In sbcl-0.7.8.53, DEFSTRUCT blew up in non-toplevel contexts
519 ;;; because it implicitly assumed that EVAL-WHEN (COMPILE) stuff
520 ;;; setting up compiler-layout information would run before the
521 ;;; constructor function installing the layout was compiled. Make sure
522 ;;; that doesn't happen again.
523 (defun foo-0-7-8-53 () (defstruct foo-0-7-8-53 x (y :not)))
524 (assert (not (find-class 'foo-0-7-8-53 nil)))
525 (foo-0-7-8-53)
526 (assert (find-class 'foo-0-7-8-53 nil))
527 (let ((foo-0-7-8-53 (make-foo-0-7-8-53 :x :s)))
528   (assert (eq (foo-0-7-8-53-x foo-0-7-8-53) :s))
529   (assert (eq (foo-0-7-8-53-y foo-0-7-8-53) :not)))
530 \f
531 ;;; tests of behaviour of colliding accessors.
532 (defstruct (bug127-foo (:conc-name bug127-baz-)) a)
533 (assert (= (bug127-baz-a (make-bug127-foo :a 1)) 1))
534 (defstruct (bug127-bar (:conc-name bug127-baz-) (:include bug127-foo)) b)
535 (assert (= (bug127-baz-a (make-bug127-bar :a 1 :b 2)) 1))
536 (assert (= (bug127-baz-b (make-bug127-bar :a 1 :b 2)) 2))
537 (assert (= (bug127-baz-a (make-bug127-foo :a 1)) 1))
538
539 (defun bug127-flurble (x)
540   x)
541 (defstruct bug127 flurble)
542 (assert (= (bug127-flurble (make-bug127 :flurble 7)) 7))
543
544 (defstruct bug127-a b-c)
545 (assert (= (bug127-a-b-c (make-bug127-a :b-c 9)) 9))
546 (defstruct (bug127-a-b (:include bug127-a)) c)
547 (assert (= (bug127-a-b-c (make-bug127-a :b-c 9)) 9))
548 (assert (= (bug127-a-b-c (make-bug127-a-b :b-c 11 :c 13)) 11))
549
550 (defstruct (bug127-e (:conc-name bug127--)) foo)
551 (assert (= (bug127--foo (make-bug127-e :foo 3)) 3))
552 (defstruct (bug127-f (:conc-name bug127--)) foo)
553 (assert (= (bug127--foo (make-bug127-f :foo 3)) 3))
554 (assert (raises-error? (bug127--foo (make-bug127-e :foo 3)) type-error))
555
556 ;;; FIXME: should probably do the same tests on DEFSTRUCT :TYPE
557 \f
558 ;;; As noted by Paul Dietz for CMUCL, :CONC-NAME handling was a little
559 ;;; too fragile:
560 (defstruct (conc-name-syntax :conc-name) a-conc-name-slot)
561 (assert (eq (a-conc-name-slot (make-conc-name-syntax :a-conc-name-slot 'y))
562             'y))
563 ;;; and further :CONC-NAME NIL was being wrongly treated:
564 (defpackage "DEFSTRUCT-TEST-SCRATCH")
565 (defstruct (conc-name-nil :conc-name)
566   defstruct-test-scratch::conc-name-nil-slot)
567 (assert (= (defstruct-test-scratch::conc-name-nil-slot
568             (make-conc-name-nil :conc-name-nil-slot 1)) 1))
569 (assert (raises-error? (conc-name-nil-slot (make-conc-name-nil))
570                        undefined-function))
571 \f
572 ;;; The named/typed predicates were a little fragile, in that they
573 ;;; could throw errors on innocuous input:
574 (defstruct (list-struct (:type list) :named) a-slot)
575 (assert (list-struct-p (make-list-struct)))
576 (assert (not (list-struct-p nil)))
577 (assert (not (list-struct-p 1)))
578 (defstruct (offset-list-struct (:type list) :named (:initial-offset 1)) a-slot)
579 (assert (offset-list-struct-p (make-offset-list-struct)))
580 (assert (not (offset-list-struct-p nil)))
581 (assert (not (offset-list-struct-p 1)))
582 (assert (not (offset-list-struct-p '(offset-list-struct))))
583 (assert (not (offset-list-struct-p '(offset-list-struct . 3))))
584 (defstruct (vector-struct (:type vector) :named) a-slot)
585 (assert (vector-struct-p (make-vector-struct)))
586 (assert (not (vector-struct-p nil)))
587 (assert (not (vector-struct-p #())))
588 \f
589
590 ;;; bug 3d: type safety with redefined type constraints on slots
591 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
592 (macrolet
593     ((test (type)
594        (let* ((base-name (intern (format nil "bug3d-~A" type)))
595               (up-name (intern (format nil "~A-up" base-name)))
596               (accessor (intern (format nil "~A-X" base-name)))
597               (up-accessor (intern (format nil "~A-X" up-name)))
598               (type-options (when type `((:type ,type)))))
599          `(progn
600             (defstruct (,base-name ,@type-options)
601               x y)
602             (defstruct (,up-name (:include ,base-name
603                                            (x "x" :type simple-string)
604                                            (y "y" :type simple-string))
605                                  ,@type-options))
606             (let ((ob (,(intern (format nil "MAKE-~A" up-name)))))
607               (setf (,accessor ob) 0)
608               (loop for decl in '(inline notinline)
609                     for fun = `(lambda (s)
610                                  (declare (optimize (safety 3))
611                                           (,decl ,',up-accessor))
612                                  (,',up-accessor s))
613                     do (assert (raises-error? (funcall (compile nil fun) ob)
614                                               type-error))))))))
615   (test nil)
616   (test list)
617   (test vector))
618
619 (let* ((name (gensym))
620        (form `(defstruct ,name
621                 (x nil :type (or null (function (integer)
622                                                 (values number &optional foo)))))))
623   (eval (copy-tree form))
624   (eval (copy-tree form)))
625
626 ;;; 322: "DEFSTRUCT :TYPE LIST predicate and improper lists"
627 ;;; reported by Bruno Haible sbcl-devel "various SBCL bugs" from CLISP
628 ;;; test suite.
629 (defstruct (bug-332a (:type list) (:initial-offset 5) :named))
630 (defstruct (bug-332b (:type list) (:initial-offset 2) :named (:include bug-332a)))
631 (assert (not (bug-332b-p (list* nil nil nil nil nil 'foo73 nil 'tail))))
632 (assert (not (bug-332b-p 873257)))
633 (assert (not (bug-332b-p '(1 2 3 4 5 x 1 2 bug-332a))))
634 (assert (bug-332b-p '(1 2 3 4 5 x 1 2 bug-332b)))
635
636 ;;; Similar test for vectors, just for good measure.
637 (defstruct (bug-332a-aux (:type vector)
638                          (:initial-offset 5) :named))
639 (defstruct (bug-332b-aux (:type vector)
640                          (:initial-offset 2) :named
641                          (:include bug-332a-aux)))
642 (assert (not (bug-332b-aux-p #(1 2 3 4 5 x 1 premature-end))))
643 (assert (not (bug-332b-aux-p 873257)))
644 (assert (not (bug-332b-aux-p #(1 2 3 4 5 x 1 2 bug-332a-aux))))
645 (assert (bug-332b-aux-p #(1 2 3 4 5 x 1 2 bug-332b-aux)))
646
647 ;;; In sbcl-0.8.11.8 FBOUNDPness potential collisions of structure
648 ;;; slot accessors signalled a condition at macroexpansion time, not
649 ;;; when the code was actually compiled or loaded.
650 (let ((defstruct-form '(defstruct bug-in-0-8-11-8 x)))
651   (defun bug-in-0-8-11-8-x (z) (print "some unrelated thing"))
652   (handler-case (macroexpand defstruct-form)
653     (warning (c)
654       (error "shouldn't warn just from macroexpansion here"))))
655
656 ;;; bug 318 symptom no 1. (rest not fixed yet)
657 (catch :ok
658   (handler-bind ((error (lambda (c)
659                           ;; Used to cause stack-exhaustion
660                           (unless (typep c 'storage-condition)
661                             (throw :ok t)))))
662     (eval '(progn
663             (defstruct foo a)
664             (setf (find-class 'foo) nil)
665             (defstruct foo slot-1)))))
666
667 ;;; bug 348, evaluation order of slot writer arguments. Fixed by Gabor
668 ;;; Melis.
669 (defstruct bug-348 x)
670
671 (assert (eql -1 (let ((i (eval '-2))
672                       (x (make-bug-348)))
673                   (funcall #'(setf bug-348-x)
674                            (incf i)
675                            (aref (vector x) (incf i)))
676                   (bug-348-x x))))
677
678 ;;; obsolete instance trapping
679 ;;;
680 ;;; FIXME: Both error conditions below should possibly be instances
681 ;;; of the same class. (Putting this FIXME here, since this is the only
682 ;;; place where they appear together.)
683
684 (with-test (:name :obsolete-defstruct/print-object)
685   (eval '(defstruct born-to-change))
686   (let ((x (make-born-to-change)))
687     (handler-bind ((error 'continue))
688       (eval '(defstruct born-to-change slot)))
689     (assert (eq :error
690                 (handler-case
691                     (princ-to-string x)
692                   (sb-pcl::obsolete-structure ()
693                     :error))))))
694
695 (with-test (:name :obsolete-defstruct/typep)
696   (eval '(defstruct born-to-change-2))
697   (let ((x (make-born-to-change-2)))
698     (handler-bind ((error 'continue))
699       (eval '(defstruct born-to-change-2 slot)))
700       (assert (eq :error2
701                   (handler-case
702                       (typep x (find-class 'standard-class))
703                     (sb-kernel:layout-invalid ()
704                       :error2))))))
705
706 ;; EQUALP didn't work for structures with float slots (reported by
707 ;; Vjacheslav Fyodorov).
708 (defstruct raw-slot-equalp-bug
709   (b 0s0 :type single-float)
710   c
711   (a 0d0 :type double-float))
712
713 (with-test (:name :raw-slot-equalp)
714   (assert (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
715                   (make-raw-slot-equalp-bug :a 1d0 :b 2s0)))
716   (assert (equalp (make-raw-slot-equalp-bug :a 1d0 :b 0s0)
717                   (make-raw-slot-equalp-bug :a 1d0 :b -0s0)))
718   (assert (not (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
719                        (make-raw-slot-equalp-bug :a 1d0 :b 3s0))))
720   (assert (not (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
721                        (make-raw-slot-equalp-bug :a 2d0 :b 2s0)))))
722
723 ;;; Check that all slot types (non-raw and raw) can be initialized with
724 ;;; constant arguments.
725 (defstruct constant-arg-inits
726   (a 42 :type t)
727   (b 1 :type fixnum)
728   (c 2 :type sb-vm:word)
729   (d 3.0 :type single-float)
730   (e 4.0d0 :type double-float)
731   (f #c(5.0 5.0) :type (complex single-float))
732   (g #c(6.0d0 6.0d0) :type (complex double-float)))
733 (defun test-constant-arg-inits ()
734   (let ((foo (make-constant-arg-inits)))
735     (declare (dynamic-extent foo))
736     (assert (eql 42 (constant-arg-inits-a foo)))
737     (assert (eql 1 (constant-arg-inits-b foo)))
738     (assert (eql 2 (constant-arg-inits-c foo)))
739     (assert (eql 3.0 (constant-arg-inits-d foo)))
740     (assert (eql 4.0d0 (constant-arg-inits-e foo)))
741     (assert (eql #c(5.0 5.0) (constant-arg-inits-f foo)))
742     (assert (eql #c(6.0d0 6.0d0) (constant-arg-inits-g foo)))))
743 (make-constant-arg-inits)
744
745 ;;; bug reported by John Morrison, 2008-07-22 on sbcl-devel
746 (defstruct (raw-slot-struct-with-unknown-init (:constructor make-raw-slot-struct-with-unknown-init ()))
747  (x (#:unknown-function) :type double-float))
748 \f
749 ;;; Some checks for the behavior of incompatibly redefining structure
750 ;;; classes.  We don't actually check that our detection of
751 ;;; "incompatible" is comprehensive, only that if an incompatible
752 ;;; definition is processed, we do various things.
753 (defmacro with-files ((&rest vars) &body body)
754   "Evaluate BODY with VARS bound to a number of filenames, then
755 delete the files at the end."
756   (let* ((paths (loop for var in vars
757                       as index upfrom 0
758                       collect (make-pathname
759                                    :case :common
760                                    :name (format nil
761                                                  "DEFSTRUCT-REDEF-TEST-~D"
762                                                  index)
763                                    :type "LISP")))
764          (binding-spec (mapcar
765                         (lambda (var path) `(,var ,path)) vars paths)))
766     (labels ((frob (n)
767                `((unwind-protect
768                      (progn
769                        ,@(if (plusp n)
770                              (frob (1- n))
771                              body))
772                    (delete-file ,(elt paths n))))))
773       `(let ,binding-spec
774          ,@(frob (1- (length vars)))))))
775
776 (defun noclobber (pathspec &rest forms)
777   "Write FORMS to the file named by PATHSPEC, erroring if
778 PATHSPEC already names an existing file."
779   (with-open-file (*standard-output* pathspec :direction :output
780                                      :if-exists :error)
781     (print '(in-package "CL-USER"))
782     (mapc #'print forms)))
783
784 (defun compile-file-assert (file &optional (want-error-p t) (want-warning-p t))
785   "Compile FILE and assert some things about the results."
786   (multiple-value-bind (fasl errors-p warnings-p)
787       (compile-file file)
788     (assert fasl)
789     (assert (eq errors-p want-error-p))
790     (assert (eq warnings-p want-warning-p))
791     fasl))
792
793 (defun continue-from-incompatible-defstruct-error (error)
794   "Invoke the CONTINUE restart for an incompatible DEFSTRUCT
795 redefinition."
796   ;; FIXME: want distinct error type for incompatible defstruct.
797   (when (search "attempt to redefine" (simple-condition-format-control error))
798     (when (find-restart 'continue)
799       (invoke-restart 'continue))))
800
801 (defun recklessly-continue-from-incompatible-defstruct-error (error)
802   "Invoke the RECKLESSLY-CONTINUE restart for an incompatible DEFSTRUCT
803 redefinition."
804   ;; FIXME: want distinct error type for incompatible defstruct.
805   (when (search "attempt to redefine" (simple-condition-format-control error))
806     (when (find-restart 'sb-kernel::recklessly-continue)
807       (invoke-restart 'sb-kernel::recklessly-continue))))
808
809 (defun assert-is (predicate instance)
810   (assert (funcall predicate instance)))
811
812 (defun assert-invalid (predicate instance)
813   (assert (typep (nth-value 1 (ignore-errors (funcall predicate instance)))
814                  'sb-kernel::layout-invalid)))
815
816 ;; Don't try to understand this macro; just look at its expansion.
817 (defmacro with-defstruct-redefinition-test (name
818                                             (&rest defstruct-form-bindings)
819                                             (&rest path-form-specs)
820                                             handler-function
821                                             &body body)
822   (labels ((make-defstruct-form (&key class-name super-name slots)
823              (let* ((predicate-name
824                      (read-from-string (format nil "~A-p" class-name)))
825                     (constructor-name
826                      (read-from-string (format nil "make-~A" class-name))))
827                `(values
828                  '(defstruct (,class-name
829                              (:constructor ,constructor-name)
830                              ,@(when super-name
831                                  `((:include ,super-name))))
832                     ,@slots)
833                  ',constructor-name
834                  ',predicate-name)))
835            (frob (bindspecs classno)
836              (if bindspecs
837                  `((multiple-value-bind ,(first (first bindspecs))
838                        ,(apply #'make-defstruct-form (rest (first bindspecs)))
839                      (declare (ignorable ,@(first (first bindspecs))))
840                      ,@(frob (rest bindspecs) (1+ classno))))
841                  `((with-files ,(mapcar #'first path-form-specs)
842                      ,@(mapcar (lambda (path-form) `(noclobber ,@path-form))
843                                path-form-specs)
844                      (handler-bind
845                          ((simple-error ',handler-function))
846                        ,@body))))))
847     `(with-test (:name ,name)
848       ,(first (frob defstruct-form-bindings 0)))))
849
850 ;; When eyeballing these, it's helpful to see when various things are
851 ;; happening.
852 (setq *compile-verbose* t *load-verbose* t)
853 \f
854 ;;; Tests begin.
855 ;; Base case: recklessly-continue.
856 (with-defstruct-redefinition-test :defstruct/recklessly
857     (((defstruct ctor pred) :class-name redef-test-1 :slots (a))
858      ((defstruct*) :class-name redef-test-1 :slots (a b)))
859     ((path1 defstruct)
860      (path2 defstruct*))
861     recklessly-continue-from-incompatible-defstruct-error
862   (load path1)
863   (let ((instance (funcall ctor)))
864     (load path2)
865     (assert-is pred instance)))
866
867 ;; Base case: continue (i.e., invalidate instances).
868 (with-defstruct-redefinition-test :defstruct/continue
869     (((defstruct ctor pred) :class-name redef-test-2 :slots (a))
870      ((defstruct*) :class-name redef-test-2 :slots (a b)))
871     ((path1 defstruct)
872      (path2 defstruct*))
873     continue-from-incompatible-defstruct-error
874   (load path1)
875   (let ((instance (funcall ctor)))
876     (load path2)
877     (assert-invalid pred instance)))
878
879 ;; Compiling a file with an incompatible defstruct should emit a
880 ;; warning and an error, but the fasl should be loadable.
881 (with-defstruct-redefinition-test :defstruct/compile-file-should-warn
882     (((defstruct) :class-name redef-test-3 :slots (a))
883      ((defstruct*) :class-name redef-test-3 :slots (a b)))
884     ((path1 defstruct)
885      (path2 defstruct*))
886     continue-from-incompatible-defstruct-error
887   (load path1)
888   (load (compile-file-assert path2)))
889
890 ;; After compiling a file with an incompatible DEFSTRUCT, load the
891 ;; fasl and ensure that an old instance remains valid.
892 (with-defstruct-redefinition-test :defstruct/compile-file-reckless
893     (((defstruct ctor pred) :class-name redef-test-4 :slots (a))
894      ((defstruct*) :class-name redef-test-4 :slots (a b)))
895     ((path1 defstruct)
896      (path2 defstruct*))
897     recklessly-continue-from-incompatible-defstruct-error
898   (load path1)
899   (let ((instance (funcall ctor)))
900     (load (compile-file-assert path2))
901     (assert-is pred instance)))
902
903 ;; After compiling a file with an incompatible DEFSTRUCT, load the
904 ;; fasl and ensure that an old instance has become invalid.
905 (with-defstruct-redefinition-test :defstruct/compile-file-continue
906     (((defstruct ctor pred) :class-name redef-test-5 :slots (a))
907      ((defstruct*) :class-name redef-test-5 :slots (a b)))
908     ((path1 defstruct)
909      (path2 defstruct*))
910     continue-from-incompatible-defstruct-error
911   (load path1)
912   (let ((instance (funcall ctor)))
913     (load (compile-file-assert path2))
914     (assert-invalid pred instance)))
915 \f
916 ;;; Subclasses.
917 ;; Ensure that recklessly continuing DT(expected)T to instances of
918 ;; subclasses.  (This is a case where recklessly continuing is
919 ;; actually dangerous, but we don't care.)
920 (with-defstruct-redefinition-test :defstruct/subclass-reckless
921     (((defstruct ignore pred1) :class-name redef-test-6 :slots (a))
922      ((substruct ctor pred2) :class-name redef-test-6-sub
923                              :super-name redef-test-6 :slots (z))
924      ((defstruct*) :class-name redef-test-6 :slots (a b)))
925     ((path1 defstruct substruct)
926      (path2 defstruct* substruct))
927     recklessly-continue-from-incompatible-defstruct-error
928   (load path1)
929   (let ((instance (funcall ctor)))
930     (load (compile-file-assert path2))
931     (assert-is pred1 instance)
932     (assert-is pred2 instance)))
933
934 ;; Ensure that continuing invalidates instances of subclasses.
935 (with-defstruct-redefinition-test :defstruct/subclass-continue
936     (((defstruct) :class-name redef-test-7 :slots (a))
937      ((substruct ctor pred) :class-name redef-test-7-sub
938                             :super-name redef-test-7 :slots (z))
939      ((defstruct*) :class-name redef-test-7 :slots (a b)))
940     ((path1 defstruct substruct)
941      (path2 defstruct* substruct))
942     continue-from-incompatible-defstruct-error
943   (load path1)
944   (let ((instance (funcall ctor)))
945     (load (compile-file-assert path2))
946     (assert-invalid pred instance)))
947
948 ;; Reclkessly continuing doesn't invalidate instances of subclasses.
949 (with-defstruct-redefinition-test :defstruct/subclass-in-other-file-reckless
950     (((defstruct ignore pred1) :class-name redef-test-8 :slots (a))
951      ((substruct ctor pred2) :class-name redef-test-8-sub
952                              :super-name redef-test-8 :slots (z))
953      ((defstruct*) :class-name redef-test-8 :slots (a b)))
954     ((path1 defstruct)
955      (path2 substruct)
956      (path3 defstruct*))
957     recklessly-continue-from-incompatible-defstruct-error
958   (load path1)
959   (load path2)
960   (let ((instance (funcall ctor)))
961     (load (compile-file-assert path3))
962     (assert-is pred1 instance)
963     (assert-is pred2 instance)))
964
965 ;; This is an icky case: when a subclass is defined in a separate
966 ;; file, CONTINUE'ing from LOAD of a file containing an incompatible
967 ;; superclass definition leaves the predicates and accessors into the
968 ;; subclass in a bad way until the subclass form is evaluated.
969 (with-defstruct-redefinition-test :defstruct/subclass-in-other-file-continue
970     (((defstruct ignore pred1) :class-name redef-test-9 :slots (a))
971      ((substruct ctor pred2) :class-name redef-test-9-sub
972                              :super-name redef-test-9 :slots (z))
973      ((defstruct*) :class-name redef-test-9 :slots (a b)))
974     ((path1 defstruct)
975      (path2 substruct)
976      (path3 defstruct*))
977     continue-from-incompatible-defstruct-error
978   (load path1)
979   (load path2)
980   (let ((instance (funcall ctor)))
981     (load (compile-file-assert path3))
982     ;; At this point, the instance of the subclass will not count as
983     ;; an instance of the superclass or of the subclass, but PRED2's
984     ;; predicate will error with "an obsolete structure accessor
985     ;; function was called".
986     (assert-invalid pred1 instance)
987     (format t "~&~A~%" (nth-value 1 (ignore-errors (funcall pred2 instance))))
988     ;; After loading PATH2, we'll get the desired LAYOUT-INVALID error.
989     (load path2)
990     (assert-invalid pred2 instance)))
991
992 ;; Some other subclass wrinkles have to do with splitting definitions
993 ;; accross files and compiling and loading things in a funny order.
994 (with-defstruct-redefinition-test
995     :defstruct/subclass-in-other-file-funny-operation-order-continue
996     (((defstruct ignore pred1) :class-name redef-test-10 :slots (a))
997      ((substruct ctor pred2) :class-name redef-test-10-sub
998                              :super-name redef-test-10 :slots (z))
999      ((defstruct*) :class-name redef-test-10 :slots (a b)))
1000     ((path1 defstruct)
1001      (path2 substruct)
1002      (path3 defstruct*))
1003     continue-from-incompatible-defstruct-error
1004   (load path1)
1005   (load path2)
1006   (let ((instance (funcall ctor)))
1007     ;; First we clobber the compiler's layout for the superclass.
1008     (compile-file-assert path3)
1009     ;; Then we recompile the subclass definition (which generates a
1010     ;; warning about the compiled layout for the superclass being
1011     ;; incompatible with the loaded layout, because we haven't loaded
1012     ;; path3 since recompiling).
1013     (compile-file path2)
1014     ;; Ugh.  I don't want to think about loading these in the wrong
1015     ;; order.
1016     (load (compile-file-pathname path3))
1017     (load (compile-file-pathname path2))
1018     (assert-invalid pred1 instance)
1019     (assert-invalid pred2 instance)))
1020
1021 (with-defstruct-redefinition-test
1022     :defstruct/subclass-in-other-file-funny-operation-order-continue
1023     (((defstruct ignore pred1) :class-name redef-test-11 :slots (a))
1024      ((substruct ctor pred2) :class-name redef-test-11-sub
1025                              :super-name redef-test-11 :slots (z))
1026      ((defstruct*) :class-name redef-test-11 :slots (a b)))
1027     ((path1 defstruct)
1028      (path2 substruct)
1029      (path3 defstruct*))
1030     continue-from-incompatible-defstruct-error
1031   (load path1)
1032   (load path2)
1033   (let ((instance (funcall ctor)))
1034     ;; This clobbers the compiler's layout for REDEF-TEST-11.
1035     (compile-file-assert path3)
1036     ;; This recompiles REDEF-TEST-11-SUB, using the new REDEF-TEST-11
1037     ;; compiler-layout.
1038     (load (compile-file-pathname path2))
1039     ;; Note that because we haven't loaded PATH3, we haven't clobbered
1040     ;; the class's layout REDEF-TEST-11, so REDEF-11's predicate will
1041     ;; still work.  That's probably bad.
1042     (assert-is pred1 instance)
1043     (assert-is pred2 instance)))
1044
1045 (with-test (:name :raw-slot/circle-subst)
1046   ;; CIRCLE-SUBSTS used %INSTANCE-REF on raw slots
1047   (multiple-value-bind (list n)
1048       (eval '(progn
1049               (defstruct raw-slot/circle-subst
1050                 (x 0.0 :type single-float))
1051               (read-from-string "((#1=#S(raw-slot/circle-subst :x 2.7158911)))")))
1052     (destructuring-bind ((struct)) list
1053       (assert (raw-slot/circle-subst-p struct))
1054       (assert (eql 2.7158911 (raw-slot/circle-subst-x struct)))
1055       (assert (eql 45 n)))))
1056
1057 (defstruct (bug-3b (:constructor make-bug-3b (&aux slot)))
1058   (slot nil :type string))
1059
1060 (with-test (:name :bug-3b)
1061   (handler-case
1062       (progn
1063         (bug-3b-slot (make-bug-3b))
1064         (error "fail"))
1065     (type-error (e)
1066       (assert (eq 'string (type-error-expected-type e)))
1067       (assert (zerop (type-error-datum e))))))
1068
1069 (with-test (:name :defstruct-copier-typechecks-argument)
1070   (assert (not (raises-error? (copy-person (make-astronaut :name "Neil")))))
1071   (assert (raises-error? (copy-astronaut (make-person :name "Fred")))))
1072
1073 (with-test (:name :bug-528807)
1074   (let ((*evaluator-mode* :compile))
1075     (handler-bind ((style-warning #'error))
1076       (eval `(defstruct (bug-528807 (:constructor make-528807 (&aux x)))
1077                (x nil :type fixnum))))))
1078
1079 (with-test (:name :bug-520607)
1080   (assert
1081     (raises-error?
1082       (eval '(defstruct (typed-struct (:type list) (:predicate typed-struct-p))
1083               (a 42 :type fixnum)))))
1084   ;; NIL is ok, though.
1085   (eval '(defstruct (typed-struct (:type list) (:predicate nil))
1086           (a 42 :type fixnum)))
1087   ;; So's empty.
1088   (eval '(defstruct (typed-struct2 (:type list) (:predicate))
1089           (a 42 :type fixnum))))
1090
1091 (with-test (:name (:boa-supplied-p &optional))
1092   (handler-bind ((warning #'error))
1093     (eval `(defstruct (boa-supplied-p.1 (:constructor make-boa-supplied-p.1
1094                                             (&optional (bar t barp))))
1095              bar
1096              barp)))
1097   (let ((b1 (make-boa-supplied-p.1))
1098         (b2 (make-boa-supplied-p.1 t)))
1099     (assert (eq t (boa-supplied-p.1-bar b1)))
1100     (assert (eq t (boa-supplied-p.1-bar b2)))
1101     (assert (eq nil (boa-supplied-p.1-barp b1)))
1102     (assert (eq t (boa-supplied-p.1-barp b2)))))
1103
1104 (with-test (:name (:boa-supplied-p &key))
1105   (handler-bind ((warning #'error))
1106     (eval `(defstruct (boa-supplied-p.2 (:constructor make-boa-supplied-p.2
1107                                             (&key (bar t barp))))
1108              bar
1109              barp)))
1110   (let ((b1 (make-boa-supplied-p.2))
1111         (b2 (make-boa-supplied-p.2 :bar t)))
1112     (assert (eq t (boa-supplied-p.2-bar b1)))
1113     (assert (eq t (boa-supplied-p.2-bar b2)))
1114     (assert (eq nil (boa-supplied-p.2-barp b1)))
1115     (assert (eq t (boa-supplied-p.2-barp b2)))))
1116
1117 (defstruct structure-with-predicate)
1118 (defclass class-to-be-redefined () ())
1119 (let ((x (make-instance 'class-to-be-redefined)))
1120   (defun function-trampoline (fun) (funcall fun x)))
1121
1122 (with-test (:name (:struct-predicate :obsolete-instance))
1123   (defclass class-to-be-redefined () ((a :initarg :a :initform 1)))
1124   (function-trampoline #'structure-with-predicate-p))
1125
1126 (with-test (:name (:defstruct :not-toplevel-silent))
1127   (let ((sb-ext:*evaluator-mode* :compile))
1128     (handler-bind ((warning #'error))
1129      (eval `(let ()
1130               (defstruct destruct-no-warning-not-at-toplevel bar))))))
1131
1132 (with-test (:name :bug-941102)
1133   (let ((test `((defstruct bug-941102)
1134                  (setf (find-class 'bug-941102-alias) (find-class 'bug-941102))
1135                  (setf (find-class 'bug-941102-alias) nil))))
1136     (multiple-value-bind (warn fail) (ctu:file-compile test :load t)
1137       (assert (not warn))
1138       (assert (not fail)))
1139     (multiple-value-bind (warn2 fail2) (ctu:file-compile test)
1140       (assert (not warn2))
1141       (assert (not fail2)))))