Accessing &MORE arguments checks bounds.
[sbcl.git] / tests / dynamic-extent.impure.lisp
1 ;;;; tests that dynamic-extent functionality works.
2
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.
13
14 (when (eq sb-ext:*evaluator-mode* :interpret)
15   (sb-ext:exit :code 104))
16
17 (load "compiler-test-util.lisp")
18 (use-package :ctu)
19
20 (setq sb-c::*check-consistency* t
21       sb-ext:*stack-allocate-dynamic-extent* t)
22
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)))))
35
36 (declaim (notinline opaque-identity))
37 (defun opaque-identity (x)
38   x)
39
40 ;;; &REST lists
41
42 (defun-with-dx dxlength (&rest rest)
43   (declare (dynamic-extent rest))
44   (length rest))
45
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)))
50
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))))
54
55 (defun-with-dx dxcaller (&rest rest)
56   (declare (dynamic-extent rest))
57   (callee rest))
58
59 (with-test (:name (:dx-&rest :pass-down-to-callee :tail-call))
60   (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
61
62 (defun-with-dx dxcaller-align-1 (x &rest rest)
63   (declare (dynamic-extent rest))
64   (+ x (callee rest)))
65
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)))
69
70 ;;; %NIP-VALUES
71
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))))
81
82 (with-test (:name (:nip-values))
83   (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a))))
84
85 ;;; LET-variable substitution
86
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))))
93
94 (with-test (:name (:let-variable-substitution))
95   (assert (eql (test-let-var-subst1 17) 2)))
96
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))))
103
104 (with-test (:name (:let-variable-substitution-2))
105   (assert (eql (test-let-var-subst2 17) 2)))
106
107
108 ;;; DX propagation through LET-return.
109
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))))
116
117 (with-test (:name (:dx-propagation-through-let-return))
118   (assert (eql (test-lvar-subst 11) 10)))
119
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)))
128 \f
129 ;;; alignment
130
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)))))
137
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))))))
144
145 ;;; closure
146
147 (declaim (notinline true))
148 (defun true (x)
149   (declare (ignore x))
150   t)
151
152 (defun-with-dx dxclosure (x)
153   (flet ((f (y)
154            (+ y x)))
155     (declare (dynamic-extent #'f))
156     (true #'f)))
157
158 (with-test (:name (:dx-closure))
159   (assert (eq t (dxclosure 13))))
160
161 ;;; value-cells
162
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))))
172
173 ;;; CONS
174
175 (defun-with-dx cons-on-stack (x)
176   (let ((cons (cons x x)))
177     (declare (dynamic-extent cons))
178     (true cons)
179     nil))
180
181 ;;; MAKE-ARRAY
182
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))
190
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))
197
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))
205
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))
214
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))
221
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))
228
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))
235
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))
242
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))
249
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))
256
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))
263
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))
270
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))
276
277 ;;; MAKE-STRUCTURE
278
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))
283
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)))))
289
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)))))
295
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)))))
301
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)))))
307
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))
316
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))
321
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)))))
327
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)))))
333
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)))))
339
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)))))
345
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))
354
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)))
359
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)))))
365
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)))))
371
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)))))
377
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)))))
383
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)))
392
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)))
397
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)))))
403
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)))))
409
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)))))
415
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)))))
421
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)))
430
431 (declaim (inline make-foo1 make-foo2 make-foo3))
432 (defstruct foo1 x)
433
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)))))
438
439 (defstruct foo2
440   (x 0.0 :type single-float)
441   (y 0.0d0 :type double-float)
442   a
443   b
444   c)
445
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)))))
453
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))
462
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)))))
471
472 ;;; Nested DX
473
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))
479
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))
485
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))
493
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))
504
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)
510
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)))
515
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     (caar x)))
522
523 (with-test (:name :conservative-nested-dx)
524   ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
525   (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
526   (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
527
528 ;;; multiple uses for dx lvar
529
530 (defun-with-dx multiple-dx-uses ()
531   (let ((dx (if (true t)
532                 (list 1 2 3)
533                 (list 2 3 4))))
534     (declare (dynamic-extent dx))
535     (true dx)
536     nil))
537
538 ;;; handler-case and handler-bind should use DX internally
539
540 (defun dx-handler-bind (x)
541   (handler-bind ((error
542                   #'(lambda (c)
543                       (break "OOPS: ~S caused ~S" x c)))
544                  ((and serious-condition (not error))
545                   #'(lambda (c)
546                       (break "OOPS2: ~S did ~S" x c))))
547     (/ 2 x)))
548
549 (defun dx-handler-case (x)
550   (assert (zerop (handler-case (/ 2 x)
551                    (error (c)
552                      (break "OOPS: ~S caused ~S" x c)
553                      -1)
554                    (:no-error (res)
555                      (1- res))))))
556
557 (defvar *a-cons* (cons nil nil))
558
559 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
560   (assert-no-consing (dxclosure 42)))
561
562 (with-test (:name (:no-consing :dx-lists) :skipped-on '(not :stack-allocatable-lists))
563   (assert-no-consing (dxlength 1 2 3))
564   (assert-no-consing (dxlength t t t t t t))
565   (assert-no-consing (dxlength))
566   (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
567   (assert-no-consing (test-nip-values))
568   (assert-no-consing (test-let-var-subst2 17))
569   (assert-no-consing (test-lvar-subst 11))
570   (assert-no-consing (nested-dx-lists))
571   (assert-consing (nested-dx-not-used *a-cons*))
572   (assert-no-consing (nested-evil-dx-used *a-cons*))
573   (assert-no-consing (multiple-dx-uses)))
574
575 (with-test (:name (:no-consing :dx-value-cell))
576   (assert-no-consing (dx-value-cell 13)))
577
578 (with-test (:name (:no-consing :dx-fixed-objects) :skipped-on '(not :stack-allocatable-fixed-objects))
579   (assert-no-consing (cons-on-stack 42))
580   (assert-no-consing (make-foo1-on-stack 123))
581   (assert-no-consing (nested-good 42))
582   (assert-no-consing (nested-dx-conses))
583   (assert-no-consing (dx-handler-bind 2))
584   (assert-no-consing (dx-handler-case 2)))
585
586 (with-test (:name (:no-consing :dx-vectors) :skipped-on '(not :stack-allocatable-vectors))
587   (assert-no-consing (force-make-array-on-stack 128))
588   (assert-no-consing (make-array-on-stack-1))
589   (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
590   (assert-no-consing (make-array-on-stack-3 9 8 7))
591   (assert-no-consing (make-array-on-stack-4))
592   (assert-no-consing (make-array-on-stack-5))
593   (assert-no-consing (vector-on-stack :x :y)))
594
595 (with-test (:name (:no-consing :specialized-dx-vectors)
596             :fails-on :x86
597             :skipped-on `(not (and :stack-allocatable-vectors
598                                    :c-stack-is-control-stack)))
599   (assert-no-consing (make-array-on-stack-6))
600   (assert-no-consing (make-array-on-stack-7))
601   (assert-no-consing (make-array-on-stack-8))
602   (assert-no-consing (make-array-on-stack-9))
603   (assert-no-consing (make-array-on-stack-10))
604   (assert-no-consing (make-array-on-stack-11)))
605
606 (with-test (:name (:no-consing :dx-raw-instances) :skipped-on '(or (not :raw-instance-init-vops)
607                                                                    (not (and :gencgc :c-stack-is-control-stack))))
608   (let (a b)
609     (setf a 1.24 b 1.23d0)
610     (assert-no-consing (make-foo2-on-stack a b)))
611     (assert-no-consing (make-foo3-on-stack)))
612
613 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
614
615 (defvar *table* (make-hash-table))
616
617 (defun test-hash-table ()
618   (setf (gethash 5 *table*) 13)
619   (gethash 5 *table*))
620
621 ;; This fails on threaded PPC because the hash-table implementation
622 ;; uses recursive system locks, which cons (see below for test
623 ;; (:no-consing :lock), which also fails on threaded PPC).
624 ;;
625 ;; -- That may have been the situation in 2010 when the above comment
626 ;; was written, but AFAICT now, hash tables use WITH-PINNED-OBJECTS,
627 ;; which conses on PPC and SPARC when GENCGC is enabled.  So neither is
628 ;; this actually about threading, nor about PPC.  Yet since we are
629 ;; failing most of this file on SPARC anyway (for some tests even on
630 ;; cheneygc), I won't bother to mark this particular test as failing.
631 ;; It would be nice if someone could go through this file and figure it
632 ;; all out... --DFL
633 (with-test (:name (:no-consing :hash-tables) :fails-on '(and :ppc :sb-thread))
634   (assert-no-consing (test-hash-table)))
635
636 ;;; with-mutex should use DX and not cons
637
638 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
639
640 (defun test-mutex ()
641   (sb-thread:with-mutex (*mutex*)
642     (true *mutex*)))
643
644 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
645   (assert-no-consing (test-mutex)))
646 \f
647
648 ;;; Bugs found by Paul F. Dietz
649
650 (with-test (:name (:dx-bug-misc :pfdietz))
651   (assert
652    (eq
653     (funcall
654      (compile
655       nil
656       '(lambda (a b)
657         (declare (optimize (speed 2) (space 0) (safety 0)
658                   (debug 1) (compilation-speed 3)))
659         (let* ((v5 (cons b b)))
660           (declare (dynamic-extent v5))
661           a)))
662      'x 'y)
663     'x)))
664
665 ;;; bug reported by Svein Ove Aas
666 (defun svein-2005-ii-07 (x y)
667   (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
668   (let ((args (list* y 1 2 x)))
669     (declare (dynamic-extent args))
670     (apply #'aref args)))
671
672 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
673   (assert (eql
674            (svein-2005-ii-07
675             '(0)
676             #3A(((1 1 1) (1 1 1) (1 1 1))
677                 ((1 1 1) (1 1 1) (4 1 1))
678                 ((1 1 1) (1 1 1) (1 1 1))))
679            4)))
680
681 ;;; bug reported by Brian Downing: stack-allocated arrays were not
682 ;;; filled with zeroes.
683 (defun-with-dx bdowning-2005-iv-16 ()
684   (let ((a (make-array 11 :initial-element 0)))
685     (declare (dynamic-extent a))
686     (assert (every (lambda (x) (eql x 0)) a))))
687
688 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
689   #+(or hppa mips x86 x86-64)
690   (assert-no-consing (bdowning-2005-iv-16))
691   (bdowning-2005-iv-16))
692
693 (declaim (inline my-nconc))
694 (defun my-nconc (&rest lists)
695   (declare (dynamic-extent lists))
696   (apply #'nconc lists))
697 (defun-with-dx my-nconc-caller (a b c)
698   (let ((l1 (list a b c))
699         (l2 (list a b c)))
700     (my-nconc l1 l2)))
701 (with-test (:name :rest-stops-the-buck)
702   (let ((list1 (my-nconc-caller 1 2 3))
703         (list2 (my-nconc-caller 9 8 7)))
704     (assert (equal list1 '(1 2 3 1 2 3)))
705     (assert (equal list2 '(9 8 7 9 8 7)))))
706
707 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
708   (let* ((a (list x y z))
709          (b (list x y z))
710          (c (list a b)))
711     (declare (dynamic-extent c))
712     (values (first c) (second c))))
713 (with-test (:name :let-converted-vars-dx-allocated-bug)
714   (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
715     (assert (and (equal i j)
716                  (equal i (list 1 2 3))))))
717
718 ;;; workaround for bug 419 -- real issue remains, but check that the
719 ;;; bandaid holds.
720 (defun-with-dx bug419 (x)
721   (multiple-value-call #'list
722     (eval '(values 1 2 3))
723     (let ((x x))
724       (declare (dynamic-extent x))
725       (flet ((mget (y)
726                (+ x y))
727              (mset (z)
728                (incf x z)))
729         (declare (dynamic-extent #'mget #'mset))
730         ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
731
732 (with-test (:name (:dx-bug-misc :bug419))
733   (assert (equal (bug419 42) '(1 2 3 4 5 6))))
734
735 ;;; Multiple DX arguments in a local function call
736 (defun test-dx-flet-test (fun n f1 f2 f3)
737   (let ((res (with-output-to-string (s)
738                (assert (eql n (ignore-errors (funcall fun s)))))))
739     (multiple-value-bind (x pos) (read-from-string res nil)
740       (assert (equalp f1 x))
741       (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
742         (assert (equalp f2 y))
743         (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
744   #+(or hppa mips x86 x86-64)
745   (assert-no-consing (assert (eql n (funcall fun nil))))
746   (assert (eql n (funcall fun nil))))
747
748 (macrolet ((def (n f1 f2 f3)
749              (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
750                `(progn
751                   (defun-with-dx ,name (s)
752                     (flet ((f (x)
753                              (declare (dynamic-extent x))
754                              (when s
755                                (print x s)
756                                (finish-output s))
757                              nil))
758                       (f ,f1)
759                       (f ,f2)
760                       (f ,f3)
761                       ,n))
762                   (with-test (:name (:dx-flet-test ,n))
763                     (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
764   (def 0 (list :one) (list :two) (list :three))
765   (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
766   (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
767
768 ;;; Test that unknown-values coming after a DX value won't mess up the
769 ;;; stack analysis
770 (defun test-update-uvl-live-sets (x y z)
771  (declare (optimize speed (safety 0)))
772  (flet ((bar (a b)
773           (declare (dynamic-extent a))
774           (eval `(list (length ',a) ',b))))
775    (list (bar x y)
776          (bar (list x y z)                  ; dx push
777               (list
778                (multiple-value-call 'list
779                  (eval '(values 1 2 3))     ; uv push
780                  (max y z)
781                )                            ; uv pop
782                14)
783          ))))
784
785 (with-test (:name (:update-uvl-live-sets))
786   (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
787                  (test-update-uvl-live-sets #() 4 5))))
788
789 (with-test (:name :regression-1.0.23.38)
790   (compile nil '(lambda ()
791                  (flet ((make (x y)
792                           (let ((res (cons x x)))
793                             (setf (cdr res) y)
794                             res)))
795                    (declaim (inline make))
796                    (let ((z (make 1 2)))
797                      (declare (dynamic-extent z))
798                      (print z)
799                      t))))
800   (compile nil '(lambda ()
801                  (flet ((make (x y)
802                           (let ((res (cons x x)))
803                             (setf (cdr res) y)
804                             (if x res y))))
805                    (declaim (inline make))
806                    (let ((z (make 1 2)))
807                      (declare (dynamic-extent z))
808                      (print z)
809                      t)))))
810
811 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
812 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
813 ;;; location, leading to all manner of badness. ...reproducing this
814 ;;; reliably is hard, but this it at least used to break on x86-64.
815 (defun length-and-words-packed-in-same-tn (m)
816   (declare (optimize speed (safety 0) (debug 0) (space 0)))
817   (let ((array (make-array (max 1 m) :element-type 'fixnum)))
818     (declare (dynamic-extent array))
819     (array-total-size array)))
820 (with-test (:name :length-and-words-packed-in-same-tn)
821   (assert (= 1 (length-and-words-packed-in-same-tn -3))))
822
823 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
824   (handler-bind
825       ((compiler-note (lambda (note)
826                         (error "compiler issued note ~S during test" note))))
827     ;; Taken from SWANK, used to signal a bogus stack allocation
828     ;; failure note.
829     (compile nil
830              `(lambda (files fasl-dir load)
831                 (let ((needs-recompile nil))
832                   (dolist (src files)
833                     (let ((dest (binary-pathname src fasl-dir)))
834                       (handler-case
835                           (progn
836                             (when (or needs-recompile
837                                       (not (probe-file dest))
838                                       (file-newer-p src dest))
839                               (setq needs-recompile t)
840                               (ensure-directories-exist dest)
841                               (compile-file src :output-file dest :print nil :verbose t))
842                             (when load
843                               (load dest :verbose t)))
844                         (serious-condition (c)
845                           (handle-loadtime-error c dest))))))))))
846
847 (declaim (inline foovector barvector))
848 (defun foovector (x y z)
849   (let ((v (make-array 3)))
850     (setf (aref v 0) x
851           (aref v 1) y
852           (aref v 2) z)
853     v))
854 (defun barvector (x y z)
855   (make-array 3 :initial-contents (list x y z)))
856 (with-test (:name :dx-compiler-notes :fails-on :ppc)
857   (flet ((assert-notes (j lambda)
858            (let ((n 0))
859              (handler-bind ((compiler-note (lambda (c)
860                                              (declare (ignore c))
861                                              (incf n))))
862                (compile nil lambda)
863                (unless (= j n)
864                  (error "Wanted ~S notes, got ~S for~%   ~S"
865                         j n lambda))))))
866     ;; These ones should complain.
867     (assert-notes 1 `(lambda (x)
868                        (let ((v (make-array x)))
869                          (declare (dynamic-extent v))
870                          (length v))))
871     (assert-notes 2 `(lambda (x)
872                        (let ((y (if (plusp x)
873                                     (true x)
874                                     (true (- x)))))
875                          (declare (dynamic-extent y))
876                          (print y)
877                          nil)))
878     (assert-notes 1 `(lambda (x)
879                        (let ((y (foovector x x x)))
880                          (declare (sb-int:truly-dynamic-extent y))
881                          (print y)
882                          nil)))
883     ;; These ones should not complain.
884     (assert-notes 0 `(lambda (name)
885                        (with-alien
886                            ((posix-getenv (function c-string c-string)
887                                           :EXTERN "getenv"))
888                          (values
889                           (alien-funcall posix-getenv name)))))
890     (assert-notes 0 `(lambda (x)
891                        (let ((y (barvector x x x)))
892                          (declare (dynamic-extent y))
893                          (print y)
894                          nil)))
895     (assert-notes 0 `(lambda (list)
896                        (declare (optimize (space 0)))
897                        (sort list (lambda (x y) ; shut unrelated notes up
898                                     (< (truly-the fixnum x)
899                                        (truly-the fixnum y))))))
900     (assert-notes 0 `(lambda (other)
901                        #'(lambda (s c n)
902                            (ignore-errors (funcall other s c n)))))))
903
904 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
905 ;;; in an unfortunate loop.
906 (defun handler-case-eating-stack ()
907   (let ((sp nil))
908     (do ((n 0 (logand most-positive-fixnum (1+ n))))
909         ((>= n 1024))
910      (multiple-value-bind (value error) (ignore-errors)
911        (when (and value error) nil))
912       (if sp
913           (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
914           (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
915 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
916   (assert-no-consing (handler-case-eating-stack)))
917
918 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
919 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
920 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
921 (deftype vec ()
922   `(simple-array single-float (3)))
923 (declaim (ftype (function (t t t) vec) vec))
924 (declaim (inline vec))
925 (defun vec (a b c)
926   (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
927 (defun bad-boy (vec)
928   (declare (type vec vec))
929   (lambda (fun)
930     (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
931       (declare (dynamic-extent vec))
932       (funcall fun vec))))
933 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
934   (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
935                    (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
936   (flet ((foo (x) (declare (ignore x))))
937     (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
938       (assert-no-consing (funcall bad-boy #'foo)))))
939
940 (with-test (:name :bug-497321)
941   (flet ((test (lambda type)
942            (let ((n 0))
943              (handler-bind ((condition (lambda (c)
944                                          (incf n)
945                                          (unless (typep c type)
946                                            (error "wanted ~S for~%  ~S~%got ~S"
947                                                   type lambda (type-of c))))))
948                (compile nil lambda))
949              (assert (= n 1)))))
950     (test `(lambda () (declare (dynamic-extent #'bar)))
951           'style-warning)
952     (test `(lambda () (declare (dynamic-extent bar)))
953           'style-warning)
954     (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
955           'sb-ext:compiler-note)
956     (test `(lambda ()
957              (flet ((bar () t))
958                (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
959           'sb-ext:compiler-note)))
960
961 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
962                                                    :stack-allocatable-lists)))
963   (flet ((test (x)
964            (let ((vec (make-array 1 :initial-contents (list (list x)))))
965              (declare (dynamic-extent vec))
966              (assert (eql x (car (aref vec 0)))))))
967     (assert-no-consing (test 42))))
968 \f
969 (defun bug-681092 ()
970   (declare (optimize speed))
971   (let ((c 0))
972     (flet ((bar () c))
973       (declare (dynamic-extent #'bar))
974       (do () ((list) (bar))
975         (setf c 10)
976         (return (bar))))))
977 (with-test (:name :bug-681092)
978   (assert (= 10 (bug-681092))))
979
980 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
981 ;;;; but required by sanity.
982
983 (declaim (inline rest-stops-dx))
984 (defun-with-dx rest-stops-dx (&rest args)
985   (declare (dynamic-extent args))
986   (apply #'opaque-identity args))
987
988 (defun-with-dx rest-stops-dx-ok ()
989   (equal '(:foo) (rest-stops-dx (list :foo))))
990
991 (with-test (:name :rest-stops-dynamic-extent)
992   (assert (rest-stops-dx-ok)))
993
994 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
995 ;;;; conversion.
996 (with-test (:name :rest-to-more-conversion)
997   (let ((f1 (compile nil `(lambda (f &rest args)
998                             (apply f args)))))
999     (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
1000   (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
1001                             (values (apply f1 args) (apply f2 args))))))
1002     (assert-no-consing (multiple-value-bind (a b)
1003                            (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
1004                                     1 2 3)
1005                          (assert (and (eql 6 a) (eql -4 b))))))
1006   (let ((f3 (compile nil `(lambda (f &optional x &rest args)
1007                             (when x
1008                               (apply f x args))))))
1009     (assert-no-consing (assert (eql 42 (funcall f3
1010                                                 (lambda (a b c) (+ a b c))
1011                                                 11
1012                                                 10
1013                                                 21)))))
1014   (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
1015                             (apply f y x args)))))
1016     (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
1017                                      (assert (eq y 'y))
1018                                      (assert (= x 2))
1019                                      (assert (eq :y yk))
1020                                      (assert (eq y2 'y))
1021                                      (assert (eq b 'b))
1022                                      (assert (eq c 'c)))
1023                                 2 :y 'y 'b 'c)))
1024   (let ((f5 (compile nil `(lambda (a b c &rest args)
1025                             (apply #'list* a b c args)))))
1026     (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
1027   (let ((f6 (compile nil `(lambda (x y)
1028                             (declare (optimize speed))
1029                             (concatenate 'string x y)))))
1030     (assert (equal "foobar" (funcall f6 "foo" "bar"))))
1031   (let ((f7 (compile nil `(lambda (&rest args)
1032                             (lambda (f)
1033                               (apply f args))))))
1034     (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
1035   (let ((f8 (compile nil `(lambda (&rest args)
1036                             (flet ((foo (f)
1037                                      (apply f args)))
1038                               #'foo)))))
1039     (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
1040   (let ((f9 (compile nil `(lambda (f &rest args)
1041                             (flet ((foo (g)
1042                                      (apply g args)))
1043                               (declare (dynamic-extent #'foo))
1044                               (funcall f #'foo))))))
1045     (assert (equal '(a b c d e f)
1046                    (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1047   (let ((f10 (compile nil `(lambda (f &rest args)
1048                             (flet ((foo (g)
1049                                      (apply g args)))
1050                               (funcall f #'foo))))))
1051     (assert (equal '(a b c d e f)
1052                    (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1053   (let ((f11 (compile nil `(lambda (x y z)
1054                              (block out
1055                                (labels ((foo (x &rest rest)
1056                                           (apply (lambda (&rest rest2)
1057                                                    (return-from out (values-list rest2)))
1058                                                  x rest)))
1059                                 (if x
1060                                     (foo x y z)
1061                                     (foo y z x))))))))
1062     (multiple-value-bind (a b c) (funcall f11 1 2 3)
1063       (assert (eql a 1))
1064       (assert (eql b 2))
1065       (assert (eql c 3)))))
1066
1067 (defun opaque-funcall (function &rest arguments)
1068   (apply function arguments))
1069
1070 (with-test (:name :implicit-value-cells)
1071   (flet ((test-it (type input output)
1072            (let ((f (compile nil `(lambda (x)
1073                                     (declare (type ,type x))
1074                                     (flet ((inc ()
1075                                              (incf x)))
1076                                       (declare (dynamic-extent #'inc))
1077                                       (list (opaque-funcall #'inc) x))))))
1078              (assert (equal (funcall f input)
1079                             (list output output))))))
1080     (let ((width sb-vm:n-word-bits))
1081       (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1082       (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1083       (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1084       (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1085       (test-it 'single-float 3f0 4f0)
1086       (test-it 'double-float 3d0 4d0)
1087       (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1088       (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1089
1090 (with-test (:name :sap-implicit-value-cells)
1091   (let ((f (compile nil `(lambda (x)
1092                            (declare (type system-area-pointer x))
1093                            (flet ((inc ()
1094                                     (setf x (sb-sys:sap+ x 16))))
1095                              (declare (dynamic-extent #'inc))
1096                              (list (opaque-funcall #'inc) x)))))
1097         (width sb-vm:n-machine-word-bits))
1098     (assert (every (lambda (x)
1099                      (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1100                    (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))
1101
1102 (with-test (:name :&more-bounds)
1103   ;; lp#1154946
1104   (assert (not (funcall (compile nil '(lambda (&rest args) (car args))))))
1105   (assert (not (funcall (compile nil '(lambda (&rest args) (nth 6 args))))))
1106   (assert (not (funcall (compile nil '(lambda (&rest args) (elt args 10))))))
1107   (assert (not (funcall (compile nil '(lambda (&rest args) (cadr args))))))
1108   (assert (not (funcall (compile nil '(lambda (&rest args) (third args)))))))