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