1 ;;;; This software is part of the SBCL system. See the README file for
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
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 "assertoid.lisp")
13 (load "compiler-test-util.lisp")
14 (use-package "ASSERTOID")
16 ;;;; examples from, or close to, the Common Lisp DEFSTRUCT spec
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
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)
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)
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))
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
51 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
52 (let ((s (make-boa-saux)))
53 (locally (declare (optimize (safety 0))
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)))
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)))
73 (defstruct (astronaut (:include person)
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)))
84 (favorite-beverage 'tang))
85 (assert (eql (ancient-astronaut-age (make-ancient-astronaut :name "John")) 77))
87 ;;; interaction of :TYPE and :INCLUDE and :INITIAL-OFFSET
88 (defstruct (binop (:type list) :named (:initial-offset 2))
89 (operator '? :type symbol)
92 (defstruct (annotated-binop (:type list)
95 commutative associative identity)
96 (assert (equal (make-annotated-binop :operator '*
102 '(nil nil binop * x 5 nil nil nil t t 1)))
104 ;;; effect of :NAMED on :TYPE
105 (defstruct (named-binop (:type list) :named)
106 (operator '? :type symbol)
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)))))
122 (firetrucks 1 :type fixnum)
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
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
144 (defstruct (clown (:conc-name bozo-))
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))
156 (assert (is-a-bozo-p (make-up-klown)))
158 ;;;; systematically testing variants of DEFSTRUCT:
159 ;;;; * native, :TYPE LIST, and :TYPE VECTOR
161 ;;; FIXME: things to test:
162 ;;; * Slot readers work.
163 ;;; * Slot writers work.
164 ;;; * Predicates work.
166 ;;; FIXME: things that would be nice to test systematically someday:
167 ;;; * constructors (default, boa..)
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
174 (defun string+ (&rest rest)
175 (apply #'concatenate 'string
176 (mapcar #'string rest)))
177 (defun symbol+ (&rest rest)
178 (values (intern (apply #'string+ rest))))
180 (defun accessor-name (conc-name slot-name)
181 (symbol+ conc-name slot-name))
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)))
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
196 (,(accessor-name conc-name slot-name) 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)
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))
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)
218 (write-slot-inline new-value conc-name slot-name instance)
219 (write-slot-notinline new-value conc-name slot-name instance)))
221 ;;; bound during the tests so that we can get to it even if the
222 ;;; debugger is having a bad day
225 (declaim (optimize (debug 2)))
227 (defmacro test-variant (defstructname &key colontype boa-constructor-p)
230 (format t "~&/beginning PROGN for COLONTYPE=~S~%" ',colontype)
232 (defstruct (,defstructname
233 ,@(when colontype `((:type ,colontype)))
234 ,@(when boa-constructor-p
235 `((:constructor ,(symbol+ "CREATE-" defstructname)
238 (optional-test 2 optional-test-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))
247 ;; some ordinary tagged slots
249 (home nil :type package :read-only t)
250 (comment "" :type simple-string)
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)))
257 (format t "~&/done with DEFSTRUCT~%")
259 (let* ((cn (string+ ',defstructname "-")) ; conc-name
260 (ctor (symbol-function ',(symbol+ (if boa-constructor-p
264 (*instance* (funcall ctor
265 ,@(unless boa-constructor-p
267 ,@(when boa-constructor-p
269 :home (find-package :cl)
270 :hash (+ 14 most-positive-fixnum)
271 ,@(unless boa-constructor-p
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*)))
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.)
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~%")
307 ;; :TYPE FOO objects don't go in the Lisp type system, so we
308 ;; can't test TYPEP stuff for them.
310 ;; FIXME: However, when they're named, they do define
311 ;; predicate functions, and we could test those.
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))))
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)
334 (compile nil `(lambda (x) (typep x ',typename)))))
335 (assert (funcall compiled-typep *instance*))
336 (assert (not (funcall compiled-typep nil))))))))
338 (format t "~&/done with PROGN for COLONTYPE=~S~%" ',colontype)))
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)
348 ;;;; testing raw slots harder
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).
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)))
372 (defvar *manyraw* (make-manyraw))
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)))
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))
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))))
404 ;;;; Since GC treats raw slots specially now, let's try this with more objects
405 ;;;; and random values as a stress test.
409 (defconstant +n-manyraw+ 10)
410 (defconstant +m-manyraw+ 1000)
412 (defun check-manyraws (manyraws)
413 (assert (eql (length manyraws) (* +n-manyraw+ +m-manyraw+)))
415 for m in (reverse manyraws)
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)
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)))
435 (cdr (manyraw-unraw-slot-just-for-variety m)))))
437 (defstruct (manyraw-subclass (:include manyraw))
438 (stolperstein 0 :type (unsigned-byte 32)))
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))
447 (random most-positive-single-float)
448 (random most-positive-single-float)))
450 (random most-positive-double-float)
451 (random most-positive-double-float))))
452 (push (funcall (if (zerop (mod x 3))
453 #'make-manyraw-subclass
455 :unraw-slot-just-for-variety
456 (list (+ x (* y +m-manyraw+)) a b c d e)
465 (with-test (:name :defstruct-raw-slot-gc)
466 (check-manyraws *manyraw*))
468 ;;; try a full GC, too
470 (with-test (:name (:defstruct-raw-slot-gc :full))
471 (check-manyraws *manyraw*))
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)
477 :sb-just-dump-it-normally)
478 (with-open-file (s "tmp-defstruct.manyraw.lisp"
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")
485 ;;; nuke the objects and try another GC just to be extra careful
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)))
495 ;;;; miscellaneous old bugs
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."))
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.
508 (defstruct (bug210a (:constructor bug210a ()))
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))
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)))
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)))
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))
539 (defun bug127-flurble (x)
541 (defstruct bug127 flurble)
542 (assert (= (bug127-flurble (make-bug127 :flurble 7)) 7))
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))
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))
556 ;;; FIXME: should probably do the same tests on DEFSTRUCT :TYPE
558 ;;; As noted by Paul Dietz for CMUCL, :CONC-NAME handling was a little
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))
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))
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 #())))
590 ;;; bug 3d: type safety with redefined type constraints on slots
591 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
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)))))
600 (defstruct (,base-name ,@type-options)
602 (defstruct (,up-name (:include ,base-name
603 (x "x" :type simple-string)
604 (y "y" :type simple-string))
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))
613 do (assert (raises-error? (funcall (compile nil fun) ob)
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)))
626 ;;; 322: "DEFSTRUCT :TYPE LIST predicate and improper lists"
627 ;;; reported by Bruno Haible sbcl-devel "various SBCL bugs" from CLISP
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)))
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)))
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)
654 (error "shouldn't warn just from macroexpansion here"))))
656 ;;; bug 318 symptom no 1. (rest not fixed yet)
658 (handler-bind ((error (lambda (c)
659 ;; Used to cause stack-exhaustion
660 (unless (typep c 'storage-condition)
664 (setf (find-class 'foo) nil)
665 (defstruct foo slot-1)))))
667 ;;; bug 348, evaluation order of slot writer arguments. Fixed by Gabor
669 (defstruct bug-348 x)
671 (assert (eql -1 (let ((i (eval '-2))
673 (funcall #'(setf bug-348-x)
675 (aref (vector x) (incf i)))
678 ;;; obsolete instance trapping
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.)
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)))
692 (sb-pcl::obsolete-structure ()
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)))
702 (typep x (find-class 'standard-class))
703 (sb-kernel:layout-invalid ()
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)
711 (a 0d0 :type double-float))
713 (defstruct raw-slot-equalp-bug-2
714 (b (complex 1d0) :type (complex double-float))
715 (x (complex 1d0) :type (complex double-float))
717 (a 1s0 :type single-float))
719 (with-test (:name :raw-slot-equalp)
720 (assert (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
721 (make-raw-slot-equalp-bug :a 1d0 :b 2s0)))
722 (assert (equalp (make-raw-slot-equalp-bug :a 1d0 :b 0s0)
723 (make-raw-slot-equalp-bug :a 1d0 :b -0s0)))
724 (assert (not (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
725 (make-raw-slot-equalp-bug :a 1d0 :b 3s0))))
726 (assert (not (equalp (make-raw-slot-equalp-bug :a 1d0 :b 2s0)
727 (make-raw-slot-equalp-bug :a 2d0 :b 2s0))))
728 (assert (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)
729 (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)))
730 (assert (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 0s0)
731 (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a -0s0)))
732 (assert (not (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)
733 (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 3s0))))
734 (assert (not (equalp (make-raw-slot-equalp-bug-2 :b (complex 1d0) :a 2s0)
735 (make-raw-slot-equalp-bug-2 :b (complex 2d0) :a 2s0)))))
737 ;;; Check that all slot types (non-raw and raw) can be initialized with
738 ;;; constant arguments.
739 (defstruct constant-arg-inits
742 (c 2 :type sb-vm:word)
743 (d 3.0 :type single-float)
744 (e 4.0d0 :type double-float)
745 (f #c(5.0 5.0) :type (complex single-float))
746 (g #c(6.0d0 6.0d0) :type (complex double-float)))
747 (defun test-constant-arg-inits ()
748 (let ((foo (make-constant-arg-inits)))
749 (declare (dynamic-extent foo))
750 (assert (eql 42 (constant-arg-inits-a foo)))
751 (assert (eql 1 (constant-arg-inits-b foo)))
752 (assert (eql 2 (constant-arg-inits-c foo)))
753 (assert (eql 3.0 (constant-arg-inits-d foo)))
754 (assert (eql 4.0d0 (constant-arg-inits-e foo)))
755 (assert (eql #c(5.0 5.0) (constant-arg-inits-f foo)))
756 (assert (eql #c(6.0d0 6.0d0) (constant-arg-inits-g foo)))))
757 (make-constant-arg-inits)
759 ;;; bug reported by John Morrison, 2008-07-22 on sbcl-devel
760 (defstruct (raw-slot-struct-with-unknown-init (:constructor make-raw-slot-struct-with-unknown-init ()))
761 (x (#:unknown-function) :type double-float))
763 ;;; Some checks for the behavior of incompatibly redefining structure
764 ;;; classes. We don't actually check that our detection of
765 ;;; "incompatible" is comprehensive, only that if an incompatible
766 ;;; definition is processed, we do various things.
767 (defmacro with-files ((&rest vars) &body body)
768 "Evaluate BODY with VARS bound to a number of filenames, then
769 delete the files at the end."
770 (let* ((paths (loop for var in vars
772 collect (make-pathname
775 "DEFSTRUCT-REDEF-TEST-~D"
778 (binding-spec (mapcar
779 (lambda (var path) `(,var ,path)) vars paths)))
786 (delete-file ,(elt paths n))))))
788 ,@(frob (1- (length vars)))))))
790 (defun noclobber (pathspec &rest forms)
791 "Write FORMS to the file named by PATHSPEC, erroring if
792 PATHSPEC already names an existing file."
793 (with-open-file (*standard-output* pathspec :direction :output
795 (print '(in-package "CL-USER"))
796 (mapc #'print forms)))
798 (defun compile-file-assert (file &optional (want-error-p t) (want-warning-p t))
799 "Compile FILE and assert some things about the results."
800 (multiple-value-bind (fasl errors-p warnings-p)
803 (assert (eq errors-p want-error-p))
804 (assert (eq warnings-p want-warning-p))
807 (defun continue-from-incompatible-defstruct-error (error)
808 "Invoke the CONTINUE restart for an incompatible DEFSTRUCT
810 ;; FIXME: want distinct error type for incompatible defstruct.
811 (when (search "attempt to redefine" (simple-condition-format-control error))
812 (when (find-restart 'continue)
813 (invoke-restart 'continue))))
815 (defun recklessly-continue-from-incompatible-defstruct-error (error)
816 "Invoke the RECKLESSLY-CONTINUE restart for an incompatible DEFSTRUCT
818 ;; FIXME: want distinct error type for incompatible defstruct.
819 (when (search "attempt to redefine" (simple-condition-format-control error))
820 (when (find-restart 'sb-kernel::recklessly-continue)
821 (invoke-restart 'sb-kernel::recklessly-continue))))
823 (defun assert-is (predicate instance)
824 (assert (funcall predicate instance)))
826 (defun assert-invalid (predicate instance)
827 (assert (typep (nth-value 1 (ignore-errors (funcall predicate instance)))
828 'sb-kernel::layout-invalid)))
830 ;; Don't try to understand this macro; just look at its expansion.
831 (defmacro with-defstruct-redefinition-test (name
832 (&rest defstruct-form-bindings)
833 (&rest path-form-specs)
836 (labels ((make-defstruct-form (&key class-name super-name slots)
837 (let* ((predicate-name
838 (read-from-string (format nil "~A-p" class-name)))
840 (read-from-string (format nil "make-~A" class-name))))
842 '(defstruct (,class-name
843 (:constructor ,constructor-name)
845 `((:include ,super-name))))
849 (frob (bindspecs classno)
851 `((multiple-value-bind ,(first (first bindspecs))
852 ,(apply #'make-defstruct-form (rest (first bindspecs)))
853 (declare (ignorable ,@(first (first bindspecs))))
854 ,@(frob (rest bindspecs) (1+ classno))))
855 `((with-files ,(mapcar #'first path-form-specs)
856 ,@(mapcar (lambda (path-form) `(noclobber ,@path-form))
859 ((simple-error ',handler-function))
861 `(with-test (:name ,name)
862 ,(first (frob defstruct-form-bindings 0)))))
864 ;; When eyeballing these, it's helpful to see when various things are
866 (setq *compile-verbose* t *load-verbose* t)
869 ;; Base case: recklessly-continue.
870 (with-defstruct-redefinition-test :defstruct/recklessly
871 (((defstruct ctor pred) :class-name redef-test-1 :slots (a))
872 ((defstruct*) :class-name redef-test-1 :slots (a b)))
875 recklessly-continue-from-incompatible-defstruct-error
877 (let ((instance (funcall ctor)))
879 (assert-is pred instance)))
881 ;; Base case: continue (i.e., invalidate instances).
882 (with-defstruct-redefinition-test :defstruct/continue
883 (((defstruct ctor pred) :class-name redef-test-2 :slots (a))
884 ((defstruct*) :class-name redef-test-2 :slots (a b)))
887 continue-from-incompatible-defstruct-error
889 (let ((instance (funcall ctor)))
891 (assert-invalid pred instance)))
893 ;; Compiling a file with an incompatible defstruct should emit a
894 ;; warning and an error, but the fasl should be loadable.
895 (with-defstruct-redefinition-test :defstruct/compile-file-should-warn
896 (((defstruct) :class-name redef-test-3 :slots (a))
897 ((defstruct*) :class-name redef-test-3 :slots (a b)))
900 continue-from-incompatible-defstruct-error
902 (load (compile-file-assert path2)))
904 ;; After compiling a file with an incompatible DEFSTRUCT, load the
905 ;; fasl and ensure that an old instance remains valid.
906 (with-defstruct-redefinition-test :defstruct/compile-file-reckless
907 (((defstruct ctor pred) :class-name redef-test-4 :slots (a))
908 ((defstruct*) :class-name redef-test-4 :slots (a b)))
911 recklessly-continue-from-incompatible-defstruct-error
913 (let ((instance (funcall ctor)))
914 (load (compile-file-assert path2))
915 (assert-is pred instance)))
917 ;; After compiling a file with an incompatible DEFSTRUCT, load the
918 ;; fasl and ensure that an old instance has become invalid.
919 (with-defstruct-redefinition-test :defstruct/compile-file-continue
920 (((defstruct ctor pred) :class-name redef-test-5 :slots (a))
921 ((defstruct*) :class-name redef-test-5 :slots (a b)))
924 continue-from-incompatible-defstruct-error
926 (let ((instance (funcall ctor)))
927 (load (compile-file-assert path2))
928 (assert-invalid pred instance)))
931 ;; Ensure that recklessly continuing DT(expected)T to instances of
932 ;; subclasses. (This is a case where recklessly continuing is
933 ;; actually dangerous, but we don't care.)
934 (with-defstruct-redefinition-test :defstruct/subclass-reckless
935 (((defstruct ignore pred1) :class-name redef-test-6 :slots (a))
936 ((substruct ctor pred2) :class-name redef-test-6-sub
937 :super-name redef-test-6 :slots (z))
938 ((defstruct*) :class-name redef-test-6 :slots (a b)))
939 ((path1 defstruct substruct)
940 (path2 defstruct* substruct))
941 recklessly-continue-from-incompatible-defstruct-error
943 (let ((instance (funcall ctor)))
944 (load (compile-file-assert path2))
945 (assert-is pred1 instance)
946 (assert-is pred2 instance)))
948 ;; Ensure that continuing invalidates instances of subclasses.
949 (with-defstruct-redefinition-test :defstruct/subclass-continue
950 (((defstruct) :class-name redef-test-7 :slots (a))
951 ((substruct ctor pred) :class-name redef-test-7-sub
952 :super-name redef-test-7 :slots (z))
953 ((defstruct*) :class-name redef-test-7 :slots (a b)))
954 ((path1 defstruct substruct)
955 (path2 defstruct* substruct))
956 continue-from-incompatible-defstruct-error
958 (let ((instance (funcall ctor)))
959 (load (compile-file-assert path2))
960 (assert-invalid pred instance)))
962 ;; Reclkessly continuing doesn't invalidate instances of subclasses.
963 (with-defstruct-redefinition-test :defstruct/subclass-in-other-file-reckless
964 (((defstruct ignore pred1) :class-name redef-test-8 :slots (a))
965 ((substruct ctor pred2) :class-name redef-test-8-sub
966 :super-name redef-test-8 :slots (z))
967 ((defstruct*) :class-name redef-test-8 :slots (a b)))
971 recklessly-continue-from-incompatible-defstruct-error
974 (let ((instance (funcall ctor)))
975 (load (compile-file-assert path3))
976 (assert-is pred1 instance)
977 (assert-is pred2 instance)))
979 ;; This is an icky case: when a subclass is defined in a separate
980 ;; file, CONTINUE'ing from LOAD of a file containing an incompatible
981 ;; superclass definition leaves the predicates and accessors into the
982 ;; subclass in a bad way until the subclass form is evaluated.
983 (with-defstruct-redefinition-test :defstruct/subclass-in-other-file-continue
984 (((defstruct ignore pred1) :class-name redef-test-9 :slots (a))
985 ((substruct ctor pred2) :class-name redef-test-9-sub
986 :super-name redef-test-9 :slots (z))
987 ((defstruct*) :class-name redef-test-9 :slots (a b)))
991 continue-from-incompatible-defstruct-error
994 (let ((instance (funcall ctor)))
995 (load (compile-file-assert path3))
996 ;; At this point, the instance of the subclass will not count as
997 ;; an instance of the superclass or of the subclass, but PRED2's
998 ;; predicate will error with "an obsolete structure accessor
999 ;; function was called".
1000 (assert-invalid pred1 instance)
1001 (format t "~&~A~%" (nth-value 1 (ignore-errors (funcall pred2 instance))))
1002 ;; After loading PATH2, we'll get the desired LAYOUT-INVALID error.
1004 (assert-invalid pred2 instance)))
1006 ;; Some other subclass wrinkles have to do with splitting definitions
1007 ;; accross files and compiling and loading things in a funny order.
1008 (with-defstruct-redefinition-test
1009 :defstruct/subclass-in-other-file-funny-operation-order-continue
1010 (((defstruct ignore pred1) :class-name redef-test-10 :slots (a))
1011 ((substruct ctor pred2) :class-name redef-test-10-sub
1012 :super-name redef-test-10 :slots (z))
1013 ((defstruct*) :class-name redef-test-10 :slots (a b)))
1017 continue-from-incompatible-defstruct-error
1020 (let ((instance (funcall ctor)))
1021 ;; First we clobber the compiler's layout for the superclass.
1022 (compile-file-assert path3)
1023 ;; Then we recompile the subclass definition (which generates a
1024 ;; warning about the compiled layout for the superclass being
1025 ;; incompatible with the loaded layout, because we haven't loaded
1026 ;; path3 since recompiling).
1027 (compile-file path2)
1028 ;; Ugh. I don't want to think about loading these in the wrong
1030 (load (compile-file-pathname path3))
1031 (load (compile-file-pathname path2))
1032 (assert-invalid pred1 instance)
1033 (assert-invalid pred2 instance)))
1035 (with-defstruct-redefinition-test
1036 :defstruct/subclass-in-other-file-funny-operation-order-continue
1037 (((defstruct ignore pred1) :class-name redef-test-11 :slots (a))
1038 ((substruct ctor pred2) :class-name redef-test-11-sub
1039 :super-name redef-test-11 :slots (z))
1040 ((defstruct*) :class-name redef-test-11 :slots (a b)))
1044 continue-from-incompatible-defstruct-error
1047 (let ((instance (funcall ctor)))
1048 ;; This clobbers the compiler's layout for REDEF-TEST-11.
1049 (compile-file-assert path3)
1050 ;; This recompiles REDEF-TEST-11-SUB, using the new REDEF-TEST-11
1052 (load (compile-file-pathname path2))
1053 ;; Note that because we haven't loaded PATH3, we haven't clobbered
1054 ;; the class's layout REDEF-TEST-11, so REDEF-11's predicate will
1055 ;; still work. That's probably bad.
1056 (assert-is pred1 instance)
1057 (assert-is pred2 instance)))
1059 (with-test (:name :raw-slot/circle-subst)
1060 ;; CIRCLE-SUBSTS used %INSTANCE-REF on raw slots
1061 (multiple-value-bind (list n)
1063 (defstruct raw-slot/circle-subst
1064 (x 0.0 :type single-float))
1065 (read-from-string "((#1=#S(raw-slot/circle-subst :x 2.7158911)))")))
1066 (destructuring-bind ((struct)) list
1067 (assert (raw-slot/circle-subst-p struct))
1068 (assert (eql 2.7158911 (raw-slot/circle-subst-x struct)))
1069 (assert (eql 45 n)))))
1071 (defstruct (bug-3b (:constructor make-bug-3b (&aux slot)))
1072 (slot nil :type string))
1074 (with-test (:name :bug-3b)
1077 (bug-3b-slot (make-bug-3b))
1080 (assert (eq 'string (type-error-expected-type e)))
1081 (assert (zerop (type-error-datum e))))))
1083 (with-test (:name :defstruct-copier-typechecks-argument)
1084 (assert (not (raises-error? (copy-person (make-astronaut :name "Neil")))))
1085 (assert (raises-error? (copy-astronaut (make-person :name "Fred")))))
1087 (with-test (:name :bug-528807)
1088 (let ((*evaluator-mode* :compile))
1089 (handler-bind ((style-warning #'error))
1090 (eval `(defstruct (bug-528807 (:constructor make-528807 (&aux x)))
1091 (x nil :type fixnum))))))
1093 (with-test (:name :bug-520607)
1096 (eval '(defstruct (typed-struct (:type list) (:predicate typed-struct-p))
1097 (a 42 :type fixnum)))))
1098 ;; NIL is ok, though.
1099 (eval '(defstruct (typed-struct (:type list) (:predicate nil))
1100 (a 42 :type fixnum)))
1102 (eval '(defstruct (typed-struct2 (:type list) (:predicate))
1103 (a 42 :type fixnum))))
1105 (with-test (:name (:boa-supplied-p &optional))
1106 (handler-bind ((warning #'error))
1107 (eval `(defstruct (boa-supplied-p.1 (:constructor make-boa-supplied-p.1
1108 (&optional (bar t barp))))
1111 (let ((b1 (make-boa-supplied-p.1))
1112 (b2 (make-boa-supplied-p.1 t)))
1113 (assert (eq t (boa-supplied-p.1-bar b1)))
1114 (assert (eq t (boa-supplied-p.1-bar b2)))
1115 (assert (eq nil (boa-supplied-p.1-barp b1)))
1116 (assert (eq t (boa-supplied-p.1-barp b2)))))
1118 (with-test (:name (:boa-supplied-p &key))
1119 (handler-bind ((warning #'error))
1120 (eval `(defstruct (boa-supplied-p.2 (:constructor make-boa-supplied-p.2
1121 (&key (bar t barp))))
1124 (let ((b1 (make-boa-supplied-p.2))
1125 (b2 (make-boa-supplied-p.2 :bar t)))
1126 (assert (eq t (boa-supplied-p.2-bar b1)))
1127 (assert (eq t (boa-supplied-p.2-bar b2)))
1128 (assert (eq nil (boa-supplied-p.2-barp b1)))
1129 (assert (eq t (boa-supplied-p.2-barp b2)))))
1131 (defstruct structure-with-predicate)
1132 (defclass class-to-be-redefined () ())
1133 (let ((x (make-instance 'class-to-be-redefined)))
1134 (defun function-trampoline (fun) (funcall fun x)))
1136 (with-test (:name (:struct-predicate :obsolete-instance))
1137 (defclass class-to-be-redefined () ((a :initarg :a :initform 1)))
1138 (function-trampoline #'structure-with-predicate-p))
1140 (with-test (:name (:defstruct :not-toplevel-silent))
1141 (let ((sb-ext:*evaluator-mode* :compile))
1142 (handler-bind ((warning #'error))
1144 (defstruct defstruct-no-warning-not-at-toplevel bar))))))
1146 (with-test (:name :bug-941102)
1147 (let ((test `((defstruct bug-941102)
1148 (setf (find-class 'bug-941102-alias) (find-class 'bug-941102))
1149 (setf (find-class 'bug-941102-alias) nil))))
1150 (multiple-value-bind (warn fail) (ctu:file-compile test :load t)
1152 (assert (not fail)))
1153 (multiple-value-bind (warn2 fail2) (ctu:file-compile test)
1154 (assert (not warn2))
1155 (assert (not fail2)))))
1157 (with-test (:name (defstruct :constant-slot-names))
1158 (defstruct defstruct-constant-slot-names t)
1159 (assert (= 3 (defstruct-constant-slot-names-t
1160 (make-defstruct-constant-slot-names :t 3)))))