Rework test infrastructure to keep track of tests which are disabled
[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:quit :unix-status 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   `(defun ,name ,arglist
25      ,@body))
26
27 (declaim (notinline opaque-identity))
28 (defun opaque-identity (x)
29   x)
30
31 ;;; &REST lists
32
33 (defun-with-dx dxlength (&rest rest)
34   (declare (dynamic-extent rest))
35   (length rest))
36
37 (with-test (:name (:dx-&rest :basics))
38   (assert (= (dxlength 1 2 3) 3))
39   (assert (= (dxlength t t t t t t) 6))
40   (assert (= (dxlength) 0)))
41
42 (defun callee (list)
43   (destructuring-bind (a b c d e f &rest g) list
44     (+ a b c d e f (length g))))
45
46 (defun-with-dx dxcaller (&rest rest)
47   (declare (dynamic-extent rest))
48   (callee rest))
49
50 (with-test (:name (:dx-&rest :pass-down-to-callee :tail-call))
51   (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
52
53 (defun-with-dx dxcaller-align-1 (x &rest rest)
54   (declare (dynamic-extent rest))
55   (+ x (callee rest)))
56
57 (with-test (:name (:dx-&rest :pass-down-to-callee :non-tail-call))
58   (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
59   (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
60
61 ;;; %NIP-VALUES
62
63 (defun-with-dx test-nip-values ()
64   (flet ((bar (x &rest y)
65            (declare (dynamic-extent y))
66            (if (> x 0)
67                (values x (length y))
68                (values (car y)))))
69     (multiple-value-call #'values
70       (bar 1 2 3 4 5 6)
71       (bar -1 'a 'b))))
72
73 (with-test (:name (:nip-values))
74   (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a))))
75
76 ;;; LET-variable substitution
77
78 (defun-with-dx test-let-var-subst1 (x)
79   (let ((y (list x (1- x))))
80     (opaque-identity :foo)
81     (let ((z (the list y)))
82       (declare (dynamic-extent z))
83       (length z))))
84
85 (with-test (:name (:let-variable-substitution))
86   (assert (eql (test-let-var-subst1 17) 2)))
87
88 (defun-with-dx test-let-var-subst2 (x)
89   (let ((y (list x (1- x))))
90     (declare (dynamic-extent y))
91     (opaque-identity :foo)
92     (let ((z (the list y)))
93       (length z))))
94
95 (with-test (:name (:let-variable-substitution-2))
96   (assert (eql (test-let-var-subst2 17) 2)))
97
98
99 ;;; DX propagation through LET-return.
100
101 (defun-with-dx test-lvar-subst (x)
102   (let ((y (list x (1- x))))
103     (declare (dynamic-extent y))
104     (second (let ((z (the list y)))
105               (opaque-identity :foo)
106               z))))
107
108 (with-test (:name (:dx-propagation-through-let-return))
109   (assert (eql (test-lvar-subst 11) 10)))
110
111 ;;; this code is incorrect, but the compiler should not fail
112 (defun-with-dx test-let-var-subst-incorrect (x)
113   (let ((y (list x (1- x))))
114     (opaque-identity :foo)
115     (let ((z (the list y)))
116       (declare (dynamic-extent z))
117       (opaque-identity :bar)
118       z)))
119 \f
120 ;;; alignment
121
122 (defvar *x*)
123 (defun-with-dx test-alignment-dx-list (form)
124   (multiple-value-prog1 (eval form)
125     (let ((l (list 1 2 3 4)))
126       (declare (dynamic-extent l))
127       (setq *x* (copy-list l)))))
128
129 (with-test (:name (:dx-list :alignment))
130   (dotimes (n 64)
131     (let* ((res (loop for i below n collect i))
132            (form `(values ,@res)))
133       (assert (equal (multiple-value-list (test-alignment-dx-list form)) res))
134       (assert (equal *x* '(1 2 3 4))))))
135
136 ;;; closure
137
138 (declaim (notinline true))
139 (defun true (x)
140   (declare (ignore x))
141   t)
142
143 (defun-with-dx dxclosure (x)
144   (flet ((f (y)
145            (+ y x)))
146     (declare (dynamic-extent #'f))
147     (true #'f)))
148
149 (with-test (:name (:dx-closure))
150   (assert (eq t (dxclosure 13))))
151
152 ;;; value-cells
153
154 (defun-with-dx dx-value-cell (x)
155   ;; Not implemented everywhere, yet.
156   #+(or x86 x86-64 mips hppa)
157   (let ((cell x))
158     (declare (sb-int:truly-dynamic-extent cell))
159     (flet ((f ()
160              (incf cell)))
161       (declare (dynamic-extent #'f))
162       (true #'f))))
163
164 ;;; CONS
165
166 (defun-with-dx cons-on-stack (x)
167   (let ((cons (cons x x)))
168     (declare (dynamic-extent cons))
169     (true cons)
170     nil))
171
172 ;;; MAKE-ARRAY
173
174 (defun force-make-array-on-stack (n)
175   (declare (optimize safety))
176   (let ((v (make-array (min n 1))))
177     (declare (sb-int:truly-dynamic-extent v))
178     (true v)
179     nil))
180
181 (defun-with-dx make-array-on-stack-1 ()
182   (let ((v (make-array '(42) :element-type 'single-float)))
183     (declare (dynamic-extent v))
184     (true v)
185     nil))
186
187 (defun-with-dx make-array-on-stack-2 (n x)
188   (declare (integer n))
189   (let ((v (make-array n :initial-contents x)))
190     (declare (sb-int:truly-dynamic-extent v))
191     (true v)
192     nil))
193
194 (defun-with-dx make-array-on-stack-3 (x y z)
195   (let ((v (make-array 3
196                        :element-type 'fixnum :initial-contents (list x y z)
197                        :element-type t :initial-contents x)))
198     (declare (sb-int:truly-dynamic-extent v))
199     (true v)
200     nil))
201
202 (defun-with-dx make-array-on-stack-4 ()
203   (let ((v (make-array 3 :initial-contents '(1 2 3))))
204     (declare (sb-int:truly-dynamic-extent v))
205     (true v)
206     nil))
207
208 (defun-with-dx make-array-on-stack-5 ()
209   (let ((v (make-array 3 :initial-element 12 :element-type t)))
210     (declare (sb-int:truly-dynamic-extent v))
211     (true v)
212     nil))
213
214 (defun-with-dx vector-on-stack (x y)
215   (let ((v (vector 1 x 2 y 3)))
216     (declare (sb-int:truly-dynamic-extent v))
217     (true v)
218     nil))
219
220 ;;; MAKE-STRUCTURE
221
222 (declaim (inline make-fp-struct-1))
223 (defstruct fp-struct-1
224   (s 0.0 :type single-float)
225   (d 0.0d0 :type double-float))
226
227 (defun-with-dx test-fp-struct-1.1 (s d)
228   (let ((fp (make-fp-struct-1 :s s)))
229     (declare (dynamic-extent fp))
230     (assert (eql s (fp-struct-1-s fp)))
231     (assert (eql 0.0d0 (fp-struct-1-d fp)))))
232
233 (defun-with-dx test-fp-struct-1.2 (s d)
234   (let ((fp (make-fp-struct-1 :d d)))
235     (declare (dynamic-extent fp))
236     (assert (eql 0.0 (fp-struct-1-s fp)))
237     (assert (eql d (fp-struct-1-d fp)))))
238
239 (defun-with-dx test-fp-struct-1.3 (s d)
240   (let ((fp (make-fp-struct-1 :d d :s s)))
241     (declare (dynamic-extent fp))
242     (assert (eql s (fp-struct-1-s fp)))
243     (assert (eql d (fp-struct-1-d fp)))))
244
245 (defun-with-dx test-fp-struct-1.4 (s d)
246   (let ((fp (make-fp-struct-1 :s s :d d)))
247     (declare (dynamic-extent fp))
248     (assert (eql s (fp-struct-1-s fp)))
249     (assert (eql d (fp-struct-1-d fp)))))
250
251 (with-test (:name (:test-fp-struct-1.1))
252   (test-fp-struct-1.1 123.456 876.243d0))
253 (with-test (:name (:test-fp-struct-1.2))
254   (test-fp-struct-1.2 123.456 876.243d0))
255 (with-test (:name (:test-fp-struct-1.3))
256   (test-fp-struct-1.3 123.456 876.243d0))
257 (with-test (:name (:test-fp-struct-1.4))
258   (test-fp-struct-1.4 123.456 876.243d0))
259
260 (declaim (inline make-fp-struct-2))
261 (defstruct fp-struct-2
262   (d 0.0d0 :type double-float)
263   (s 0.0 :type single-float))
264
265 (defun-with-dx test-fp-struct-2.1 (s d)
266   (let ((fp (make-fp-struct-2 :s s)))
267     (declare (dynamic-extent fp))
268     (assert (eql s (fp-struct-2-s fp)))
269     (assert (eql 0.0d0 (fp-struct-2-d fp)))))
270
271 (defun-with-dx test-fp-struct-2.2 (s d)
272   (let ((fp (make-fp-struct-2 :d d)))
273     (declare (dynamic-extent fp))
274     (assert (eql 0.0 (fp-struct-2-s fp)))
275     (assert (eql d (fp-struct-2-d fp)))))
276
277 (defun-with-dx test-fp-struct-2.3 (s d)
278   (let ((fp (make-fp-struct-2 :d d :s s)))
279     (declare (dynamic-extent fp))
280     (assert (eql s (fp-struct-2-s fp)))
281     (assert (eql d (fp-struct-2-d fp)))))
282
283 (defun-with-dx test-fp-struct-2.4 (s d)
284   (let ((fp (make-fp-struct-2 :s s :d d)))
285     (declare (dynamic-extent fp))
286     (assert (eql s (fp-struct-2-s fp)))
287     (assert (eql d (fp-struct-2-d fp)))))
288
289 (with-test (:name (:test-fp-struct-2.1))
290   (test-fp-struct-2.1 123.456 876.243d0))
291 (with-test (:name (:test-fp-struct-2.2))
292   (test-fp-struct-2.2 123.456 876.243d0))
293 (with-test (:name (:test-fp-struct-2.3))
294   (test-fp-struct-2.3 123.456 876.243d0))
295 (with-test (:name (:test-fp-struct-2.4))
296   (test-fp-struct-2.4 123.456 876.243d0))
297
298 (declaim (inline make-cfp-struct-1))
299 (defstruct cfp-struct-1
300   (s (complex 0.0) :type (complex single-float))
301   (d (complex 0.0d0) :type (complex double-float)))
302
303 (defun-with-dx test-cfp-struct-1.1 (s d)
304   (let ((cfp (make-cfp-struct-1 :s s)))
305     (declare (dynamic-extent cfp))
306     (assert (eql s (cfp-struct-1-s cfp)))
307     (assert (eql (complex 0.0d0) (cfp-struct-1-d cfp)))))
308
309 (defun-with-dx test-cfp-struct-1.2 (s d)
310   (let ((cfp (make-cfp-struct-1 :d d)))
311     (declare (dynamic-extent cfp))
312     (assert (eql (complex 0.0) (cfp-struct-1-s cfp)))
313     (assert (eql d (cfp-struct-1-d cfp)))))
314
315 (defun-with-dx test-cfp-struct-1.3 (s d)
316   (let ((cfp (make-cfp-struct-1 :d d :s s)))
317     (declare (dynamic-extent cfp))
318     (assert (eql s (cfp-struct-1-s cfp)))
319     (assert (eql d (cfp-struct-1-d cfp)))))
320
321 (defun-with-dx test-cfp-struct-1.4 (s d)
322   (let ((cfp (make-cfp-struct-1 :s s :d d)))
323     (declare (dynamic-extent cfp))
324     (assert (eql s (cfp-struct-1-s cfp)))
325     (assert (eql d (cfp-struct-1-d cfp)))))
326
327 (with-test (:name (:test-cfp-struct-1.1))
328   (test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
329 (with-test (:name (:test-cfp-struct-1.2))
330   (test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
331 (with-test (:name (:test-cfp-struct-1.3))
332   (test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
333 (with-test (:name (:test-cfp-struct-1.4))
334   (test-cfp-struct-1.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
335
336 (declaim (inline make-cfp-struct-2))
337 (defstruct cfp-struct-2
338   (d (complex 0.0d0) :type (complex double-float))
339   (s (complex 0.0) :type (complex single-float)))
340
341 (defun-with-dx test-cfp-struct-2.1 (s d)
342   (let ((cfp (make-cfp-struct-2 :s s)))
343     (declare (dynamic-extent cfp))
344     (assert (eql s (cfp-struct-2-s cfp)))
345     (assert (eql (complex 0.0d0) (cfp-struct-2-d cfp)))))
346
347 (defun-with-dx test-cfp-struct-2.2 (s d)
348   (let ((cfp (make-cfp-struct-2 :d d)))
349     (declare (dynamic-extent cfp))
350     (assert (eql (complex 0.0) (cfp-struct-2-s cfp)))
351     (assert (eql d (cfp-struct-2-d cfp)))))
352
353 (defun-with-dx test-cfp-struct-2.3 (s d)
354   (let ((cfp (make-cfp-struct-2 :d d :s s)))
355     (declare (dynamic-extent cfp))
356     (assert (eql s (cfp-struct-2-s cfp)))
357     (assert (eql d (cfp-struct-2-d cfp)))))
358
359 (defun-with-dx test-cfp-struct-2.4 (s d)
360   (let ((cfp (make-cfp-struct-2 :s s :d d)))
361     (declare (dynamic-extent cfp))
362     (assert (eql s (cfp-struct-2-s cfp)))
363     (assert (eql d (cfp-struct-2-d cfp)))))
364
365 (with-test (:name (:test-cfp-struct-2.1))
366   (test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
367 (with-test (:name (:test-cfp-struct-2.2))
368   (test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
369 (with-test (:name (:test-cfp-struct-2.3))
370   (test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
371 (with-test (:name (:test-cfp-struct-2.4))
372   (test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
373
374 (declaim (inline make-foo1 make-foo2 make-foo3))
375 (defstruct foo1 x)
376
377 (defun-with-dx make-foo1-on-stack (x)
378   (let ((foo (make-foo1 :x x)))
379     (declare (dynamic-extent foo))
380     (assert (eql x (foo1-x foo)))))
381
382 (defstruct foo2
383   (x 0.0 :type single-float)
384   (y 0.0d0 :type double-float)
385   a
386   b
387   c)
388
389 (defun-with-dx make-foo2-on-stack (x y)
390   (let ((foo (make-foo2 :y y :c 'c)))
391     (declare (dynamic-extent foo))
392     (assert (eql 0.0 (foo2-x foo)))
393     (assert (eql y (foo2-y foo)))
394     (assert (eql 'c (foo2-c foo)))
395     (assert (eql nil (foo2-b foo)))))
396
397 ;;; Check that constants work out as argument for all relevant
398 ;;; slot types.
399 (defstruct foo3
400   (a 0 :type t)
401   (b 1 :type fixnum)
402   (c 2 :type sb-vm:word)
403   (d 3.0 :type single-float)
404   (e 4.0d0 :type double-float))
405
406 (defun-with-dx make-foo3-on-stack ()
407   (let ((foo (make-foo3)))
408     (declare (dynamic-extent foo))
409     (assert (eql 0 (foo3-a foo)))
410     (assert (eql 1 (foo3-b foo)))
411     (assert (eql 2 (foo3-c foo)))
412     (assert (eql 3.0 (foo3-d foo)))
413     (assert (eql 4.0d0 (foo3-e foo)))))
414
415 ;;; Nested DX
416
417 (defun-with-dx nested-dx-lists ()
418   (let ((dx (list (list 1 2) (list 3 4))))
419     (declare (dynamic-extent dx))
420     (true dx)
421     nil))
422
423 (defun-with-dx nested-dx-conses ()
424   (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil))))))
425     (declare (dynamic-extent dx))
426     (true dx)
427     nil))
428
429 (defun-with-dx nested-dx-not-used (x)
430   (declare (list x))
431   (let ((l (setf (car x) (list x x x))))
432     (declare (dynamic-extent l))
433     (true l)
434     (true (length l))
435     nil))
436
437 (defun-with-dx nested-evil-dx-used (x)
438   (declare (list x))
439   (let ((l (list x x x)))
440     (declare (dynamic-extent l))
441     (unwind-protect
442          (progn
443            (setf (car x) l)
444            (true l))
445       (setf (car x) nil))
446     nil))
447
448 (defparameter *bar* nil)
449 (declaim (inline make-nested-bad make-nested-good))
450 (defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar))))
451                    (:constructor make-nested-good (&key bar)))
452   bar)
453
454 (defun-with-dx nested-good (y)
455   (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y)))))))))
456     (declare (dynamic-extent x))
457     (true x)))
458
459 (defun-with-dx nested-bad (y)
460   (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y)))))))))
461     (declare (dynamic-extent x))
462     (unless (equalp (caar x) (make-nested-good :bar *bar*))
463       (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*)))
464     (caar x)))
465
466 (with-test (:name :conservative-nested-dx)
467   ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
468   (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
469   (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
470
471 ;;; multiple uses for dx lvar
472
473 (defun-with-dx multiple-dx-uses ()
474   (let ((dx (if (true t)
475                 (list 1 2 3)
476                 (list 2 3 4))))
477     (declare (dynamic-extent dx))
478     (true dx)
479     nil))
480
481 ;;; handler-case and handler-bind should use DX internally
482
483 (defun dx-handler-bind (x)
484   (handler-bind ((error
485                   #'(lambda (c)
486                       (break "OOPS: ~S caused ~S" x c)))
487                  ((and serious-condition (not error))
488                   #'(lambda (c)
489                       (break "OOPS2: ~S did ~S" x c))))
490     (/ 2 x)))
491
492 (defun dx-handler-case (x)
493   (assert (zerop (handler-case (/ 2 x)
494                    (error (c)
495                      (break "OOPS: ~S caused ~S" x c)
496                      -1)
497                    (:no-error (res)
498                      (1- res))))))
499
500 (defvar *a-cons* (cons nil nil))
501
502 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
503   (assert-no-consing (dxclosure 42)))
504
505 (with-test (:name (:no-consing :dx-lists) :skipped-on '(not :stack-allocatable-lists))
506   (assert-no-consing (dxlength 1 2 3))
507   (assert-no-consing (dxlength t t t t t t))
508   (assert-no-consing (dxlength))
509   (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
510   (assert-no-consing (test-nip-values))
511   (assert-no-consing (test-let-var-subst2 17))
512   (assert-no-consing (test-lvar-subst 11))
513   (assert-no-consing (nested-dx-lists))
514   (assert-consing (nested-dx-not-used *a-cons*))
515   (assert-no-consing (nested-evil-dx-used *a-cons*))
516   (assert-no-consing (multiple-dx-uses)))
517
518 (with-test (:name (:no-consing :dx-value-cell))
519   (assert-no-consing (dx-value-cell 13)))
520
521 (with-test (:name (:no-consing :dx-fixed-objects) :skipped-on '(not :stack-allocatable-fixed-objects))
522   (assert-no-consing (cons-on-stack 42))
523   (assert-no-consing (make-foo1-on-stack 123))
524   (assert-no-consing (nested-good 42))
525   (assert-no-consing (nested-dx-conses))
526   (assert-no-consing (dx-handler-bind 2))
527   (assert-no-consing (dx-handler-case 2)))
528
529 (with-test (:name (:no-consing :dx-vectors) :skipped-on '(not :stack-allocatable-vectors))
530   (assert-no-consing (force-make-array-on-stack 128))
531   (assert-no-consing (make-array-on-stack-1))
532   (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
533   (assert-no-consing (make-array-on-stack-3 9 8 7))
534   (assert-no-consing (make-array-on-stack-4))
535   (assert-no-consing (make-array-on-stack-5))
536   (assert-no-consing (vector-on-stack :x :y)))
537
538 (with-test (:name (:no-consing :dx-raw-instances) :fails-on :ppc :skipped-on '(not :raw-instance-init-vops))
539   (let (a b)
540     (setf a 1.24 b 1.23d0)
541     (assert-no-consing (make-foo2-on-stack a b)))
542     (assert-no-consing (make-foo3-on-stack)))
543
544 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
545
546 (defvar *table* (make-hash-table))
547
548 (defun test-hash-table ()
549   (setf (gethash 5 *table*) 13)
550   (gethash 5 *table*))
551
552 ;; This fails on threaded PPC because the hash-table implementation
553 ;; uses recursive system spinlocks, which cons (see below for test
554 ;; (:no-consing :spinlock), which also fails on threaded PPC).
555 (with-test (:name (:no-consing :hash-tables) :fails-on '(and :ppc :sb-thread))
556   (assert-no-consing (test-hash-table)))
557
558 ;;; with-spinlock and with-mutex should use DX and not cons
559
560 (defvar *slock* (sb-thread::make-spinlock :name "slocklock"))
561
562 (defun test-spinlock ()
563   (sb-thread::with-spinlock (*slock*)
564     (true *slock*)))
565
566 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
567
568 (defun test-mutex ()
569   (sb-thread:with-mutex (*mutex*)
570     (true *mutex*)))
571
572 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
573   (assert-no-consing (test-mutex)))
574
575 (with-test (:name (:no-consing :spinlock) :fails-on :ppc :skipped-on '(not :sb-thread))
576   (assert-no-consing (test-spinlock)))
577
578 \f
579
580 ;;; Bugs found by Paul F. Dietz
581
582 (with-test (:name (:dx-bug-misc :pfdietz))
583   (assert
584    (eq
585     (funcall
586      (compile
587       nil
588       '(lambda (a b)
589         (declare (optimize (speed 2) (space 0) (safety 0)
590                   (debug 1) (compilation-speed 3)))
591         (let* ((v5 (cons b b)))
592           (declare (dynamic-extent v5))
593           a)))
594      'x 'y)
595     'x)))
596
597 ;;; bug reported by Svein Ove Aas
598 (defun svein-2005-ii-07 (x y)
599   (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
600   (let ((args (list* y 1 2 x)))
601     (declare (dynamic-extent args))
602     (apply #'aref args)))
603
604 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
605   (assert (eql
606            (svein-2005-ii-07
607             '(0)
608             #3A(((1 1 1) (1 1 1) (1 1 1))
609                 ((1 1 1) (1 1 1) (4 1 1))
610                 ((1 1 1) (1 1 1) (1 1 1))))
611            4)))
612
613 ;;; bug reported by Brian Downing: stack-allocated arrays were not
614 ;;; filled with zeroes.
615 (defun-with-dx bdowning-2005-iv-16 ()
616   (let ((a (make-array 11 :initial-element 0)))
617     (declare (dynamic-extent a))
618     (assert (every (lambda (x) (eql x 0)) a))))
619
620 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
621   #+(or hppa mips x86 x86-64)
622   (assert-no-consing (bdowning-2005-iv-16))
623   (bdowning-2005-iv-16))
624
625 (declaim (inline my-nconc))
626 (defun-with-dx my-nconc (&rest lists)
627   (declare (dynamic-extent lists))
628   (apply #'nconc lists))
629 (defun-with-dx my-nconc-caller (a b c)
630   (let ((l1 (list a b c))
631         (l2 (list a b c)))
632     (my-nconc l1 l2)))
633 (with-test (:name :rest-stops-the-buck)
634   (let ((list1 (my-nconc-caller 1 2 3))
635         (list2 (my-nconc-caller 9 8 7)))
636     (assert (equal list1 '(1 2 3 1 2 3)))
637     (assert (equal list2 '(9 8 7 9 8 7)))))
638
639 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
640   (let* ((a (list x y z))
641          (b (list x y z))
642          (c (list a b)))
643     (declare (dynamic-extent c))
644     (values (first c) (second c))))
645 (with-test (:name :let-converted-vars-dx-allocated-bug)
646   (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
647     (assert (and (equal i j)
648                  (equal i (list 1 2 3))))))
649
650 ;;; workaround for bug 419 -- real issue remains, but check that the
651 ;;; bandaid holds.
652 (defun-with-dx bug419 (x)
653   (multiple-value-call #'list
654     (eval '(values 1 2 3))
655     (let ((x x))
656       (declare (dynamic-extent x))
657       (flet ((mget (y)
658                (+ x y))
659              (mset (z)
660                (incf x z)))
661         (declare (dynamic-extent #'mget #'mset))
662         ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
663
664 (with-test (:name (:dx-bug-misc :bug419))
665   (assert (equal (bug419 42) '(1 2 3 4 5 6))))
666
667 ;;; Multiple DX arguments in a local function call
668 (defun test-dx-flet-test (fun n f1 f2 f3)
669   (let ((res (with-output-to-string (s)
670                (assert (eql n (ignore-errors (funcall fun s)))))))
671     (multiple-value-bind (x pos) (read-from-string res nil)
672       (assert (equalp f1 x))
673       (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
674         (assert (equalp f2 y))
675         (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
676   #+(or hppa mips x86 x86-64)
677   (assert-no-consing (assert (eql n (funcall fun nil))))
678   (assert (eql n (funcall fun nil))))
679
680 (macrolet ((def (n f1 f2 f3)
681              (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
682                `(progn
683                   (defun-with-dx ,name (s)
684                     (flet ((f (x)
685                              (declare (dynamic-extent x))
686                              (when s
687                                (print x s)
688                                (finish-output s))
689                              nil))
690                       (f ,f1)
691                       (f ,f2)
692                       (f ,f3)
693                       ,n))
694                   (with-test (:name (:dx-flet-test ,n))
695                     (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
696   (def 0 (list :one) (list :two) (list :three))
697   (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
698   (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
699
700 ;;; Test that unknown-values coming after a DX value won't mess up the
701 ;;; stack analysis
702 (defun test-update-uvl-live-sets (x y z)
703  (declare (optimize speed (safety 0)))
704  (flet ((bar (a b)
705           (declare (dynamic-extent a))
706           (eval `(list (length ',a) ',b))))
707    (list (bar x y)
708          (bar (list x y z)                  ; dx push
709               (list
710                (multiple-value-call 'list
711                  (eval '(values 1 2 3))     ; uv push
712                  (max y z)
713                )                            ; uv pop
714                14)
715          ))))
716
717 (with-test (:name (:update-uvl-live-sets))
718   (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
719                  (test-update-uvl-live-sets #() 4 5))))
720
721 (with-test (:name :regression-1.0.23.38)
722   (compile nil '(lambda ()
723                  (flet ((make (x y)
724                           (let ((res (cons x x)))
725                             (setf (cdr res) y)
726                             res)))
727                    (declaim (inline make))
728                    (let ((z (make 1 2)))
729                      (declare (dynamic-extent z))
730                      (print z)
731                      t))))
732   (compile nil '(lambda ()
733                  (flet ((make (x y)
734                           (let ((res (cons x x)))
735                             (setf (cdr res) y)
736                             (if x res y))))
737                    (declaim (inline make))
738                    (let ((z (make 1 2)))
739                      (declare (dynamic-extent z))
740                      (print z)
741                      t)))))
742
743 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
744 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
745 ;;; location, leading to all manner of badness. ...reproducing this
746 ;;; reliably is hard, but this it at least used to break on x86-64.
747 (defun length-and-words-packed-in-same-tn (m)
748   (declare (optimize speed (safety 0) (debug 0) (space 0)))
749   (let ((array (make-array (max 1 m) :element-type 'fixnum)))
750     (declare (dynamic-extent array))
751     (array-total-size array)))
752 (with-test (:name :length-and-words-packed-in-same-tn)
753   (assert (= 1 (length-and-words-packed-in-same-tn -3))))
754
755 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
756   (handler-bind
757       ((compiler-note (lambda (note)
758                         (error "compiler issued note ~S during test" note))))
759     ;; Taken from SWANK, used to signal a bogus stack allocation
760     ;; failure note.
761     (compile nil
762              `(lambda (files fasl-dir load)
763                 (let ((needs-recompile nil))
764                   (dolist (src files)
765                     (let ((dest (binary-pathname src fasl-dir)))
766                       (handler-case
767                           (progn
768                             (when (or needs-recompile
769                                       (not (probe-file dest))
770                                       (file-newer-p src dest))
771                               (setq needs-recompile t)
772                               (ensure-directories-exist dest)
773                               (compile-file src :output-file dest :print nil :verbose t))
774                             (when load
775                               (load dest :verbose t)))
776                         (serious-condition (c)
777                           (handle-loadtime-error c dest))))))))))
778
779 (declaim (inline foovector barvector))
780 (defun foovector (x y z)
781   (let ((v (make-array 3)))
782     (setf (aref v 0) x
783           (aref v 1) y
784           (aref v 2) z)
785     v))
786 (defun barvector (x y z)
787   (make-array 3 :initial-contents (list x y z)))
788 (with-test (:name :dx-compiler-notes :fails-on :ppc)
789   (flet ((assert-notes (j lambda)
790            (let ((n 0))
791              (handler-bind ((compiler-note (lambda (c)
792                                              (declare (ignore c))
793                                              (incf n))))
794                (compile nil lambda)
795                (unless (= j n)
796                  (error "Wanted ~S notes, got ~S for~%   ~S"
797                         j n lambda))))))
798     ;; These ones should complain.
799     (assert-notes 1 `(lambda (x)
800                        (let ((v (make-array x)))
801                          (declare (dynamic-extent v))
802                          (length v))))
803     (assert-notes 2 `(lambda (x)
804                        (let ((y (if (plusp x)
805                                     (true x)
806                                     (true (- x)))))
807                          (declare (dynamic-extent y))
808                          (print y)
809                          nil)))
810     (assert-notes 1 `(lambda (x)
811                        (let ((y (foovector x x x)))
812                          (declare (sb-int:truly-dynamic-extent y))
813                          (print y)
814                          nil)))
815     ;; These ones should not complain.
816     (assert-notes 0 `(lambda (name)
817                        (with-alien
818                            ((posix-getenv (function c-string c-string)
819                                           :EXTERN "getenv"))
820                          (values
821                           (alien-funcall posix-getenv name)))))
822     (assert-notes 0 `(lambda (x)
823                        (let ((y (barvector x x x)))
824                          (declare (dynamic-extent y))
825                          (print y)
826                          nil)))
827     (assert-notes 0 `(lambda (list)
828                        (declare (optimize (space 0)))
829                        (sort list #'<)))
830     (assert-notes 0 `(lambda (other)
831                        #'(lambda (s c n)
832                            (ignore-errors (funcall other s c n)))))))
833
834 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
835 ;;; in an unfortunate loop.
836 (defun handler-case-eating-stack ()
837   (let ((sp nil))
838     (do ((n 0 (logand most-positive-fixnum (1+ n))))
839         ((>= n 1024))
840      (multiple-value-bind (value error) (ignore-errors)
841        (when (and value error) nil))
842       (if sp
843           (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
844           (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
845 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
846   (assert-no-consing (handler-case-eating-stack)))
847
848 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
849 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
850 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
851 (deftype vec ()
852   `(simple-array single-float (3)))
853 (declaim (ftype (function (t t t) vec) vec))
854 (declaim (inline vec))
855 (defun vec (a b c)
856   (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
857 (defun bad-boy (vec)
858   (declare (type vec vec))
859   (lambda (fun)
860     (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
861       (declare (dynamic-extent vec))
862       (funcall fun vec))))
863 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
864   (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
865                    (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
866   (flet ((foo (x) (declare (ignore x))))
867     (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
868       (assert-no-consing (funcall bad-boy #'foo)))))
869
870 (with-test (:name :bug-497321)
871   (flet ((test (lambda type)
872            (let ((n 0))
873              (handler-bind ((condition (lambda (c)
874                                          (incf n)
875                                          (unless (typep c type)
876                                            (error "wanted ~S for~%  ~S~%got ~S"
877                                                   type lambda (type-of c))))))
878                (compile nil lambda))
879              (assert (= n 1)))))
880     (test `(lambda () (declare (dynamic-extent #'bar)))
881           'style-warning)
882     (test `(lambda () (declare (dynamic-extent bar)))
883           'style-warning)
884     (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
885           'sb-ext:compiler-note)
886     (test `(lambda ()
887              (flet ((bar () t))
888                (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
889           'sb-ext:compiler-note)))
890
891 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
892                                                    :stack-allocatable-lists)))
893   (flet ((test (x)
894            (let ((vec (make-array 1 :initial-contents (list (list x)))))
895              (declare (dynamic-extent vec))
896              (assert (eql x (car (aref vec 0)))))))
897     (assert-no-consing (test 42))))
898 \f
899 (defun bug-681092 ()
900   (declare (optimize speed))
901   (let ((c 0))
902     (flet ((bar () c))
903       (declare (dynamic-extent #'bar))
904       (do () ((list) (bar))
905         (setf c 10)
906         (return (bar))))))
907 (with-test (:name :bug-681092)
908   (assert (= 10 (bug-681092))))
909
910 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
911 ;;;; but required by sanity.
912
913 (declaim (inline rest-stops-dx))
914 (defun-with-dx rest-stops-dx (&rest args)
915   (declare (dynamic-extent args))
916   (apply #'opaque-identity args))
917
918 (defun-with-dx rest-stops-dx-ok ()
919   (equal '(:foo) (rest-stops-dx (list :foo))))
920
921 (with-test (:name :rest-stops-dynamic-extent)
922   (assert (rest-stops-dx-ok)))
923
924 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
925 ;;;; conversion.
926 (with-test (:name :rest-to-more-conversion)
927   (let ((f1 (compile nil `(lambda (f &rest args)
928                             (apply f args)))))
929     (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
930   (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
931                             (values (apply f1 args) (apply f2 args))))))
932     (assert-no-consing (multiple-value-bind (a b)
933                            (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
934                                     1 2 3)
935                          (assert (and (eql 6 a) (eql -4 b))))))
936   (let ((f3 (compile nil `(lambda (f &optional x &rest args)
937                             (when x
938                               (apply f x args))))))
939     (assert-no-consing (assert (eql 42 (funcall f3
940                                                 (lambda (a b c) (+ a b c))
941                                                 11
942                                                 10
943                                                 21)))))
944   (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
945                             (apply f y x args)))))
946     (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
947                                      (assert (eq y 'y))
948                                      (assert (= x 2))
949                                      (assert (eq :y yk))
950                                      (assert (eq y2 'y))
951                                      (assert (eq b 'b))
952                                      (assert (eq c 'c)))
953                                 2 :y 'y 'b 'c)))
954   (let ((f5 (compile nil `(lambda (a b c &rest args)
955                             (apply #'list* a b c args)))))
956     (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
957   (let ((f6 (compile nil `(lambda (x y)
958                             (declare (optimize speed))
959                             (concatenate 'string x y)))))
960     (assert (equal "foobar" (funcall f6 "foo" "bar"))))
961   (let ((f7 (compile nil `(lambda (&rest args)
962                             (lambda (f)
963                               (apply f args))))))
964     (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
965   (let ((f8 (compile nil `(lambda (&rest args)
966                             (flet ((foo (f)
967                                      (apply f args)))
968                               #'foo)))))
969     (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
970   (let ((f9 (compile nil `(lambda (f &rest args)
971                             (flet ((foo (g)
972                                      (apply g args)))
973                               (declare (dynamic-extent #'foo))
974                               (funcall f #'foo))))))
975     (assert (equal '(a b c d e f)
976                    (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
977   (let ((f10 (compile nil `(lambda (f &rest args)
978                             (flet ((foo (g)
979                                      (apply g args)))
980                               (funcall f #'foo))))))
981     (assert (equal '(a b c d e f)
982                    (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
983   (let ((f11 (compile nil `(lambda (x y z)
984                              (block out
985                                (labels ((foo (x &rest rest)
986                                           (apply (lambda (&rest rest2)
987                                                    (return-from out (values-list rest2)))
988                                                  x rest)))
989                                 (if x
990                                     (foo x y z)
991                                     (foo y z x))))))))
992     (multiple-value-bind (a b c) (funcall f11 1 2 3)
993       (assert (eql a 1))
994       (assert (eql b 2))
995       (assert (eql c 3)))))