0.8alpha.0.9:
[sbcl.git] / tests / compiler.impure.lisp
1 ;;;; This file is for compiler tests which have side effects (e.g.
2 ;;;; executing DEFUN) but which don't need any special side-effecting
3 ;;;; environmental stuff (e.g. DECLAIM of particular optimization
4 ;;;; settings). Similar tests which *do* expect special settings may
5 ;;;; be in files compiler-1.impure.lisp, compiler-2.impure.lisp, etc.
6
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; While most of SBCL is derived from the CMU CL system, the test
11 ;;;; files (like this one) were written from scratch after the fork
12 ;;;; from CMU CL.
13 ;;;; 
14 ;;;; This software is in the public domain and is provided with
15 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
16 ;;;; more information.
17
18 (load "assertoid.lisp")
19 (use-package "ASSERTOID")
20
21 ;;; Old CMU CL code assumed that the names of "keyword" arguments are
22 ;;; necessarily self-evaluating symbols, but ANSI Common Lisp allows
23 ;;; them to be any symbols, not necessarily keywords, and thus not
24 ;;; necessarily self-evaluating. Make sure that this works.
25 (defun newfangled-cons (&key ((left-thing x)) ((right-thing y)))
26   (cons x y))
27 (assert (equal (cons 1 2) (newfangled-cons 'right-thing 2 'left-thing 1)))
28
29 ;;; ANSI specifically says that duplicate keys are OK in lambda lists,
30 ;;; with no special exception for macro lambda lists. (As reported by
31 ;;; Pierre Mai on cmucl-imp 2001-03-30, Python didn't think so. The
32 ;;; rest of the thread had some entertainment value, at least for me
33 ;;; (WHN). The unbelievers were besmote and now even CMU CL will
34 ;;; conform to the spec in this regard. Who needs diplomacy when you
35 ;;; have brimstone?:-)
36 (defmacro ayup-duplicate-keys-are-ok-i-see-the-lite (&key k)
37   k)
38 (assert (equal (ayup-duplicate-keys-are-ok-i-see-the-lite :k 112) 112))
39 (assert (equal (ayup-duplicate-keys-are-ok-i-see-the-lite :k 'x :k 'y) 'x))
40
41 ;;; As reported by Alexey Dejneka (sbcl-devel 2002-01-30), in
42 ;;; sbcl-0.7.1 plus his patch (i.e. essentially sbcl-0.7.1.2), the
43 ;;; compiler barfed on this, blowing up in FIND-IN-PHYSENV looking for
44 ;;; the LAMBDA-VAR named NUM. That was fixed in sbcl-0.7.1.3.
45 (defun parse-num (index)
46   (let (num x)
47     (flet ((digs ()
48              (setq num index))
49            (z ()
50              (let ()
51                (setq x nil))))
52       (when (and (digs) (digs)) x))))
53
54 ;;; Bug 132: The compiler used to fail to compile INTEGER-valued CATCH
55 ;;; tags. This was fixed by Alexey Dejneka in sbcl-0.7.1.14. (INTEGER
56 ;;; catch tags are still a bad idea because EQ is used to compare
57 ;;; tags, and EQ comparison on INTEGERs is unportable; but now it's a
58 ;;; compiler warning instead of a failure to compile.)
59 (defun foo ()
60   (catch 0 (print 1331)))
61
62 ;;; Bug 150: In sbcl-0.7.1.15, compiling this code caused a failure in
63 ;;; SB-C::ADD-TEST-CONSTRAINTS:
64 ;;;    The value NIL is not of type SB-C::CONTINUATION.
65 ;;; This bug was fixed by APD in sbcl-0.7.1.30.
66 (defun bug150-test1 ()
67   (let* ()
68     (flet ((wufn () (glorp table1 4.9)))
69       (gleep *uustk* #'wufn "#1" (list)))
70     (if (eql (lo foomax 3.2))
71         (values)
72         (error "not ~S" '(eql (lo foomax 3.2))))
73     (values)))
74 ;;; A simpler test case for bug 150: The compiler died with the
75 ;;; same type error when trying to compile this.
76 (defun bug150-test2 ()
77   (let ()
78     (<)))
79
80 ;;; bug 147, fixed by APD 2002-04-28
81 ;;;
82 ;;; This test case used to crash the compiler, e.g. with
83 ;;;   failed AVER: "(= (LENGTH (BLOCK-SUCC CALL-BLOCK)) 1)"
84 (defun bug147 (string ind)
85   (flet ((digs ()
86            (let (old-index)
87              (if (and (< ind ind)
88                       (typep (char string ind) '(member #\1)))
89                  nil))))))
90
91 ;;; bug reported and fixed by Matthias Hoelzl sbcl-devel 2002-05-13
92 (defmacro foo-2002-05-13 () ''x)
93 (eval '(foo-2002-05-13))
94 (compile 'foo-2002-05-13)
95 (foo-2002-05-13) ; (The bug caused UNDEFINED-FUNCTION to be signalled here.)
96
97 ;;; floating point pain on the PPC.
98 ;;;
99 ;;; This test case used to fail to compile on most powerpcs prior to
100 ;;; sbcl-0.7.4.2x, as floating point traps were being incorrectly
101 ;;; masked.
102 (defun floating-point-pain (x)
103   (declare (single-float x))
104   (log x))
105
106 ;;; bug found and fixed ca. sbcl-0.7.5.12: The INTERSECTION-TYPE
107 ;;; here satisfies "is a subtype of ARRAY-TYPE", but can't be
108 ;;; accessed with ARRAY-TYPE accessors like
109 ;;; ARRAY-TYPE-SPECIALIZED-ELEMENT-TYPE, so ARRAY-related
110 ;;; DEFTRANSFORMs died with TYPE-ERROR at compile time when
111 ;;; compiling the DEFUN here.
112 (defun stupid-input-to-smart-array-deftransforms-0-7-5-12 (v)
113   (declare (type (and simple-vector fwd-type-ref) v))
114   (aref v 0))
115
116 ;;; Ca. sbcl-0.7.5.15 the compiler would fail an internal consistency
117 ;;; check on this code because it expected all calls to %INSTANCE-REF
118 ;;; to be transformed away, but its expectations were dashed by perverse
119 ;;; code containing app programmer errors like this.
120 (defstruct something-known-to-be-a-struct x y)
121 (multiple-value-bind (fun warnings-p failure-p)
122     (compile nil
123              '(lambda ()
124                 (labels ((a1 (a2 a3)
125                              (cond (t (a4 a2 a3))))
126                          (a4 (a2 a3 a5 a6)
127                              (declare (type (or simple-vector null) a5 a6))
128                              (something-known-to-be-a-struct-x a5))
129                          (a8 (a2 a3)
130                              (a9 #'a1 a10 a2 a3))
131                          (a11 (a2 a3)
132                               (cond ((and (funcall a12 a2)
133                                           (funcall a12 a3))
134                                      (funcall a13 a2 a3))
135                                     (t
136                                      (when a14
137                                      (let ((a15 (a1 a2 a3)))
138                                        ))
139                                      a16))))
140                   (values #'a17 #'a11))))
141   ;; Python sees the structure accessor on the known-not-to-be-a-struct
142   ;; A5 value and is very, very disappointed in you. (But it doesn't
143   ;; signal BUG any more.)
144   (assert failure-p))
145
146 ;;; On the SPARC, there was an erroneous definition of some VOPs used
147 ;;; to compile LOGANDs, which would lead to compilation of the
148 ;;; following function giving rise to a compile-time error (bug
149 ;;; spotted and fixed by Raymond Toy for CMUCL)
150 (defun logand-sparc-bogons (a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10)
151   (declare (type (unsigned-byte 32) a0)
152            (type (signed-byte 32) a1 a2 a3 a4 a5 a6 a7 a8 a9 a10)
153            ;; to ensure that the call is a candidate for
154            ;; transformation
155            (optimize (speed 3) (safety 0) (compilation-speed 0) (debug 0)))
156   (values
157    ;; the call that fails compilation
158    (logand a0 a10)
159    ;; a call to prevent the other arguments from being optimized away
160    (logand a1 a2 a3 a4 a5 a6 a7 a8 a9)))
161
162 ;;; bug 192, reported by Einar Floystad Dorum sbcl-devel 2002-08-14,
163 ;;; fixed in sbcl-0.7.6.26: Compiling this function in 0.7.6 caused
164 ;;; the compiler to try to constant-fold DATA-VECTOR-REF, which is OK,
165 ;;; except that there was no non-VOP definition of DATA-VECTOR-REF, so
166 ;;; it would fail.
167 (defun bug192 ()
168       (funcall 
169        (LAMBDA (TEXT I L )
170          (LABELS ((G908 (I)
171                     (LET ((INDEX
172                            (OR
173                             (IF (= I L)
174                                 NIL
175                                 (LET ((S TEXT)
176                                       (E (ELT TEXT I)))
177                                   (DECLARE (IGNORABLE S E))
178                                   (WHEN (EQL #\a E)
179                                     (G909 (1+ I))))))))
180                       INDEX))
181                   (G909 (I)
182                     (OR
183                      (IF (= I L)
184                          NIL
185                          (LET ((S TEXT)
186                                (E (ELT TEXT I)))
187                            (DECLARE (IGNORABLE S E))
188                            (WHEN (EQL #\b E) (G910 (1+ I)))))))
189                   (G910 (I)
190                     (LET ((INDEX
191                            (OR
192                             (IF NIL
193                                 NIL
194                                 (LET ((S TEXT))
195                                   (DECLARE (IGNORABLE S))
196                                   (WHEN T I))))))
197                       INDEX)))
198            (G908 I))) "abcdefg" 0 (length "abcdefg")))
199
200 ;;; bugs #65, #70, and #109, closed by APD's patch sbcl-devel 2002-08-17
201 ;;;
202 ;;; This was "YA code deletion bug" whose symptom was the failure of
203 ;;; the assertion
204 ;;;   (EQ (C::LAMBDA-TAIL-SET C::CALLER)
205 ;;;       (C::LAMBDA-TAIL-SET (C::LAMBDA-HOME C::CALLEE)))
206 ;;; at compile time.
207 (defun bug65-1 (termx termy) ; from Carl Witty on submit bugs list, debian.org
208   (labels
209     ((alpha-equal-bound-term-lists (listx listy)
210        (or (and (null listx) (null listy))
211            (and listx listy
212                 (let ((bindings-x (bindings-of-bound-term (car listx)))
213                       (bindings-y (bindings-of-bound-term (car listy))))
214                   (if (and (null bindings-x) (null bindings-y))
215                       (alpha-equal-terms (term-of-bound-term (car listx))
216                                          (term-of-bound-term (car listy)))
217                       (and (= (length bindings-x) (length bindings-y))
218                            (prog2
219                                (enter-binding-pairs (bindings-of-bound-term (car listx))
220                                                     (bindings-of-bound-term (car listy)))
221                                (alpha-equal-terms (term-of-bound-term (car listx))
222                                                   (term-of-bound-term (car listy)))
223                              (exit-binding-pairs (bindings-of-bound-term (car listx))
224                                                  (bindings-of-bound-term (car listy)))))))
225                 (alpha-equal-bound-term-lists (cdr listx) (cdr listy)))))
226
227      (alpha-equal-terms (termx termy)
228        (if (and (variable-p termx)
229                 (variable-p termy))
230            (equal-bindings (id-of-variable-term termx)
231                            (id-of-variable-term termy))
232            (and (equal-operators-p (operator-of-term termx) (operator-of-term termy))
233                 (alpha-equal-bound-term-lists (bound-terms-of-term termx)
234                                               (bound-terms-of-term termy))))))
235
236     (or (eq termx termy)
237         (and termx termy
238              (with-variable-invocation (alpha-equal-terms termx termy))))))
239 (defun bug65-2 () ; from Bob Rogers cmucl-imp 1999-07-28
240   ;; Given an FSSP alignment file named by the argument . . .
241   (labels ((get-fssp-char ()
242              (get-fssp-char))
243            (read-fssp-char ()
244              (get-fssp-char)))
245     ;; Stub body, enough to tickle the bug.
246     (list (read-fssp-char)
247           (read-fssp-char))))
248 (defun bug70 ; from David Young cmucl-help 30 Nov 2000
249     (item sequence &key (test #'eql))
250   (labels ((find-item (obj seq test &optional (val nil))
251                       (let ((item (first seq)))
252                         (cond ((null seq)
253                                (values nil nil))
254                               ((funcall test obj item)
255                                (values val seq))
256                               (t        
257                                (find-item obj
258                                           (rest seq)
259                                           test
260                                           (nconc val `(,item))))))))
261     (find-item item sequence test)))
262 (defun bug109 () ; originally from CMU CL bugs collection, reported as
263                  ; SBCL bug by MNA 2001-06-25
264   (labels 
265       ((eff (&key trouble)
266             (eff)
267             ;; nil
268             ;; Uncomment and it works
269             ))
270     (eff)))
271
272 ;;; bug 192a, fixed by APD "more strict type checking" patch
273 ;;; (sbcl-devel 2002-08-07)
274 (defun bug192a (x)
275   (declare (optimize (speed 0) (safety 3)))
276   ;; Even with bug 192a, this declaration was checked as an assertion.
277   (declare (real x))
278   (+ x
279      (locally
280        ;; Because of bug 192a, this declaration was trusted without checking.
281        (declare (single-float x))
282        (sin x))))
283 (assert (null (ignore-errors (bug192a nil))))
284 (multiple-value-bind (result error) (ignore-errors (bug192a 100))
285   (assert (null result))
286   (assert (equal (type-error-expected-type error) 'single-float)))
287
288 ;;; bug 194, fixed in part by APD "more strict type checking" patch
289 ;;; (sbcl-devel 2002-08-07)
290 (progn
291   #+nil ; FIXME: still broken in 0.7.7.19 (after patch)
292   (multiple-value-bind (result error)
293       (ignore-errors (multiple-value-prog1 (progn (the real '(1 2 3)))))
294     (assert (null result))
295     (assert (typep error 'type-error)))
296   #+nil ; FIXME: still broken in 0.7.7.19 (after patch)
297   (multiple-value-bind (result error)
298       (ignore-errors (the real '(1 2 3)))
299     (assert (null result))
300     (assert (typep error 'type-error))))
301 \f
302 ;;; BUG 48a. and b. (symbol-macrolet handling), fixed by Eric Marsden
303 ;;; and Raymond Toy for CMUCL, fix ported for sbcl-0.7.6.18.
304 (multiple-value-bind (function warnings-p failure-p)
305     (compile nil '(lambda () (symbol-macrolet ((t nil)) t)))
306   (assert failure-p)
307   (assert (raises-error? (funcall function) program-error)))
308 (multiple-value-bind (function warnings-p failure-p)
309     (compile nil
310              '(lambda ()
311                 (symbol-macrolet ((*standard-input* nil))
312                   *standard-input*)))
313   (assert failure-p)
314   (assert (raises-error? (funcall function) program-error)))
315 #||
316 BUG 48c, not yet fixed:
317 (multiple-value-bind (function warnings-p failure-p)
318     (compile nil '(lambda () (symbol-macrolet ((s nil)) (declare (special s)) s)))
319   (assert failure-p)
320   (assert (raises-error? (funcall function) program-error)))
321 ||#
322 \f
323 ;;; bug 120a: Turned out to be constraining code looking like (if foo
324 ;;; <X> <X>) where <X> was optimized by the compiler to be the exact
325 ;;; same block in both cases, but not turned into (PROGN FOO <X>).
326 ;;; Fixed by APD in sbcl-0.7.7.2, who provided this test:
327 (declaim (inline dont-constrain-if-too-much))
328 (defun dont-constrain-if-too-much (frame up-frame)
329   (declare (optimize (speed 3) (safety 1) (debug 1)))
330   (if (or (not frame) t)
331       frame
332       "bar"))
333 (defun dont-constrain-if-too-much-aux (x y)
334   (declare (optimize (speed 3) (safety 1) (debug 1)))
335   (if x t (if y t (dont-constrain-if-too-much x y))))
336
337 (assert (null (dont-constrain-if-too-much-aux nil nil)))  
338
339 ;;; TYPE-ERROR confusion ca. sbcl-0.7.7.24, reported and fixed by
340 ;;; APD sbcl-devel 2002-09-14
341 (defun exercise-0-7-7-24-bug (x)
342   (declare (integer x))
343   (let (y)
344     (setf y (the single-float (if (> x 0) x 3f0)))
345     (list y y)))
346 (multiple-value-bind (v e) (ignore-errors (exercise-0-7-7-24-bug 4))
347   (assert (null v))
348   (assert (typep e 'type-error)))
349 (assert (equal (exercise-0-7-7-24-bug -4) '(3f0 3f0)))
350
351 ;;; non-intersecting type declarations were DWIMing in a confusing
352 ;;; fashion until sbcl-0.7.7.28, when APD reported and fixed the
353 ;;; problem.
354 (defun non-intersecting-the (x)
355   (let (y)
356     (setf y (the single-float (the integer x)))
357     (list y y)))
358
359 (raises-error? (foo 3) type-error)
360 (raises-error? (foo 3f0) type-error)
361
362 ;;; until 0.8.2 SBCL did not check THEs in arguments
363 (defun the-in-arguments-aux (x)
364   x)
365 (defun the-in-arguments-1 (x)
366   (list x (the-in-arguments-aux (the (single-float 0s0) x))))
367 (defun the-in-arguments-2 (x)
368   (list x (the-in-arguments-aux (the single-float x))))
369
370 (multiple-value-bind (result condition)
371     (ignore-errors (the-in-arguments-1 1))
372   (assert (null result))
373   (assert (typep condition 'type-error)))
374 (multiple-value-bind (result condition)
375     (ignore-errors (the-in-arguments-2 1))
376   (assert (null result))
377   (assert (typep condition 'type-error)))
378
379 ;;; bug 153: a hole in a structure slot type checking
380 (declaim (optimize safety))
381 (defstruct foo153
382   (bla 0 :type fixnum))
383 (defun bug153-1 ()
384   (let ((foo (make-foo153)))
385     (setf (foo153-bla foo) '(1 . 1))
386     (format t "Is ~a of type ~a a cons? => ~a~%"
387             (foo153-bla foo)
388             (type-of (foo153-bla foo))
389             (consp (foo153-bla foo)))))
390 (defun bug153-2 (x)
391   (let ((foo (make-foo153)))
392     (setf (foo153-bla foo) x)
393     (format t "Is ~a of type ~a a cons? => ~a~%"
394             (foo153-bla foo)
395             (type-of (foo153-bla foo))
396             (consp (foo153-bla foo)))))
397
398 (multiple-value-bind (result condition)
399     (ignore-errors (bug153-1))
400   (declare (ignore result))
401   (assert (typep condition 'type-error)))
402 (multiple-value-bind (result condition)
403     (ignore-errors (bug153-2 '(1 . 1)))
404   (declare (ignore result))
405   (assert (typep condition 'type-error)))
406
407 ;;;; bug 110: the compiler flushed the argument type test and the default
408 ;;;; case in the cond.
409 ;
410 ;(locally (declare (optimize (safety 3) (speed 2)))
411 ;  (defun bug110 (x)
412 ;    (declare (optimize (safety 2) (speed 3)))
413 ;    (declare (type (or string stream) x))
414 ;    (cond ((typep x 'string) 'string)
415 ;          ((typep x 'stream) 'stream)
416 ;          (t
417 ;           'none))))
418 ;
419 ;(multiple-value-bind (result condition)
420 ;    (ignore-errors (bug110 0))
421 ;  (declare (ignore result))
422 ;  (assert (typep condition 'type-error)))
423
424 ;;; bug 202: the compiler failed to compile a function, which derived
425 ;;; type contradicted declared.
426 (declaim (ftype (function () null) bug202))
427 (defun bug202 ()
428   t)
429
430 ;;; bugs 178, 199: compiler failed to compile a call of a function
431 ;;; with a hairy type
432 (defun bug178 (x)
433       (funcall (the function (the standard-object x))))
434
435 (defun bug199-aux (f)
436   (eq nil (funcall f)))
437
438 (defun bug199 (f x)
439   (declare (type (and function (satisfies bug199-aux)) f))
440   (funcall f x))
441
442 ;;; check non-toplevel DEFMACRO
443 (defvar *defmacro-test-status* nil)
444
445 (defun defmacro-test ()
446   (fmakunbound 'defmacro-test-aux)
447   (let* ((src "defmacro-test.lisp")
448          (obj (compile-file-pathname src)))
449     (unwind-protect
450          (progn
451            (compile-file src)
452            (assert (equal *defmacro-test-status* '(function a)))
453            (setq *defmacro-test-status* nil)
454            (load obj)
455            (assert (equal *defmacro-test-status* nil))
456            (macroexpand '(defmacro-test-aux 'a))
457            (assert (equal *defmacro-test-status* '(macro 'a z-value)))
458            (eval '(defmacro-test-aux 'a))
459            (assert (equal *defmacro-test-status* '(expanded 'a z-value))))
460       (ignore-errors (delete-file obj)))))
461
462 (defmacro-test)
463
464 ;;; bug 204: EVAL-WHEN inside a local environment
465 (defvar *bug204-test-status*)
466
467 (defun bug204-test ()
468   (let* ((src "bug204-test.lisp")
469          (obj (compile-file-pathname src)))
470     (unwind-protect
471          (progn
472            (setq *bug204-test-status* nil)
473            (compile-file src)
474            (assert (equal *bug204-test-status* '((:expanded :load-toplevel)
475                                                  (:called :compile-toplevel)
476                                                  (:expanded :compile-toplevel))))
477            (setq *bug204-test-status* nil)
478            (load obj)
479            (assert (equal *bug204-test-status* '((:called :load-toplevel)))))
480       (ignore-errors (delete-file obj)))))
481
482 (bug204-test)
483
484 ;;; toplevel SYMBOL-MACROLET
485 (defvar *symbol-macrolet-test-status*)
486
487 (defun symbol-macrolet-test ()
488   (let* ((src "symbol-macrolet-test.lisp")
489          (obj (compile-file-pathname src)))
490     (unwind-protect
491          (progn
492            (setq *symbol-macrolet-test-status* nil)
493            (compile-file src)
494            (assert (equal *symbol-macrolet-test-status*
495                           '(2 1)))
496            (setq *symbol-macrolet-test-status* nil)
497            (load obj)
498            (assert (equal *symbol-macrolet-test-status* '(2))))
499       (ignore-errors (delete-file obj)))))
500
501 (symbol-macrolet-test)
502
503 ;;; On the x86, this code failed to compile until sbcl-0.7.8.37:
504 (defun x86-assembler-failure (x)
505   (declare (optimize (speed 3) (safety 0)))
506   (eq (setf (car x) 'a) nil))
507
508 ;;; bug 211: :ALLOW-OTHER-KEYS
509 (defun bug211d (&key (x :x x-p) ((:allow-other-keys y) :y y-p))
510   (list x x-p y y-p))
511
512 (assert (equal (bug211d) '(:x nil :y nil)))
513 (assert (equal (bug211d :x 1) '(1 t :y nil)))
514 (assert (raises-error? (bug211d :y 2) program-error))
515 (assert (equal (bug211d :y 2 :allow-other-keys t :allow-other-keys nil)
516                '(:x nil t t)))
517 (assert (raises-error? (bug211d :y 2 :allow-other-keys nil) program-error))
518
519 (let ((failure-p
520        (nth-value
521         3
522         (compile 'bug211b
523                  '(lambda ()
524                    (flet ((test (&key (x :x x-p) ((:allow-other-keys y) :y y-p))
525                             (list x x-p y y-p)))
526                      (assert (equal (test) '(:x nil :y nil)))
527                      (assert (equal (test :x 1) '(1 t :y nil)))
528                      (assert (equal (test :y 2 :allow-other-keys 11 :allow-other-keys nil)
529                                     '(:x nil 11 t)))))))))
530   (assert (not failure-p))
531   (bug211b))
532
533 (let ((failure-p
534        (nth-value
535         3
536         (compile 'bug211c
537                  '(lambda ()
538                    (flet ((test (&key (x :x x-p))
539                             (list x x-p)))
540                      (assert (equal (test) '(:x nil)))
541                      (assert (equal (test :x 1) '(1 t)))
542                      (assert (equal (test :y 2 :allow-other-keys 11 :allow-other-keys nil)
543                                     '(:x nil)))))))))
544   (assert (not failure-p))
545   (bug211c))
546
547 (dolist (form '((test :y 2)
548                 (test :y 2 :allow-other-keys nil)
549                 (test :y 2 :allow-other-keys nil :allow-other-keys t)))
550   (multiple-value-bind (result warnings-p failure-p)
551       (compile nil `(lambda ()
552                      (flet ((test (&key (x :x x-p) ((:allow-other-keys y) :y y-p))
553                               (list x x-p y y-p)))
554                        ,form)))
555     (assert failure-p)
556     (assert (raises-error? (funcall result) program-error))))
557
558 ;;; bug 217: wrong type inference
559 (defun bug217-1 (x s)
560   (let ((f (etypecase x
561              (character #'write-char)
562              (integer #'write-byte))))
563     (funcall f x s)
564     (etypecase x
565       (character (write-char x s))
566       (integer (write-byte x s)))))
567 (bug217-1 #\1 *standard-output*)
568
569
570 ;;; bug 221: tried and died on CSUBTYPEP (not VALUES-SUBTYPEP) of the
571 ;;; function return types when inferring the type of the IF expression
572 (declaim (ftype (function (fixnum) (values package boolean)) bug221f1))
573 (declaim (ftype (function (t) (values package boolean)) bug221f2))
574 (defun bug221 (b x)
575   (funcall (if b #'bug221f1 #'bug221f2) x))
576 \f
577 ;;; bug 172: macro lambda lists were too permissive until 0.7.9.28
578 ;;; (fix provided by Matthew Danish) on sbcl-devel
579 (assert (null (ignore-errors
580                 (defmacro bug172 (&rest rest foo) `(list ,rest ,foo)))))
581
582 ;;; embedded THEs
583 (defun check-embedded-thes (policy1 policy2 x y)
584   (handler-case
585       (funcall (compile nil
586                         `(lambda (f)
587                            (declare (optimize (speed 2) (safety ,policy1)))
588                            (multiple-value-list
589                             (the (values (integer 2 3) t)
590                               (locally (declare (optimize (safety ,policy2)))
591                                 (the (values t (single-float 2f0 3f0))
592                                   (funcall f)))))))
593                (lambda () (values x y)))
594     (type-error (error)
595       error)))
596
597 (assert (equal (check-embedded-thes 0 0  :a :b) '(:a :b)))
598
599 (assert (equal (check-embedded-thes 0 3  :a 2.5f0) '(:a 2.5f0)))
600 (assert (typep (check-embedded-thes 0 3  2 3.5f0) 'type-error))
601
602 (assert (equal (check-embedded-thes 0 1  :a 3.5f0) '(:a 3.5f0)))
603 (assert (typep (check-embedded-thes 0 1  2 2.5d0) 'type-error))
604
605 #+nil
606 (assert (equal (check-embedded-thes 3 0  2 :a) '(2 :a)))
607 (assert (typep (check-embedded-thes 3 0  4 2.5f0) 'type-error))
608
609 (assert (equal (check-embedded-thes 1 0  4 :b) '(4 :b)))
610 (assert (typep (check-embedded-thes 1 0  1.0 2.5f0) 'type-error))
611
612
613 (assert (equal (check-embedded-thes 3 3  2 2.5f0) '(2 2.5f0)))
614 (assert (typep (check-embedded-thes 3 3  0 2.5f0) 'type-error))
615 (assert (typep (check-embedded-thes 3 3  2 3.5f0) 'type-error))
616
617 \f
618 ;;; INLINE inside MACROLET
619 (declaim (inline to-be-inlined))
620 (macrolet ((def (x) `(defun ,x (y) (+ y 1))))
621   (def to-be-inlined))
622 (defun call-inlined (z)
623   (to-be-inlined z))
624 (assert (= (call-inlined 3) 4))
625 (macrolet ((frob (x) `(+ ,x 3)))
626   (defun to-be-inlined (y)
627     (frob y)))
628 (assert (= (call-inlined 3)
629            ;; we should have inlined the previous definition, so the
630            ;; new one won't show up yet.
631            4))
632 (defun call-inlined (z)
633   (to-be-inlined z))
634 (assert (= (call-inlined 3) 6))
635 (defun to-be-inlined (y)
636   (+ y 5))
637 (assert (= (call-inlined 3) 6))
638 \f
639 ;;; DEFINE-COMPILER-MACRO to work as expected, not via weird magical
640 ;;; IR1 pseudo-:COMPILE-TOPLEVEL handling
641 (defvar *bug219-a-expanded-p* nil)
642 (defun bug219-a (x)
643   (+ x 1))
644 (define-compiler-macro bug219-a (&whole form y)
645   (setf *bug219-a-expanded-p* t)
646   (if (constantp y)
647       (+ (eval y) 2)
648       form))
649 (defun bug219-a-aux ()
650   (bug219-a 2))
651 (assert (= (bug219-a-aux)
652            (if *bug219-a-expanded-p* 4 3)))
653 (defvar *bug219-a-temp* 3)
654 (assert (= (bug219-a *bug219-a-temp*) 4))
655
656 (defvar *bug219-b-expanded-p* nil)
657 (defun bug219-b-aux1 (x)
658   (when x
659     (define-compiler-macro bug219-b (y)
660       (setf *bug219-b-expanded-p* t)
661       `(+ ,y 2))))
662 (defun bug219-b-aux2 (z)
663   (bug219-b z))
664 (assert (not *bug219-b-expanded-p*))
665 (assert (raises-error? (bug219-b-aux2 1) undefined-function))
666 (bug219-b-aux1 t)
667 (defun bug219-b-aux2 (z)
668   (bug219-b z))
669 (defun bug219-b (x)
670   x)
671 (assert (= (bug219-b-aux2 1)
672            (if *bug219-b-expanded-p* 3 1)))
673
674 ;;; bug 224: failure in unreachable code deletion
675 (defmacro do-optimizations (&body body)
676   `(dotimes (.speed. 4)
677      (dotimes (.space. 4)
678        (dotimes (.debug. 4)
679          (dotimes (.compilation-speed. 4)
680            (proclaim `(optimize (speed , .speed.) (space , .space.)
681                                 (debug , .debug.)
682                                 (compilation-speed , .compilation-speed.)))
683            ,@body)))))
684
685 (do-optimizations
686     (compile nil
687              (read-from-string
688               "(lambda () (#:localy (declare (optimize (safety 3)))
689                                     (ignore-errors (progn (values-list (car (list '(1 . 2)))) t))))")))
690
691 (do-optimizations
692     (compile nil '(lambda ()
693                    (labels ((ext ()
694                               (tagbody
695                                  (labels ((i1 () (list (i2) (i2)))
696                                           (i2 () (list (int) (i1)))
697                                           (int () (go :exit)))
698                                    (list (i1) (i1) (i1)))
699                                :exit (return-from ext)
700                                  )))
701                      (list (error "nih") (ext) (ext))))))
702
703 (do-optimizations
704   (compile nil '(lambda (x) (let ((y (error ""))) (list x y)))))
705
706 ;;; bug 223: invalid moving of global function name referencing
707 (defun bug223-int (n)
708   `(int ,n))
709
710 (defun bug223-wrap ()
711   (let ((old #'bug223-int))
712     (setf (fdefinition 'bug223-int)
713           (lambda (n)
714             (assert (> n 0))
715             `(ext ,@(funcall old (1- n)))))))
716 (compile 'bug223-wrap)
717
718 (assert (equal (bug223-int 4) '(int 4)))
719 (bug223-wrap)
720 (assert (equal (bug223-int 4) '(ext int 3)))
721 (bug223-wrap)
722 (assert (equal (bug223-int 4) '(ext ext int 2)))
723 \f
724 ;;; COERCE got its own DEFOPTIMIZER which has to reimplement most of
725 ;;; SPECIFIER-TYPE-NTH-ARG.  For a while, an illegal type would throw
726 ;;; you into the debugger on compilation.
727 (defun coerce-defopt (x)
728   ;; illegal, but should be compilable.
729   (coerce x '(values t)))
730 (assert (null (ignore-errors (coerce-defopt 3))))
731 \f
732 ;;; Oops.  In part of the (CATCH ..) implementation of DEBUG-RETURN,
733 ;;; it was possible to confuse the type deriver of the compiler
734 ;;; sufficiently that compiler invariants were broken (explained by
735 ;;; APD sbcl-devel 2003-01-11).
736
737 ;;; WHN's original report
738 (defun debug-return-catch-break1 ()
739   (with-open-file (s "/tmp/foo"
740                      :direction :output
741                      :element-type (list
742                                     'signed-byte
743                                     (1+
744                                      (integer-length most-positive-fixnum))))
745     (read-byte s)
746     (read-byte s)
747     (read-byte s)
748     (read-byte s)))
749
750 ;;; APD's simplified test case
751 (defun debug-return-catch-break2 (x)
752   (declare (type (vector (unsigned-byte 8)) x))
753   (setq *y* (the (unsigned-byte 8) (aref x 4))))
754 \f
755 ;;; FUNCTION-LAMBDA-EXPRESSION should return something that's COMPILE
756 ;;; can understand.  Here's a simple test for that on a function
757 ;;; that's likely to return a hairier list than just a lambda:
758 (macrolet ((def (fn) `(progn
759                        (declaim (inline ,fn))
760                        (defun ,fn (x) (1+ x)))))
761   (def bug228))
762 (let ((x (function-lambda-expression #'bug228)))
763   (when x
764     (assert (= (funcall (compile nil x) 1) 2))))
765
766 ;;; Bug reported by reported by rif on c.l.l 2003-03-05
767 (defun test-type-of-special-1 (x)
768   (declare (special x)
769            (fixnum x)
770            (optimize (safety 3)))
771   (list x))
772 (defun test-type-of-special-2 (x)
773   (declare (special x)
774            (fixnum x)
775            (optimize (safety 3)))
776   (list x (setq x (/ x 2)) x))
777 (assert (raises-error? (test-type-of-special-1 3/2) type-error))
778 (assert (raises-error? (test-type-of-special-2 3) type-error))
779 (assert (equal (test-type-of-special-2 8) '(8 4 4)))
780
781 ;;; bug which existed in 0.8alpha.0.4 for several milliseconds before
782 ;;; APD fixed it in 0.8alpha.0.5
783 (defun frob8alpha04 (x y)
784   (+ x y))
785 (defun baz8alpha04 (this kids)
786   (flet ((n-i (&rest rest)
787            ;; Removing the #+NIL here makes the bug go away.
788            #+nil (format t "~&in N-I REST=~S~%" rest)
789            (apply #'frob8alpha04 this rest)))
790     (n-i kids)))
791 ;;; failed in 0.8alpha.0.4 with "The value 13 is not of type LIST."
792 (assert (= (baz8alpha04 12 13) 25))
793 \f
794 ;;;; tests not in the problem domain, but of the consistency of the
795 ;;;; compiler machinery itself
796
797 (in-package "SB-C")
798
799 ;;; Hunt for wrong-looking things in fundamental compiler definitions,
800 ;;; and gripe about them.
801 ;;;
802 ;;; FIXME: It should be possible to (1) repair the things that this
803 ;;; code gripes about, and then (2) make the code signal errors
804 ;;; instead of just printing complaints to standard output, in order
805 ;;; to prevent the code from later falling back into disrepair.
806 (defun grovel-results (function)
807   (dolist (template (fun-info-templates (info :function :info function)))
808     (when (template-more-results-type template)
809       (format t "~&Template ~A has :MORE results, and translates ~A.~%"
810               (template-name template)
811               function)
812       (return nil))
813     (when (eq (template-result-types template) :conditional)
814       ;; dunno.
815       (return t))
816     (let ((types (template-result-types template))
817           (result-type (fun-type-returns (info :function :type function))))
818       (cond
819         ((values-type-p result-type)
820          (do ((ltypes (append (args-type-required result-type)
821                               (args-type-optional result-type))
822                       (rest ltypes))
823               (types types (rest types)))
824              ((null ltypes)
825               (unless (null types)
826                 (format t "~&More types than ltypes in ~A, translating ~A.~%"
827                         (template-name template)
828                         function)
829                 (return nil)))
830            (when (null types)
831              (unless (null ltypes)
832                (format t "~&More ltypes than types in ~A, translating ~A.~%"
833                        (template-name template)
834                        function)
835                (return nil)))))
836         ((eq result-type (specifier-type nil))
837          (unless (null types)
838            (format t "~&Template ~A returns values for function ~A with RESULT-TYPE NIL.~%"
839                    (template-name template)
840                    function)
841            (return nil)))
842         ((/= (length types) 1)
843          (format t "~&Template ~A isn't returning 1 value for ~A.~%"
844                  (template-name template)
845                  function)
846          (return nil))
847         (t t)))))
848 (defun identify-suspect-vops (&optional (env (first
849                                               (last *info-environment*))))
850   (do-info (env :class class :type type :name name :value value)
851     (when (and (eq class :function) (eq type :type))
852       ;; OK, so we have an entry in the INFO database. Now, if ...
853       (let* ((info (info :function :info name))
854              (templates (and info (fun-info-templates info))))
855         (when templates
856           ;; ... it has translators
857           (grovel-results name))))))
858 (identify-suspect-vops)
859 \f
860 ;;; success
861 (quit :unix-status 104)