tests: Refactor CHECKED-COMPILE
[sbcl.git] / tests / defstruct.impure.lisp
blobb366c617e84e1f5c030015cff66a6786060fe11c
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.
12 (load "compiler-test-util.lisp")
13 (use-package "ASSERTOID")
15 ;;;; examples from, or close to, the Common Lisp DEFSTRUCT spec
17 ;;; Type mismatch of slot default init value isn't an error until the
18 ;;; default init value is actually used. (The justification is
19 ;;; somewhat bogus, but the requirement is clear.)
20 (defstruct person age (name 007 :type string)) ; not an error until 007 used
21 (make-person :name "James") ; not an error, 007 not used
23 #.(if (legacy-eval-p) (values)
24 '(assert-error (make-person) type-error))
25 #.(if (legacy-eval-p) (values)
26 '(assert-error (setf (person-name (make-person :name "Q")) 1)
27 type-error))
29 ;;; An &AUX variable in a boa-constructor without a default value
30 ;;; means "do not initialize slot" and does not cause type error
31 (declaim (notinline opaque-identity))
32 (defun opaque-identity (x) x)
34 (defstruct (boa-saux (:constructor make-boa-saux (&aux a (b 3) (c))))
35 (a #\! :type (integer 1 2))
36 (b #\? :type (integer 3 4))
37 (c #\# :type (integer 5 6)))
38 (defstruct (boa-kid (:include boa-saux)))
39 (defstruct (boa-grandkid (:include boa-kid)))
40 (with-test (:name :defstruct-boa-typecheck)
41 (dolist (dsd (sb-kernel:dd-slots
42 (sb-kernel:find-defstruct-description 'boa-saux)))
43 (let ((name (sb-kernel:dsd-name dsd))
44 (always-boundp (sb-kernel::dsd-always-boundp dsd)))
45 (ecase name
46 ((a c) (assert (not always-boundp)))
47 (b (assert always-boundp)))))
48 (let ((dd (sb-kernel:find-defstruct-description 'boa-grandkid)))
49 (assert (not (sb-kernel::dsd-always-boundp (car (sb-kernel:dd-slots dd))))))
50 (let ((s (make-boa-saux)))
51 (locally (declare (optimize (safety 3))
52 (inline boa-saux-a))
53 (assert-error (opaque-identity (boa-saux-a s)) type-error))
54 (setf (boa-saux-a s) 1)
55 (setf (boa-saux-c s) 5)
56 (assert (eql (boa-saux-a s) 1))
57 (assert (eql (boa-saux-b s) 3))
58 (assert (eql (boa-saux-c s) 5))))
60 (with-test (:name :defstruct-boa-nice-error :skipped-on :interpreter)
61 (let ((err (nth-value 1 (ignore-errors (boa-saux-a (make-boa-saux))))))
62 (assert (and (typep err 'simple-type-error)
63 (search "Accessed uninitialized slot"
64 (simple-condition-format-control err))))))
66 ; these two checks should be
67 ; kept separated
69 (with-test (:name :defstruct-boa-no-error :skipped-on :interpreter)
70 (let ((s (make-boa-saux)))
71 (locally (declare (optimize (safety 0))
72 (inline boa-saux-a))
73 (assert (sb-int:unbound-marker-p (opaque-identity (boa-saux-a s)))))
74 (setf (boa-saux-a s) 1)
75 (setf (boa-saux-c s) 5)
76 (assert (eql (boa-saux-a s) 1))
77 (assert (eql (boa-saux-b s) 3))
78 (assert (eql (boa-saux-c s) 5))))
80 (with-test (:name :defstruct-boa-typecheck.2)
81 (let ((s (make-boa-saux)))
82 (locally (declare (optimize (safety 3))
83 (notinline boa-saux-a))
84 (assert-error (opaque-identity (boa-saux-a s)) type-error))
85 (setf (boa-saux-a s) 1)
86 (setf (boa-saux-c s) 5)
87 (assert (eql (boa-saux-a s) 1))
88 (assert (eql (boa-saux-b s) 3))
89 (assert (eql (boa-saux-c s) 5))))
91 ;;; basic inheritance
92 (defstruct (astronaut (:include person)
93 (:conc-name astro-))
94 helmet-size
95 (favorite-beverage 'tang))
96 (let ((x (make-astronaut :name "Buzz" :helmet-size 17.5)))
97 (assert (equal (person-name x) "Buzz"))
98 (assert (equal (astro-name x) "Buzz"))
99 (assert (eql (astro-favorite-beverage x) 'tang))
100 (assert (null (astro-age x))))
101 (defstruct (ancient-astronaut (:include person (age 77)))
102 helmet-size
103 (favorite-beverage 'tang))
104 (assert (eql (ancient-astronaut-age (make-ancient-astronaut :name "John")) 77))
106 ;;; interaction of :TYPE and :INCLUDE and :INITIAL-OFFSET
107 (defstruct (binop (:type list) :named (:initial-offset 2))
108 (operator '? :type symbol)
109 operand-1
110 operand-2)
111 (defstruct (annotated-binop (:type list)
112 (:initial-offset 3)
113 (:include binop))
114 commutative associative identity)
115 (assert (equal (make-annotated-binop :operator '*
116 :operand-1 'x
117 :operand-2 5
118 :commutative t
119 :associative t
120 :identity 1)
121 '(nil nil binop * x 5 nil nil nil t t 1)))
123 ;;; effect of :NAMED on :TYPE
124 (defstruct (named-binop (:type list) :named)
125 (operator '? :type symbol)
126 operand-1
127 operand-2)
128 (let ((named-binop (make-named-binop :operator '+ :operand-1 'x :operand-2 5)))
129 ;; The data representation is specified to look like this.
130 (assert (equal named-binop '(named-binop + x 5)))
131 ;; A meaningful NAMED-BINOP-P is defined.
132 (assert (named-binop-p named-binop))
133 (assert (named-binop-p (copy-list named-binop)))
134 (assert (not (named-binop-p (cons 11 named-binop))))
135 (assert (not (named-binop-p (find-package :cl)))))
137 ;;; example 1
138 (defstruct town
139 area
140 watertowers
141 (firetrucks 1 :type fixnum)
142 population
143 (elevation 5128 :read-only t))
144 (let ((town1 (make-town :area 0 :watertowers 0)))
145 (assert (town-p town1))
146 (assert (not (town-p 1)))
147 (assert (eql (town-area town1) 0))
148 (assert (eql (town-elevation town1) 5128))
149 (assert (null (town-population town1)))
150 (setf (town-population town1) 99)
151 (assert (eql (town-population town1) 99))
152 (let ((town2 (copy-town town1)))
153 (dolist (slot-accessor-name '(town-area
154 town-watertowers
155 town-firetrucks
156 town-population
157 town-elevation))
158 (assert (eql (funcall slot-accessor-name town1)
159 (funcall slot-accessor-name town2))))
160 (assert (not (fboundp '(setf town-elevation)))) ; 'cause it's :READ-ONLY
161 ;; The source-transform for SETF was too eager,
162 ;; and would accept read-only slots.
163 (assert-error
164 (setf (town-elevation (make-town)) 5))
165 (assert-error
166 (funcall (compile nil (lambda (x y)
167 (setf (town-readonly x) y)
169 (make-town) 5))))
171 ;;; example 2
172 (defstruct (clown (:conc-name bozo-))
173 (nose-color 'red)
174 frizzy-hair-p
175 polkadots)
176 (let ((funny-clown (make-clown)))
177 (assert (eql (bozo-nose-color funny-clown) 'red)))
178 (defstruct (klown (:constructor make-up-klown)
179 (:copier clone-klown)
180 (:predicate is-a-bozo-p))
181 nose-color
182 frizzy-hair-p
183 polkadots)
184 (assert (is-a-bozo-p (make-up-klown)))
186 ;;;; systematically testing variants of DEFSTRUCT:
187 ;;;; * native, :TYPE LIST, and :TYPE VECTOR
189 ;;; FIXME: things to test:
190 ;;; * Slot readers work.
191 ;;; * Slot writers work.
192 ;;; * Predicates work.
194 ;;; FIXME: things that would be nice to test systematically someday:
195 ;;; * constructors (default, boa..)
196 ;;; * copiers
197 ;;; * no type checks when (> SPEED SAFETY)
198 ;;; * Tests of inclusion would be good. (It's tested very lightly
199 ;;; above, and then tested a fair amount by the system compiling
200 ;;; itself.)
202 (defun string+ (&rest rest)
203 (apply #'concatenate 'string
204 (mapcar #'string rest)))
205 (defun symbol+ (&rest rest)
206 (values (intern (apply #'string+ rest))))
208 (defun accessor-name (conc-name slot-name)
209 (symbol+ conc-name slot-name))
211 ;;; Use the ordinary FDEFINITIONs of accessors (not inline expansions)
212 ;;; to read and write a structure slot.
213 (defun read-slot-notinline (conc-name slot-name instance)
214 (funcall (accessor-name conc-name slot-name) instance))
215 (defun write-slot-notinline (new-value conc-name slot-name instance)
216 (funcall (fdefinition `(setf ,(accessor-name conc-name slot-name)))
217 new-value instance))
219 ;;; Use inline expansions of slot accessors, if possible, to read and
220 ;;; write a structure slot.
221 (defun read-slot-inline (conc-name slot-name instance)
222 (funcall (compile nil
223 `(lambda (instance)
224 (,(accessor-name conc-name slot-name) instance)))
225 instance))
226 (defun write-slot-inline (new-value conc-name slot-name instance)
227 (funcall (compile nil
228 `(lambda (new-value instance)
229 (setf (,(accessor-name conc-name slot-name) instance)
230 new-value)))
231 new-value
232 instance))
234 ;;; Read a structure slot, checking that the inline and out-of-line
235 ;;; accessors give the same result.
236 (defun read-slot (conc-name slot-name instance)
237 (let ((inline-value (read-slot-inline conc-name slot-name instance))
238 (notinline-value (read-slot-notinline conc-name slot-name instance)))
239 (assert (eql inline-value notinline-value))
240 inline-value))
242 ;;; Write a structure slot, using INLINEP argument to decide
243 ;;; on inlineness of accessor used.
244 (defun write-slot (new-value conc-name slot-name instance inlinep)
245 (if inlinep
246 (write-slot-inline new-value conc-name slot-name instance)
247 (write-slot-notinline new-value conc-name slot-name instance)))
249 ;;; bound during the tests so that we can get to it even if the
250 ;;; debugger is having a bad day
251 (defvar *instance*)
253 (declaim (optimize (debug 2)))
255 (defmacro test-variant (defstructname &key colontype boa-constructor-p)
256 `(locally
257 (declare (muffle-conditions style-warning)) ; &OPTIONAL and &KEY
258 #+nil(format t "~&/beginning PROGN for COLONTYPE=~S~%" ',colontype)
259 (defstruct (,defstructname
260 ,@(when colontype `((:type ,colontype)))
261 ,@(when boa-constructor-p
262 `((:constructor ,(symbol+ "CREATE-" defstructname)
264 &optional
265 (optional-test 2 optional-test-p)
266 &key
267 (home nil home-p)
268 (no-home-comment "Home package CL not provided.")
269 (comment (if home-p "" no-home-comment))
270 (refcount (if optional-test-p optional-test nil))
271 hash
272 weight)))))
274 ;; some ordinary tagged slots
276 (home nil :type package :read-only t)
277 (comment "" :type simple-string)
278 ;; some raw slots
279 (weight 1.0 :type single-float)
280 (hash 1 :type (integer 1 #.(* 3 most-positive-fixnum)) :read-only t)
281 ;; more ordinary tagged slots
282 (refcount 0 :type (and unsigned-byte fixnum)))
284 #+nil(format t "~&/done with DEFSTRUCT~%")
286 (let* ((cn (string+ ',defstructname "-")) ; conc-name
287 (ctor (symbol-function ',(symbol+ (if boa-constructor-p
288 "CREATE-"
289 "MAKE-")
290 defstructname)))
291 (*instance* (funcall ctor
292 ,@(unless boa-constructor-p
293 `(:id)) "some id"
294 ,@(when boa-constructor-p
295 '(1))
296 :home (find-package :cl)
297 :hash (+ 14 most-positive-fixnum)
298 ,@(unless boa-constructor-p
299 `(:refcount 1)))))
301 ;; Check that ctor set up slot values correctly.
302 #+nil(format t "~&/checking constructed structure~%")
303 (assert (string= "some id" (read-slot cn "ID" *instance*)))
304 (assert (eql (find-package :cl) (read-slot cn "HOME" *instance*)))
305 (assert (string= "" (read-slot cn "COMMENT" *instance*)))
306 (assert (= 1.0 (read-slot cn "WEIGHT" *instance*)))
307 (assert (eql (+ 14 most-positive-fixnum)
308 (read-slot cn "HASH" *instance*)))
309 (assert (= 1 (read-slot cn "REFCOUNT" *instance*)))
311 ;; There should be no writers for read-only slots.
312 #+nil(format t "~&/checking no read-only writers~%")
313 (assert (not (fboundp `(setf ,(symbol+ cn "HOME")))))
314 (assert (not (fboundp `(setf ,(symbol+ cn "HASH")))))
315 ;; (Read-only slot values are checked in the loop below.)
317 (dolist (inlinep '(t nil))
318 #+nil(format t "~&/doing INLINEP=~S~%" inlinep)
319 ;; Fiddle with writable slot values.
320 (let ((new-id (format nil "~S" (random 100)))
321 (new-comment (format nil "~X" (random 5555)))
322 (new-weight (random 10.0)))
323 (write-slot new-id cn "ID" *instance* inlinep)
324 (write-slot new-comment cn "COMMENT" *instance* inlinep)
325 (write-slot new-weight cn "WEIGHT" *instance* inlinep)
326 (assert (eql new-id (read-slot cn "ID" *instance*)))
327 (assert (eql new-comment (read-slot cn "COMMENT" *instance*)))
328 ;;(unless (eql new-weight (read-slot cn "WEIGHT" *instance*))
329 ;; (error "WEIGHT mismatch: ~S vs. ~S"
330 ;; new-weight (read-slot cn "WEIGHT" *instance*)))
331 (assert (eql new-weight (read-slot cn "WEIGHT" *instance*)))))
332 #+nil(format t "~&/done with INLINEP loop~%")
334 ;; :TYPE FOO objects don't go in the Lisp type system, so we
335 ;; can't test TYPEP stuff for them.
337 ;; FIXME: However, when they're named, they do define
338 ;; predicate functions, and we could test those.
339 ,@(unless colontype
340 `(;; Fiddle with predicate function.
341 (let ((pred-name (symbol+ ',defstructname "-P")))
342 #+nil(format t "~&/doing tests on PRED-NAME=~S~%" pred-name)
343 (assert (funcall pred-name *instance*))
344 (assert (not (funcall pred-name 14)))
345 (assert (not (funcall pred-name "test")))
346 (assert (not (funcall pred-name (make-hash-table))))
347 (let ((compiled-pred
348 (compile nil `(lambda (x) (,pred-name x)))))
349 #+nil(format t "~&/doing COMPILED-PRED tests~%")
350 (assert (funcall compiled-pred *instance*))
351 (assert (not (funcall compiled-pred 14)))
352 (assert (not (funcall compiled-pred #()))))
353 ;; Fiddle with TYPEP.
354 #+nil(format t "~&/doing TYPEP tests, COLONTYPE=~S~%" ',colontype)
355 (assert (typep *instance* ',defstructname))
356 (assert (not (typep 0 ',defstructname)))
357 (assert (funcall (symbol+ "TYPEP") *instance* ',defstructname))
358 (assert (not (funcall (symbol+ "TYPEP") nil ',defstructname)))
359 (let* ((typename ',defstructname)
360 (compiled-typep
361 (compile nil `(lambda (x) (typep x ',typename)))))
362 (assert (funcall compiled-typep *instance*))
363 (assert (not (funcall compiled-typep nil))))))))
365 #+nil(format t "~&/done with PROGN for COLONTYPE=~S~%" ',colontype)))
367 (test-variant vanilla-struct)
368 (test-variant vector-struct :colontype vector)
369 (test-variant list-struct :colontype list)
370 (test-variant vanilla-struct :boa-constructor-p t)
371 (test-variant vector-struct :colontype vector :boa-constructor-p t)
372 (test-variant list-struct :colontype list :boa-constructor-p t)
375 ;;;; testing raw slots harder
376 ;;;;
377 ;;;; The offsets of raw slots need to be rescaled during the punning
378 ;;;; process which is used to access them. That seems like a good
379 ;;;; place for errors to lurk, so we'll try hunting for them by
380 ;;;; verifying that all the raw slot data gets written successfully
381 ;;;; into the object, can be copied with the object, and can then be
382 ;;;; read back out (with none of it ending up bogusly outside the
383 ;;;; object, so that it couldn't be copied, or bogusly overwriting
384 ;;;; some other raw slot).
386 (defstruct manyraw
387 (a (expt 2 30) :type (unsigned-byte #.sb-vm:n-word-bits))
388 (b 0.1 :type single-float)
389 (c 0.2d0 :type double-float)
390 (d #c(0.3 0.3) :type (complex single-float))
391 unraw-slot-just-for-variety
392 (e #c(0.4d0 0.4d0) :type (complex double-float))
393 (aa (expt 2 30) :type (unsigned-byte #.sb-vm:n-word-bits))
394 (bb 0.1 :type single-float)
395 (cc 0.2d0 :type double-float)
396 (dd #c(0.3 0.3) :type (complex single-float))
397 (ee #c(0.4d0 0.4d0) :type (complex double-float)))
399 (defvar *manyraw* (make-manyraw))
401 (assert (eql (manyraw-a *manyraw*) (expt 2 30)))
402 (assert (eql (manyraw-b *manyraw*) 0.1))
403 (assert (eql (manyraw-c *manyraw*) 0.2d0))
404 (assert (eql (manyraw-d *manyraw*) #c(0.3 0.3)))
405 (assert (eql (manyraw-e *manyraw*) #c(0.4d0 0.4d0)))
406 (assert (eql (manyraw-aa *manyraw*) (expt 2 30)))
407 (assert (eql (manyraw-bb *manyraw*) 0.1))
408 (assert (eql (manyraw-cc *manyraw*) 0.2d0))
409 (assert (eql (manyraw-dd *manyraw*) #c(0.3 0.3)))
410 (assert (eql (manyraw-ee *manyraw*) #c(0.4d0 0.4d0)))
412 (setf (manyraw-aa *manyraw*) (expt 2 31)
413 (manyraw-bb *manyraw*) 0.11
414 (manyraw-cc *manyraw*) 0.22d0
415 (manyraw-dd *manyraw*) #c(0.33 0.33)
416 (manyraw-ee *manyraw*) #c(0.44d0 0.44d0))
418 (let ((copy (copy-manyraw *manyraw*)))
419 (assert (eql (manyraw-a copy) (expt 2 30)))
420 (assert (eql (manyraw-b copy) 0.1))
421 (assert (eql (manyraw-c copy) 0.2d0))
422 (assert (eql (manyraw-d copy) #c(0.3 0.3)))
423 (assert (eql (manyraw-e copy) #c(0.4d0 0.4d0)))
424 (assert (eql (manyraw-aa copy) (expt 2 31)))
425 (assert (eql (manyraw-bb copy) 0.11))
426 (assert (eql (manyraw-cc copy) 0.22d0))
427 (assert (eql (manyraw-dd copy) #c(0.33 0.33)))
428 (assert (eql (manyraw-ee copy) #c(0.44d0 0.44d0))))
431 ;;;; Since GC treats raw slots specially now, let's try this with more objects
432 ;;;; and random values as a stress test.
434 (setf *manyraw* nil)
436 (defconstant +n-manyraw+ 10)
437 (defconstant +m-manyraw+ 1000)
439 (defun check-manyraws (manyraws)
440 (assert (eql (length manyraws) (* +n-manyraw+ +m-manyraw+)))
441 (loop
442 for m in (reverse manyraws)
443 for i from 0
445 ;; Compare the tagged reference values with raw reffer results.
446 (destructuring-bind (j a b c d e)
447 (manyraw-unraw-slot-just-for-variety m)
448 (assert (eql i j))
449 (assert (= (manyraw-a m) a))
450 (assert (= (manyraw-b m) b))
451 (assert (= (manyraw-c m) c))
452 (assert (= (manyraw-d m) d))
453 (assert (= (manyraw-e m) e)))
454 ;; Test the funny out-of-line OAOOM-style closures, too.
455 (mapcar (lambda (fn value)
456 (assert (= (funcall fn m) value)))
457 (list #'manyraw-a
458 #'manyraw-b
459 #'manyraw-c
460 #'manyraw-d
461 #'manyraw-e)
462 (cdr (manyraw-unraw-slot-just-for-variety m)))))
464 (defstruct (manyraw-subclass (:include manyraw))
465 (stolperstein 0 :type (unsigned-byte 32)))
467 ;;; create lots of manyraw objects, triggering GC every now and then
468 (dotimes (y +n-manyraw+)
469 (dotimes (x +m-manyraw+)
470 (let ((a (random (expt 2 32)))
471 (b (random most-positive-single-float))
472 (c (random most-positive-double-float))
473 (d (complex
474 (random most-positive-single-float)
475 (random most-positive-single-float)))
476 (e (complex
477 (random most-positive-double-float)
478 (random most-positive-double-float))))
479 (push (funcall (if (zerop (mod x 3))
480 #'make-manyraw-subclass
481 #'make-manyraw)
482 :unraw-slot-just-for-variety
483 (list (+ x (* y +m-manyraw+)) a b c d e)
484 :a a
485 :b b
486 :c c
487 :d d
488 :e e)
489 *manyraw*)))
490 (let ((*standard-output* (make-broadcast-stream))) (room))
491 (sb-ext:gc))
492 (with-test (:name :defstruct-raw-slot-gc)
493 (check-manyraws *manyraw*))
495 ;;; try a full GC, too
496 (sb-ext:gc :full t)
497 (with-test (:name (:defstruct-raw-slot-gc :full))
498 (check-manyraws *manyraw*))
500 (macrolet ((def-it ()
501 `(defstruct (huge-manyraw (:include manyraw))
502 ,@(loop for n from 1 to 130
503 for s = (write-to-string n)
504 when (zerop (random 10))
505 collect `(,(sb-int:symbolicate "WORD-SLOT-" s)
506 ,n :type sb-ext:word)
507 collect `(,(sb-int:symbolicate "SLOT-" s) ,s))
508 (df 8.207880688335944d-304 :type double-float)
509 (aaa 'aaa)
510 (sf 1.5679403e-38 :type single-float)
511 (cdf #c(9d0 -2d10) :type (complex double-float))
512 (bbb 'bbb)
513 (csf #c(2f1 2f0) :type (complex single-float))
514 (ccc 'ccc)
515 (w1 #xffee :type sb-ext:word)
516 (w2 #xeeee :type sb-ext:word))))
517 (def-it))
519 (defstruct (hugest-manyraw (:include huge-manyraw))
520 (another-slot '(whocares)))
521 (defmethod make-load-form ((self hugest-manyraw) &optional e)
522 (declare (ignore e))
523 (make-load-form-saving-slots
524 self
525 :slot-names
526 ;; skip the slot named A so that the optimization that turns
527 ;; MAKE-LOAD-FORM-SAVING-SLOTS into "dump normally"
528 ;; (change 4bf626e745d5d2e34630ec4dd67b7c17bd9b8f28) can not be used.
529 (delete 'a (mapcar 'sb-kernel:dsd-name
530 (sb-kernel:dd-slots
531 (sb-kernel:find-defstruct-description 'hugest-manyraw))))))
533 (defun check-huge-manyraw (s)
534 (assert (and (eql (huge-manyraw-df s) 8.207880688335944d-304)
535 (eql (huge-manyraw-aaa s) 'aaa)
536 (eql (huge-manyraw-sf s) 1.5679403e-38)
537 (eql (huge-manyraw-cdf s) #c(9d0 -2d10))
538 (eql (huge-manyraw-bbb s) 'bbb)
539 (eql (huge-manyraw-csf s) #c(2f1 2f0))
540 (eql (huge-manyraw-ccc s) 'ccc)
541 (eql (huge-manyraw-w1 s) #xffee)
542 (eql (huge-manyraw-w2 s) #xeeee)))
543 (dolist (slot (sb-kernel:dd-slots
544 (sb-kernel:layout-info (sb-kernel:layout-of s))))
545 (let ((name (string (sb-kernel:dsd-name slot))))
546 (cond ((eql (mismatch name "SLOT-") 5)
547 (let ((n (parse-integer name :start 5)))
548 (assert (string= (funcall (sb-kernel:dsd-accessor-name slot) s)
549 (write-to-string n)))))
550 ((eql (mismatch name "WORD-SLOT-") 10)
551 (let ((n (parse-integer name :start 10)))
552 (assert (= (funcall (sb-kernel:dsd-accessor-name slot) s)
553 n))))))))
555 ;;; fasl dumper and loader also have special handling of raw slots, so
556 ;;; dump all of them into a fasl
557 (defmethod make-load-form ((self manyraw) &optional env)
558 (make-load-form-saving-slots self :environment env))
559 (with-open-file (s "tmp-defstruct.manyraw.lisp"
560 :direction :output
561 :if-exists :supersede)
562 (write-string "(defun dumped-manyraws () '#.*manyraw*)" s)
563 (terpri s)
564 (write-string "(defun dumped-huge-manyraw () '#.(make-huge-manyraw))" s)
565 (write-string "(defun dumped-hugest-manyraw () '#.(make-hugest-manyraw))" s))
566 (compile-file "tmp-defstruct.manyraw.lisp")
567 (delete-file "tmp-defstruct.manyraw.lisp")
569 ;;; nuke the objects and try another GC just to be extra careful
570 (setf *manyraw* nil)
571 (sb-ext:gc :full t)
573 ;;; re-read the dumped structures and check them
574 (load "tmp-defstruct.manyraw.fasl")
575 (with-test (:name (:defstruct-raw-slot load))
576 (check-manyraws (dumped-manyraws))
577 (check-huge-manyraw (make-huge-manyraw))
578 (assert (equalp (make-huge-manyraw) (dumped-huge-manyraw)))
579 ;; make-load-form omits slot A. it reads as 0
580 (assert (equalp (make-hugest-manyraw :a 0) (dumped-hugest-manyraw))))
583 ;;;; miscellaneous old bugs
585 (defstruct ya-struct)
586 (when (ignore-errors (or (ya-struct-p) 12))
587 (error "YA-STRUCT-P of no arguments should signal an error."))
588 (when (ignore-errors (or (ya-struct-p 'too 'many 'arguments) 12))
589 (error "YA-STRUCT-P of three arguments should signal an error."))
591 ;;; bug 210: Until sbcl-0.7.8.32 BOA constructors had SAFETY 0
592 ;;; declared inside on the theory that slot types were already
593 ;;; checked, which bogusly suppressed unbound-variable and other
594 ;;; checks within the evaluation of initforms.
595 (defvar *bug210*)
596 (defstruct (bug210a (:constructor bug210a ()))
597 (slot *bug210*))
598 (defstruct bug210b
599 (slot *bug210*))
600 ;;; Because of bug 210, this assertion used to fail.
601 (assert (typep (nth-value 1 (ignore-errors (bug210a))) 'unbound-variable))
602 ;;; Even with bug 210, these assertions succeeded.
603 (assert (typep (nth-value 1 (ignore-errors *bug210*)) 'unbound-variable))
604 (assert (typep (nth-value 1 (ignore-errors (make-bug210b))) 'unbound-variable))
606 ;;; In sbcl-0.7.8.53, DEFSTRUCT blew up in non-toplevel contexts
607 ;;; because it implicitly assumed that EVAL-WHEN (COMPILE) stuff
608 ;;; setting up compiler-layout information would run before the
609 ;;; constructor function installing the layout was compiled. Make sure
610 ;;; that doesn't happen again.
611 (defun foo-0-7-8-53 () (defstruct foo-0-7-8-53 x (y :not)))
612 (assert (not (find-class 'foo-0-7-8-53 nil)))
613 (foo-0-7-8-53)
614 (assert (find-class 'foo-0-7-8-53 nil))
615 (let ((foo-0-7-8-53 (make-foo-0-7-8-53 :x :s)))
616 (assert (eq (foo-0-7-8-53-x foo-0-7-8-53) :s))
617 (assert (eq (foo-0-7-8-53-y foo-0-7-8-53) :not)))
619 ;;; tests of behaviour of colliding accessors.
620 (defstruct (bug127-foo (:conc-name bug127-baz-)) a)
621 (assert (= (bug127-baz-a (make-bug127-foo :a 1)) 1))
622 (defstruct (bug127-bar (:conc-name bug127-baz-) (:include bug127-foo)) b)
623 (assert (= (bug127-baz-a (make-bug127-bar :a 1 :b 2)) 1))
624 (assert (= (bug127-baz-b (make-bug127-bar :a 1 :b 2)) 2))
625 (assert (= (bug127-baz-a (make-bug127-foo :a 1)) 1))
627 (defun bug127-flurble (x)
629 (defstruct bug127 flurble)
630 (assert (= (bug127-flurble (make-bug127 :flurble 7)) 7))
632 (defstruct bug127-a b-c)
633 (assert (= (bug127-a-b-c (make-bug127-a :b-c 9)) 9))
634 (defstruct (bug127-a-b (:include bug127-a)) c)
635 (assert (= (bug127-a-b-c (make-bug127-a :b-c 9)) 9))
636 (assert (= (bug127-a-b-c (make-bug127-a-b :b-c 11 :c 13)) 11))
638 (defstruct (bug127-e (:conc-name bug127--)) foo)
639 (assert (= (bug127--foo (make-bug127-e :foo 3)) 3))
640 (defstruct (bug127-f (:conc-name bug127--)) foo)
641 (assert (= (bug127--foo (make-bug127-f :foo 3)) 3))
642 (assert-error (bug127--foo (make-bug127-e :foo 3)) type-error)
644 ;;; FIXME: should probably do the same tests on DEFSTRUCT :TYPE
646 ;;; As noted by Paul Dietz for CMUCL, :CONC-NAME handling was a little
647 ;;; too fragile:
648 (defstruct (conc-name-syntax :conc-name) a-conc-name-slot)
649 (assert (eq (a-conc-name-slot (make-conc-name-syntax :a-conc-name-slot 'y))
650 'y))
651 ;;; and further :CONC-NAME NIL was being wrongly treated:
652 (defpackage "DEFSTRUCT-TEST-SCRATCH")
653 (defstruct (conc-name-nil :conc-name)
654 defstruct-test-scratch::conc-name-nil-slot)
655 (assert (= (defstruct-test-scratch::conc-name-nil-slot
656 (make-conc-name-nil :conc-name-nil-slot 1)) 1))
657 (assert-error (conc-name-nil-slot (make-conc-name-nil))
658 undefined-function)
660 ;;; The named/typed predicates were a little fragile, in that they
661 ;;; could throw errors on innocuous input:
662 (defstruct (list-struct (:type list) :named) a-slot)
663 (assert (list-struct-p (make-list-struct)))
664 (assert (not (list-struct-p nil)))
665 (assert (not (list-struct-p 1)))
666 (defstruct (offset-list-struct (:type list) :named (:initial-offset 1)) a-slot)
667 (assert (offset-list-struct-p (make-offset-list-struct)))
668 (assert (not (offset-list-struct-p nil)))
669 (assert (not (offset-list-struct-p 1)))
670 (assert (not (offset-list-struct-p '(offset-list-struct))))
671 (assert (not (offset-list-struct-p '(offset-list-struct . 3))))
672 (defstruct (vector-struct (:type vector) :named) a-slot)
673 (assert (vector-struct-p (make-vector-struct)))
674 (assert (not (vector-struct-p nil)))
675 (assert (not (vector-struct-p #())))
678 ;;; bug 3d: type safety with redefined type constraints on slots
679 #+#.(cl:if (assertoid:legacy-eval-p) '(or) '(and))
680 (macrolet
681 ((test (type)
682 (let* ((base-name (intern (format nil "bug3d-~A" type)))
683 (up-name (intern (format nil "~A-up" base-name)))
684 (accessor (intern (format nil "~A-X" base-name)))
685 (up-accessor (intern (format nil "~A-X" up-name)))
686 (type-options (when type `((:type ,type)))))
687 `(progn
688 (defstruct (,base-name ,@type-options)
689 x y)
690 (defstruct (,up-name (:include ,base-name
691 (x "x" :type simple-string)
692 (y "y" :type simple-string))
693 ,@type-options))
694 (let ((ob (,(intern (format nil "MAKE-~A" up-name)))))
695 (setf (,accessor ob) 0)
696 (loop for decl in '(inline notinline)
697 for fun = `(lambda (s)
698 (declare (optimize (safety 3))
699 (,decl ,',up-accessor))
700 (,',up-accessor s))
701 do (assert-error (funcall (compile nil fun) ob)
702 type-error)))))))
703 (test nil)
704 (test list)
705 (test vector))
707 (let* ((name (gensym))
708 (form `(defstruct ,name
709 (x nil :type (or null (function (integer)
710 (values number &optional foo)))))))
711 (eval (copy-tree form))
712 (eval (copy-tree form)))
714 ;;; 322: "DEFSTRUCT :TYPE LIST predicate and improper lists"
715 ;;; reported by Bruno Haible sbcl-devel "various SBCL bugs" from CLISP
716 ;;; test suite.
717 (defstruct (bug-332a (:type list) (:initial-offset 5) :named))
718 (defstruct (bug-332b (:type list) (:initial-offset 2) :named (:include bug-332a)))
719 (assert (not (bug-332b-p (list* nil nil nil nil nil 'foo73 nil 'tail))))
720 (assert (not (bug-332b-p 873257)))
721 (assert (not (bug-332b-p '(1 2 3 4 5 x 1 2 bug-332a))))
722 (assert (bug-332b-p '(1 2 3 4 5 x 1 2 bug-332b)))
724 ;;; Similar test for vectors, just for good measure.
725 (defstruct (bug-332a-aux (:type vector)
726 (:initial-offset 5) :named))
727 (defstruct (bug-332b-aux (:type vector)
728 (:initial-offset 2) :named
729 (:include bug-332a-aux)))
730 (assert (not (bug-332b-aux-p #(1 2 3 4 5 x 1 premature-end))))
731 (assert (not (bug-332b-aux-p 873257)))
732 (assert (not (bug-332b-aux-p #(1 2 3 4 5 x 1 2 bug-332a-aux))))
733 (assert (bug-332b-aux-p #(1 2 3 4 5 x 1 2 bug-332b-aux)))
735 ;;; In sbcl-0.8.11.8 FBOUNDPness potential collisions of structure
736 ;;; slot accessors signalled a condition at macroexpansion time, not
737 ;;; when the code was actually compiled or loaded.
738 (let ((defstruct-form '(defstruct bug-in-0-8-11-8 x)))
739 (defun bug-in-0-8-11-8-x (z) (print "some unrelated thing"))
740 (handler-case (macroexpand defstruct-form)
741 (warning (c)
742 (error "shouldn't warn just from macroexpansion here"))))
744 ;;; bug 318 symptom no 1. (rest not fixed yet)
745 (catch :ok
746 (handler-bind ((error (lambda (c)
747 ;; Used to cause stack-exhaustion
748 (unless (typep c 'storage-condition)
749 (throw :ok t)))))
750 (eval '(progn
751 (defstruct foo a)
752 (setf (find-class 'foo) nil)
753 (defstruct foo slot-1)))))
755 ;;; bug 348, evaluation order of slot writer arguments. Fixed by Gabor
756 ;;; Melis.
757 (defstruct bug-348 x)
759 (assert (eql -1 (let ((i (eval '-2))
760 (x (make-bug-348)))
761 (funcall #'(setf bug-348-x)
762 (incf i)
763 (aref (vector x) (incf i)))
764 (bug-348-x x))))
766 ;;; obsolete instance trapping
768 ;;; FIXME: Both error conditions below should possibly be instances
769 ;;; of the same class. (Putting this FIXME here, since this is the only
770 ;;; place where they appear together.)
772 (with-test (:name :obsolete-defstruct/print-object)
773 (eval '(defstruct born-to-change))
774 (let ((x (make-born-to-change)))
775 (handler-bind ((error 'continue))
776 (eval '(defstruct born-to-change slot)))
777 (assert (eq :error
778 (handler-case
779 (princ-to-string x)
780 (sb-pcl::obsolete-structure ()
781 :error))))))
783 (with-test (:name :obsolete-defstruct/typep)
784 (eval '(defstruct born-to-change-2))
785 (let ((x (make-born-to-change-2)))
786 (handler-bind ((error 'continue))
787 (eval '(defstruct born-to-change-2 slot)))
788 (assert (eq :error2
789 (handler-case
790 (typep x (find-class 'standard-class))
791 (sb-kernel:layout-invalid ()
792 :error2))))))
794 ;; EQUALP didn't work for structures with float slots (reported by
795 ;; Vjacheslav Fyodorov).
796 (defstruct raw-slot-equalp-bug
797 (b 0s0 :type single-float)
799 (a 0d0 :type double-float))
801 (defstruct raw-slot-equalp-bug-2
802 (b (complex 1d0) :type (complex double-float))
803 (x (complex 1d0) :type (complex double-float))
805 (a 1s0 :type single-float))
807 (with-test (:name :raw-slot-equalp)
808 (assert (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
809 (make-raw-slot-equalp-bug :a 1d0 :b 2s0)))
810 (assert (equalp (make-raw-slot-equalp-bug :a 1d0 :b 0s0)
811 (make-raw-slot-equalp-bug :a 1d0 :b -0s0)))
812 (assert (not (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
813 (make-raw-slot-equalp-bug :a 1d0 :b 3s0))))
814 (assert (not (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
815 (make-raw-slot-equalp-bug :a 2d0 :b 2s0))))
816 (assert (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)
817 (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)))
818 (assert (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 0s0)
819 (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a -0s0)))
820 (assert (not (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)
821 (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 3s0))))
822 (assert (not (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)
823 (make-raw-slot-equalp-bug-2 :b (complex 2d0) :a 2s0)))))
825 ;;; Check that all slot types (non-raw and raw) can be initialized with
826 ;;; constant arguments.
827 (defstruct constant-arg-inits
828 (a 42 :type t)
829 (b 1 :type fixnum)
830 (c 2 :type sb-vm:word)
831 (d 3.0 :type single-float)
832 (e 4.0d0 :type double-float)
833 (f #c(5.0 5.0) :type (complex single-float))
834 (g #c(6.0d0 6.0d0) :type (complex double-float)))
835 (defun test-constant-arg-inits ()
836 (let ((foo (make-constant-arg-inits)))
837 (declare (dynamic-extent foo))
838 (assert (eql 42 (constant-arg-inits-a foo)))
839 (assert (eql 1 (constant-arg-inits-b foo)))
840 (assert (eql 2 (constant-arg-inits-c foo)))
841 (assert (eql 3.0 (constant-arg-inits-d foo)))
842 (assert (eql 4.0d0 (constant-arg-inits-e foo)))
843 (assert (eql #c(5.0 5.0) (constant-arg-inits-f foo)))
844 (assert (eql #c(6.0d0 6.0d0) (constant-arg-inits-g foo)))))
845 (make-constant-arg-inits)
847 ;;; bug reported by John Morrison, 2008-07-22 on sbcl-devel
848 (defstruct (raw-slot-struct-with-unknown-init (:constructor make-raw-slot-struct-with-unknown-init ()))
849 (x (#:unknown-function) :type double-float))
851 ;;; Some checks for the behavior of incompatibly redefining structure
852 ;;; classes. We don't actually check that our detection of
853 ;;; "incompatible" is comprehensive, only that if an incompatible
854 ;;; definition is processed, we do various things.
855 (defmacro with-files ((&rest vars) &body body)
856 "Evaluate BODY with VARS bound to a number of filenames, then
857 delete the files at the end."
858 (let* ((paths (loop for var in vars
859 as index upfrom 0
860 collect (make-pathname
861 :case :common
862 :name (format nil
863 "DEFSTRUCT-REDEF-TEST-~D"
864 index)
865 :type "LISP")))
866 (binding-spec (mapcar
867 (lambda (var path) `(,var ,path)) vars paths)))
868 (labels ((frob (n)
869 `((unwind-protect
870 (progn
871 ,@(if (plusp n)
872 (frob (1- n))
873 body))
874 (delete-file ,(elt paths n))))))
875 `(let ,binding-spec
876 ,@(frob (1- (length vars)))))))
878 (defun noclobber (pathspec &rest forms)
879 "Write FORMS to the file named by PATHSPEC, erroring if
880 PATHSPEC already names an existing file."
881 (with-open-file (*standard-output* pathspec :direction :output
882 :if-exists :error)
883 (print '(in-package "CL-USER"))
884 (mapc #'print forms)))
886 (defun compile-file-assert (file &optional (want-error-p t) (want-warning-p t))
887 "Compile FILE and assert some things about the results."
888 (multiple-value-bind (fasl errors-p warnings-p)
889 (compile-file file)
890 (assert fasl)
891 (assert (eq errors-p want-error-p))
892 (assert (eq warnings-p want-warning-p))
893 fasl))
895 (defun continue-from-incompatible-defstruct-error (error)
896 "Invoke the CONTINUE restart for an incompatible DEFSTRUCT
897 redefinition."
898 ;; FIXME: want distinct error type for incompatible defstruct.
899 (when (search "attempt to redefine" (simple-condition-format-control error))
900 (when (find-restart 'continue)
901 (invoke-restart 'continue))))
903 (defun recklessly-continue-from-incompatible-defstruct-error (error)
904 "Invoke the RECKLESSLY-CONTINUE restart for an incompatible DEFSTRUCT
905 redefinition."
906 ;; FIXME: want distinct error type for incompatible defstruct.
907 (when (search "attempt to redefine" (simple-condition-format-control error))
908 (when (find-restart 'sb-kernel::recklessly-continue)
909 (invoke-restart 'sb-kernel::recklessly-continue))))
911 (defun assert-is (predicate instance)
912 (assert (funcall predicate instance)))
914 ;;; It used to call the predicate function, but out-of-line predicate
915 ;;; functions now don't signal layout-invalid, just like inlined
916 ;;; predicates didn't signal it.
917 (defun assert-invalid (instance)
918 (declare (notinline typep))
919 (assert (typep (nth-value 1 (ignore-errors (typep instance 'structure-object)))
920 'sb-kernel::layout-invalid)))
922 ;; Don't try to understand this macro; just look at its expansion.
923 (defmacro with-defstruct-redefinition-test (name
924 (&rest defstruct-form-bindings)
925 (&rest path-form-specs)
926 handler-function
927 &body body)
928 (labels ((make-defstruct-form (&key class-name super-name slots)
929 (let* ((predicate-name
930 (read-from-string (format nil "~A-p" class-name)))
931 (constructor-name
932 (read-from-string (format nil "make-~A" class-name))))
933 `(values
934 '(defstruct (,class-name
935 (:constructor ,constructor-name)
936 ,@(when super-name
937 `((:include ,super-name))))
938 ,@slots)
939 ',constructor-name
940 ',predicate-name)))
941 (frob (bindspecs classno)
942 (if bindspecs
943 `((multiple-value-bind ,(first (first bindspecs))
944 ,(apply #'make-defstruct-form (rest (first bindspecs)))
945 (declare (ignorable ,@(first (first bindspecs))))
946 ,@(frob (rest bindspecs) (1+ classno))))
947 `((with-files ,(mapcar #'first path-form-specs)
948 ,@(mapcar (lambda (path-form) `(noclobber ,@path-form))
949 path-form-specs)
950 (handler-bind
951 ((simple-error ',handler-function))
952 ,@body))))))
953 `(with-test (:name ,name)
954 ,(first (frob defstruct-form-bindings 0)))))
956 ;; When eyeballing these, it's helpful to see when various things are
957 ;; happening.
958 (setq *compile-verbose* t *load-verbose* t)
960 ;;; Tests begin.
961 ;; Base case: recklessly-continue.
962 (with-defstruct-redefinition-test :defstruct/recklessly
963 (((defstruct ctor pred) :class-name redef-test-1 :slots (a))
964 ((defstruct*) :class-name redef-test-1 :slots (a b)))
965 ((path1 defstruct)
966 (path2 defstruct*))
967 recklessly-continue-from-incompatible-defstruct-error
968 (load path1)
969 (let ((instance (funcall ctor)))
970 (load path2)
971 (assert-is pred instance)))
973 ;; Base case: continue (i.e., invalidate instances).
974 (with-defstruct-redefinition-test :defstruct/continue
975 (((defstruct ctor) :class-name redef-test-2 :slots (a))
976 ((defstruct*) :class-name redef-test-2 :slots (a b)))
977 ((path1 defstruct)
978 (path2 defstruct*))
979 continue-from-incompatible-defstruct-error
980 (load path1)
981 (let ((instance (funcall ctor)))
982 (load path2)
983 (assert-invalid instance)))
985 ;; Compiling a file with an incompatible defstruct should emit a
986 ;; warning and an error, but the fasl should be loadable.
987 (with-defstruct-redefinition-test :defstruct/compile-file-should-warn
988 (((defstruct) :class-name redef-test-3 :slots (a))
989 ((defstruct*) :class-name redef-test-3 :slots (a b)))
990 ((path1 defstruct)
991 (path2 defstruct*))
992 continue-from-incompatible-defstruct-error
993 (load path1)
994 (load (compile-file-assert path2)))
996 ;; After compiling a file with an incompatible DEFSTRUCT, load the
997 ;; fasl and ensure that an old instance remains valid.
998 (with-defstruct-redefinition-test :defstruct/compile-file-reckless
999 (((defstruct ctor pred) :class-name redef-test-4 :slots (a))
1000 ((defstruct*) :class-name redef-test-4 :slots (a b)))
1001 ((path1 defstruct)
1002 (path2 defstruct*))
1003 recklessly-continue-from-incompatible-defstruct-error
1004 (load path1)
1005 (let ((instance (funcall ctor)))
1006 (load (compile-file-assert path2))
1007 (assert-is pred instance)))
1009 ;; After compiling a file with an incompatible DEFSTRUCT, load the
1010 ;; fasl and ensure that an old instance has become invalid.
1011 (with-defstruct-redefinition-test :defstruct/compile-file-continue
1012 (((defstruct ctor) :class-name redef-test-5 :slots (a))
1013 ((defstruct*) :class-name redef-test-5 :slots (a b)))
1014 ((path1 defstruct)
1015 (path2 defstruct*))
1016 continue-from-incompatible-defstruct-error
1017 (load path1)
1018 (let ((instance (funcall ctor)))
1019 (load (compile-file-assert path2))
1020 (assert-invalid instance)))
1022 ;;; Subclasses.
1023 ;; Ensure that recklessly continuing DT(expected)T to instances of
1024 ;; subclasses. (This is a case where recklessly continuing is
1025 ;; actually dangerous, but we don't care.)
1026 (with-defstruct-redefinition-test :defstruct/subclass-reckless
1027 (((defstruct ignore pred1) :class-name redef-test-6 :slots (a))
1028 ((substruct ctor pred2) :class-name redef-test-6-sub
1029 :super-name redef-test-6 :slots (z))
1030 ((defstruct*) :class-name redef-test-6 :slots (a b)))
1031 ((path1 defstruct substruct)
1032 (path2 defstruct* substruct))
1033 recklessly-continue-from-incompatible-defstruct-error
1034 (load path1)
1035 (let ((instance (funcall ctor)))
1036 (load (compile-file-assert path2))
1037 (assert-is pred1 instance)
1038 (assert-is pred2 instance)))
1040 ;; Ensure that continuing invalidates instances of subclasses.
1041 (with-defstruct-redefinition-test :defstruct/subclass-continue
1042 (((defstruct) :class-name redef-test-7 :slots (a))
1043 ((substruct ctor) :class-name redef-test-7-sub
1044 :super-name redef-test-7 :slots (z))
1045 ((defstruct*) :class-name redef-test-7 :slots (a b)))
1046 ((path1 defstruct substruct)
1047 (path2 defstruct* substruct))
1048 continue-from-incompatible-defstruct-error
1049 (load path1)
1050 (let ((instance (funcall ctor)))
1051 (load (compile-file-assert path2))
1052 (assert-invalid instance)))
1054 ;; Reclkessly continuing doesn't invalidate instances of subclasses.
1055 (with-defstruct-redefinition-test :defstruct/subclass-in-other-file-reckless
1056 (((defstruct ignore pred1) :class-name redef-test-8 :slots (a))
1057 ((substruct ctor pred2) :class-name redef-test-8-sub
1058 :super-name redef-test-8 :slots (z))
1059 ((defstruct*) :class-name redef-test-8 :slots (a b)))
1060 ((path1 defstruct)
1061 (path2 substruct)
1062 (path3 defstruct*))
1063 recklessly-continue-from-incompatible-defstruct-error
1064 (load path1)
1065 (load path2)
1066 (let ((instance (funcall ctor)))
1067 (load (compile-file-assert path3))
1068 (assert-is pred1 instance)
1069 (assert-is pred2 instance)))
1071 ;; This is an icky case: when a subclass is defined in a separate
1072 ;; file, CONTINUE'ing from LOAD of a file containing an incompatible
1073 ;; superclass definition leaves the predicates and accessors into the
1074 ;; subclass in a bad way until the subclass form is evaluated.
1075 (with-defstruct-redefinition-test :defstruct/subclass-in-other-file-continue
1076 (((defstruct ignore) :class-name redef-test-9 :slots (a))
1077 ((substruct ctor) :class-name redef-test-9-sub
1078 :super-name redef-test-9 :slots (z))
1079 ((defstruct*) :class-name redef-test-9 :slots (a b)))
1080 ((path1 defstruct)
1081 (path2 substruct)
1082 (path3 defstruct*))
1083 continue-from-incompatible-defstruct-error
1084 (load path1)
1085 (load path2)
1086 (let ((instance (funcall ctor)))
1087 (load (compile-file-assert path3))
1088 ;; At this point, the instance of the subclass will not count as
1089 ;; an instance of the superclass or of the subclass, but PRED2's
1090 ;; predicate will error with "an obsolete structure accessor
1091 ;; function was called".
1092 (assert-invalid instance)
1093 (format t "~&~A~%" (nth-value 1 (ignore-errors (funcall pred2 instance))))
1094 ;; After loading PATH2, we'll get the desired LAYOUT-INVALID error.
1095 (load path2)
1096 (assert-invalid instance)))
1098 ;; Some other subclass wrinkles have to do with splitting definitions
1099 ;; accross files and compiling and loading things in a funny order.
1100 (with-defstruct-redefinition-test
1101 :defstruct/subclass-in-other-file-funny-operation-order-continue
1102 (((defstruct ignore pred1) :class-name redef-test-10 :slots (a))
1103 ((substruct ctor pred2) :class-name redef-test-10-sub
1104 :super-name redef-test-10 :slots (z))
1105 ((defstruct*) :class-name redef-test-10 :slots (a b)))
1106 ((path1 defstruct)
1107 (path2 substruct)
1108 (path3 defstruct*))
1109 continue-from-incompatible-defstruct-error
1110 (load path1)
1111 (load path2)
1112 (let ((instance (funcall ctor)))
1113 ;; First we clobber the compiler's layout for the superclass.
1114 (compile-file-assert path3)
1115 ;; Then we recompile the subclass definition (which generates a
1116 ;; warning about the compiled layout for the superclass being
1117 ;; incompatible with the loaded layout, because we haven't loaded
1118 ;; path3 since recompiling).
1119 (compile-file path2)
1120 ;; Ugh. I don't want to think about loading these in the wrong
1121 ;; order.
1122 (load (compile-file-pathname path3))
1123 (load (compile-file-pathname path2))
1124 (assert-invalid instance)
1125 (assert-invalid instance)))
1127 (with-defstruct-redefinition-test
1128 :defstruct/subclass-in-other-file-funny-operation-order-continue
1129 (((defstruct ignore pred1) :class-name redef-test-11 :slots (a))
1130 ((substruct ctor pred2) :class-name redef-test-11-sub
1131 :super-name redef-test-11 :slots (z))
1132 ((defstruct*) :class-name redef-test-11 :slots (a b)))
1133 ((path1 defstruct)
1134 (path2 substruct)
1135 (path3 defstruct*))
1136 continue-from-incompatible-defstruct-error
1137 (load path1)
1138 (load path2)
1139 (let ((instance (funcall ctor)))
1140 ;; This clobbers the compiler's layout for REDEF-TEST-11.
1141 (compile-file-assert path3)
1142 ;; This recompiles REDEF-TEST-11-SUB, using the new REDEF-TEST-11
1143 ;; compiler-layout.
1144 (load (compile-file-pathname path2))
1145 ;; Note that because we haven't loaded PATH3, we haven't clobbered
1146 ;; the class's layout REDEF-TEST-11, so REDEF-11's predicate will
1147 ;; still work. That's probably bad.
1148 (assert-is pred1 instance)
1149 (assert-is pred2 instance)))
1151 (with-test (:name :raw-slot/circle-subst)
1152 ;; CIRCLE-SUBSTS used %INSTANCE-REF on raw slots
1153 (multiple-value-bind (list n)
1154 (eval '(progn
1155 (defstruct raw-slot/circle-subst
1156 (x 0.0 :type single-float))
1157 (read-from-string "((#1=#S(raw-slot/circle-subst :x 2.7158911)))")))
1158 (destructuring-bind ((struct)) list
1159 (assert (raw-slot/circle-subst-p struct))
1160 (assert (eql 2.7158911 (raw-slot/circle-subst-x struct)))
1161 (assert (eql 45 n)))))
1163 (defstruct (bug-3b (:constructor make-bug-3b (&aux slot)))
1164 (slot nil :type string))
1166 (with-test (:name :bug-3b)
1167 (handler-case
1168 (progn
1169 (bug-3b-slot (make-bug-3b))
1170 (error "fail"))
1171 (type-error (e)
1172 (assert (eq 'string (type-error-expected-type e)))
1173 (assert (sb-int:unbound-marker-p (type-error-datum e))))))
1175 (with-test (:name :defstruct-copier-typechecks-argument)
1176 (copy-person (make-astronaut :name "Neil"))
1177 (assert-error (copy-astronaut (make-person :name "Fred"))))
1179 (with-test (:name :bug-528807)
1180 (let ((*evaluator-mode* :compile))
1181 (handler-bind ((style-warning #'error))
1182 (eval `(defstruct (bug-528807 (:constructor make-528807 (&aux x)))
1183 (x nil :type fixnum))))))
1185 (with-test (:name :bug-520607)
1186 (assert-error
1187 (eval '(defstruct (typed-struct (:type list) (:predicate typed-struct-p))
1188 (a 42 :type fixnum))))
1189 ;; NIL is ok, though.
1190 (eval '(defstruct (typed-struct (:type list) (:predicate nil))
1191 (a 42 :type fixnum)))
1193 ;; (:predicate) is not ok because absence of the argument does not mean
1194 ;; that the value of the option is NIL, as it must be for :typed un-:named.
1195 ;; ":predicate
1196 ;; This option takes one argument ...
1197 ;; If the argument is not supplied ... the name of the predicate is made
1198 ;; by concatenating the name of the structure to the string "-P"
1199 ;; If the argument is provided and is nil, no predicate is defined.
1200 ;; ... if :type is supplied and :named is not supplied, then :predicate
1201 ;; must either be unsupplied or have the value nil."
1203 ;; The last piece says that the entire option must be unsupplied
1204 ;; or else "have the value NIL", and is preceded by a description of the
1205 ;; precise manner in which absence of an argument is not the same as nil.
1207 (assert-error
1208 (eval '(defstruct (typed-struct2 (:type list) (:predicate))
1209 (a 42 :type fixnum)))))
1211 (with-test (:name (:boa-supplied-p &optional))
1212 (handler-bind ((sb-c:inlining-dependency-failure #'muffle-warning)
1213 (warning #'error))
1214 (eval `(defstruct (boa-supplied-p.1 (:constructor make-boa-supplied-p.1
1215 (&optional (bar t barp))))
1217 barp)))
1218 (let ((b1 (make-boa-supplied-p.1))
1219 (b2 (make-boa-supplied-p.1 t)))
1220 (assert (eq t (boa-supplied-p.1-bar b1)))
1221 (assert (eq t (boa-supplied-p.1-bar b2)))
1222 (assert (eq nil (boa-supplied-p.1-barp b1)))
1223 (assert (eq t (boa-supplied-p.1-barp b2)))))
1225 (with-test (:name (:boa-supplied-p &key))
1226 (handler-bind ((sb-c:inlining-dependency-failure #'muffle-warning)
1227 (warning #'error))
1228 (eval `(defstruct (boa-supplied-p.2 (:constructor make-boa-supplied-p.2
1229 (&key (bar t barp))))
1231 barp)))
1232 (let ((b1 (make-boa-supplied-p.2))
1233 (b2 (make-boa-supplied-p.2 :bar t)))
1234 (assert (eq t (boa-supplied-p.2-bar b1)))
1235 (assert (eq t (boa-supplied-p.2-bar b2)))
1236 (assert (eq nil (boa-supplied-p.2-barp b1)))
1237 (assert (eq t (boa-supplied-p.2-barp b2)))))
1239 (defstruct structure-with-predicate)
1240 (defclass class-to-be-redefined () ())
1241 (let ((x (make-instance 'class-to-be-redefined)))
1242 (defun function-trampoline (fun) (funcall fun x)))
1244 (with-test (:name (:struct-predicate :obsolete-instance))
1245 (defclass class-to-be-redefined () ((a :initarg :a :initform 1)))
1246 (function-trampoline #'structure-with-predicate-p))
1248 (with-test (:name (:defstruct :not-toplevel-silent))
1249 (let ((sb-ext:*evaluator-mode* :compile))
1250 (handler-bind ((warning #'error))
1251 (eval `(let ()
1252 (defstruct defstruct-no-warning-not-at-toplevel bar))))))
1254 (with-test (:name :bug-941102)
1255 (let ((test `((defstruct bug-941102)
1256 (setf (find-class 'bug-941102-alias) (find-class 'bug-941102))
1257 (setf (find-class 'bug-941102-alias) nil))))
1258 (multiple-value-bind (warn fail) (ctu:file-compile test :load t)
1259 (assert (not warn))
1260 (assert (not fail)))
1261 (multiple-value-bind (warn2 fail2) (ctu:file-compile test)
1262 (assert (not warn2))
1263 (assert (not fail2)))))
1265 (with-test (:name (:defstruct :constant-slot-names))
1266 (defstruct defstruct-constant-slot-names t)
1267 (assert (= 3 (defstruct-constant-slot-names-t
1268 (make-defstruct-constant-slot-names :t 3)))))
1270 (with-test (:name (:defstruct :bogus-inherited-slot-specs))
1271 (defstruct flopsie a b c)
1272 (assert
1273 (handler-case (macroexpand '(defstruct (mopsie (:include flopsie (a 3) (z 9))) q))
1274 (error (c)
1275 (search "slot name Z not present" (write-to-string c :escape nil)))
1276 (:no-error (x) x nil)))
1277 (assert
1278 (handler-case (macroexpand '(defstruct (mopsie (:include flopsie (a 3) (a 'a))) q))
1279 (error (c)
1280 (search "slot name A specified more than once" (write-to-string c :escape nil)))
1281 (:no-error (x) x nil))))
1283 (assert-error
1284 (defstruct bogus-aux.1 (:constructor make-bogus-aux.1 (&aux (a b c)))))
1286 (with-test (:name (:defstruct :lexical-default))
1287 (let ((x 0)) (defstruct lexical-default (a (incf x)))
1288 (assert (= (lexical-default-a (make-lexical-default))
1290 1))))
1292 (with-test (:name (:defstruct :find-defstruct-description))
1293 (assert (null (sb-kernel:find-defstruct-description 'not-foo nil)))
1295 (assert-error (sb-kernel:find-defstruct-description 'not-foo t)))
1297 (defstruct (a-named-struct :named (:type vector)) a b c)
1298 (defstruct (a-kid-struct :named (:type vector) (:include a-named-struct)) n)
1299 (with-test (:name (:defstruct :named-typed-struct-subtype-pred))
1300 (let ((par (make-a-named-struct :b 6))
1301 (kid (make-a-kid-struct :n 5)))
1302 (assert (a-named-struct-p par))
1303 (assert (a-named-struct-p kid))
1304 (assert (not (a-kid-struct-p par)))
1305 (assert (a-kid-struct-p kid))))
1307 (with-test (:name :defstruct-parse-strictly)
1308 (dolist (form
1309 '((defstruct (s :conc-name (:conc-name b1-)) x y)
1310 (defstruct (s :copier :copier) x y)
1311 (defstruct (s (:include) (:include)) x y)
1312 (defstruct (s (:initial-offset 2) (:initial-offset nil)) x y)
1313 (defstruct (s (:predicate nil) (:predicate foolp)) x y)
1314 (defstruct (s (:type list) (:type vector)) x y)
1315 ;; The :NAMED option requires that SYMBOL be a subtype of the
1316 ;; *supplied* element type (not the upgraded element-type).
1317 ;; Defining a subtype of the structure places another symbol in
1318 ;; the vector, and we can't anticipate what that will be.
1319 ;; [Though in practice it is somewhere between unlikely and
1320 ;; impossible that an implementation would be able to specialize
1321 ;; on only one particular symbol and not also allow any symbol]
1322 (defstruct (s (:type (vector (or (eql s) integer))) :named) x y)
1324 (assert-error (macroexpand form))))
1326 (defstruct (foo-not-too-something
1327 (:constructor make-foo-not-too-strong (x &aux (x (abs x))))
1328 (:constructor make-foo-not-too-weak (&optional x)))
1329 (x nil :type unsigned-byte))
1331 (with-test (:name :defstruct-ftype-correctness)
1332 (assert (make-foo-not-too-strong -3)) ; should be allowed
1333 (assert-error (make-foo-not-too-weak))) ; should not set X slot to NIL
1335 (defstruct fruitbat a (b #xbadf00d :type sb-ext:word) (c 'hi))
1336 (mapc 'fmakunbound '(fruitbat-a fruitbat-b fruitbat-c))
1337 (with-test (:name :defstruct-printer-robust)
1338 (assert (string= (princ-to-string (make-fruitbat :a "test"))
1339 "#S(FRUITBAT :A test :B 195948557 :C HI)")))
1341 ;; lp#540063
1342 (defstruct x y)
1343 (with-test (:name :redefine-accessor-as-random-defun)
1344 (flet ((assert-that (expect form)
1345 ;; test by STRING= since there's no condition class,
1346 ;; being a little more thorough than merely ASSERT-SIGNAL.
1347 (let (win)
1348 (handler-bind ((simple-warning
1349 (lambda (c)
1350 (when (string= (princ-to-string c) expect)
1351 (setq win t)
1352 (muffle-warning)))))
1353 (eval form)
1354 (assert win)))))
1355 (assert-that "redefinition of X-Y clobbers structure accessor"
1356 '(defun x-y (z) (list :x-y z)))
1357 (assert-that "redefinition of X-P clobbers structure predicate"
1358 '(defun x-p (z) (list 'bork z))))
1359 (assert (equalp
1360 (funcall (compile nil '(lambda (z) (x-y z)))
1361 (make-x :y t))
1362 '(:X-Y #S(X :Y T))))
1363 (assert (equalp
1364 (funcall (compile nil '(lambda (z)
1365 (declare (notinline x-y))
1366 (x-y z)))
1367 (make-x :y t))
1368 '(:X-Y #S(X :Y T)))))
1370 (in-package sb-kernel)
1372 ;; The word order for halves of double-floats on 32-bit platforms
1373 ;; should match the platform's native order.
1374 (defun compare-memory (obj1 obj1-word-ofs obj2 obj2-word-ofs n-words)
1375 (with-pinned-objects (obj1 obj2)
1376 (let ((sap1 (int-sap (logandc2 (get-lisp-obj-address obj1) sb-vm:lowtag-mask)))
1377 (sap2 (int-sap (logandc2 (get-lisp-obj-address obj2) sb-vm:lowtag-mask))))
1378 (dotimes (i n-words)
1379 (let ((w1 (sap-ref-32 sap1 (ash (+ obj1-word-ofs i) sb-vm:word-shift)))
1380 (w2 (sap-ref-32 sap2 (ash (+ obj2-word-ofs i) sb-vm:word-shift))))
1381 (assert (= w1 w2)))))))
1383 (defstruct struct-df (a pi :type double-float))
1384 (defvar *c* (complex (exp 1d0) pi))
1385 (defstruct struct-cdf (a *c* :type (complex double-float)))
1387 (defvar *adf* (make-array 1 :element-type 'double-float
1388 :initial-element pi))
1389 (defvar *acdf* (make-array 1 :element-type '(complex double-float)
1390 :initial-element *c*))
1392 (test-util:with-test (:name :dfloat-endianness
1393 :skipped-on '(not (or :mips :x86))) ; only tested on these
1394 (compare-memory pi 2 *adf* 2 2) ; Array
1395 (compare-memory pi 2 (make-struct-df) 2 2) ; Structure
1397 (compare-memory *c* 2 *acdf* 2 4) ; Array
1398 (compare-memory *c* 2 (make-struct-cdf) 2 4)) ; Structure
1400 (test-util:with-test (:name :recklessly-continuable-defstruct)
1401 (flet ((redefine-defstruct (from to)
1402 (eval from)
1403 (handler-bind
1404 ((error (lambda (c)
1405 (declare (ignore c))
1406 (return-from redefine-defstruct
1407 ;; RESTARTs are DX, don't return it.
1408 (not (null (find 'sb-kernel::recklessly-continue
1409 (compute-restarts)
1410 :key 'restart-name))))))
1411 (warning #'muffle-warning))
1412 (eval to))))
1413 (assert (not (redefine-defstruct
1414 '(defstruct not-redefinable (a 0 :type sb-ext:word))
1415 '(defstruct not-redefinable (a)))))
1416 (assert (redefine-defstruct
1417 ;; Incompatible types has nothing to do with whether
1418 ;; RECKLESSLY-CONTINUE is offered.
1419 '(defstruct redefinable (a nil :type symbol))
1420 '(defstruct redefinable (a nil :type cons))))))