Make INFO's compiler-macro more forgiving.
[sbcl.git] / tests / dynamic-extent.impure.lisp
blob722b9096b24e9255a7d021c5079a7282cdde62e8
1 ;;;; tests that dynamic-extent functionality works.
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (when (eq sb-ext:*evaluator-mode* :interpret)
15 (sb-ext:exit :code 104))
17 (load "compiler-test-util.lisp")
18 (use-package :ctu)
20 (setq sb-c::*check-consistency* t
21 sb-ext:*stack-allocate-dynamic-extent* t)
23 (defmacro defun-with-dx (name arglist &body body)
24 (let ((debug-name (sb-int:symbolicate name "-HIGH-DEBUG"))
25 (default-name (sb-int:symbolicate name "-DEFAULT")))
26 `(progn
27 (defun ,debug-name ,arglist
28 (declare (optimize debug))
29 ,@body)
30 (defun ,default-name ,arglist
31 ,@body)
32 (defun ,name (&rest args)
33 (apply #',debug-name args)
34 (apply #',default-name args)))))
36 (declaim (notinline opaque-identity))
37 (defun opaque-identity (x)
40 ;;; &REST lists
42 (defun-with-dx dxlength (&rest rest)
43 (declare (dynamic-extent rest))
44 (length rest))
46 (with-test (:name (:dx-&rest :basics))
47 (assert (= (dxlength 1 2 3) 3))
48 (assert (= (dxlength t t t t t t) 6))
49 (assert (= (dxlength) 0)))
51 (defun callee (list)
52 (destructuring-bind (a b c d e f &rest g) list
53 (+ a b c d e f (length g))))
55 (defun-with-dx dxcaller (&rest rest)
56 (declare (dynamic-extent rest))
57 (callee rest))
59 (with-test (:name (:dx-&rest :pass-down-to-callee :tail-call))
60 (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
62 (defun-with-dx dxcaller-align-1 (x &rest rest)
63 (declare (dynamic-extent rest))
64 (+ x (callee rest)))
66 (with-test (:name (:dx-&rest :pass-down-to-callee :non-tail-call))
67 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
68 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
70 ;;; %NIP-VALUES
72 (defun-with-dx test-nip-values ()
73 (flet ((bar (x &rest y)
74 (declare (dynamic-extent y))
75 (if (> x 0)
76 (values x (length y))
77 (values (car y)))))
78 (multiple-value-call #'values
79 (bar 1 2 3 4 5 6)
80 (bar -1 'a 'b))))
82 (with-test (:name (:nip-values))
83 (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a))))
85 ;;; LET-variable substitution
87 (defun-with-dx test-let-var-subst1 (x)
88 (let ((y (list x (1- x))))
89 (opaque-identity :foo)
90 (let ((z (the list y)))
91 (declare (dynamic-extent z))
92 (length z))))
94 (with-test (:name (:let-variable-substitution))
95 (assert (eql (test-let-var-subst1 17) 2)))
97 (defun-with-dx test-let-var-subst2 (x)
98 (let ((y (list x (1- x))))
99 (declare (dynamic-extent y))
100 (opaque-identity :foo)
101 (let ((z (the list y)))
102 (length z))))
104 (with-test (:name (:let-variable-substitution-2))
105 (assert (eql (test-let-var-subst2 17) 2)))
108 ;;; DX propagation through LET-return.
110 (defun-with-dx test-lvar-subst (x)
111 (let ((y (list x (1- x))))
112 (declare (dynamic-extent y))
113 (second (let ((z (the list y)))
114 (opaque-identity :foo)
115 z))))
117 (with-test (:name (:dx-propagation-through-let-return))
118 (assert (eql (test-lvar-subst 11) 10)))
120 ;;; this code is incorrect, but the compiler should not fail
121 (defun-with-dx test-let-var-subst-incorrect (x)
122 (let ((y (list x (1- x))))
123 (opaque-identity :foo)
124 (let ((z (the list y)))
125 (declare (dynamic-extent z))
126 (opaque-identity :bar)
127 z)))
129 ;;; alignment
131 (defvar *x*)
132 (defun-with-dx test-alignment-dx-list (form)
133 (multiple-value-prog1 (eval form)
134 (let ((l (list 1 2 3 4)))
135 (declare (dynamic-extent l))
136 (setq *x* (copy-list l)))))
138 (with-test (:name (:dx-list :alignment))
139 (dotimes (n 64)
140 (let* ((res (loop for i below n collect i))
141 (form `(values ,@res)))
142 (assert (equal (multiple-value-list (test-alignment-dx-list form)) res))
143 (assert (equal *x* '(1 2 3 4))))))
145 ;;; closure
147 (declaim (notinline true))
148 (defun true (x)
149 (declare (ignore x))
152 (defun-with-dx dxclosure (x)
153 (flet ((f (y)
154 (+ y x)))
155 (declare (dynamic-extent #'f))
156 (true #'f)))
158 (with-test (:name (:dx-closure))
159 (assert (eq t (dxclosure 13))))
161 ;;; value-cells
163 (defun-with-dx dx-value-cell (x)
164 ;; Not implemented everywhere, yet.
165 #+(or x86 x86-64 mips hppa)
166 (let ((cell x))
167 (declare (sb-int:truly-dynamic-extent cell))
168 (flet ((f ()
169 (incf cell)))
170 (declare (dynamic-extent #'f))
171 (true #'f))))
173 ;;; CONS
175 (defun-with-dx cons-on-stack (x)
176 (let ((cons (cons x x)))
177 (declare (dynamic-extent cons))
178 (true cons)
179 nil))
181 ;;; MAKE-ARRAY
183 (defun force-make-array-on-stack (n)
184 (declare (optimize safety))
185 (let ((v (make-array (min n 1))))
186 (declare (sb-int:truly-dynamic-extent v))
187 (true v)
188 (true v)
189 nil))
191 (defun-with-dx make-array-on-stack-1 ()
192 (let ((v (make-array '(42) :element-type 'single-float)))
193 (declare (dynamic-extent v))
194 (true v)
195 (true v)
196 nil))
198 (defun-with-dx make-array-on-stack-2 (n x)
199 (declare (integer n))
200 (let ((v (make-array n :initial-contents x)))
201 (declare (sb-int:truly-dynamic-extent v))
202 (true v)
203 (true v)
204 nil))
206 (defun-with-dx make-array-on-stack-3 (x y z)
207 (let ((v (make-array 3
208 :element-type 'fixnum :initial-contents (list x y z)
209 :element-type t :initial-contents x)))
210 (declare (sb-int:truly-dynamic-extent v))
211 (true v)
212 (true v)
213 nil))
215 (defun-with-dx make-array-on-stack-4 ()
216 (let ((v (make-array 3 :initial-contents '(1 2 3))))
217 (declare (sb-int:truly-dynamic-extent v))
218 (true v)
219 (true v)
220 nil))
222 (defun-with-dx make-array-on-stack-5 ()
223 (let ((v (make-array 3 :initial-element 12 :element-type t)))
224 (declare (sb-int:truly-dynamic-extent v))
225 (true v)
226 (true v)
227 nil))
229 (defun-with-dx make-array-on-stack-6 ()
230 (let ((v (make-array 3 :initial-element 12 :element-type '(unsigned-byte 8))))
231 (declare (sb-int:truly-dynamic-extent v))
232 (true v)
233 (true v)
234 nil))
236 (defun-with-dx make-array-on-stack-7 ()
237 (let ((v (make-array 3 :initial-element 12 :element-type '(signed-byte 8))))
238 (declare (sb-int:truly-dynamic-extent v))
239 (true v)
240 (true v)
241 nil))
243 (defun-with-dx make-array-on-stack-8 ()
244 (let ((v (make-array 3 :initial-element 12 :element-type 'word)))
245 (declare (sb-int:truly-dynamic-extent v))
246 (true v)
247 (true v)
248 nil))
250 (defun-with-dx make-array-on-stack-9 ()
251 (let ((v (make-array 3 :initial-element 12.0 :element-type 'single-float)))
252 (declare (sb-int:truly-dynamic-extent v))
253 (true v)
254 (true v)
255 nil))
257 (defun-with-dx make-array-on-stack-10 ()
258 (let ((v (make-array 3 :initial-element 12.0d0 :element-type 'double-float)))
259 (declare (sb-int:truly-dynamic-extent v))
260 (true v)
261 (true v)
262 nil))
264 (defun-with-dx make-array-on-stack-11 ()
265 (let ((v (make-array (the integer (opaque-identity 3)) :initial-element 12.0d0 :element-type 'double-float)))
266 (declare (sb-int:truly-dynamic-extent v))
267 (true v)
268 (true v)
269 nil))
271 (defun-with-dx vector-on-stack (x y)
272 (let ((v (vector 1 x 2 y 3)))
273 (declare (sb-int:truly-dynamic-extent v))
274 (true v)
275 nil))
277 ;;; MAKE-STRUCTURE
279 (declaim (inline make-fp-struct-1))
280 (defstruct fp-struct-1
281 (s 0.0 :type single-float)
282 (d 0.0d0 :type double-float))
284 (defun-with-dx test-fp-struct-1.1 (s d)
285 (let ((fp (make-fp-struct-1 :s s)))
286 (declare (dynamic-extent fp))
287 (assert (eql s (fp-struct-1-s fp)))
288 (assert (eql 0.0d0 (fp-struct-1-d fp)))))
290 (defun-with-dx test-fp-struct-1.2 (s d)
291 (let ((fp (make-fp-struct-1 :d d)))
292 (declare (dynamic-extent fp))
293 (assert (eql 0.0 (fp-struct-1-s fp)))
294 (assert (eql d (fp-struct-1-d fp)))))
296 (defun-with-dx test-fp-struct-1.3 (s d)
297 (let ((fp (make-fp-struct-1 :d d :s s)))
298 (declare (dynamic-extent fp))
299 (assert (eql s (fp-struct-1-s fp)))
300 (assert (eql d (fp-struct-1-d fp)))))
302 (defun-with-dx test-fp-struct-1.4 (s d)
303 (let ((fp (make-fp-struct-1 :s s :d d)))
304 (declare (dynamic-extent fp))
305 (assert (eql s (fp-struct-1-s fp)))
306 (assert (eql d (fp-struct-1-d fp)))))
308 (with-test (:name (:test-fp-struct-1.1))
309 (test-fp-struct-1.1 123.456 876.243d0))
310 (with-test (:name (:test-fp-struct-1.2))
311 (test-fp-struct-1.2 123.456 876.243d0))
312 (with-test (:name (:test-fp-struct-1.3))
313 (test-fp-struct-1.3 123.456 876.243d0))
314 (with-test (:name (:test-fp-struct-1.4))
315 (test-fp-struct-1.4 123.456 876.243d0))
317 (declaim (inline make-fp-struct-2))
318 (defstruct fp-struct-2
319 (d 0.0d0 :type double-float)
320 (s 0.0 :type single-float))
322 (defun-with-dx test-fp-struct-2.1 (s d)
323 (let ((fp (make-fp-struct-2 :s s)))
324 (declare (dynamic-extent fp))
325 (assert (eql s (fp-struct-2-s fp)))
326 (assert (eql 0.0d0 (fp-struct-2-d fp)))))
328 (defun-with-dx test-fp-struct-2.2 (s d)
329 (let ((fp (make-fp-struct-2 :d d)))
330 (declare (dynamic-extent fp))
331 (assert (eql 0.0 (fp-struct-2-s fp)))
332 (assert (eql d (fp-struct-2-d fp)))))
334 (defun-with-dx test-fp-struct-2.3 (s d)
335 (let ((fp (make-fp-struct-2 :d d :s s)))
336 (declare (dynamic-extent fp))
337 (assert (eql s (fp-struct-2-s fp)))
338 (assert (eql d (fp-struct-2-d fp)))))
340 (defun-with-dx test-fp-struct-2.4 (s d)
341 (let ((fp (make-fp-struct-2 :s s :d d)))
342 (declare (dynamic-extent fp))
343 (assert (eql s (fp-struct-2-s fp)))
344 (assert (eql d (fp-struct-2-d fp)))))
346 (with-test (:name (:test-fp-struct-2.1))
347 (test-fp-struct-2.1 123.456 876.243d0))
348 (with-test (:name (:test-fp-struct-2.2))
349 (test-fp-struct-2.2 123.456 876.243d0))
350 (with-test (:name (:test-fp-struct-2.3))
351 (test-fp-struct-2.3 123.456 876.243d0))
352 (with-test (:name (:test-fp-struct-2.4))
353 (test-fp-struct-2.4 123.456 876.243d0))
355 (declaim (inline make-cfp-struct-1))
356 (defstruct cfp-struct-1
357 (s (complex 0.0) :type (complex single-float))
358 (d (complex 0.0d0) :type (complex double-float)))
360 (defun-with-dx test-cfp-struct-1.1 (s d)
361 (let ((cfp (make-cfp-struct-1 :s s)))
362 (declare (dynamic-extent cfp))
363 (assert (eql s (cfp-struct-1-s cfp)))
364 (assert (eql (complex 0.0d0) (cfp-struct-1-d cfp)))))
366 (defun-with-dx test-cfp-struct-1.2 (s d)
367 (let ((cfp (make-cfp-struct-1 :d d)))
368 (declare (dynamic-extent cfp))
369 (assert (eql (complex 0.0) (cfp-struct-1-s cfp)))
370 (assert (eql d (cfp-struct-1-d cfp)))))
372 (defun-with-dx test-cfp-struct-1.3 (s d)
373 (let ((cfp (make-cfp-struct-1 :d d :s s)))
374 (declare (dynamic-extent cfp))
375 (assert (eql s (cfp-struct-1-s cfp)))
376 (assert (eql d (cfp-struct-1-d cfp)))))
378 (defun-with-dx test-cfp-struct-1.4 (s d)
379 (let ((cfp (make-cfp-struct-1 :s s :d d)))
380 (declare (dynamic-extent cfp))
381 (assert (eql s (cfp-struct-1-s cfp)))
382 (assert (eql d (cfp-struct-1-d cfp)))))
384 (with-test (:name (:test-cfp-struct-1.1))
385 (test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
386 (with-test (:name (:test-cfp-struct-1.2))
387 (test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
388 (with-test (:name (:test-cfp-struct-1.3))
389 (test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
390 (with-test (:name (:test-cfp-struct-1.4))
391 (test-cfp-struct-1.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
393 (declaim (inline make-cfp-struct-2))
394 (defstruct cfp-struct-2
395 (d (complex 0.0d0) :type (complex double-float))
396 (s (complex 0.0) :type (complex single-float)))
398 (defun-with-dx test-cfp-struct-2.1 (s d)
399 (let ((cfp (make-cfp-struct-2 :s s)))
400 (declare (dynamic-extent cfp))
401 (assert (eql s (cfp-struct-2-s cfp)))
402 (assert (eql (complex 0.0d0) (cfp-struct-2-d cfp)))))
404 (defun-with-dx test-cfp-struct-2.2 (s d)
405 (let ((cfp (make-cfp-struct-2 :d d)))
406 (declare (dynamic-extent cfp))
407 (assert (eql (complex 0.0) (cfp-struct-2-s cfp)))
408 (assert (eql d (cfp-struct-2-d cfp)))))
410 (defun-with-dx test-cfp-struct-2.3 (s d)
411 (let ((cfp (make-cfp-struct-2 :d d :s s)))
412 (declare (dynamic-extent cfp))
413 (assert (eql s (cfp-struct-2-s cfp)))
414 (assert (eql d (cfp-struct-2-d cfp)))))
416 (defun-with-dx test-cfp-struct-2.4 (s d)
417 (let ((cfp (make-cfp-struct-2 :s s :d d)))
418 (declare (dynamic-extent cfp))
419 (assert (eql s (cfp-struct-2-s cfp)))
420 (assert (eql d (cfp-struct-2-d cfp)))))
422 (with-test (:name (:test-cfp-struct-2.1))
423 (test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
424 (with-test (:name (:test-cfp-struct-2.2))
425 (test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
426 (with-test (:name (:test-cfp-struct-2.3))
427 (test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
428 (with-test (:name (:test-cfp-struct-2.4))
429 (test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
431 (declaim (inline make-foo1 make-foo2 make-foo3))
432 (defstruct foo1 x)
434 (defun-with-dx make-foo1-on-stack (x)
435 (let ((foo (make-foo1 :x x)))
436 (declare (dynamic-extent foo))
437 (assert (eql x (foo1-x foo)))))
439 (defstruct foo2
440 (x 0.0 :type single-float)
441 (y 0.0d0 :type double-float)
446 (defun-with-dx make-foo2-on-stack (x y)
447 (let ((foo (make-foo2 :y y :c 'c)))
448 (declare (dynamic-extent foo))
449 (assert (eql 0.0 (foo2-x foo)))
450 (assert (eql y (foo2-y foo)))
451 (assert (eql 'c (foo2-c foo)))
452 (assert (eql nil (foo2-b foo)))))
454 ;;; Check that constants work out as argument for all relevant
455 ;;; slot types.
456 (defstruct foo3
457 (a 0 :type t)
458 (b 1 :type fixnum)
459 (c 2 :type sb-vm:word)
460 (d 3.0 :type single-float)
461 (e 4.0d0 :type double-float))
463 (defun-with-dx make-foo3-on-stack ()
464 (let ((foo (make-foo3)))
465 (declare (dynamic-extent foo))
466 (assert (eql 0 (foo3-a foo)))
467 (assert (eql 1 (foo3-b foo)))
468 (assert (eql 2 (foo3-c foo)))
469 (assert (eql 3.0 (foo3-d foo)))
470 (assert (eql 4.0d0 (foo3-e foo)))))
472 ;;; Nested DX
474 (defun-with-dx nested-dx-lists ()
475 (let ((dx (list (list 1 2) (list 3 4))))
476 (declare (dynamic-extent dx))
477 (true dx)
478 nil))
480 (defun-with-dx nested-dx-conses ()
481 (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil))))))
482 (declare (dynamic-extent dx))
483 (true dx)
484 nil))
486 (defun-with-dx nested-dx-not-used (x)
487 (declare (list x))
488 (let ((l (setf (car x) (list x x x))))
489 (declare (dynamic-extent l))
490 (true l)
491 (true (length l))
492 nil))
494 (defun-with-dx nested-evil-dx-used (x)
495 (declare (list x))
496 (let ((l (list x x x)))
497 (declare (dynamic-extent l))
498 (unwind-protect
499 (progn
500 (setf (car x) l)
501 (true l))
502 (setf (car x) nil))
503 nil))
505 (defparameter *bar* nil)
506 (declaim (inline make-nested-bad make-nested-good))
507 (defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar))))
508 (:constructor make-nested-good (&key bar)))
509 bar)
511 (defun-with-dx nested-good (y)
512 (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y)))))))))
513 (declare (dynamic-extent x))
514 (true x)))
516 (defun-with-dx nested-bad (y)
517 (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y)))))))))
518 (declare (dynamic-extent x))
519 (unless (equalp (caar x) (make-nested-good :bar *bar*))
520 (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*)))
521 ;; the NESTED instance itself *should* be DX!
522 (copy-nested (caar x))))
524 (with-test (:name :conservative-nested-dx)
525 ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
526 (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
527 (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
529 ;;; multiple uses for dx lvar
531 (defun-with-dx multiple-dx-uses ()
532 (let ((dx (if (true t)
533 (list 1 2 3)
534 (list 2 3 4))))
535 (declare (dynamic-extent dx))
536 (true dx)
537 nil))
539 ;;; mapfoo should make the initial cons dx
541 (defun mapcar-negate (x) (mapcar #'- x))
542 (defun mapcan-reverse (x) (mapcan #'reverse x))
544 ;;; handler-case and handler-bind should use DX internally
546 (defun dx-handler-bind (x)
547 (handler-bind ((error
548 #'(lambda (c)
549 (break "OOPS: ~S caused ~S" x c)))
550 ((and serious-condition (not error))
551 #'(lambda (c)
552 (break "OOPS2: ~S did ~S" x c))))
553 (/ 2 x)))
555 (defun dx-handler-case (x)
556 (assert (zerop (handler-case (/ 2 x)
557 (error (c)
558 (break "OOPS: ~S caused ~S" x c)
560 (:no-error (res)
561 (1- res))))))
563 (defun list-delete-some-stuff ()
564 ;; opaque-identity hides the fact that we are calling a destructive function
565 ;; on a constant, which is technically illegal. But no deletion occurs,
566 ;; so it's innocuous.
567 (delete 'a (opaque-identity '(x y)))
568 (delete 'a (opaque-identity '(x y)) :from-end t)
569 (delete-duplicates (opaque-identity '(x y))))
571 (defvar *a-cons* (cons nil nil))
573 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
574 (assert-no-consing (dxclosure 42)))
576 (with-test (:name (:no-consing :dx-lists) :skipped-on '(not :stack-allocatable-lists))
577 (assert-no-consing (dxlength 1 2 3))
578 (assert-no-consing (dxlength t t t t t t))
579 (assert-no-consing (dxlength))
580 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
581 (assert-no-consing (test-nip-values))
582 (assert-no-consing (test-let-var-subst2 17))
583 (assert-no-consing (test-lvar-subst 11))
584 (assert-no-consing (nested-dx-lists))
585 (assert-consing (nested-dx-not-used *a-cons*))
586 (assert-no-consing (nested-evil-dx-used *a-cons*))
587 (assert-no-consing (mapcar-negate nil))
588 (assert-no-consing (mapcan-reverse nil))
589 (assert-no-consing (list-delete-some-stuff))
590 (assert-no-consing (multiple-dx-uses)))
592 (with-test (:name (:no-consing :dx-value-cell))
593 (assert-no-consing (dx-value-cell 13)))
595 (with-test (:name (:no-consing :dx-fixed-objects) :skipped-on '(not :stack-allocatable-fixed-objects))
596 (assert-no-consing (cons-on-stack 42))
597 (assert-no-consing (make-foo1-on-stack 123))
598 (assert-no-consing (nested-good 42))
599 (assert-no-consing (nested-dx-conses))
600 (assert-no-consing (dx-handler-bind 2))
601 (assert-no-consing (dx-handler-case 2)))
603 (with-test (:name (:no-consing :dx-vectors) :skipped-on '(not :stack-allocatable-vectors))
604 (assert-no-consing (force-make-array-on-stack 128))
605 (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
606 (assert-no-consing (make-array-on-stack-3 9 8 7))
607 (assert-no-consing (make-array-on-stack-4))
608 (assert-no-consing (make-array-on-stack-5))
609 (assert-no-consing (vector-on-stack :x :y)))
611 (with-test (:name (:no-consing :specialized-dx-vectors)
612 :fails-on :x86
613 :skipped-on `(not (and :stack-allocatable-vectors
614 :c-stack-is-control-stack)))
615 (assert-no-consing (make-array-on-stack-1))
616 (assert-no-consing (make-array-on-stack-6))
617 (assert-no-consing (make-array-on-stack-7))
618 (assert-no-consing (make-array-on-stack-8))
619 (assert-no-consing (make-array-on-stack-9))
620 (assert-no-consing (make-array-on-stack-10))
621 (assert-no-consing (make-array-on-stack-11)))
623 (with-test (:name (:no-consing :dx-raw-instances) :skipped-on '(or (not :raw-instance-init-vops)
624 (not (and :gencgc :c-stack-is-control-stack))))
625 (let (a b)
626 (setf a 1.24 b 1.23d0)
627 (assert-no-consing (make-foo2-on-stack a b)))
628 (assert-no-consing (make-foo3-on-stack)))
630 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
632 (defvar *table* (make-hash-table))
634 (defun test-hash-table ()
635 (setf (gethash 5 *table*) 13)
636 (gethash 5 *table*))
638 (with-test (:name (:no-consing :hash-tables))
639 (assert-no-consing (test-hash-table)))
641 ;;; Both with-pinned-objects and without-gcing should not cons
643 (defun call-without-gcing (fun)
644 (sb-sys:without-gcing (funcall fun)))
646 (defun call-with-pinned-object (fun obj)
647 (sb-sys:with-pinned-objects (obj)
648 (funcall fun obj)))
650 (with-test (:name (:no-consing :without-gcing))
651 (assert-no-consing (call-without-gcing (lambda ()))))
653 (with-test (:name (:no-consing :with-pinned-objects))
654 (assert-no-consing (call-with-pinned-object #'identity 42)))
656 ;;; with-mutex should use DX and not cons
658 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
660 (defun test-mutex ()
661 (sb-thread:with-mutex (*mutex*)
662 (true *mutex*)))
664 (with-test (:name (:no-consing :mutex) :skipped-on '(not :sb-thread))
665 (assert-no-consing (test-mutex)))
668 ;;; Bugs found by Paul F. Dietz
670 (with-test (:name (:dx-bug-misc :pfdietz))
671 (assert
673 (funcall
674 (compile
676 '(lambda (a b)
677 (declare (optimize (speed 2) (space 0) (safety 0)
678 (debug 1) (compilation-speed 3)))
679 (let* ((v5 (cons b b)))
680 (declare (dynamic-extent v5))
681 a)))
682 'x 'y)
683 'x)))
685 ;;; bug reported by Svein Ove Aas
686 (defun svein-2005-ii-07 (x y)
687 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
688 (let ((args (list* y 1 2 x)))
689 (declare (dynamic-extent args))
690 (apply #'aref args)))
692 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
693 (assert (eql
694 (svein-2005-ii-07
695 '(0)
696 #3A(((1 1 1) (1 1 1) (1 1 1))
697 ((1 1 1) (1 1 1) (4 1 1))
698 ((1 1 1) (1 1 1) (1 1 1))))
699 4)))
701 ;;; bug reported by Brian Downing: stack-allocated arrays were not
702 ;;; filled with zeroes.
703 (defun-with-dx bdowning-2005-iv-16 ()
704 (let ((a (make-array 11 :initial-element 0)))
705 (declare (dynamic-extent a))
706 (assert (every (lambda (x) (eql x 0)) a))))
708 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
709 #+(or hppa mips x86 x86-64)
710 (assert-no-consing (bdowning-2005-iv-16))
711 (bdowning-2005-iv-16))
713 (declaim (inline my-nconc))
714 (defun my-nconc (&rest lists)
715 (declare (dynamic-extent lists))
716 (apply #'nconc lists))
717 (defun-with-dx my-nconc-caller (a b c)
718 (let ((l1 (list a b c))
719 (l2 (list a b c)))
720 (my-nconc l1 l2)))
721 (with-test (:name :rest-stops-the-buck)
722 (let ((list1 (my-nconc-caller 1 2 3))
723 (list2 (my-nconc-caller 9 8 7)))
724 (assert (equal list1 '(1 2 3 1 2 3)))
725 (assert (equal list2 '(9 8 7 9 8 7)))))
727 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
728 (let* ((a (list x y z))
729 (b (list x y z))
730 (c (list a b)))
731 (declare (dynamic-extent c))
732 (values (first c) (second c))))
733 (with-test (:name :let-converted-vars-dx-allocated-bug)
734 (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
735 (assert (and (equal i j)
736 (equal i (list 1 2 3))))))
738 ;;; workaround for bug 419 -- real issue remains, but check that the
739 ;;; bandaid holds.
740 (defun-with-dx bug419 (x)
741 (multiple-value-call #'list
742 (eval '(values 1 2 3))
743 (let ((x x))
744 (declare (dynamic-extent x))
745 (flet ((mget (y)
746 (+ x y))
747 (mset (z)
748 (incf x z)))
749 (declare (dynamic-extent #'mget #'mset))
750 ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
752 (with-test (:name (:dx-bug-misc :bug419))
753 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
755 ;;; Multiple DX arguments in a local function call
756 (defun test-dx-flet-test (fun n f1 f2 f3)
757 (let ((res (with-output-to-string (s)
758 (assert (eql n (ignore-errors (funcall fun s)))))))
759 (multiple-value-bind (x pos) (read-from-string res nil)
760 (assert (equalp f1 x))
761 (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
762 (assert (equalp f2 y))
763 (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
764 #+(or hppa mips x86 x86-64)
765 (assert-no-consing (assert (eql n (funcall fun nil))))
766 (assert (eql n (funcall fun nil))))
768 (macrolet ((def (n f1 f2 f3)
769 (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
770 `(progn
771 (defun-with-dx ,name (s)
772 (flet ((f (x)
773 (declare (dynamic-extent x))
774 (when s
775 (print x s)
776 (finish-output s))
777 nil))
778 (f ,f1)
779 (f ,f2)
780 (f ,f3)
781 ,n))
782 (with-test (:name (:dx-flet-test ,n))
783 (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
784 (def 0 (list :one) (list :two) (list :three))
785 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
786 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
788 ;;; Test that unknown-values coming after a DX value won't mess up the
789 ;;; stack analysis
790 (defun test-update-uvl-live-sets (x y z)
791 (declare (optimize speed (safety 0)))
792 (flet ((bar (a b)
793 (declare (dynamic-extent a))
794 (eval `(list (length ',a) ',b))))
795 (list (bar x y)
796 (bar (list x y z) ; dx push
797 (list
798 (multiple-value-call 'list
799 (eval '(values 1 2 3)) ; uv push
800 (max y z)
801 ) ; uv pop
803 ))))
805 (with-test (:name (:update-uvl-live-sets))
806 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
807 (test-update-uvl-live-sets #() 4 5))))
809 (with-test (:name :regression-1.0.23.38)
810 (compile nil '(lambda ()
811 (flet ((make (x y)
812 (let ((res (cons x x)))
813 (setf (cdr res) y)
814 res)))
815 (declaim (inline make))
816 (let ((z (make 1 2)))
817 (declare (dynamic-extent z))
818 (print z)
819 t))))
820 (compile nil '(lambda ()
821 (flet ((make (x y)
822 (let ((res (cons x x)))
823 (setf (cdr res) y)
824 (if x res y))))
825 (declaim (inline make))
826 (let ((z (make 1 2)))
827 (declare (dynamic-extent z))
828 (print z)
829 t)))))
831 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
832 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
833 ;;; location, leading to all manner of badness. ...reproducing this
834 ;;; reliably is hard, but this it at least used to break on x86-64.
835 (defun length-and-words-packed-in-same-tn (m)
836 (declare (optimize speed (safety 0) (debug 0) (space 0)))
837 (let ((array (make-array (max 1 m) :element-type 'fixnum)))
838 (declare (dynamic-extent array))
839 (array-total-size array)))
840 (with-test (:name :length-and-words-packed-in-same-tn)
841 (assert (= 1 (length-and-words-packed-in-same-tn -3))))
843 (with-test (:name :handler-case-bogus-compiler-note)
844 (handler-bind
845 ((compiler-note (lambda (note)
846 (error "compiler issued note ~S during test" note))))
847 ;; Taken from SWANK, used to signal a bogus stack allocation
848 ;; failure note.
849 (compile nil
850 `(lambda (files fasl-dir load)
851 (let ((needs-recompile nil))
852 (dolist (src files)
853 (let ((dest (binary-pathname src fasl-dir)))
854 (handler-case
855 (progn
856 (when (or needs-recompile
857 (not (probe-file dest))
858 (file-newer-p src dest))
859 (setq needs-recompile t)
860 (ensure-directories-exist dest)
861 (compile-file src :output-file dest :print nil :verbose t))
862 (when load
863 (load dest :verbose t)))
864 (serious-condition (c)
865 (handle-loadtime-error c dest))))))))))
867 (declaim (inline foovector barvector))
868 (defun foovector (x y z)
869 (let ((v (make-array 3)))
870 (setf (aref v 0) x
871 (aref v 1) y
872 (aref v 2) z)
874 (defun barvector (x y z)
875 (make-array 3 :initial-contents (list x y z)))
876 (with-test (:name :dx-compiler-notes)
877 (flet ((assert-notes (j lambda)
878 (let ((n 0))
879 (handler-bind ((compiler-note (lambda (c)
880 (declare (ignore c))
881 (incf n))))
882 (compile nil lambda)
883 (unless (= j n)
884 (error "Wanted ~S notes, got ~S for~% ~S"
885 j n lambda))))))
886 ;; These ones should complain.
887 (assert-notes 1 `(lambda (x)
888 (let ((v (make-array x)))
889 (declare (dynamic-extent v))
890 (length v))))
891 (assert-notes 2 `(lambda (x)
892 (let ((y (if (plusp x)
893 (true x)
894 (true (- x)))))
895 (declare (dynamic-extent y))
896 (print y)
897 nil)))
898 (assert-notes 1 `(lambda (x)
899 (let ((y (foovector x x x)))
900 (declare (sb-int:truly-dynamic-extent y))
901 (print y)
902 nil)))
903 ;; These ones should not complain.
904 (assert-notes 0 `(lambda (name)
905 (with-alien
906 ((posix-getenv (function c-string c-string)
907 :EXTERN "getenv"))
908 (values
909 (alien-funcall posix-getenv name)))))
910 (assert-notes 0 `(lambda (x)
911 (let ((y (barvector x x x)))
912 (declare (dynamic-extent y))
913 (print y)
914 nil)))
915 (assert-notes 0 `(lambda (list)
916 (declare (optimize (space 0)))
917 (sort list (lambda (x y) ; shut unrelated notes up
918 (< (truly-the fixnum x)
919 (truly-the fixnum y))))))
920 (assert-notes 0 `(lambda (other)
921 #'(lambda (s c n)
922 (ignore-errors (funcall other s c n)))))))
924 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
925 ;;; in an unfortunate loop.
926 (defun handler-case-eating-stack ()
927 (let ((sp nil))
928 (do ((n 0 (logand most-positive-fixnum (1+ n))))
929 ((>= n 1024))
930 (multiple-value-bind (value error) (ignore-errors)
931 (when (and value error) nil))
932 (if sp
933 (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
934 (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
935 (with-test (:name :handler-case-eating-stack)
936 (assert-no-consing (handler-case-eating-stack)))
938 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
939 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
940 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
941 (deftype vec ()
942 `(simple-array t (3)))
943 (declaim (ftype (function (t t t) vec) vec))
944 (declaim (inline vec))
945 (defun vec (a b c)
946 (make-array 3 :initial-contents (list a b c)))
947 (defun bad-boy (vec)
948 (declare (type vec vec))
949 (lambda (fun)
950 (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
951 (declare (dynamic-extent vec))
952 (funcall fun vec))))
953 (with-test (:name :recheck-nested-dx-bug)
954 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
955 (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
956 (flet ((foo (x) (declare (ignore x))))
957 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
958 (assert-no-consing (funcall bad-boy #'foo)))))
960 (with-test (:name :bug-497321)
961 (flet ((test (lambda type)
962 (let ((n 0))
963 (handler-bind ((condition (lambda (c)
964 (incf n)
965 (unless (typep c type)
966 (error "wanted ~S for~% ~S~%got ~S"
967 type lambda (type-of c))))))
968 (compile nil lambda))
969 (assert (= n 1)))))
970 (test `(lambda () (declare (dynamic-extent #'bar)))
971 'style-warning)
972 (test `(lambda () (declare (dynamic-extent bar)))
973 'style-warning)
974 (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
975 'sb-ext:compiler-note)
976 (test `(lambda ()
977 (flet ((bar () t))
978 (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
979 'sb-ext:compiler-note)))
981 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
982 :stack-allocatable-lists)))
983 (flet ((test (x)
984 (let ((vec1 (make-array 1 :initial-contents (list (list x))))
985 (vec2 (make-array 1 :initial-contents `((,x))))
986 (vec3 (make-array 1 :initial-contents `#((,x))))
987 (vec4 (make-array 1 :initial-contents `(#(,x)))))
988 (declare (dynamic-extent vec1 vec2 vec3 vec4))
989 (assert (eql x (car (aref vec1 0))))
990 (assert (eql x (car (aref vec2 0))))
991 (assert (eql x (car (aref vec3 0))))
992 (assert (eql x (elt (aref vec4 0) 0))))))
993 (assert-no-consing (test 42))))
995 (defun bug-681092 ()
996 (declare (optimize speed))
997 (let ((c 0))
998 (flet ((bar () c))
999 (declare (dynamic-extent #'bar))
1000 (do () ((list) (bar))
1001 (setf c 10)
1002 (return (bar))))))
1003 (with-test (:name :bug-681092)
1004 (assert (= 10 (bug-681092))))
1006 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
1007 ;;;; but required by sanity.
1009 (declaim (inline rest-stops-dx))
1010 (defun-with-dx rest-stops-dx (&rest args)
1011 (declare (dynamic-extent args))
1012 (apply #'opaque-identity args))
1014 (defun-with-dx rest-stops-dx-ok ()
1015 (equal '(:foo) (rest-stops-dx (list :foo))))
1017 (with-test (:name :rest-stops-dynamic-extent)
1018 (assert (rest-stops-dx-ok)))
1020 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
1021 ;;;; conversion.
1022 (with-test (:name :rest-to-more-conversion)
1023 (let ((f1 (compile nil `(lambda (f &rest args)
1024 (apply f args)))))
1025 (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
1026 (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
1027 (values (apply f1 args) (apply f2 args))))))
1028 (assert-no-consing (multiple-value-bind (a b)
1029 (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
1030 1 2 3)
1031 (assert (and (eql 6 a) (eql -4 b))))))
1032 (let ((f3 (compile nil `(lambda (f &optional x &rest args)
1033 (when x
1034 (apply f x args))))))
1035 (assert-no-consing (assert (eql 42 (funcall f3
1036 (lambda (a b c) (+ a b c))
1039 21)))))
1040 (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
1041 (apply f y x args)))))
1042 (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
1043 (assert (eq y 'y))
1044 (assert (= x 2))
1045 (assert (eq :y yk))
1046 (assert (eq y2 'y))
1047 (assert (eq b 'b))
1048 (assert (eq c 'c)))
1049 2 :y 'y 'b 'c)))
1050 (let ((f5 (compile nil `(lambda (a b c &rest args)
1051 (apply #'list* a b c args)))))
1052 (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
1053 (let ((f6 (compile nil `(lambda (x y)
1054 (declare (optimize speed))
1055 (concatenate 'string x y)))))
1056 (assert (equal "foobar" (funcall f6 "foo" "bar"))))
1057 (let ((f7 (compile nil `(lambda (&rest args)
1058 (lambda (f)
1059 (apply f args))))))
1060 (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
1061 (let ((f8 (compile nil `(lambda (&rest args)
1062 (flet ((foo (f)
1063 (apply f args)))
1064 #'foo)))))
1065 (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
1066 (let ((f9 (compile nil `(lambda (f &rest args)
1067 (flet ((foo (g)
1068 (apply g args)))
1069 (declare (dynamic-extent #'foo))
1070 (funcall f #'foo))))))
1071 (assert (equal '(a b c d e f)
1072 (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1073 (let ((f10 (compile nil `(lambda (f &rest args)
1074 (flet ((foo (g)
1075 (apply g args)))
1076 (funcall f #'foo))))))
1077 (assert (equal '(a b c d e f)
1078 (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1079 (let ((f11 (compile nil `(lambda (x y z)
1080 (block out
1081 (labels ((foo (x &rest rest)
1082 (apply (lambda (&rest rest2)
1083 (return-from out (values-list rest2)))
1084 x rest)))
1085 (if x
1086 (foo x y z)
1087 (foo y z x))))))))
1088 (multiple-value-bind (a b c) (funcall f11 1 2 3)
1089 (assert (eql a 1))
1090 (assert (eql b 2))
1091 (assert (eql c 3)))))
1093 (defun opaque-funcall (function &rest arguments)
1094 (apply function arguments))
1096 (with-test (:name :implicit-value-cells)
1097 (flet ((test-it (type input output)
1098 (let ((f (compile nil `(lambda (x)
1099 (declare (type ,type x))
1100 (flet ((inc ()
1101 (incf x)))
1102 (declare (dynamic-extent #'inc))
1103 (list (opaque-funcall #'inc) x))))))
1104 (assert (equal (funcall f input)
1105 (list output output))))))
1106 (let ((width sb-vm:n-word-bits))
1107 (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1108 (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1109 (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1110 (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1111 (test-it 'single-float 3f0 4f0)
1112 (test-it 'double-float 3d0 4d0)
1113 (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1114 (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1116 (with-test (:name :sap-implicit-value-cells)
1117 (let ((f (compile nil `(lambda (x)
1118 (declare (type system-area-pointer x))
1119 (flet ((inc ()
1120 (setf x (sb-sys:sap+ x 16))))
1121 (declare (dynamic-extent #'inc))
1122 (list (opaque-funcall #'inc) x)))))
1123 (width sb-vm:n-machine-word-bits))
1124 (assert (every (lambda (x)
1125 (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1126 (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))
1128 (with-test (:name :&more-bounds)
1129 ;; lp#1154946
1130 (assert (not (funcall (compile nil '(lambda (&rest args) (car args))))))
1131 (assert (not (funcall (compile nil '(lambda (&rest args) (nth 6 args))))))
1132 (assert (not (funcall (compile nil '(lambda (&rest args) (elt args 10))))))
1133 (assert (not (funcall (compile nil '(lambda (&rest args) (cadr args))))))
1134 (assert (not (funcall (compile nil '(lambda (&rest args) (third args)))))))