Hoist tests from scan_weak_pointers() into scav_weak_pointer()
[sbcl.git] / tests / compiler.impure.lisp
blobabee4a711bcf4fd21d90a198b0ea43b3f8b2b92e
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.
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.
18 (in-package :cl-user)
20 ;; The tests in this file do not work under the legacy interpreter.
21 ;; They mostly do work in the fast interpreter, and are either harmless
22 ;; or actually reasonable things to test.
23 (when (and (eq sb-ext:*evaluator-mode* :interpret)
24 (not (member :sb-fasteval *features*)))
25 (sb-ext:exit :code 104))
27 (load "compiler-test-util.lisp")
28 (use-package "TEST-UTIL")
29 (use-package "ASSERTOID")
31 ;;; Old CMU CL code assumed that the names of "keyword" arguments are
32 ;;; necessarily self-evaluating symbols, but ANSI Common Lisp allows
33 ;;; them to be any symbols, not necessarily keywords, and thus not
34 ;;; necessarily self-evaluating. Make sure that this works.
35 (defun newfangled-cons (&key ((left-thing x)) ((right-thing y)))
36 (cons x y))
37 (with-test (:name (:lambda-list &key :self-evaluating))
38 (assert (equal (cons 1 2) (newfangled-cons 'right-thing 2 'left-thing 1))))
40 ;;; ANSI specifically says that duplicate keys are OK in lambda lists,
41 ;;; with no special exception for macro lambda lists. (As reported by
42 ;;; Pierre Mai on cmucl-imp 2001-03-30, Python didn't think so. The
43 ;;; rest of the thread had some entertainment value, at least for me
44 ;;; (WHN). The unbelievers were besmote and now even CMU CL will
45 ;;; conform to the spec in this regard. Who needs diplomacy when you
46 ;;; have brimstone?:-)
47 (defmacro ayup-duplicate-keys-are-ok-i-see-the-lite (&key k)
49 (with-test (:name (:macro :lambda-list :duplicate &key :arguments))
50 (assert (equal (funcall (checked-compile
51 '(lambda ()
52 (ayup-duplicate-keys-are-ok-i-see-the-lite
53 :k 112))))
54 112))
55 (assert (equal (funcall (checked-compile
56 '(lambda ()
57 (ayup-duplicate-keys-are-ok-i-see-the-lite
58 :k 'x :k 'y))))
59 'x)))
61 ;;; As reported by Alexey Dejneka (sbcl-devel 2002-01-30), in
62 ;;; sbcl-0.7.1 plus his patch (i.e. essentially sbcl-0.7.1.2), the
63 ;;; compiler barfed on this, blowing up in FIND-IN-PHYSENV looking for
64 ;;; the LAMBDA-VAR named NUM. That was fixed in sbcl-0.7.1.3.
65 (defun parse-num (index)
66 (let (num x)
67 (flet ((digs ()
68 (setq num index))
69 (z ()
70 (let ()
71 (setq x nil))))
72 (when (and (digs) (digs)) x))))
74 ;;; Bug 132: The compiler used to fail to compile INTEGER-valued CATCH
75 ;;; tags. This was fixed by Alexey Dejneka in sbcl-0.7.1.14. (INTEGER
76 ;;; catch tags are still a bad idea because EQ is used to compare
77 ;;; tags, and EQ comparison on INTEGERs is unportable; but now it's a
78 ;;; compiler warning instead of a failure to compile.)
79 (defun foo ()
80 (catch 0 (print 1331)))
82 ;;; Bug 150: In sbcl-0.7.1.15, compiling this code caused a failure in
83 ;;; SB-C::ADD-TEST-CONSTRAINTS:
84 ;;; The value NIL is not of type SB-C::CONTINUATION.
85 ;;; This bug was fixed by APD in sbcl-0.7.1.30.
86 (defun bug150-test1 ()
87 (let* ()
88 (flet ((wufn () (glorp table1 4.9)))
89 (gleep *uustk* #'wufn "#1" (list)))
90 (if (eql (lo foomax 3.2))
91 (values)
92 (error "not ~S" '(eql (lo foomax 3.2))))
93 (values)))
94 ;;; A simpler test case for bug 150: The compiler died with the
95 ;;; same type error when trying to compile this.
96 (defun bug150-test2 ()
97 (let ()
98 (<)))
100 ;;; bug 147, fixed by APD 2002-04-28
102 ;;; This test case used to crash the compiler, e.g. with
103 ;;; failed AVER: "(= (LENGTH (BLOCK-SUCC CALL-BLOCK)) 1)"
104 (defun bug147 (string ind)
105 (flet ((digs ()
106 (let (old-index)
107 (if (and (< ind ind)
108 (typep (char string ind) '(member #\1)))
109 nil))))))
111 ;;; bug reported and fixed by Matthias Hoelzl sbcl-devel 2002-05-13
112 (defmacro foo-2002-05-13 () ''x)
113 (eval '(foo-2002-05-13))
114 (compile 'foo-2002-05-13)
115 (foo-2002-05-13) ; (The bug caused UNDEFINED-FUNCTION to be signalled here.)
117 ;;; floating point pain on the PPC.
119 ;;; This test case used to fail to compile on most powerpcs prior to
120 ;;; sbcl-0.7.4.2x, as floating point traps were being incorrectly
121 ;;; masked.
122 (defun floating-point-pain (x)
123 (declare (single-float x))
124 (log x))
126 ;;; bug found and fixed ca. sbcl-0.7.5.12: The INTERSECTION-TYPE
127 ;;; here satisfies "is a subtype of ARRAY-TYPE", but can't be
128 ;;; accessed with ARRAY-TYPE accessors like
129 ;;; ARRAY-TYPE-SPECIALIZED-ELEMENT-TYPE, so ARRAY-related
130 ;;; DEFTRANSFORMs died with TYPE-ERROR at compile time when
131 ;;; compiling the DEFUN here.
132 (defun stupid-input-to-smart-array-deftransforms-0-7-5-12 (v)
133 (declare (type (and simple-vector fwd-type-ref) v))
134 (aref v 0))
136 ;;; Ca. sbcl-0.7.5.15 the compiler would fail an internal consistency
137 ;;; check on this code because it expected all calls to %INSTANCE-REF
138 ;;; to be transformed away, but its expectations were dashed by perverse
139 ;;; code containing app programmer errors like this.
140 (defstruct something-known-to-be-a-struct x y)
141 (with-test (:name (compile defstruct :instance-ref))
142 (assert (nth-value
143 1 (checked-compile
144 ;; Python sees the structure accessor on the
145 ;; known-not-to-be-a-struct A5 value and is very, very
146 ;; disappointed in you. (But it doesn't signal BUG any
147 ;; more.)
148 `(lambda ()
149 (labels ((a1 (a2 a3)
150 (cond (t (a4 a2 a3))))
151 (a4 (a2 a3 a5 a6)
152 (declare (type (or simple-vector null) a5 a6))
153 (something-known-to-be-a-struct-x a5))
154 (a8 (a2 a3)
155 (a9 #'a1 a10 a2 a3))
156 (a11 (a2 a3)
157 (cond ((and (funcall a12 a2)
158 (funcall a12 a3))
159 (funcall a13 a2 a3))
161 (when a14
162 (let ((a15 (a1 a2 a3)))
164 a16))))
165 (values #'a17 #'a11)))
166 :allow-failure t :allow-warnings t :allow-style-warnings t))))
168 ;;; On the SPARC, there was an erroneous definition of some VOPs used
169 ;;; to compile LOGANDs, which would lead to compilation of the
170 ;;; following function giving rise to a compile-time error (bug
171 ;;; spotted and fixed by Raymond Toy for CMUCL)
172 (defun logand-sparc-bogons (a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10)
173 (declare (type (unsigned-byte 32) a0)
174 (type (signed-byte 32) a1 a2 a3 a4 a5 a6 a7 a8 a9 a10)
175 ;; to ensure that the call is a candidate for
176 ;; transformation
177 (optimize (speed 3) (safety 0) (compilation-speed 0) (debug 0)))
178 (values
179 ;; the call that fails compilation
180 (logand a0 a10)
181 ;; a call to prevent the other arguments from being optimized away
182 (logand a1 a2 a3 a4 a5 a6 a7 a8 a9)))
184 ;;; bug 192, reported by Einar Floystad Dorum sbcl-devel 2002-08-14,
185 ;;; fixed in sbcl-0.7.6.26: Compiling this function in 0.7.6 caused
186 ;;; the compiler to try to constant-fold DATA-VECTOR-REF, which is OK,
187 ;;; except that there was no non-VOP definition of DATA-VECTOR-REF, so
188 ;;; it would fail.
189 (defun bug192 ()
190 (funcall
191 (LAMBDA (TEXT I L )
192 (LABELS ((G908 (I)
193 (LET ((INDEX
195 (IF (= I L)
197 (LET ((S TEXT)
198 (E (ELT TEXT I)))
199 (DECLARE (IGNORABLE S E))
200 (WHEN (EQL #\a E)
201 (G909 (1+ I))))))))
202 INDEX))
203 (G909 (I)
205 (IF (= I L)
207 (LET ((S TEXT)
208 (E (ELT TEXT I)))
209 (DECLARE (IGNORABLE S E))
210 (WHEN (EQL #\b E) (G910 (1+ I)))))))
211 (G910 (I)
212 (LET ((INDEX
214 (IF NIL
216 (LET ((S TEXT))
217 (DECLARE (IGNORABLE S))
218 (WHEN T I))))))
219 INDEX)))
220 (G908 I))) "abcdefg" 0 (length "abcdefg")))
222 ;;; bugs #65, #70, and #109, closed by APD's patch sbcl-devel 2002-08-17
224 ;;; This was "YA code deletion bug" whose symptom was the failure of
225 ;;; the assertion
226 ;;; (EQ (C::LAMBDA-TAIL-SET C::CALLER)
227 ;;; (C::LAMBDA-TAIL-SET (C::LAMBDA-HOME C::CALLEE)))
228 ;;; at compile time.
229 (defun bug65-1 (termx termy) ; from Carl Witty on submit bugs list, debian.org
230 (labels
231 ((alpha-equal-bound-term-lists (listx listy)
232 (or (and (null listx) (null listy))
233 (and listx listy
234 (let ((bindings-x (bindings-of-bound-term (car listx)))
235 (bindings-y (bindings-of-bound-term (car listy))))
236 (if (and (null bindings-x) (null bindings-y))
237 (alpha-equal-terms (term-of-bound-term (car listx))
238 (term-of-bound-term (car listy)))
239 (and (= (length bindings-x) (length bindings-y))
240 (prog2
241 (enter-binding-pairs (bindings-of-bound-term (car listx))
242 (bindings-of-bound-term (car listy)))
243 (alpha-equal-terms (term-of-bound-term (car listx))
244 (term-of-bound-term (car listy)))
245 (exit-binding-pairs (bindings-of-bound-term (car listx))
246 (bindings-of-bound-term (car listy)))))))
247 (alpha-equal-bound-term-lists (cdr listx) (cdr listy)))))
249 (alpha-equal-terms (termx termy)
250 (if (and (variable-p termx)
251 (variable-p termy))
252 (equal-bindings (id-of-variable-term termx)
253 (id-of-variable-term termy))
254 (and (equal-operators-p (operator-of-term termx) (operator-of-term termy))
255 (alpha-equal-bound-term-lists (bound-terms-of-term termx)
256 (bound-terms-of-term termy))))))
258 (or (eq termx termy)
259 (and termx termy
260 (with-variable-invocation (alpha-equal-terms termx termy))))))
261 (defun bug65-2 () ; from Bob Rogers cmucl-imp 1999-07-28
262 ;; Given an FSSP alignment file named by the argument . . .
263 (labels ((get-fssp-char ()
264 (get-fssp-char))
265 (read-fssp-char ()
266 (get-fssp-char)))
267 ;; Stub body, enough to tickle the bug.
268 (list (read-fssp-char)
269 (read-fssp-char))))
270 (defun bug70 ; from David Young cmucl-help 30 Nov 2000
271 (item sequence &key (test #'eql))
272 (labels ((find-item (obj seq test &optional (val nil))
273 (let ((item (first seq)))
274 (cond ((null seq)
275 (values nil nil))
276 ((funcall test obj item)
277 (values val seq))
279 (find-item obj
280 (rest seq)
281 test
282 (nconc val `(,item))))))))
283 (find-item item sequence test)))
284 (defun bug109 () ; originally from CMU CL bugs collection, reported as
285 ; SBCL bug by MNA 2001-06-25
286 (labels
287 ((eff (&key trouble)
288 (eff)
289 ;; nil
290 ;; Uncomment and it works
292 (eff)))
294 ;;; bug 192a, fixed by APD "more strict type checking" patch
295 ;;; (sbcl-devel 2002-08-07)
296 (defun bug192a (x)
297 (declare (optimize (speed 0) (safety 3)))
298 ;; Even with bug 192a, this declaration was checked as an assertion.
299 (declare (real x))
300 (+ x
301 (locally
302 ;; Because of bug 192a, this declaration was trusted without checking.
303 (declare (single-float x))
304 (sin x))))
305 (with-test (:name (declare type :bug-192a))
306 (assert-error (bug192a nil) type-error)
307 (multiple-value-bind (result error) (ignore-errors (bug192a 100))
308 (assert (null result))
309 (assert (equal (type-error-expected-type error) 'single-float))))
311 ;;; bug 194, fixed in part by APD "more strict type checking" patch
312 ;;; (sbcl-devel 2002-08-07)
313 (with-test (:name (the :bug-194))
314 (assert-error (multiple-value-prog1 (progn (the real '(1 2 3)))) type-error)
315 (assert-error (the real '(1 2 3)) type-error))
317 (defun bug194d ()
318 (null (ignore-errors
319 (let ((arg1 1)
320 (arg2 (identity (the real #(1 2 3)))))
321 (if (< arg1 arg2) arg1 arg2)))))
322 (with-test (:name (the :bug-194d))
323 (assert (eq (bug194d) t)))
326 ;;; BUG 48a. and b. (symbol-macrolet handling), fixed by Eric Marsden
327 ;;; and Raymond Toy for CMUCL, fix ported for sbcl-0.7.6.18.
328 (flet ((test (form)
329 (multiple-value-bind (function failure-p)
330 (checked-compile form :allow-failure t)
331 (assert failure-p)
332 (assert-error (funcall function) program-error))))
334 (with-test (:name (compile symbol-macrolet :bug-48a))
335 (test `(lambda ()
336 ;; not interested in the package lock violation here
337 (declare (sb-ext:disable-package-locks t))
338 (symbol-macrolet ((t nil)) t))))
340 (with-test (:name (compile symbol-macrolet :bug-48b))
341 (test `(lambda ()
342 ;; not interested in the package lock violation here
343 (declare (sb-ext:disable-package-locks *standard-input*))
344 (symbol-macrolet ((*standard-input* nil))
345 *standard-input*))))
347 (with-test (:name (compile symbol-macrolet :bug-48c))
348 (test `(lambda ()
349 (symbol-macrolet ((s nil)) (declare (special s)) s)))))
352 ;;; bug 120a: Turned out to be constraining code looking like (if foo
353 ;;; <X> <X>) where <X> was optimized by the compiler to be the exact
354 ;;; same block in both cases, but not turned into (PROGN FOO <X>).
355 ;;; Fixed by APD in sbcl-0.7.7.2, who provided this test:
356 (declaim (inline dont-constrain-if-too-much))
357 (defun dont-constrain-if-too-much (frame up-frame)
358 (declare (optimize (speed 3) (safety 1) (debug 1)))
359 (if (or (not frame) t)
360 frame
361 "bar"))
362 (defun dont-constrain-if-too-much-aux (x y)
363 (declare (optimize (speed 3) (safety 1) (debug 1)))
364 (if x t (if y t (dont-constrain-if-too-much x y))))
366 (with-test (:name (if :constraint-propagation :bug-120a))
367 (assert (null (dont-constrain-if-too-much-aux nil nil))))
369 ;;; TYPE-ERROR confusion ca. sbcl-0.7.7.24, reported and fixed by
370 ;;; APD sbcl-devel 2002-09-14
371 (defun exercise-0-7-7-24-bug (x)
372 (declare (integer x))
373 (let (y)
374 (setf y (the single-float (if (> x 0) x 3f0)))
375 (list y y)))
376 (with-test (:name (type-error :0.7.7.24))
377 (assert-error (exercise-0-7-7-24-bug 4) type-error)
378 (assert (equal (exercise-0-7-7-24-bug -4) '(3f0 3f0))))
380 ;;; non-intersecting type declarations were DWIMing in a confusing
381 ;;; fashion until sbcl-0.7.7.28, when APD reported and fixed the
382 ;;; problem.
383 (defun non-intersecting-the (x)
384 (let (y)
385 (setf y (the single-float (the integer x)))
386 (list y y)))
388 (with-test (:name :non-intersecting-the)
389 (assert-error (non-intersecting-the 3) type-error)
390 (assert-error (non-intersecting-the 3f0) type-error))
392 ;;; until 0.8.2 SBCL did not check THEs in arguments
393 (defun the-in-arguments-aux (x)
395 (defun the-in-arguments-1 (x)
396 (list x (the-in-arguments-aux (the (single-float 0s0) x))))
397 (defun the-in-arguments-2 (x)
398 (list x (the-in-arguments-aux (the single-float x))))
400 (with-test (:name (the :argument))
401 (assert-error (the-in-arguments-1 1) type-error)
402 (assert-error (the-in-arguments-2 1) type-error))
404 ;;; bug 153: a hole in a structure slot type checking
405 (declaim (optimize safety))
406 (defstruct foo153
407 (bla 0 :type fixnum))
408 (defun bug153-1 ()
409 (let ((foo (make-foo153)))
410 (setf (foo153-bla foo) '(1 . 1))
411 (format t "Is ~a of type ~a a cons? => ~a~%"
412 (foo153-bla foo)
413 (type-of (foo153-bla foo))
414 (consp (foo153-bla foo)))))
415 (defun bug153-2 (x)
416 (let ((foo (make-foo153)))
417 (setf (foo153-bla foo) x)
418 (format t "Is ~a of type ~a a cons? => ~a~%"
419 (foo153-bla foo)
420 (type-of (foo153-bla foo))
421 (consp (foo153-bla foo)))))
423 (with-test (:name (:name defstruct :slot :type :bug-153))
424 (assert-error (bug153-1) type-error)
425 (assert-error (bug153-2 '(1 . 1)) type-error))
427 ;;;; bug 110: the compiler flushed the argument type test and the default
428 ;;;; case in the cond.
430 ;(locally (declare (optimize (safety 3) (speed 2)))
431 ; (defun bug110 (x)
432 ; (declare (optimize (safety 2) (speed 3)))
433 ; (declare (type (or string stream) x))
434 ; (cond ((typep x 'string) 'string)
435 ; ((typep x 'stream) 'stream)
436 ; (t
437 ; 'none))))
439 ;(multiple-value-bind (result condition)
440 ; (ignore-errors (bug110 0))
441 ; (declare (ignore result))
442 ; (assert (typep condition 'type-error)))
444 ;;; bug 202: the compiler failed to compile a function, which derived
445 ;;; type contradicted declared.
446 (declaim (ftype (function () null) bug202))
447 (defun bug202 ()
450 ;;; bugs 178, 199: compiler failed to compile a call of a function
451 ;;; with a hairy type
452 (defun bug178 (x)
453 (funcall (the function (the standard-object x))))
455 (defun bug199-aux (f)
456 (eq nil (funcall f)))
458 (defun bug199 (f x)
459 (declare (type (and function (satisfies bug199-aux)) f))
460 (funcall f x))
462 (with-test (:name (declaim &optional &rest :bogus style-warning))
463 (assert-no-signal
464 (ctu:file-compile
465 "(declaim (ftype (function (symbol &optional t &rest t)) foo))
466 (defun foo (x &optional y &rest z)
467 (declare (ignore x y z)))"
468 :load nil)
469 style-warning))
471 ;;; check non-toplevel DEFMACRO
472 (defvar *defmacro-test-status* nil)
474 (with-test (:name (compile-file defmacro))
475 (fmakunbound 'defmacro-test-aux)
476 (let* ((src "defmacro-test.lisp")
477 (obj (compile-file-pathname src)))
478 (unwind-protect
479 (progn
480 (compile-file src)
481 (assert (equal *defmacro-test-status* '(function a)))
482 (setq *defmacro-test-status* nil)
483 (load obj)
484 (assert (equal *defmacro-test-status* nil))
485 (macroexpand '(defmacro-test-aux 'a))
486 (assert (equal *defmacro-test-status* '(macro 'a z-value)))
487 (eval '(defmacro-test-aux 'a))
488 (assert (equal *defmacro-test-status* '(expanded 'a z-value))))
489 (ignore-errors (delete-file obj)))))
491 ;;; bug 204: EVAL-WHEN inside a local environment
492 (defvar *bug204-test-status*)
494 (with-test (:name (compile-file eval-when :local-environment :bug-204))
495 (let* ((src "bug204-test.lisp")
496 (obj (compile-file-pathname src)))
497 (unwind-protect
498 (progn
499 (setq *bug204-test-status* nil)
500 (compile-file src)
501 (assert (equal *bug204-test-status* '((:expanded :load-toplevel)
502 (:called :compile-toplevel)
503 (:expanded :compile-toplevel))))
504 (setq *bug204-test-status* nil)
505 (load obj)
506 (assert (equal *bug204-test-status* '((:called :load-toplevel)))))
507 (ignore-errors (delete-file obj)))))
509 ;;; toplevel SYMBOL-MACROLET
510 ;;; FIXME: I didn't look into why this fails in the interpreter, but it does.
511 (defvar *symbol-macrolet-test-status*)
513 (with-test (:name (compile-file symbol-macrolet) :skipped-on :interpreter)
514 (let* ((src "symbol-macrolet-test.lisp")
515 (obj (compile-file-pathname src)))
516 (unwind-protect
517 (progn
518 (setq *symbol-macrolet-test-status* nil)
519 (compile-file src)
520 (assert (equal *symbol-macrolet-test-status*
521 '(2 1)))
522 (setq *symbol-macrolet-test-status* nil)
523 (load obj)
524 (assert (equal *symbol-macrolet-test-status* '(2))))
525 (ignore-errors (delete-file obj)))))
527 ;;; On the x86, this code failed to compile until sbcl-0.7.8.37:
528 (defun x86-assembler-failure (x)
529 (declare (optimize (speed 3) (safety 0)))
530 (eq (setf (car x) 'a) nil))
532 ;;; bug 211: :ALLOW-OTHER-KEYS
533 (defun bug211d (&key (x :x x-p) ((:allow-other-keys y) :y y-p))
534 (list x x-p y y-p))
536 (with-test (:name (:allow-other-keys :bug-211d))
537 (assert (equal (bug211d) '(:x nil :y nil)))
538 (assert (equal (bug211d :x 1) '(1 t :y nil)))
539 (assert-error (bug211d :y 2) program-error)
540 (assert (equal (bug211d :y 2 :allow-other-keys t :allow-other-keys nil)
541 '(:x nil t t)))
542 (assert-error (bug211d :y 2 :allow-other-keys nil) program-error))
544 (checked-compile
545 '(lambda ()
546 (flet ((test (&key (x :x x-p) ((:allow-other-keys y) :y y-p))
547 (list x x-p y y-p)))
548 (assert (equal (test) '(:x nil :y nil)))
549 (assert (equal (test :x 1) '(1 t :y nil)))
550 (assert (equal (test :y 2 :allow-other-keys 11 :allow-other-keys nil)
551 '(:x nil 11 t)))))
552 :name 'bug211b)
553 (with-test (:name (compile :lambda-list :bug-211b))
554 (bug211b))
556 (with-test (:name (compile :lambda-list :bug-211c))
557 (let ((fun (checked-compile
558 '(lambda ()
559 (flet ((test (&key (x :x x-p))
560 (list x x-p)))
561 (assert (equal (test) '(:x nil)))
562 (assert (equal (test :x 1) '(1 t)))
563 (assert (equal (test :y 2 :allow-other-keys 11 :allow-other-keys nil)
564 '(:x nil))))))))
565 (funcall fun)))
567 (with-test (:name (compile :lambda-list :allow-other-keys
568 :bug-211 :do-not-allow))
569 (dolist (form '((test :y 2)
570 (test :y 2 :allow-other-keys nil)
571 (test :y 2 :allow-other-keys nil :allow-other-keys t)))
572 (multiple-value-bind (result failure-p)
573 (checked-compile
574 `(lambda ()
575 (flet ((test (&key (x :x x-p) ((:allow-other-keys y) :y y-p))
576 (list x x-p y y-p)))
577 ,form))
578 :allow-failure t :allow-warnings t)
579 (assert failure-p)
580 (assert-error (funcall result) program-error))))
582 ;;; bug 217: wrong type inference
583 (defun bug217-1 (x s)
584 (let ((f (etypecase x
585 (character #'write-char)
586 (integer #'write-byte))))
587 (funcall f x s)
588 (etypecase x
589 (character (write-char x s))
590 (integer (write-byte x s)))))
591 (with-test (:name (:bug-217))
592 (bug217-1 #\1 (make-broadcast-stream)))
594 ;;; bug 221: tried and died on CSUBTYPEP (not VALUES-SUBTYPEP) of the
595 ;;; function return types when inferring the type of the IF expression
596 (declaim (ftype (function (fixnum) (values package boolean)) bug221f1))
597 (declaim (ftype (function (t) (values package boolean)) bug221f2))
598 (defun bug221 (b x)
599 (funcall (if b #'bug221f1 #'bug221f2) x))
601 ;;; bug 172: macro lambda lists were too permissive until 0.7.9.28
602 ;;; (fix provided by Matthew Danish) on sbcl-devel
603 (with-test (:name (defmacro :lambda-list :bug-172))
604 (assert-error (defmacro bug172 (&rest rest foo) `(list ,rest ,foo))))
606 (with-test (:name (compile :embedded the))
607 (flet ((check-embedded-thes (policy1 policy2 x y)
608 (handler-case
609 (funcall (checked-compile
610 `(lambda (f)
611 (declare (optimize (speed 2) (safety ,policy1)))
612 (multiple-value-list
613 (the (values (integer 2 3) t &optional)
614 (locally (declare (optimize (safety ,policy2)))
615 (the (values t (single-float 2f0 3f0) &optional)
616 (funcall f)))))))
617 (lambda () (values x y)))
618 (type-error (error)
619 error))))
621 (assert (equal (check-embedded-thes 0 0 :a :b) '(:a :b)))
623 (assert (equal (check-embedded-thes 0 3 :a 2.5f0) '(:a 2.5f0)))
624 (assert (typep (check-embedded-thes 0 3 2 3.5f0) 'type-error))
626 (assert (equal (check-embedded-thes 0 1 :a 3.5f0) '(:a 3.5f0)))
627 (assert (typep (check-embedded-thes 0 1 2 2.5d0) 'type-error))
629 (assert (equal (check-embedded-thes 3 0 2 :a) '(2 :a)))
630 (assert (typep (check-embedded-thes 3 0 4 2.5f0) 'type-error))
632 (assert (equal (check-embedded-thes 1 0 3 :b) '(3 :b)))
633 (assert (typep (check-embedded-thes 1 0 1.0 2.5f0) 'type-error))
636 (assert (equal (check-embedded-thes 3 3 2 2.5f0) '(2 2.5f0)))
637 (assert (typep (check-embedded-thes 3 3 0 2.5f0) 'type-error))
638 (assert (typep (check-embedded-thes 3 3 2 3.5f0) 'type-error))))
641 ;;; INLINE inside MACROLET
642 (declaim (inline to-be-inlined))
643 (macrolet ((def (x) `(defun ,x (y) (+ y 1))))
644 (def to-be-inlined))
645 (defun call-inlined (z)
646 (to-be-inlined z))
647 (assert (= (call-inlined 3) 4))
648 (macrolet ((frob (x) `(+ ,x 3)))
649 (defun to-be-inlined (y)
650 (frob y)))
651 #-interpreter
652 (assert (= (call-inlined 3)
653 ;; we should have inlined the previous definition, so the
654 ;; new one won't show up yet.
656 (defun call-inlined (z)
657 (to-be-inlined z))
658 #-interpreter (assert (= (call-inlined 3) 6))
659 (defun to-be-inlined (y)
660 (+ y 5))
661 #-interpreter (assert (= (call-inlined 3) 6))
663 ;;; DEFINE-COMPILER-MACRO to work as expected, not via weird magical
664 ;;; IR1 pseudo-:COMPILE-TOPLEVEL handling
665 (defvar *bug219-a-expanded-p* nil)
666 (defun bug219-a (x)
667 (+ x 1))
668 (define-compiler-macro bug219-a (&whole form y)
669 (setf *bug219-a-expanded-p* t)
670 (if (constantp y)
671 (+ (eval y) 2)
672 form))
673 (defun bug219-a-aux ()
674 (bug219-a 2))
675 (defvar *bug219-a-temp* 3)
676 (with-test (:name (define-compiler-macro :bug-219a))
677 (assert (= (bug219-a-aux) (if *bug219-a-expanded-p* 4 3)))
678 (assert (= (bug219-a *bug219-a-temp*) 4)))
680 (defvar *bug219-b-expanded-p* nil)
681 (defun bug219-b-aux1 (x)
682 (when x
683 (define-compiler-macro bug219-b (y)
684 (setf *bug219-b-expanded-p* t)
685 `(+ ,y 2))))
686 (defun bug219-b-aux2 (z)
687 (bug219-b z))
688 (assert (not *bug219-b-expanded-p*))
689 (assert-error (bug219-b-aux2 1) undefined-function)
690 (bug219-b-aux1 t)
691 (defun bug219-b-aux2 (z)
692 (bug219-b z))
693 (defun bug219-b (x)
695 (assert (= (bug219-b-aux2 1)
696 (if *bug219-b-expanded-p* 3 1)))
698 ;;; bug 224: failure in unreachable code deletion
699 (defmacro do-optimizations (&body body)
700 `(dotimes (.speed. 4)
701 (dotimes (.space. 4)
702 (dotimes (.debug. 4)
703 (dotimes (.compilation-speed. 4)
704 (proclaim `(optimize (speed , .speed.) (space , .space.)
705 (debug , .debug.)
706 (compilation-speed , .compilation-speed.)))
707 ,@body)))))
709 (with-test (:name (:unreachable-code locally declare :bug-224))
710 (do-optimizations
711 (checked-compile
712 (read-from-string
713 "(lambda ()
714 (#:localy (declare (optimize (safety 3)))
715 (ignore-errors (progn (values-list (car (list '(1 . 2)))) t))))")
716 :allow-failure t :allow-style-warnings t)))
718 (with-test (:name (:unreachable-code error labels :bug-224))
719 (do-optimizations
720 (checked-compile
721 '(lambda ()
722 (labels ((ext ()
723 (tagbody
724 (labels ((i1 () (list (i2) (i2)))
725 (i2 () (list (int) (i1)))
726 (int () (go :exit)))
727 (list (i1) (i1) (i1)))
728 :exit (return-from ext))))
729 (list (error "nih") (ext) (ext)))))))
731 (with-test (:name (:unreachable-code error let :bug-224))
732 (do-optimizations
733 (checked-compile '(lambda (x) (let ((y (error ""))) (list x y))))))
735 ;;; bug 223: invalid moving of global function name referencing
736 (defun bug223-int (n)
737 `(int ,n))
739 (defun bug223-wrap ()
740 (let ((old #'bug223-int))
741 (setf (fdefinition 'bug223-int)
742 (lambda (n)
743 (assert (> n 0))
744 `(ext ,@(funcall old (1- n)))))))
746 (with-test (:name (compile setf fdefinition :bug-223))
747 (compile 'bug223-wrap)
748 (assert (equal (bug223-int 4) '(int 4)))
749 (bug223-wrap)
750 (assert (equal (bug223-int 4) '(ext int 3)))
751 (bug223-wrap)
752 (assert (equal (bug223-int 4) '(ext ext int 2))))
754 ;;; COERCE got its own DEFOPTIMIZER which has to reimplement most of
755 ;;; SPECIFIER-TYPE-NTH-ARG. For a while, an illegal type would throw
756 ;;; you into the debugger on compilation.
757 (defun coerce-defopt1 (x)
758 ;; illegal, but should be compilable.
759 (coerce x '(values t)))
760 (defun coerce-defopt2 (x)
761 ;; illegal, but should be compilable.
762 (coerce x '(values t &optional)))
763 (with-test (:name (coerce :optimizer))
764 (assert-error (coerce-defopt1 3))
765 (assert-error (coerce-defopt2 3)))
767 ;;; Oops. In part of the (CATCH ..) implementation of DEBUG-RETURN,
768 ;;; it was possible to confuse the type deriver of the compiler
769 ;;; sufficiently that compiler invariants were broken (explained by
770 ;;; APD sbcl-devel 2003-01-11).
772 ;;; WHN's original report
773 (defun debug-return-catch-break1 ()
774 (with-open-file (s "/tmp/foo"
775 :direction :output
776 :element-type (list
777 'signed-byte
779 (integer-length most-positive-fixnum))))
780 (read-byte s)
781 (read-byte s)
782 (read-byte s)
783 (read-byte s)))
785 ;;; APD's simplified test case
786 (defun debug-return-catch-break2 (x)
787 (declare (type (vector (unsigned-byte 8)) x))
788 (setq *y* (the (unsigned-byte 8) (aref x 4))))
790 ;;; FUNCTION-LAMBDA-EXPRESSION should return something that COMPILE
791 ;;; can understand. Here's a simple test for that on a function
792 ;;; that's likely to return a hairier list than just a lambda:
793 (macrolet ((def (fn) `(progn
794 (declaim (inline ,fn))
795 (defun ,fn (x) (1+ x)))))
796 (def bug228))
797 (with-test (:name (function-lambda-expression compile :bug-228))
798 (let ((x (function-lambda-expression #'bug228)))
799 (when x
800 (assert (= (funcall (checked-compile x) 1) 2)))))
803 (defun bug192b (i)
804 (dotimes (j i)
805 (declare (type (mod 4) i))
806 (unless (< i 5)
807 (print j))))
808 (with-test (:name :bug-192b)
809 (assert-error (bug192b 6) type-error))
811 (defun bug192c (x y)
812 (locally (declare (type fixnum x y))
813 (+ x (* 2 y))))
814 (with-test (:name :bug-192c)
815 (assert-error (bug192c 1.1 2) type-error)
816 (assert-error (progn (the real (list 1)) t) type-error))
818 (defun bug236 (a f)
819 (declare (optimize (speed 2) (safety 0)))
820 (+ 1d0
821 (the double-float
822 (multiple-value-prog1
823 (svref a 0)
824 (unless f (return-from bug236 0))))))
825 (with-test (:name :bug-236)
826 (assert (eql (bug236 #(4) nil) 0)))
828 ;;; Bug reported by reported by rif on c.l.l 2003-03-05
829 (defun test-type-of-special-1 (x)
830 (declare (special x)
831 (fixnum x)
832 (optimize (safety 3)))
833 (list x))
834 (defun test-type-of-special-2 (x)
835 (declare (special x)
836 (fixnum x)
837 (optimize (safety 3)))
838 (list x (setq x (/ x 2)) x))
839 (with-test (:name (declare special type))
840 (assert-error (test-type-of-special-1 3/2) type-error)
841 (assert-error (test-type-of-special-2 3) type-error)
842 (assert (equal (test-type-of-special-2 8) '(8 4 4))))
844 ;;; bug which existed in 0.8alpha.0.4 for several milliseconds before
845 ;;; APD fixed it in 0.8alpha.0.5
846 (defun frob8alpha04 (x y)
847 (+ x y))
848 (defun baz8alpha04 (this kids)
849 (flet ((n-i (&rest rest)
850 ;; Removing the #+NIL here makes the bug go away.
851 #+nil (format t "~&in N-I REST=~S~%" rest)
852 (apply #'frob8alpha04 this rest)))
853 (n-i kids)))
854 ;;; failed in 0.8alpha.0.4 with "The value 13 is not of type LIST."
855 (with-test (:name (&rest type-error))
856 (assert (= (baz8alpha04 12 13) 25)))
858 ;;; evaluation order in structure slot writers
859 (defstruct sswo
860 a b)
861 (with-test (:name (:evaluation-order structure :slot-writer))
862 (let* ((i 0)
863 (s (make-sswo :a (incf i) :b (incf i)))
864 (l (list s :v)))
865 (assert (= (sswo-a s) 1))
866 (assert (= (sswo-b s) 2))
867 (setf (sswo-a (pop l)) (pop l))
868 (assert (eq l nil))
869 (assert (eq (sswo-a s) :v))))
871 (defun bug249 (x)
872 (flet ((bar (y)
873 (declare (fixnum y))
874 (incf x)))
875 (list (bar x) (bar x) (bar x))))
877 (with-test (:name (flet declare type :bug-249))
878 (assert-error (bug249 1.0) type-error))
880 ;;; bug reported by ohler on #lisp 2003-07-10
881 (defun bug-ohler-2003-07-10 (a b)
882 (declare (optimize (speed 0) (safety 3) (space 0)
883 (debug 1) (compilation-speed 0)))
884 (adjoin a b))
886 ;;; bug reported by Doug McNaught on sbcl-devel 2003-09-14:
887 ;;; COMPILE-FILE did not bind *READTABLE*
888 (with-test (:name (compile-file *readtable*))
889 (let* ((source "bug-doug-mcnaught-20030914.lisp")
890 (fasl (compile-file-pathname source)))
891 (labels ((check ()
892 (assert (null (get-macro-character #\]))))
893 (full-check ()
894 (check)
895 (assert (typep *bug-doug-mcnaught-20030914*
896 '(simple-array (unsigned-byte 4) (*))))
897 (assert (equalp *bug-doug-mcnaught-20030914* #(1 2 3)))
898 (makunbound '*bug-doug-mcnaught-20030914*)))
899 (compile-file source)
900 (check)
901 (load fasl)
902 (full-check)
903 (load source)
904 (full-check)
905 (delete-file fasl))))
907 (defun expt-derive-type-bug (a b)
908 (unless (< a b)
909 (truncate (expt a b))))
910 (with-test (:name (expt :derive-type))
911 (assert (equal (multiple-value-list (expt-derive-type-bug 1 1))
912 '(1 0))))
914 ;;; Problems with type checking in functions with EXPLICIT-CHECK
915 ;;; attribute (reported by Peter Graves)
916 (with-test (:name (:type-checking :explicit-check))
917 (loop for (fun . args) in '((= a) (/= a)
918 (< a) (<= a) (> a) (>= a))
919 do (assert-error (apply fun args) type-error)))
921 (defclass broken-input-stream (sb-gray:fundamental-input-stream) ())
922 (defmethod sb-gray:stream-read-char ((stream broken-input-stream))
923 (throw 'break :broken))
924 (with-test (:name :explicit-check)
925 (assert (eql (block return
926 (handler-case
927 (catch 'break
928 (funcall (eval ''peek-char)
929 1 (make-instance 'broken-input-stream))
930 :test-broken)
931 (type-error (c)
932 (return-from return :good))))
933 :good)))
935 ;;;; MUFFLE-CONDITIONS test (corresponds to the test in the manual)
936 ; FIXME: make a better test!
937 (with-test (:name muffle-conditions :skipped-on '(or :alpha :x86-64))
938 (multiple-value-bind (fun failure-p warnings style-warnings notes)
939 (checked-compile
940 '(lambda (x)
941 (declare (optimize speed) (fixnum x))
942 (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
943 (values (* x 5) ; no compiler note from this
944 (locally
945 (declare (sb-ext:unmuffle-conditions sb-ext:compiler-note))
946 ;; this one gives a compiler note
947 (* x -5)))))
948 (declare (ignore failure-p warnings))
949 (assert (= (length notes) 1))
950 (assert (equal (multiple-value-list (funcall fun 1)) '(5 -5)))))
952 (with-test (:name (flet labels &key))
953 (assert-error (eval '(flet ((%f (&key) nil)) (%f nil nil))))
954 (assert-error (eval '(labels ((%f (&key x) x)) (%f nil nil)))))
956 ;;; PROGV must not bind constants, or violate declared types -- ditto for SET.
957 (assert-error (set pi 3))
958 (assert-error (progv '(pi s) '(3 pi) (symbol-value x)))
959 (declaim (cons *special-cons*))
960 (assert-error (set '*special-cons* "nope") type-error)
961 (assert-error (progv '(*special-cons*) '("no hope") (car *special-cons*)) type-error)
963 ;;; No bogus warnings for calls to functions with complex lambda-lists.
964 (defun complex-function-signature (&optional x &rest y &key z1 z2)
965 (cons x y))
966 (with-test (:name :complex-call-doesnt-warn)
967 (checked-compile '(lambda (x) (complex-function-signature x :z1 1 :z2 2))))
969 (with-test (:name :non-required-args-update-info)
970 (let ((name (gensym "NON-REQUIRE-ARGS-TEST"))
971 (*evaluator-mode* :compile))
972 (eval `(defun ,name (x) x))
973 (assert (equal '(function (t) (values t &optional))
974 (sb-kernel:type-specifier (sb-int:proclaimed-ftype name))))
975 (eval `(defun ,name (x &optional y) (or x y)))
976 (assert (equal '(function (t &optional t) (values t &optional))
977 (sb-kernel:type-specifier (sb-int:proclaimed-ftype name))))))
979 ;;;; inline & maybe inline nested calls
981 (defun quux-marker (x) x)
982 (declaim (inline foo-inline))
983 (defun foo-inline (x) (quux-marker x))
984 (declaim (maybe-inline foo-maybe-inline))
985 (defun foo-maybe-inline (x) (quux-marker x))
987 (with-test (:name :nested-inline-calls)
988 (let ((fun (checked-compile `(lambda (x)
989 (foo-inline (foo-inline (foo-inline x)))))))
990 (assert (= 0 (ctu:count-full-calls "FOO-INLINE" fun)))
991 (assert (= 3 (ctu:count-full-calls "QUUX-MARKER" fun)))))
993 (with-test (:name :nested-maybe-inline-calls)
994 (let ((fun (checked-compile
995 `(lambda (x)
996 (declare (optimize (space 0)))
997 (foo-maybe-inline (foo-maybe-inline (foo-maybe-inline x)))))))
998 (assert (= 0 (ctu:count-full-calls "FOO-MAYBE-INLINE" fun)))
999 (assert (= 1 (ctu:count-full-calls "QUUX-MARKER" fun)))))
1001 (with-test (:name :inline-calls)
1002 (let ((fun (checked-compile `(lambda (x)
1003 (list (foo-inline x)
1004 (foo-inline x)
1005 (foo-inline x))))))
1006 (assert (= 0 (ctu:count-full-calls "FOO-INLINE" fun)))
1007 (assert (= 3 (ctu:count-full-calls "QUUX-MARKER" fun)))))
1009 (with-test (:name :maybe-inline-calls)
1010 (let ((fun (checked-compile `(lambda (x)
1011 (declare (optimize (space 0)))
1012 (list (foo-maybe-inline x)
1013 (foo-maybe-inline x)
1014 (foo-maybe-inline x))))))
1015 (assert (= 0 (ctu:count-full-calls "FOO-MAYBE-INLINE" fun)))
1016 (assert (= 1 (ctu:count-full-calls "QUUX-MARKER" fun)))))
1018 (with-test (:name :bug-405)
1019 ;; These used to break with a TYPE-ERROR
1020 ;; The value NIL is not of type SB-C::PHYSENV.
1021 ;; in MERGE-LETS.
1022 (ctu:file-compile
1023 '((LET (outer-let-var)
1024 (lambda ()
1025 (print outer-let-var)
1026 (MULTIPLE-VALUE-CALL 'some-function
1027 (MULTIPLE-VALUE-CALL (LAMBDA (a) 'foo)
1028 1))))))
1029 (ctu:file-compile
1030 '((declaim (optimize (debug 3)))
1031 (defstruct bug-405-foo bar)
1032 (let ()
1033 (flet ((i (x) (frob x (bug-405-foo-bar foo))))
1034 (i :five))))))
1036 ;;; bug 235a
1037 (declaim (ftype (function (cons) number) bug-235a-aux))
1038 (declaim (inline bug-235a-aux))
1039 (defun bug-235a-aux (c)
1040 (the number (car c)))
1041 (with-test (:name :bug-235a)
1042 (let ((fun (checked-compile
1043 `(lambda (x y)
1044 (values (locally (declare (optimize (safety 0)))
1045 (bug-235a-aux x))
1046 (locally (declare (optimize (safety 3)))
1047 (bug-235a-aux y)))))))
1048 (assert
1049 (eq :error
1050 (handler-case
1051 (funcall fun '(:one) '(:two))
1052 (type-error (e)
1053 (assert (eq :two (type-error-datum e)))
1054 (assert (eq 'number (type-error-expected-type e)))
1055 :error))))))
1057 (with-test (:name :compiled-debug-funs-leak)
1058 (sb-ext:gc :full t)
1059 (let ((usage-before (sb-kernel::dynamic-usage)))
1060 (dotimes (x 10000)
1061 (let ((f (checked-compile '(lambda () (error "X")))))
1062 (handler-case
1063 (funcall f)
1064 (error () nil))))
1065 (sb-ext:gc :full t)
1066 (let ((usage-after (sb-kernel::dynamic-usage)))
1067 (when (< (+ usage-before 2000000) usage-after)
1068 (error "Leak")))))
1070 ;;; PROGV compilation and type checking when the declared type
1071 ;;; includes a FUNCTION subtype.
1072 (declaim (type (or (function (t) (values boolean &optional)) string)
1073 *hairy-progv-var*))
1074 (defvar *hairy-progv-var* #'null)
1075 (with-test (:name :hairy-progv-type-checking)
1076 (assert (eq :error
1077 (handler-case
1078 (progv '(*hairy-progv-var*) (list (eval 42))
1079 *hairy-progv-var*)
1080 (type-error () :error))))
1081 (assert (equal "GOOD!"
1082 (progv '(*hairy-progv-var*) (list (eval "GOOD!"))
1083 *hairy-progv-var*))))
1085 (with-test (:name :fill-complex-single-float)
1086 (assert (every (lambda (x) (eql x #c(-1.0 -2.0)))
1087 (funcall
1088 (lambda ()
1089 (make-array 2
1090 :element-type '(complex single-float)
1091 :initial-element #c(-1.0 -2.0)))))))
1093 (with-test (:name :make-array-symbol-as-initial-element)
1094 (assert (every (lambda (x) (eq x 'a))
1095 (funcall (checked-compile
1096 `(lambda ()
1097 (make-array 12 :initial-element 'a)))))))
1099 ;;; This non-minimal test-case catches a nasty error when loading
1100 ;;; inline constants.
1101 (deftype matrix ()
1102 `(simple-array single-float (16)))
1103 (declaim (ftype (sb-int:sfunction (single-float single-float single-float single-float
1104 single-float single-float single-float single-float
1105 single-float single-float single-float single-float
1106 single-float single-float single-float single-float)
1107 matrix)
1108 matrix)
1109 (inline matrix))
1110 (defun matrix (m11 m12 m13 m14
1111 m21 m22 m23 m24
1112 m31 m32 m33 m34
1113 m41 m42 m43 m44)
1114 (make-array 16
1115 :element-type 'single-float
1116 :initial-contents (list m11 m21 m31 m41
1117 m12 m22 m32 m42
1118 m13 m23 m33 m43
1119 m14 m24 m34 m44)))
1120 (declaim (ftype (sb-int:sfunction ((simple-array single-float (3)) single-float) matrix)
1121 rotate-around))
1122 (defun rotate-around (a radians)
1123 (let ((c (cos radians))
1124 (s (sin radians))
1125 ;; The 1.0 here was misloaded on x86-64.
1126 (g (- 1.0 (cos radians))))
1127 (let* ((x (aref a 0))
1128 (y (aref a 1))
1129 (z (aref a 2))
1130 (gxx (* g x x)) (gxy (* g x y)) (gxz (* g x z))
1131 (gyy (* g y y)) (gyz (* g y z)) (gzz (* g z z)))
1132 (matrix
1133 (+ gxx c) (- gxy (* s z)) (+ gxz (* s y)) 0.0
1134 (+ gxy (* s z)) (+ gyy c) (- gyz (* s x)) 0.0
1135 (- gxz (* s y)) (+ gyz (* s x)) (+ gzz c) 0.0
1136 0.0 0.0 0.0 1.0))))
1137 (with-test (:name :regression-1.0.29.54)
1138 (assert (every #'=
1139 '(-1.0 0.0 0.0 0.0 0.0 -1.0 0.0 0.0 0.0 0.0 -1.0 0.0 0.0 0.0 0.0 1.0)
1140 (rotate-around
1141 (make-array 3 :element-type 'single-float) (coerce pi 'single-float))))
1142 ;; Same bug manifests in COMPLEX-ATANH as well.
1143 (assert (= (atanh #C(-0.7d0 1.1d0)) #C(-0.28715567731069275d0 0.9394245539093365d0))))
1145 (with-test (:name :slot-value-on-structure)
1146 (let ((f (checked-compile `(lambda (x a b)
1147 (declare (something-known-to-be-a-struct x))
1148 (setf (slot-value x 'x) a
1149 (slot-value x 'y) b)
1150 (list (slot-value x 'x)
1151 (slot-value x 'y))))))
1152 (assert (equal '(#\x #\y)
1153 (funcall f
1154 (make-something-known-to-be-a-struct :x "X" :y "Y")
1155 #\x #\y)))
1156 (assert (not (ctu:find-named-callees f)))))
1158 (defclass some-slot-thing ()
1159 ((slot :initarg :slot)))
1160 (with-test (:name :with-slots-the)
1161 (let ((x (make-instance 'some-slot-thing :slot "foo")))
1162 (with-slots (slot) (the some-slot-thing x)
1163 (assert (equal "foo" slot)))))
1165 ;;; Missing &REST type in proclamation causing a miscompile.
1166 (declaim (ftype
1167 (function
1168 (sequence unsigned-byte
1169 &key (:initial-element t) (:initial-contents sequence))
1170 (values sequence &optional))
1171 bug-458354))
1172 (defun bug-458354
1173 (sequence length
1174 &rest keys
1175 &key (initial-element nil iep) (initial-contents nil icp))
1176 (declare (sb-ext:unmuffle-conditions style-warning))
1177 (declare (ignorable keys initial-element iep initial-contents icp))
1178 (apply #'sb-sequence:make-sequence-like sequence length keys))
1179 (with-test (:name :bug-458354)
1180 (assert (equalp #((a b) (a b)) (bug-458354 #(1 2) 2 :initial-element '(a b)))))
1182 (with-test (:name :bug-542807)
1183 (handler-bind ((style-warning #'error))
1184 (eval '(defstruct bug-542807 slot)))
1185 (let (conds)
1186 (handler-bind ((style-warning (lambda (c)
1187 (push c conds))))
1188 (eval '(defstruct bug-542807 slot)))
1189 (assert (and conds
1190 (every (lambda (x) (typep x 'sb-kernel:redefinition-with-defun))
1191 conds)))))
1193 (with-test (:name :defmacro-not-list-lambda-list)
1194 (assert-error (eval `(defmacro ,(gensym) "foo"))
1195 type-error))
1197 (with-test (:name :bug-308951)
1198 (let ((x 1))
1199 (dotimes (y 10)
1200 (let ((y y))
1201 (when (funcall (eval #'(lambda (x) (eql x 2))) y)
1202 (defun bug-308951-foo (z)
1203 (incf x (incf y z))))))
1204 (defun bug-308951-bar (z)
1205 (bug-308951-foo z)
1206 (values x)))
1207 (assert (= 4 (bug-308951-bar 1))))
1209 (declaim (inline bug-308914-storage))
1210 (defun bug-308914-storage (x)
1211 (the (simple-array flt (*)) (bug-308914-unknown x)))
1213 (with-test (:name :bug-308914-workaround)
1214 ;; This used to hang in ORDER-UVL-SETS.
1215 (handler-case
1216 (with-timeout 10
1217 (checked-compile
1218 `(lambda (lumps &key cg)
1219 (let ((nodes (map 'list (lambda (lump)
1220 (bug-308914-storage lump))
1221 lumps)))
1222 (setf (aref nodes 0) 2)
1223 (assert (every #'~= (apply #'concatenate 'list nodes) '(2 3 6 9)))))
1224 :allow-warnings t :allow-style-warnings t))
1225 (sb-ext:timeout ()
1226 (error "Hang in ORDER-UVL-SETS?"))))
1228 (declaim (inline inlined-function-in-source-path))
1229 (defun inlined-function-in-source-path (x)
1230 (+ x x))
1232 (with-test (:name :inlined-function-in-source-path)
1233 (let ((output
1234 (with-output-to-string (*error-output*)
1235 (compile nil `(lambda (x)
1236 (declare (optimize speed))
1237 (funcall #'inlined-function-in-source-path x))))))
1238 ;; We want the name
1239 (assert (search "INLINED-FUNCTION-IN-SOURCE-PATH" output))
1240 ;; ...not the leaf.
1241 (assert (not (search "DEFINED-FUN" output)))))
1243 (defmacro bug-795705 ()
1246 (with-test (:name :bug-795705)
1247 (assert (macro-function 'bug-795705))
1248 (fmakunbound 'bug-795705)
1249 (assert (not (macro-function 'bug-795705))))
1251 (with-test (:name (load-time-value :type-derivation))
1252 (let ((name 'load-time-value-type-derivation-test))
1253 (labels ((funtype (fun)
1254 (sb-kernel:type-specifier
1255 (sb-kernel:single-value-type
1256 (sb-kernel:fun-type-returns
1257 (sb-kernel:specifier-type
1258 (sb-kernel:%simple-fun-type fun))))))
1259 (test (type1 type2 form value-cell-p)
1260 (let* ((lambda-form `(lambda ()
1261 (load-time-value ,form)))
1262 (core-fun (checked-compile lambda-form))
1263 (core-type (funtype core-fun))
1264 (defun-form `(defun ,name ()
1265 (load-time-value ,form)))
1266 (file-fun (progn
1267 (ctu:file-compile (list defun-form) :load t)
1268 (symbol-function name)))
1269 (file-type (funtype file-fun)))
1270 (unless (subtypep core-type type1)
1271 (error "core: wanted ~S, got ~S" type1 core-type))
1272 (unless (subtypep file-type type2)
1273 (error "file: wanted ~S, got ~S" type2 file-type)))))
1274 (let ((* 10))
1275 (test '(integer 11 11) 'number
1276 '(+ * 1) nil))
1277 (let ((* "fooo"))
1278 (test '(integer 4 4) 'unsigned-byte
1279 '(length *) nil))
1280 (test '(integer 10 10) '(integer 10 10) 10 nil)
1281 (test 'cons 'cons '(cons t t) t))))
1283 (with-test (:name (load-time-value :errors))
1284 (multiple-value-bind (warn fail)
1285 (ctu:file-compile
1286 `((defvar *load-time-value-error-value* 10)
1287 (declaim (fixnum *load-time-value-error-value*))
1288 (defun load-time-value-error-test-1 ()
1289 (the list (load-time-value *load-time-value-error-value*))))
1290 :load t)
1291 (assert warn)
1292 (assert fail))
1293 (handler-case (load-time-value-error-test-1)
1294 (type-error (e)
1295 (and (eql 10 (type-error-datum e))
1296 (eql 'list (type-error-expected-type e)))))
1297 (multiple-value-bind (warn2 fail2)
1298 (ctu:file-compile
1299 `((defun load-time-value-error-test-2 ()
1300 (the list (load-time-value 10))))
1301 :load t)
1302 (assert warn2)
1303 (assert fail2))
1304 (handler-case (load-time-value-error-test-2)
1305 (type-error (e)
1306 (and (eql 10 (type-error-datum e))
1307 (eql 'list (type-error-expected-type e))))))
1309 ;;;; tests for compiler output
1310 (with-test (:name :unexpected-compiler-output)
1311 (let* ((*error-output* (make-string-output-stream))
1312 (output (with-output-to-string (*standard-output*)
1313 (compile-file "compiler-output-test.lisp"
1314 :print nil :verbose nil))))
1315 (unless (zerop (length output))
1316 (error "Unexpected output: ~S" output))))
1318 (with-test (:name :bug-493380)
1319 (flet ((test (forms)
1320 (catch 'debug
1321 (let ((*debugger-hook* (lambda (condition if)
1322 (throw 'debug
1323 (if (typep condition 'serious-condition)
1324 :debug
1325 :oops)))))
1326 (multiple-value-bind (warned failed) (ctu:file-compile forms)
1327 (when (and warned failed)
1328 :failed))))))
1329 (assert (eq :failed (test "(defun")))
1330 (assert (eq :failed (test "(defun no-pkg::foo ())")))
1331 (assert (eq :failed (test "(cl:no-such-sym)")))
1332 (assert (eq :failed (test "...")))))
1334 (defun cmacro-signals-error () :fun)
1335 (define-compiler-macro cmacro-signals-error () (error "oops"))
1337 (with-test (:name :cmacro-signals-error)
1338 (multiple-value-bind (fun failure-p warnings)
1339 (checked-compile `(lambda () (cmacro-signals-error))
1340 :allow-failure t :allow-warnings t)
1341 (assert (and fun failure-p warnings))
1342 (assert (eq :fun (funcall fun)))))
1344 (defun cmacro-with-simple-key (&key a)
1345 (format nil "fun=~A" a))
1346 (define-compiler-macro cmacro-with-simple-key (&whole form &key a)
1347 (if (constantp a)
1348 (format nil "cmacro=~A" (eval a))
1349 form))
1351 (with-test (:name (:cmacro-with-simple-key :no-key))
1352 (let ((fun (checked-compile `(lambda () (cmacro-with-simple-key)))))
1353 (assert (string= "cmacro=NIL" (funcall fun)))))
1355 (with-test (:name (:cmacro-with-simple-key :constant-key))
1356 (let ((fun (checked-compile `(lambda () (cmacro-with-simple-key :a 42)))))
1357 (assert (string= "cmacro=42" (funcall fun)))))
1359 (with-test (:name (:cmacro-with-simple-key :variable-key))
1360 (let ((fun (checked-compile `(lambda (x) (cmacro-with-simple-key x 42)))))
1361 (assert (string= "fun=42" (funcall fun :a)))))
1363 (defun cmacro-with-nasty-key (&key ((nasty-key var)))
1364 (format nil "fun=~A" var))
1365 (define-compiler-macro cmacro-with-nasty-key (&whole form &key ((nasty-key var)))
1366 (if (constantp var)
1367 (format nil "cmacro=~A" (eval var))
1368 form))
1370 (with-test (:name (:cmacro-with-nasty-key :no-key))
1371 (let ((fun (checked-compile `(lambda () (cmacro-with-nasty-key)))))
1372 (assert (string= "cmacro=NIL" (funcall fun)))))
1374 (with-test (:name (:cmacro-with-nasty-key :constant-key))
1375 ;; This bogosity is thanks to cmacro lambda lists being /macro/ lambda
1376 ;; lists.
1377 (let ((fun (checked-compile
1378 `(lambda () (cmacro-with-nasty-key 'nasty-key 42)))))
1379 (assert (string= "fun=42" (funcall fun)))))
1381 (with-test (:name (:cmacro-with-nasty-key :variable-key))
1382 (let ((fun (checked-compile
1383 `(lambda (nasty-key) (cmacro-with-nasty-key nasty-key 42)))))
1384 (assert (string= "fun=42" (funcall fun 'nasty-key)))))
1386 (defconstant tricky-key 'tricky-key)
1387 (defun cmacro-with-tricky-key (&key ((tricky-key var)))
1388 (format nil "fun=~A" var))
1389 (define-compiler-macro cmacro-with-tricky-key (&whole form &key ((tricky-key var)))
1390 (if (constantp var)
1391 (format nil "cmacro=~A" (eval var))
1392 form))
1394 (with-test (:name (:cmacro-with-tricky-key :no-key))
1395 (let ((fun (checked-compile
1396 `(lambda () (cmacro-with-tricky-key)))))
1397 (assert (string= "cmacro=NIL" (funcall fun)))))
1399 (with-test (:name (:cmacro-with-tricky-key :constant-quoted-key))
1400 ;; This bogosity is thanks to cmacro lambda lists being /macro/ lambda
1401 ;; lists.
1402 (let ((fun (checked-compile
1403 `(lambda () (cmacro-with-tricky-key 'tricky-key 42)))))
1404 (assert (string= "fun=42" (funcall fun)))))
1406 (with-test (:name (:cmacro-with-tricky-key :constant-unquoted-key))
1407 (let ((fun (checked-compile
1408 `(lambda () (cmacro-with-tricky-key tricky-key 42)))))
1409 (assert (string= "cmacro=42" (funcall fun)))))
1411 (with-test (:name (:cmacro-with-tricky-key :variable-key))
1412 (let ((fun (checked-compile
1413 `(lambda (x) (cmacro-with-tricky-key x 42)))))
1414 (assert (string= "fun=42" (funcall fun 'tricky-key)))))
1416 (defun test-function-983 (x) x)
1417 (define-compiler-macro test-function-983 (x) x)
1419 (with-test (:name :funcall-compiler-macro)
1420 (assert
1421 (handler-case
1422 (and (compile nil
1423 `(lambda ()
1424 (funcall (function test-function-983 junk) 1)))
1425 nil)
1426 (sb-c:compiler-error () t))))
1428 (defsetf test-984 %test-984)
1430 (with-test (:name :setf-function-with-setf-expander)
1431 (assert
1432 (handler-case
1433 (and (defun (setf test-984) ())
1434 nil)
1435 (style-warning () t)))
1436 (checked-compile `(lambda () #'(setf test-984))
1437 :allow-style-warnings t))
1439 (with-test (:name :compile-setf-function)
1440 (defun (setf compile-setf) ())
1441 (assert (equal (compile '(setf compile-setf))
1442 '(setf compile-setf))))
1444 (declaim (inline cut-test))
1445 (defun cut-test (b)
1446 (cond ((integerp b) b)
1447 (b 469)
1448 (t 2)))
1450 (with-test (:name :cut-to-width-bad-constant)
1451 (assert (= (funcall (checked-compile
1452 `(lambda ()
1453 (multiple-value-bind (a b) (values t t)
1454 (declare (ignore b))
1455 (mask-field (byte 10 0) (cut-test a))))))
1456 469)))
1458 ;; META: there's a test in compiler.pure.lisp that also tests
1459 ;; interaction of PROGV with (debug 3). These tests should be together.
1460 (with-test (:name :progv-debug-3)
1461 (unwind-protect
1462 (sb-ext:restrict-compiler-policy 'debug 3)
1463 (assert (funcall (checked-compile
1464 `(lambda (*v*)
1465 (declare (special *v*))
1466 (progv '(*v*) '())
1467 (boundp '*v*)))
1469 (sb-ext:restrict-compiler-policy 'debug 0)))
1471 (with-test (:name :restrict-compiler-policy-result)
1472 (let ((sb-c::*policy-min* sb-c::*policy-min*)
1473 (sb-c::*policy-max* sb-c::*policy-max*))
1474 (sb-ext:restrict-compiler-policy 'safety 2)
1475 (checked-compile '(lambda () (declare (optimize (safety 0))))))
1476 (let ((sb-c::*policy-min* sb-c::*policy-min*)
1477 (sb-c::*policy-max* sb-c::*policy-max*))
1478 ;; Passing no arguments returns the current quality/value pairs.
1479 (assert (null (sb-ext:restrict-compiler-policy)))
1480 (let ((res (sb-ext:restrict-compiler-policy 'safety 2)))
1481 ;; returns an alist
1482 (assert (equal res '((safety . 2)))))
1483 (let ((res (sb-ext:restrict-compiler-policy 'debug 3)))
1484 ;; returns an alist, indeterminate order
1485 (assert (or (equal res '((safety . 2) (debug . 3)))
1486 (equal res '((debug . 3) (safety . 2))))))
1487 ;; remove the SAFETY restriction
1488 (let ((res (sb-ext:restrict-compiler-policy 'safety 0)))
1489 (assert (equal res '((debug . 3)))))
1490 ;; remove the DEBUG restriction
1491 (let ((res (sb-ext:restrict-compiler-policy 'debug 0)))
1492 (assert (null res)))))
1494 ;;;; tests not in the problem domain, but of the consistency of the
1495 ;;;; compiler machinery itself
1497 (in-package "SB-C")
1499 ;;; Hunt for wrong-looking things in fundamental compiler definitions,
1500 ;;; and gripe about them.
1502 ;;; FIXME: It should be possible to (1) repair the things that this
1503 ;;; code gripes about, and then (2) make the code signal errors
1504 ;;; instead of just printing complaints to standard output, in order
1505 ;;; to prevent the code from later falling back into disrepair.
1506 (defun grovel-results (function)
1507 (dolist (template (fun-info-templates (info :function :info function)))
1508 (when (template-more-results-type template)
1509 (format t "~&Template ~A has :MORE results, and translates ~A.~%"
1510 (template-name template)
1511 function)
1512 (return nil))
1513 (when (eq (template-result-types template) :conditional)
1514 ;; dunno.
1515 (return t))
1516 (let ((types (template-result-types template))
1517 (result-type (fun-type-returns (proclaimed-ftype function))))
1518 (cond
1519 ((values-type-p result-type)
1520 (do ((ltypes (append (args-type-required result-type)
1521 (args-type-optional result-type))
1522 (rest ltypes))
1523 (types types (rest types)))
1524 ((null ltypes)
1525 (unless (null types)
1526 (format t "~&More types than ltypes in ~A, translating ~A.~%"
1527 (template-name template)
1528 function)
1529 (return nil)))
1530 (when (null types)
1531 (unless (null ltypes)
1532 (format t "~&More ltypes than types in ~A, translating ~A.~%"
1533 (template-name template)
1534 function)
1535 (return nil)))))
1536 ((eq result-type (specifier-type nil))
1537 (unless (null types)
1538 (format t "~&Template ~A returns values for function ~A with RESULT-TYPE NIL.~%"
1539 (template-name template)
1540 function)
1541 (return nil)))
1542 ((/= (length types) 1)
1543 (format t "~&Template ~A isn't returning 1 value for ~A.~%"
1544 (template-name template)
1545 function)
1546 (return nil))
1547 (t t)))))
1548 (test-util:with-test (:name :identify-suspect-vops)
1549 (sb-c::call-with-each-globaldb-name
1550 (lambda (name)
1551 ;; LEGAL-FUN-NAME-P test is necessary, since (INFO :FUNCTION :TYPE)
1552 ;; has a defaulting expression that involves calling FDEFINITION.
1553 (when (and (legal-fun-name-p name) (proclaimed-ftype name))
1554 ;; OK, so we have an entry in the INFO database. Now, if ...
1555 (binding* ((info (info :function :info name) :exit-if-null)
1556 (nil (fun-info-templates info) :exit-if-null))
1557 ;; ... it has translators
1558 (grovel-results name))))))
1560 ;;;; bug 305: INLINE/NOTINLINE causing local ftype to be lost
1562 (test-util:with-test (:name (compile inline notinline))
1563 (labels ((compile-lambda (type sense allow-notes)
1564 (nth-value
1565 4 (test-util:checked-compile
1566 `(lambda ()
1567 (declare
1568 ,@(when type '((ftype (function () (integer 0 10)) bug-305)))
1569 (,sense bug-305)
1570 (optimize speed))
1571 (1+ (bug-305)))
1572 :allow-style-warnings t :allow-notes allow-notes))))
1573 (assert (compile-lambda nil 'inline t))
1574 (assert (compile-lambda nil 'notinline t))
1575 (compile-lambda t 'inline nil)
1576 (compile-lambda t 'notinline nil)))
1578 ;;; bug 211e: bogus style warning from duplicated keyword argument to
1579 ;;; a local function.
1580 (test-util:with-test (:name (compile flet :duplicate &key :bug-211e))
1581 (let ((f (test-util:checked-compile
1582 '(lambda ()
1583 (flet ((foo (&key y) (list y)))
1584 (list (foo :y 1 :y 2)))))))
1585 (assert (equal '((1)) (funcall f)))))
1587 ;;; check that EQL is optimized when other argument is (OR SYMBOL FIXNUM).
1588 (test-util:with-test (:name (compile :optimize eql))
1589 (let ((f1 (test-util:checked-compile
1590 '(lambda (x1 y1)
1591 (declare (type (or symbol fixnum) x1)
1592 (optimize speed))
1593 (eql x1 y1))
1594 :allow-notes nil))
1595 (f2 (test-util:checked-compile
1596 '(lambda (x2 y2)
1597 (declare (type (or symbol fixnum) y2)
1598 (optimize speed))
1599 (eql x2 y2))
1600 :allow-notes nil)))
1601 (let ((fix (random most-positive-fixnum))
1602 (sym (gensym))
1603 (e-count 0))
1604 (assert (funcall f1 fix fix))
1605 (assert (funcall f2 fix fix))
1606 (assert (funcall f1 sym sym))
1607 (assert (funcall f2 sym sym))
1608 (handler-bind ((type-error (lambda (c)
1609 (incf e-count)
1610 (continue c))))
1611 (flet ((test (f x y)
1612 (with-simple-restart (continue "continue with next test")
1613 (funcall f x y)
1614 (error "fell through with (~S ~S ~S)" f x y))))
1615 (test f1 "oops" 42)
1616 (test f1 (1+ most-positive-fixnum) 42)
1617 (test f2 42 "oops")
1618 (test f2 42 (1+ most-positive-fixnum))))
1619 (assert (= e-count 4)))))
1621 ;;; bug #389 (Rick Taube sbcl-devel)
1622 (defun bes-jn (unn ux)
1623 (let ((nn unn) (x ux))
1624 (let* ((n (floor (abs nn)))
1625 (besn
1626 (if (= n 0)
1627 (bes-j0 x)
1628 (if (= n 1)
1629 (bes-j1 x)
1630 (if (zerop x)
1632 (let ((iacc 40)
1633 (ans 0.0)
1634 (bigno 1.0e+10)
1635 (bigni 1.0e-10))
1636 (if (> (abs x) n)
1637 (do ((tox (/ 2.0 (abs x)))
1638 (bjm (bes-j0 (abs x)))
1639 (bj (bes-j1 (abs x)))
1640 (j 1 (+ j 1))
1641 (bjp 0.0))
1642 ((= j n) (setf ans bj))
1643 (setf bjp (- (* j tox bj) bjm))
1644 (setf bjm bj)
1645 (setf bj bjp))
1646 (let ((tox (/ 2.0 (abs x)))
1648 (* 2
1649 (floor
1650 (/ (+ n (sqrt (* iacc n)))
1651 2))))
1652 (jsum 0.0)
1653 (bjm 0.0)
1654 (sum 0.0)
1655 (bjp 0.0)
1656 (bj 1.0))
1657 (do ((j m (- j 1)))
1658 ((= j 0))
1659 (setf bjm (- (* j tox bj) bjp))
1660 (setf bjp bj)
1661 (setf bj bjm)
1662 (when (> (abs bj) bigno)
1663 (setf bj (* bj bigni))
1664 (setf bjp (* bjp bigni))
1665 (setf ans (* ans bigni))
1666 (setf sum (* sum bigni)))
1667 (if (not (= 0 jsum)) (incf sum bj))
1668 (setf jsum (- 1 jsum))
1669 (if (= j n) (setf ans bjp)))
1670 (setf sum (- (* 2.0 sum) bj))
1671 (setf ans (/ ans sum))))
1672 (if (and (minusp x) (oddp n))
1673 (- ans)
1674 ans)))))))
1675 (if (and (minusp nn) (oddp nn)) (- besn) besn))))
1678 ;;; bug 233b: lvar lambda-var equality in constraint propagation
1680 ;; Put this in a separate function.
1681 (defun test-constraint-propagation/ref ()
1682 (let ((x nil))
1683 (if (multiple-value-prog1 x (setq x t))
1685 x)))
1687 (test-util:with-test (:name (:compiler :constraint-propagation :ref))
1688 (assert (eq t (test-constraint-propagation/ref))))
1690 ;; Put this in a separate function.
1691 (defun test-constraint-propagation/typep (x y)
1692 (if (typep (multiple-value-prog1 x (setq x y))
1693 'double-float)
1694 (+ x 1d0)
1695 (+ x 2)))
1697 (test-util:with-test (:name (:compiler :constraint-propagation :typep))
1698 (assert (= 6.0d0 (test-constraint-propagation/typep 1d0 5))))
1700 (test-util:with-test (:name (:compiler :constraint-propagation :eq/eql))
1701 (assert (eq :right (let ((c :wrong))
1702 (if (eq (let ((x c))
1703 (setq c :right)
1705 :wrong)
1707 0)))))
1709 ;;; Put this in a separate function.
1710 (defun test-constraint-propagation/cast (x)
1711 (when (the double-float (multiple-value-prog1
1713 (setq x (1+ x))))
1716 (test-util:with-test (:name (:compiler :constraint-propagation :cast))
1717 (assertoid:assert-error
1718 (test-constraint-propagation/cast 1) type-error))
1720 ;;; bug #399
1721 (let ((result (make-array 50000 :fill-pointer 0 :adjustable t)))
1722 (defun string->html (string &optional (max-length nil))
1723 (when (and (numberp max-length)
1724 (> max-length (array-dimension result 0)))
1725 (setf result (make-array max-length :fill-pointer 0 :adjustable t)))
1726 (let ((index 0)
1727 (left-quote? t))
1728 (labels ((add-char (it)
1729 (setf (aref result index) it)
1730 (incf index))
1731 (add-string (it)
1732 (loop for ch across it do
1733 (add-char ch))))
1734 (loop for char across string do
1735 (cond ((char= char #\<)
1736 (add-string "&lt;"))
1737 ((char= char #\>)
1738 (add-string "&gt;"))
1739 ((char= char #\&)
1740 (add-string "&amp;"))
1741 ((char= char #\')
1742 (add-string "&#39;"))
1743 ((char= char #\newline)
1744 (add-string "<br>"))
1745 ((char= char #\")
1746 (if left-quote? (add-string "&#147;") (add-string "&#148;"))
1747 (setf left-quote? (not left-quote?)))
1749 (add-char char))))
1750 (setf (fill-pointer result) index)
1751 (coerce result 'string)))))
1753 ;;; Calling thru constant symbols
1754 (require :sb-introspect)
1756 (declaim (inline target-fun))
1757 (defun target-fun (arg0 arg1)
1758 (+ arg0 arg1))
1759 (declaim (notinline target-fun))
1761 ;; FIXME: should use compiler-test-util, not sb-introspect here.
1762 ;; That issue aside, neither sb-introspect nor ctu:find-named-callees
1763 ;; can examine an interpreted function for its callees,
1764 ;; so we can't actually use this function.
1765 (defun test-target-fun-called (fun res)
1766 (assert (member #'target-fun
1767 (sb-introspect:find-function-callees #'caller-fun-1)))
1768 (assert (equal (funcall fun) res)))
1770 (defun caller-fun-1 ()
1771 (funcall 'target-fun 1 2))
1772 #-interpreter(test-target-fun-called #'caller-fun-1 3)
1774 (defun caller-fun-2 ()
1775 (declare (inline target-fun))
1776 (apply 'target-fun 1 '(3)))
1777 #-interpreter(test-target-fun-called #'caller-fun-2 4)
1779 (defun caller-fun-3 ()
1780 (flet ((target-fun (a b)
1781 (- a b)))
1782 (list (funcall #'target-fun 1 4) (funcall 'target-fun 1 4))))
1783 #-interpreter(test-target-fun-called #'caller-fun-3 (list -3 5))
1785 ;;; Reported by NIIMI Satoshi
1786 ;;; Subject: [Sbcl-devel] compilation error with optimization
1787 ;;; Date: Sun, 09 Apr 2006 17:36:05 +0900
1788 (defun test-minimal-debug-info-for-unstored-but-used-parameter (n a)
1789 (declare (optimize (speed 3)
1790 (debug 1)))
1791 (if (= n 0)
1793 (test-minimal-debug-info-for-unstored-but-used-parameter (1- n) a)))
1795 ;;; &KEY arguments with non-constant defaults.
1796 (declaim (notinline opaque-identity))
1797 (defun opaque-identity (x) x)
1798 (defstruct tricky-defaults
1799 (fun #'identity :type function)
1800 (num (opaque-identity 3) :type fixnum))
1801 (macrolet ((frob (form expected-expected-type)
1802 `(handler-case ,form
1803 (type-error (c) (assert (eq (type-error-expected-type c)
1804 ',expected-expected-type)))
1805 (:no-error (&rest vals) (error "~S returned values: ~S" ',form vals)))))
1806 (frob (make-tricky-defaults :fun 3) function)
1807 (frob (make-tricky-defaults :num #'identity) fixnum))
1809 (test-util:with-test (:name (compile &key :non-constant :default))
1810 (let ((fun (test-util:checked-compile
1811 '(lambda (&key (key (opaque-identity 3)))
1812 (declare (optimize safety) (type integer key))
1813 key))))
1814 (assert (= (funcall fun) 3))
1815 (assert (= (funcall fun :key 17) 17))
1816 (handler-case (funcall fun :key t)
1817 (type-error (c) (assert (eq (type-error-expected-type c) 'integer)))
1818 (:no-error (&rest vals) (declare (ignore vals)) (error "no error")))))
1820 ;;; Basic compiler-macro expansion
1821 (define-compiler-macro test-cmacro-0 () ''expanded)
1823 ;; The interpreter is not required to expand compiler-macros.
1824 ;; (Actually neither is the compiler!)
1825 #-interpreter(assert (eq 'expanded (funcall (lambda () (test-cmacro-0)))))
1827 ;;; FUNCALL forms in compiler macros, lambda-list parsing
1828 (define-compiler-macro test-cmacro-1
1829 (&whole whole a (a2) &optional b &rest c &key d)
1830 (list whole a a2 b c d))
1832 (macrolet ((test (form a a2 b c d)
1833 `(let ((form ',form))
1834 (destructuring-bind (whole a a2 b c d)
1835 (funcall (compiler-macro-function 'test-cmacro-1) form nil)
1836 (assert (equal whole form))
1837 (assert (eql a ,a))
1838 (assert (eql a2 ,a2))
1839 (assert (eql b ,b))
1840 (assert (equal c ,c))
1841 (assert (eql d ,d))))) )
1842 (test (funcall 'test-cmacro-1 1 (x) 2 :d 3) 1 'x 2 '(:d 3) 3)
1843 (test (test-cmacro-1 11 (y) 12 :d 13) 11 'y 12 '(:d 13) 13))
1845 ;;; FUNCALL forms in compiler macros, expansions
1846 (define-compiler-macro test-cmacro-2 () ''ok)
1848 #-interpreter(assert (eq 'ok (funcall (lambda () (funcall 'test-cmacro-2)))))
1849 #-interpreter(assert (eq 'ok (funcall (lambda () (funcall #'test-cmacro-2)))))
1851 ;;; Shadowing of compiler-macros by local functions
1852 (define-compiler-macro test-cmacro-3 () ''global)
1854 (defmacro find-cmacro-3 (&environment env)
1855 (compiler-macro-function 'test-cmacro-3 env))
1857 (assert (funcall (lambda () (find-cmacro-3))))
1858 (assert (not (funcall (lambda () (flet ((test-cmacro-3 ()))
1859 (find-cmacro-3))))))
1860 (assert (eq 'local (funcall (lambda () (flet ((test-cmacro-3 () 'local))
1861 (test-cmacro-3))))))
1862 (assert (eq 'local (funcall (lambda () (flet ((test-cmacro-3 () 'local))
1863 (funcall #'test-cmacro-3))))))
1864 #-interpreter
1865 (assert (eq 'global (funcall (lambda () (flet ((test-cmacro-3 () 'local))
1866 (funcall 'test-cmacro-3))))))
1868 ;;; Local NOTINLINE & INLINE
1869 (defun test-cmacro-4 () 'fun)
1870 (define-compiler-macro test-cmacro-4 () ''macro)
1872 (assert (eq 'fun (funcall (lambda ()
1873 (declare (notinline test-cmacro-4))
1874 (test-cmacro-4)))))
1876 #-interpreter
1877 (assert (eq 'macro (funcall (lambda ()
1878 (declare (inline test-cmacro-4))
1879 (test-cmacro-4)))))
1881 ;;; SETF function compiler macros
1882 (define-compiler-macro (setf test-cmacro-4) (&whole form value) ''ok)
1884 #-interpreter
1885 (assert (eq 'ok (funcall (lambda () (setf (test-cmacro-4) 'zot)))))
1886 #-interpreter
1887 (assert (eq 'ok (funcall (lambda () (funcall #'(setf test-cmacro-4) 'zot)))))
1889 ;;; Step instrumentation breaking type-inference
1890 (test-util:with-test (:name (compile :insert-step-conditions :type-inference))
1891 (assert (= 42 (funcall (test-util:checked-compile
1892 '(lambda (v x)
1893 (declare (optimize sb-c:insert-step-conditions))
1894 (if (typep (the function x) 'fixnum)
1895 (svref v (the function x))
1896 (funcall x))))
1897 nil (constantly 42)))))
1899 ;;; bug 368: array type intersections in the compiler
1900 (defstruct e368)
1901 (defstruct i368)
1902 (defstruct g368
1903 (i368s (make-array 0 :fill-pointer t) :type (or (vector i368) null)))
1904 (defstruct s368
1905 (g368 (error "missing :G368") :type g368 :read-only t))
1906 (declaim (ftype (function (fixnum (vector i368) e368) t) r368))
1907 (declaim (ftype (function (fixnum (vector e368)) t) h368))
1908 (defparameter *h368-was-called-p* nil)
1909 (defun nsu (vertices e368)
1910 (let ((i368s (g368-i368s (make-g368))))
1911 (let ((fuis (r368 0 i368s e368)))
1912 (format t "~&FUIS=~S~%" fuis)
1913 (or fuis (h368 0 i368s)))))
1914 (defun r368 (w x y)
1915 (declare (ignore w x y))
1916 nil)
1917 (defun h368 (w x)
1918 (declare (ignore w x))
1919 (setf *h368-was-called-p* t)
1920 (make-s368 :g368 (make-g368)))
1921 (let ((nsu (nsu #() (make-e368))))
1922 (format t "~&NSU returned ~S~%" nsu)
1923 (format t "~&*H368-WAS-CALLED-P*=~S~%" *h368-was-called-p*)
1924 (assert (s368-p nsu))
1925 (assert *h368-was-called-p*))
1927 ;;; bug 367: array type intersections in the compiler
1928 (defstruct e367)
1929 (defstruct i367)
1930 (defstruct g367
1931 (i367s (make-array 0 :fill-pointer t) :type (or (vector i367) null)))
1932 (defstruct s367
1933 (g367 (error "missing :G367") :type g367 :read-only t))
1934 (declaim (ftype (function ((vector i367) e367) (or s367 null)) r367))
1935 (declaim (ftype (function ((vector e367)) (values)) h367))
1936 (defun frob-367 (v w)
1937 (let ((x (g367-i367s (make-g367))))
1938 (let* ((y (or (r367 x w)
1939 (h367 x)))
1940 (z (s367-g367 y)))
1941 (format t "~&Y=~S Z=~S~%" y z)
1942 (g367-i367s z))))
1943 (defun r367 (x y) (declare (ignore x y)) nil)
1944 (defun h367 (x) (declare (ignore x)) (values))
1945 (assertoid:assert-error (frob-367 0 (make-e367)) type-error)
1947 (handler-case
1948 (delete-file (compile-file "circ-tree-test.lisp"))
1949 (storage-condition (e)
1950 (error e)))
1952 ;;; warnings due to step-instrumentation
1953 (defclass debug-test-class () ())
1954 (test-util:checked-compile
1955 '(lambda ()
1956 (declare (optimize (debug 3)))
1957 (defmethod print-object ((x debug-test-class) s)
1958 (call-next-method))))
1960 ;;; program-error from bad lambda-list keyword
1961 (test-util:with-test (:name (compile :bad lambda-list keyword program-error))
1962 (multiple-value-bind (fun failure-p)
1963 (test-util:checked-compile '(lambda (&whole x)
1964 (list &whole x))
1965 :allow-failure t)
1966 (assert failure-p)
1967 (assertoid:assert-error (funcall fun) program-error)))
1969 (test-util:with-test (:name (eval :bad lambda-list keyword program-error)
1970 :skipped-on (not :sb-eval))
1971 (assertoid:assert-error (let ((*evaluator-mode* :interpret))
1972 (funcall (eval '(lambda (&whole x)
1973 (list &whole x)))))
1974 program-error))
1976 ;;; ignore &environment
1977 (test-util:with-test (:name (defmacro ignore &environment))
1978 (test-util:checked-compile
1979 '(lambda ()
1980 (defmacro macro-ignore-env (&environment env)
1981 (declare (ignore env))
1982 :foo))))
1984 (test-util:with-test (:name (defmacro :no &environment))
1985 (test-util:checked-compile
1986 '(lambda ()
1987 (defmacro macro-no-env ()
1988 :foo))))
1990 (test-util:with-test (:name (disassemble :source-form))
1991 (dolist (*evaluator-mode* '(#+sb-eval :interpret :compile))
1992 (disassemble (eval '(defun disassemble-source-form-bug (x y z)
1993 (declare (optimize debug))
1994 (list x y z)))
1995 :stream (make-broadcast-stream))))
1997 ;;; long-standing bug in defaulting unknown values on the x86-64,
1998 ;;; since changing the calling convention (test case by Christopher
1999 ;;; Laux sbcl-help 30-06-2007)
2001 (defun default-values-bug-demo-sub ()
2002 (format t "test")
2003 nil)
2004 (compile 'default-values-bug-demo-sub)
2006 (defun default-values-bug-demo-main ()
2007 (multiple-value-bind (a b c d e f g h)
2008 (default-values-bug-demo-sub)
2009 (if a (+ a b c d e f g h) t)))
2010 (compile 'default-values-bug-demo-main)
2012 (test-util:with-test (:name (multiple-value-bind :default-values))
2013 (assert (default-values-bug-demo-main)))
2015 ;;; copy propagation bug reported by Paul Khuong
2017 (defun local-copy-prop-bug-with-move-arg (x)
2018 (labels ((inner ()
2019 (values 1 0)))
2020 (if x
2021 (inner)
2022 (multiple-value-bind (a b)
2023 (inner)
2024 (values b a)))))
2026 (test-util:with-test (:name (labels multiple-value-bind :copy-propagation))
2027 (assert (equal '(0 1) (multiple-value-list
2028 (local-copy-prop-bug-with-move-arg nil))))
2029 (assert (equal '(1 0) (multiple-value-list
2030 (local-copy-prop-bug-with-move-arg t)))))
2032 ;;;; with-pinned-objects & unwind-protect, using all non-tail conventions
2034 (defun wpo-quux () (list 1 2 3))
2035 (defvar *wpo-quux* #'wpo-quux)
2037 (defun wpo-call ()
2038 (unwind-protect
2039 (sb-sys:with-pinned-objects (*wpo-quux*)
2040 (values (funcall *wpo-quux*)))))
2041 (assert (equal '(1 2 3) (wpo-call)))
2043 (defun wpo-multiple-call ()
2044 (unwind-protect
2045 (sb-sys:with-pinned-objects (*wpo-quux*)
2046 (funcall *wpo-quux*))))
2047 (assert (equal '(1 2 3) (wpo-multiple-call)))
2049 (defun wpo-call-named ()
2050 (unwind-protect
2051 (sb-sys:with-pinned-objects (*wpo-quux*)
2052 (values (wpo-quux)))))
2053 (assert (equal '(1 2 3) (wpo-call-named)))
2055 (defun wpo-multiple-call-named ()
2056 (unwind-protect
2057 (sb-sys:with-pinned-objects (*wpo-quux*)
2058 (wpo-quux))))
2059 (assert (equal '(1 2 3) (wpo-multiple-call-named)))
2061 (defun wpo-call-variable (&rest args)
2062 (unwind-protect
2063 (sb-sys:with-pinned-objects (*wpo-quux*)
2064 (values (apply *wpo-quux* args)))))
2065 (assert (equal '(1 2 3) (wpo-call-variable)))
2067 (defun wpo-multiple-call-variable (&rest args)
2068 (unwind-protect
2069 (sb-sys:with-pinned-objects (*wpo-quux*)
2070 (apply #'wpo-quux args))))
2071 (assert (equal '(1 2 3) (wpo-multiple-call-named)))
2073 (defun wpo-multiple-call-local ()
2074 (flet ((quux ()
2075 (wpo-quux)))
2076 (unwind-protect
2077 (sb-sys:with-pinned-objects (*wpo-quux*)
2078 (quux)))))
2079 (assert (equal '(1 2 3) (wpo-multiple-call-local)))
2081 ;;; bug 417: toplevel NIL confusing source path logic
2082 (handler-case
2083 (delete-file (compile-file "bug-417.lisp"))
2084 (sb-ext:code-deletion-note (e)
2085 (error e)))
2087 ;;; unknown values return convention getting disproportionate
2088 ;;; amounts of values.
2089 (declaim (notinline one-value two-values))
2090 (defun one-value (x)
2091 (not x))
2092 (defun two-values (x y)
2093 (values y x))
2094 (defun wants-many-values (x y)
2095 (multiple-value-bind (a b c d e f)
2096 (one-value y)
2097 (assert (and (eql (not y) a)
2098 (not (or b c d e f)))))
2099 (multiple-value-bind (a b c d e f)
2100 (two-values y x)
2101 (assert (and (eql a x) (eql b y)
2102 (not (or c d e f)))))
2103 (multiple-value-bind (a b c d e f g h i)
2104 (one-value y)
2105 (assert (and (eql (not y) a)
2106 (not (or b c d e f g h i)))))
2107 (multiple-value-bind (a b c d e f g h i)
2108 (two-values y x)
2109 (assert (and (eql a x) (eql b y)
2110 (not (or c d e f g h i)))))
2111 (multiple-value-bind (a b c d e f g h i j k l m n o p q r s)
2112 (one-value y)
2113 (assert (and (eql (not y) a)
2114 (not (or b c d e f g h i j k l m n o p q r s)))))
2115 (multiple-value-bind (a b c d e f g h i j k l m n o p q r s)
2116 (two-values y x)
2117 (assert (and (eql a x) (eql b y)
2118 (not (or c d e f g h i j k l m n o p q r s))))))
2119 (wants-many-values 1 42)
2121 ;;; constant coalescing
2123 (defun count-code-constants (x f)
2124 (let ((code (sb-kernel:fun-code-header f))
2125 (n 0))
2126 (loop for i from sb-vm:code-constants-offset below (sb-kernel:code-header-words code)
2127 do (when (equal x (sb-kernel:code-header-ref code i))
2128 (incf n)))
2131 (defvar *lambda*)
2133 (defun compile2 (lambda)
2134 (let* ((lisp "compiler-impure-tmp.lisp")
2135 (fasl (compile-file-pathname lisp)))
2136 (unwind-protect
2137 (progn
2138 (with-open-file (f lisp :direction :output)
2139 (prin1 `(setf *lambda* ,lambda) f))
2140 (multiple-value-bind (fasl warn fail) (compile-file lisp)
2141 (declare (ignore warn))
2142 (when fail
2143 (error "File-compiling ~S failed." lambda))
2144 (let ((*lambda* nil))
2145 (load fasl)
2146 (values *lambda* (test-util:checked-compile lambda)))))
2147 (ignore-errors (delete-file lisp))
2148 (ignore-errors (delete-file fasl)))))
2150 ;; named and unnamed
2151 (defconstant +born-to-coalesce+ '.born-to-coalesce.)
2152 (multiple-value-bind (file-fun core-fun)
2153 (compile2 '(lambda ()
2154 (let ((x (cons +born-to-coalesce+ nil))
2155 (y (cons '.born-to-coalesce. nil)))
2156 (list x y))))
2157 (assert (= 1 (count-code-constants '.born-to-coalesce. file-fun)))
2158 (assert (= 1 (count-code-constants '.born-to-coalesce. core-fun))))
2160 ;; some things must retain identity under COMPILE, but we want to coalesce them under COMPILE-FILE
2161 (defun assert-coalescing (constant)
2162 (let ((value (copy-seq (symbol-value constant))))
2163 (multiple-value-bind (file-fun core-fun)
2164 (compile2 `(lambda ()
2165 (let ((x (cons ,constant nil))
2166 (y (cons ',value nil)))
2167 (list x y))))
2168 (assert (= 1 (count-code-constants value file-fun)))
2169 (assert (= 2 (count-code-constants value core-fun)))
2170 (let* ((l (funcall file-fun))
2171 (a (car (first l)))
2172 (b (car (second l))))
2173 (assert (and (equal value a)
2174 (equal a b)
2175 (eq a b))))
2176 (let* ((l (funcall core-fun))
2177 (a (car (first l)))
2178 (b (car (second l))))
2179 (assert (and (equal value a)
2180 (equal a b)
2181 (not (eq a b))))))))
2183 (defconstant +born-to-coalesce2+ "maybe coalesce me!")
2184 (assert-coalescing '+born-to-coalesce2+)
2186 (defconstant +born-to-coalesce3+ #*01101001011101110100011)
2187 (assert-coalescing '+born-to-coalesce3+)
2189 (defconstant +born-to-coalesce4+ '(foo bar "zot" 123 (nested "quux") #*0101110010))
2190 (assert-coalescing '+born-to-coalesce4+)
2192 (defclass some-constant-thing () ())
2194 ;;; correct handling of nested things loaded via SYMBOL-VALUE
2195 (defvar *sneaky-nested-thing* (list (make-instance 'some-constant-thing)))
2196 (defconstant +sneaky-nested-thing+ *sneaky-nested-thing*)
2197 (multiple-value-bind (file-fun core-fun) (compile2 '(lambda () +sneaky-nested-thing+))
2198 (assert (equal *sneaky-nested-thing* (funcall file-fun)))
2199 (assert (equal *sneaky-nested-thing* (funcall core-fun))))
2201 ;;; catch constant modifications thru undefined variables
2202 (defun sneak-set-dont-set-me (x)
2203 (ignore-errors (setq dont-set-me x)))
2204 (defconstant dont-set-me 42)
2205 (assert (not (sneak-set-dont-set-me 13)))
2206 (assert (= 42 dont-set-me))
2207 (defun sneak-set-dont-set-me2 (x)
2208 (ignore-errors (setq dont-set-me2 x)))
2209 (defconstant dont-set-me2 (make-instance 'some-constant-thing))
2210 (assert (not (sneak-set-dont-set-me2 13)))
2211 (assert (typep dont-set-me2 'some-constant-thing))
2213 ;;; check that non-trivial constants are EQ across different files: this is
2214 ;;; not something ANSI either guarantees or requires, but we want to do it
2215 ;;; anyways.
2216 (defconstant +share-me-1+ #-inline-constants 123.456d0 #+inline-constants nil)
2217 (defconstant +share-me-2+ "a string to share")
2218 (defconstant +share-me-3+ (vector 1 2 3))
2219 (defconstant +share-me-4+ (* 2 most-positive-fixnum))
2220 (multiple-value-bind (f1 c1) (compile2 '(lambda () (values +share-me-1+
2221 +share-me-2+
2222 +share-me-3+
2223 +share-me-4+
2224 #-inline-constants pi)))
2225 (multiple-value-bind (f2 c2) (compile2 '(lambda () (values +share-me-1+
2226 +share-me-2+
2227 +share-me-3+
2228 +share-me-4+
2229 #-inline-constants pi)))
2230 (flet ((test (fa fb)
2231 (mapc (lambda (a b)
2232 (assert (eq a b)))
2233 (multiple-value-list (funcall fa))
2234 (multiple-value-list (funcall fb)))))
2235 (test f1 c1)
2236 (test f1 f2)
2237 (test f1 c2))))
2239 ;;; user-defined satisfies-types cannot be folded
2240 (deftype mystery () '(satisfies mysteryp))
2241 (defvar *mystery* nil)
2242 (defun mysteryp (x) (eq x *mystery*))
2243 (defstruct thing (slot (error "missing") :type mystery))
2244 (defun test-mystery (m) (when (eq :mystery (thing-slot m)) :ok))
2245 (setf *mystery* :mystery)
2246 (assert (eq :ok (test-mystery (make-thing :slot :mystery))))
2248 ;;; Singleton types can also be constant.
2249 (test-util:with-test (:name :propagate-singleton-types-to-eql)
2250 (macrolet ((test (type value &aux (fun (gensym "FUN")))
2251 `(progn
2252 (declaim (ftype (function () (values ,type &optional)) ,fun))
2253 (defun ,fun ()
2254 ',value)
2255 (lambda (x)
2256 (if (eql x (,fun))
2258 (eql x (,fun)))))))
2259 (values
2260 (test (eql foo) foo)
2261 (test (integer 0 0) 0)
2262 (test (double-float 0d0 0d0) 0d0)
2263 (test (eql #\c) #\c))))
2265 (declaim (ftype (function () (integer 42 42)) bug-655581))
2266 (defun bug-655581 ()
2268 (declaim (notinline bug-655581))
2269 (test-util:with-test (:name :bug-655581)
2270 (multiple-value-bind (type derived)
2271 (funcall (test-util:checked-compile
2272 `(lambda ()
2273 (ctu:compiler-derived-type (bug-655581)))))
2274 (assert derived)
2275 (assert (equal '(integer 42 42) type))))
2277 (test-util:with-test (:name :clear-derived-types-on-set-fdefn)
2278 (let ((*evaluator-mode* :compile)
2279 (*derive-function-types* t))
2280 (eval `(progn
2281 (defun clear-derived-types-on-set-fdefn-1 ()
2282 "foo")
2283 (setf (symbol-function 'clear-derived-types-on-set-fdefn-1)
2284 (constantly "foobar"))
2285 (defun clear-derived-types-on-set-fdefn-2 ()
2286 (length (clear-derived-types-on-set-fdefn-1)))))
2287 (assert (= 6 (clear-derived-types-on-set-fdefn-2)))))
2289 (test-util:with-test (:name (:bug-655126 :derive-function-types t))
2290 (let ((*evaluator-mode* :compile)
2291 (*derive-function-types* t))
2292 (eval `(defun bug-655126 (x) x))
2293 ;; Full warnings are ok due to *derive-function-types* = T.
2294 (assert (eq :full-warning
2295 (handler-case
2296 (eval `(defun bug-655126-2 ()
2297 (bug-655126)))
2298 ((and warning (not style-warning)) ()
2299 :full-warning))))
2300 (assert (eq 'bug-655126
2301 (handler-case
2302 (eval `(defun bug-655126 (x y)
2303 (cons x y)))
2304 ((and warning (not sb-kernel:redefinition-warning)) ()
2305 :oops))))
2306 (assert (eq :full-warning
2307 (handler-case
2308 (eval `(defun bug-655126 (x)
2309 (bug-655126 x y)))
2310 ((and warning
2311 (not style-warning)
2312 (not sb-kernel:redefinition-warning)) ()
2313 :full-warning))))))
2315 (test-util:with-test (:name (:bug-655126 :derive-function-types nil))
2316 (let ((*evaluator-mode* :compile))
2317 (eval `(defun bug-655126/b (x) x))
2318 ;; Just style-warning here.
2319 (assert (eq :style-warning
2320 (handler-case
2321 (eval `(defun bug-655126-2/b ()
2322 (bug-655126/b)))
2323 (style-warning ()
2324 :style-warning))))
2325 (assert (eq 'bug-655126/b
2326 (handler-case
2327 (eval `(defun bug-655126/b (x y)
2328 (cons x y)))
2329 ((and warning (not sb-kernel:redefinition-warning)) ()
2330 :oops))))
2331 ;; Bogus self-call is always worth a full one.
2332 (assert (eq :full-warning
2333 (handler-case
2334 (eval `(defun bug-655126/b (x)
2335 (bug-655126/b x y)))
2336 ((and warning
2337 (not style-warning)
2338 (not sb-kernel:redefinition-warning)) ()
2339 :full-warning))))))
2341 (test-util:with-test (:name :bug-657499)
2342 ;; Don't trust derived types within the compilation unit.
2343 (ctu:file-compile
2344 `((declaim (optimize safety))
2345 (defun bug-657499-foo ()
2346 (cons t t))
2347 (defun bug-657499-bar ()
2348 (let ((cons (bug-657499-foo)))
2349 (setf (car cons) 3)
2350 cons)))
2351 :load t)
2352 (locally (declare (optimize safety))
2353 (setf (symbol-function 'bug-657499-foo) (constantly "foobar"))
2354 (assert (eq :type-error
2355 (handler-case
2356 (funcall 'bug-657499-bar)
2357 (type-error (e)
2358 (assert (eq 'cons (type-error-expected-type e)))
2359 (assert (equal "foobar" (type-error-datum e)))
2360 :type-error))))))
2362 (declaim (unsigned-byte *symbol-value-test-var*))
2363 (defvar *symbol-value-test-var*)
2365 (declaim (unsigned-byte **global-symbol-value-test-var**))
2366 (defglobal **global-symbol-value-test-var** 0)
2368 (test-util:with-test (:name :symbol-value-type-derivation)
2369 (let ((fun (compile
2371 `(lambda ()
2372 *symbol-value-test-var*))))
2373 (assert (equal '(function () (values unsigned-byte &optional))
2374 (%simple-fun-type fun))))
2375 (let ((fun (compile
2377 `(lambda ()
2378 **global-symbol-value-test-var**))))
2379 (assert (equal '(function () (values unsigned-byte &optional))
2380 (%simple-fun-type fun))))
2381 (let ((fun (compile
2383 `(lambda (*symbol-value-test-var*)
2384 (declare (fixnum *symbol-value-test-var*))
2385 (symbol-value '*symbol-value-test-var*))))
2386 (ufix (type-specifier (specifier-type `(and unsigned-byte fixnum)))))
2387 (assert (equal `(function (,ufix) (values ,ufix &optional))
2388 (%simple-fun-type fun))))
2389 (let ((fun (compile
2391 `(lambda ()
2392 (declare (fixnum **global-symbol-value-test-var**))
2393 (symbol-global-value '**global-symbol-value-test-var**))))
2394 (ufix (type-specifier (specifier-type `(and unsigned-byte fixnum)))))
2395 (assert (equal `(function () (values ,ufix &optional))
2396 (%simple-fun-type fun)))))
2398 (test-util:with-test (:name :mv-bind-to-let-type-propagation)
2399 (let ((f (test-util:checked-compile
2400 `(lambda (x)
2401 (declare (optimize speed)
2402 (type (integer 20 50) x))
2403 (< (truncate x 10) 1))))
2404 (g (test-util:checked-compile
2405 `(lambda (x)
2406 (declare (optimize speed)
2407 (type (integer 20 50) x))
2408 (< (nth-value 1 (truncate x 10)) 10))))
2409 (h (test-util:checked-compile
2410 `(lambda (x)
2411 (declare (optimize speed)
2412 (type (integer 20 50) x))
2413 (multiple-value-bind (q r)
2414 (truncate x 10)
2415 (declare (ignore r))
2416 (< q 1)))))
2417 (type0 '(function ((integer 20 50)) (values null &optional)))
2418 (type1 '(function ((integer 20 50)) (values (member t) &optional))))
2419 (assert (equal type0 (sb-kernel:%simple-fun-type f)))
2420 (assert (equal type1 (sb-kernel:%simple-fun-type g)))
2421 (assert (equal type0 (sb-kernel:%simple-fun-type h)))))
2423 (test-util:with-test (:name :bug-308921)
2424 (let ((*check-consistency* t))
2425 (ctu:file-compile
2426 `((let ((exported-symbols-alist
2427 (loop for symbol being the external-symbols of :cl
2428 collect (cons symbol
2429 (concatenate 'string
2431 (string-downcase symbol))))))
2432 (defun hyperdoc-lookup (symbol)
2433 (cdr (assoc symbol exported-symbols-alist)))))
2434 :load nil)))
2436 (test-util:with-test (:name :bug-308941)
2437 (multiple-value-bind (warn fail)
2438 (let ((*check-consistency* t))
2439 (ctu:file-compile
2440 "(eval-when (:compile-toplevel :load-toplevel :execute)
2441 (defstruct foo3))
2442 (defstruct bar
2443 (foo #.(make-foo3)))"
2444 :load nil))
2445 ;; ...but the compiler should not break.
2446 (assert (and warn fail))))
2448 (test-util:with-test (:name :bug-903821)
2449 (let* ((fun (test-util:checked-compile
2450 '(lambda (x n)
2451 (declare (sb-ext:word x)
2452 (type (integer 0 #.(1- sb-vm:n-machine-word-bits)) n)
2453 (optimize speed))
2454 (logandc2 x (ash -1 n)))))
2455 (thing-not-to-call
2456 (intern (format nil "ASH-LEFT-MOD~D" sb-vm::n-machine-word-bits) "SB-VM")))
2457 (assert (not (member (symbol-function thing-not-to-call)
2458 (ctu:find-named-callees fun))))
2459 (assert (= 7 (funcall fun 15 3)))))
2461 (test-util:with-test (:name :bug-997528)
2462 (let ((fun (test-util:checked-compile
2463 '(lambda (x)
2464 (declare (optimize (speed 0) (space 0))
2465 (type (integer -228645653448155482 -228645653447928749) x))
2466 (floor 1.0 (the (integer -228645653448151677 -228645653448150900) x))))))
2467 (multiple-value-bind (quo rem)
2468 (funcall fun -228645653448151381)
2469 (assert (= quo -1))
2470 (assert (= rem (float -228645653448151381))))))
2472 (defmacro def-many-code-constants ()
2473 `(defun many-code-constants ()
2474 ,@(loop for i from 0 below 1000
2475 collect `(print ,(format nil "hi-~d" i)))))
2477 (test-util:with-test (:name :many-code-constants)
2478 (def-many-code-constants)
2479 (assert (search "hi-999"
2480 (with-output-to-string (*standard-output*)
2481 (many-code-constants)))))
2483 (test-util:with-test (:name :bug-943953)
2484 ;; we sometimes splice compiler structures like clambda in
2485 ;; source, and our error reporting would happily use that
2486 ;; as source forms.
2487 (let* ((src "bug-943953.lisp")
2488 (obj (compile-file-pathname src)))
2489 (unwind-protect (compile-file src)
2490 (ignore-errors (delete-file obj)))))
2492 (declaim (inline vec-1177703))
2493 (defstruct (vec-1177703 (:constructor vec-1177703 (&optional x)))
2494 (x 0.0d0 :type double-float))
2496 (declaim (inline norm-1177703))
2497 (defun norm-1177703 (v)
2498 (vec-1177703 (sqrt (vec-1177703-x v))))
2500 (test-util:with-test (:name :bug-1177703)
2501 (test-util:checked-compile `(lambda (x)
2502 (norm-1177703 (vec-1177703 x)))))
2504 (declaim (inline call-1035721))
2505 (defun call-1035721 (function)
2506 (lambda (x)
2507 (funcall function x)))
2509 (declaim (inline identity-1035721))
2510 (defun identity-1035721 (x)
2513 (test-util:with-test (:name :bug-1035721)
2514 (test-util:checked-compile
2515 `(lambda ()
2516 (list
2517 (call-1035721 #'identity-1035721)
2518 (lambda (x)
2519 (identity-1035721 x))))))
2521 (test-util:with-test (:name :expt-type-derivation-and-method-redefinition)
2522 (defmethod expt-type-derivation ((x list) &optional (y 0.0))
2523 (declare (type float y))
2524 (expt 2 y))
2525 ;; the redefinition triggers a type lookup of the old
2526 ;; fast-method-function's type, which had a bogus type specifier of
2527 ;; the form (double-float 0) from EXPT type derivation
2528 (defmethod expt-type-derivation ((x list) &optional (y 0.0))
2529 (declare (type float y))
2530 (expt 2 y)))
2532 ;; Lp# 1066451 - declarations were either misplaced or dropped
2533 ;; depending on whether the :policy argument was used in DEFTRANSFORM.
2534 ;; That was a bit random. :policy doesn't affect whether decls
2535 ;; are accepted now.
2536 (defun foo (blah)
2537 (declare (special randomness-factor))
2538 (if (constant-lvar-p randomness-factor)
2539 (format nil "Weird transform answer is ~D"
2540 (+ (lvar-value randomness-factor) blah))))
2541 (defknown weird-fn (integer symbol &key (:magic real)) t)
2542 (deftransform weird-fn ((x s &key ((:magic randomness-factor)))
2543 (fixnum t &key (:magic fixnum)))
2544 ;; I can't see much use for declarations other than SPECIAL here,
2545 ;; but we shouldn't supposedly allow them and then not handle them right.
2546 (declare (special fred) (special randomness-factor) (lvar x s))
2547 (foo fred))
2548 (test-util:with-test (:name :deftransform-bug-1066451)
2549 (let ((f (let ((fred 3))
2550 (declare (special fred))
2551 (test-util:checked-compile
2552 '(lambda () (weird-fn 2 'foo :magic 11))))))
2553 (assert (string= (funcall f)
2554 "Weird transform answer is 14"))))
2556 (defun skip-1-passthrough (a b sb-int:&more context count)
2557 (declare (ignore a b))
2558 (multiple-value-call 'list
2559 'start
2560 (sb-c::%more-arg-values context 1 (1- (truly-the fixnum count)))
2561 'end))
2562 (defun skip-2-passthrough (a b sb-int:&more context count)
2563 (declare (ignore a b))
2564 (multiple-value-call 'list
2565 'start
2566 (sb-c::%more-arg-values context 2 (- (truly-the fixnum count) 2))
2567 'end))
2568 (defun skip-n-passthrough (n-skip n-copy sb-int:&more context count)
2569 (assert (>= count (+ n-copy n-skip))) ; prevent crashes
2570 (multiple-value-call 'list
2571 'start
2572 (sb-c::%more-arg-values context n-skip n-copy)
2573 'end))
2575 ;; %MORE-ARG-VALUES was wrong on x86 and x86-64 with nonzero 'skip'.
2576 ;; It's entirely possible that other backends are also not working.
2577 (test-util:with-test (:name :more-arg-fancy
2578 :skipped-on :interpreter)
2579 (assert (equal (skip-1-passthrough 0 0 'a 'b 'c 'd 'e 'f)
2580 '(start b c d e f end)))
2581 (assert (equal (skip-2-passthrough 0 0 'a 'b 'c 'd 'e 'f)
2582 '(start c d e f end)))
2583 (assert (equal (skip-n-passthrough 1 5 'a 'b 'c 'd 'e 'f)
2584 '(start b c d e f end)))
2585 (assert (equal (skip-n-passthrough 1 5 'a 'b 'c 'd 'e 'f 'g)
2586 '(start b c d e f end)))
2587 (assert (equal (skip-n-passthrough 2 5 'a 'b 'c 'd 'e 'f 'g)
2588 '(start c d e f g end)))
2589 (assert (equal (skip-n-passthrough 2 5 'a 'b 'c 'd 'e 'f 'g 'h)
2590 '(start c d e f g end))))
2592 (test-util:with-test (:name :macro-policy)
2593 (flet ((count-notes ()
2594 (let ((count 0))
2595 (handler-bind ((compiler-note
2596 (lambda (c)
2598 (incf count)
2599 (muffle-warning))))
2600 (multiple-value-bind (fasl warnings errors)
2601 (compile-file "macro-policy-test.lisp"
2602 :print nil :verbose nil)
2603 (ignore-errors (delete-file fasl))
2604 (assert (and (not warnings) (not errors)))
2605 count)))))
2606 (let* ((baseline (count-notes))
2607 (test (progv '(*frob-macro-policy*) '(t) (count-notes)))
2608 (baseline-again (count-notes)))
2609 (assert (/= 0 baseline))
2610 (assert (= 0 test))
2611 ;; macro-policy is rebound inside compile-file
2612 (assert (= baseline-again baseline)))))
2614 (in-package :cl-user)
2616 (with-test (:name :merge-lambdas-dead-return)
2617 (let ((fasl (compile-file "merge-lambdas.lisp"
2618 :print nil :verbose nil)))
2619 (ignore-errors (delete-file fasl))))
2621 (declaim (inline ensure-a-fun))
2622 (defun ensure-a-fun (f) (coerce f 'function))
2623 (defmacro compose2 (f g)
2624 `(let ((f (ensure-a-fun ,f))
2625 (g (ensure-a-fun ,g)))
2626 (lambda (arg) (funcall f (funcall g arg)))))
2628 (with-test (:name :coerce-to-function-smarter)
2629 (let ((f (checked-compile
2630 '(lambda (x)
2631 (funcall (compose2 #'integerp #'car) x)))))
2632 ;; should be completely inlined
2633 (assert (null (ctu:find-named-callees f)))))
2635 (with-test (:name :derived-function-type-casts)
2636 (let ((fasl (compile-file "derived-function-type-casts.lisp"
2637 :print nil :verbose nil)))
2638 (load fasl)
2639 (ignore-errors (delete-file fasl))
2640 (assert (null (funcall 'derived-function-type-casts)))))
2642 (with-test (:name (compile function :invalid-function-name))
2643 (flet ((test-case (nameoid)
2644 (assert (nth-value
2645 1 (checked-compile `(lambda () (function ,nameoid))
2646 :allow-failure t)))))
2647 (test-case 1)
2648 (test-case "foo")
2649 (test-case '(foo bar))))
2651 (defun catch-compiled-program-error (form &rest args)
2652 (multiple-value-bind (function failure-p)
2653 (checked-compile form :allow-failure t :allow-warnings t)
2654 (assert failure-p)
2655 (assert-error (apply function args))))
2657 (with-test (:name :duplicate-&key-no-error)
2658 (catch-compiled-program-error
2659 '(lambda () (defun duplicate-&key-no-error (&key a a) a))))
2661 (with-test (:name :bad-type-specifiers)
2662 (catch-compiled-program-error
2663 '(lambda (x) (typep x '(values 10)))
2665 (catch-compiled-program-error
2666 '(lambda () (declare (sb-ext:muffle-conditions 10)))))
2668 (with-test (:name :coverage-and-errors)
2669 (ctu:file-compile
2670 '((locally (declare (optimize sb-c:store-coverage-data))
2671 (1)))))
2673 (symbol-macrolet ((x 30))
2674 (macrolet ((foo (y) (+ x y)))
2675 (declaim (inline environment-around-inline))
2676 (defun environment-around-inline (z)
2677 (* z (foo 4)))))
2679 (with-test (:name :environment-around-inline)
2680 (defun environment-around-inline.2 (z)
2681 (environment-around-inline z))
2682 (assert (= (environment-around-inline.2 10) 340)))
2684 (with-test (:name :defun-setf-return-value)
2685 (let ((name `(setf ,(gensym))))
2686 (assert (equal (eval `(defun ,name ()))
2687 name))))
2689 (with-test (:name :make-sequence-unknown)
2690 (let ((fun (checked-compile
2691 `(lambda (x)
2692 (let ((vector (make-sequence '(simple-array make-sequence-unknown (*)) 10)))
2693 (setf (aref vector 0) x)
2694 vector))
2695 :allow-style-warnings t)))
2696 (deftype make-sequence-unknown () 'fixnum)
2697 (assert-error (funcall fun 'abc) type-error)))
2699 (with-test (:name (:compiler-messages function :type-specifier))
2700 ;; Previously, function types were often printed confusingly, e.g.:
2702 ;; (function ()) => #'NIL
2703 ;; (function *) => #'*
2704 ;; (function (function a)) => #'#'A
2706 (flet ((test-case (spec)
2707 (destructuring-bind (type expected) spec
2708 (unwind-protect
2709 (let ((report))
2710 (proclaim '(ftype (function () boolean) my-function))
2711 (handler-bind ((warning
2712 (lambda (condition)
2713 (setf report (princ-to-string condition))
2714 (muffle-warning))))
2715 (proclaim `(ftype ,type my-function)))
2716 (assert (search expected report)))
2717 (fmakunbound 'my-function)))))
2718 (mapc
2719 #'test-case
2720 `(((function ()) "(FUNCTION NIL)")
2721 ((function *) "(FUNCTION *)")
2722 ((function (function *)) "(FUNCTION (FUNCTION *))")
2723 ((function (function (eql 1))) "(FUNCTION (FUNCTION (EQL 1))")))))
2725 (with-test (:name :boxed-ref-setf-special)
2726 (let* ((var (gensym))
2727 (fun (checked-compile `(lambda ()
2728 (declare (special ,var))
2729 (setf ,var 10d0)))))
2730 (ctu:assert-no-consing (funcall fun))))
2732 (with-test (:name :boxed-ref-bind-special)
2733 (let* ((var (gensym))
2734 (fun (checked-compile `(lambda ()
2735 (let ((,var 10d0))
2736 (declare (special ,var)))))))
2737 (ctu:assert-no-consing (funcall fun))))
2739 (with-test (:name :boxed-ref-svref)
2740 (let ((fun (checked-compile `(lambda (x)
2741 (setf (svref x 0) 10d0))))
2742 (vector (vector nil)))
2743 (ctu:assert-no-consing (funcall fun vector))))
2745 (with-test (:name :boxed-ref-instance-set)
2746 (let* ((name (gensym "STRUCT"))
2747 (fun (progn
2748 (eval `(defstruct ,name x))
2749 (checked-compile `(lambda (x)
2750 (setf (,(sb-int:symbolicate name '-x) x)
2751 10d0)))))
2752 (instance (funcall (sb-int:symbolicate 'make- name))))
2753 (ctu:assert-no-consing (funcall fun instance))))
2755 (with-test (:name :boxed-ref-car)
2756 (let ((fun (checked-compile `(lambda (x)
2757 (setf (car x) 10d0)
2758 (setf (cdr x) 10d0))))
2759 (list (list nil)))
2760 (ctu:assert-no-consing (funcall fun list))))