Declare types of *MACHINE-VERSION*, *{SHORT,LONG}-SITE-NAME*, *ED-FUNCTIONS*
[sbcl.git] / tests / compiler.impure.lisp
blob25758e2cdc65f817db42b8942aa7a32778dd637d
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 (core-cell (ctu:find-value-cell-values core-fun))
1265 (defun-form `(defun ,name ()
1266 (load-time-value ,form)))
1267 (file-fun (progn
1268 (ctu:file-compile (list defun-form) :load t)
1269 (symbol-function name)))
1270 (file-type (funtype file-fun))
1271 (file-cell (ctu:find-value-cell-values file-fun)))
1272 (if value-cell-p
1273 (assert (and core-cell file-cell))
1274 (assert (not (or core-cell file-cell))))
1275 (unless (subtypep core-type type1)
1276 (error "core: wanted ~S, got ~S" type1 core-type))
1277 (unless (subtypep file-type type2)
1278 (error "file: wanted ~S, got ~S" type2 file-type)))))
1279 (let ((* 10))
1280 (test '(integer 11 11) 'number
1281 '(+ * 1) nil))
1282 (let ((* "fooo"))
1283 (test '(integer 4 4) 'unsigned-byte
1284 '(length *) nil))
1285 (test '(integer 10 10) '(integer 10 10) 10 nil)
1286 (test 'cons 'cons '(cons t t) t))))
1288 (with-test (:name (load-time-value :errors))
1289 (multiple-value-bind (warn fail)
1290 (ctu:file-compile
1291 `((defvar *load-time-value-error-value* 10)
1292 (declaim (fixnum *load-time-value-error-value*))
1293 (defun load-time-value-error-test-1 ()
1294 (the list (load-time-value *load-time-value-error-value*))))
1295 :load t)
1296 (assert warn)
1297 (assert fail))
1298 (handler-case (load-time-value-error-test-1)
1299 (type-error (e)
1300 (and (eql 10 (type-error-datum e))
1301 (eql 'list (type-error-expected-type e)))))
1302 (multiple-value-bind (warn2 fail2)
1303 (ctu:file-compile
1304 `((defun load-time-value-error-test-2 ()
1305 (the list (load-time-value 10))))
1306 :load t)
1307 (assert warn2)
1308 (assert fail2))
1309 (handler-case (load-time-value-error-test-2)
1310 (type-error (e)
1311 (and (eql 10 (type-error-datum e))
1312 (eql 'list (type-error-expected-type e))))))
1314 ;;;; tests for compiler output
1315 (with-test (:name :unexpected-compiler-output)
1316 (let* ((*error-output* (make-string-output-stream))
1317 (output (with-output-to-string (*standard-output*)
1318 (compile-file "compiler-output-test.lisp"
1319 :print nil :verbose nil))))
1320 (unless (zerop (length output))
1321 (error "Unexpected output: ~S" output))))
1323 (with-test (:name :bug-493380)
1324 (flet ((test (forms)
1325 (catch 'debug
1326 (let ((*debugger-hook* (lambda (condition if)
1327 (throw 'debug
1328 (if (typep condition 'serious-condition)
1329 :debug
1330 :oops)))))
1331 (multiple-value-bind (warned failed) (ctu:file-compile forms)
1332 (when (and warned failed)
1333 :failed))))))
1334 (assert (eq :failed (test "(defun")))
1335 (assert (eq :failed (test "(defun no-pkg::foo ())")))
1336 (assert (eq :failed (test "(cl:no-such-sym)")))
1337 (assert (eq :failed (test "...")))))
1339 (defun cmacro-signals-error () :fun)
1340 (define-compiler-macro cmacro-signals-error () (error "oops"))
1342 (with-test (:name :cmacro-signals-error)
1343 (multiple-value-bind (fun failure-p warnings)
1344 (checked-compile `(lambda () (cmacro-signals-error))
1345 :allow-failure t :allow-warnings t)
1346 (assert (and fun failure-p warnings))
1347 (assert (eq :fun (funcall fun)))))
1349 (defun cmacro-with-simple-key (&key a)
1350 (format nil "fun=~A" a))
1351 (define-compiler-macro cmacro-with-simple-key (&whole form &key a)
1352 (if (constantp a)
1353 (format nil "cmacro=~A" (eval a))
1354 form))
1356 (with-test (:name (:cmacro-with-simple-key :no-key))
1357 (let ((fun (checked-compile `(lambda () (cmacro-with-simple-key)))))
1358 (assert (string= "cmacro=NIL" (funcall fun)))))
1360 (with-test (:name (:cmacro-with-simple-key :constant-key))
1361 (let ((fun (checked-compile `(lambda () (cmacro-with-simple-key :a 42)))))
1362 (assert (string= "cmacro=42" (funcall fun)))))
1364 (with-test (:name (:cmacro-with-simple-key :variable-key))
1365 (let ((fun (checked-compile `(lambda (x) (cmacro-with-simple-key x 42)))))
1366 (assert (string= "fun=42" (funcall fun :a)))))
1368 (defun cmacro-with-nasty-key (&key ((nasty-key var)))
1369 (format nil "fun=~A" var))
1370 (define-compiler-macro cmacro-with-nasty-key (&whole form &key ((nasty-key var)))
1371 (if (constantp var)
1372 (format nil "cmacro=~A" (eval var))
1373 form))
1375 (with-test (:name (:cmacro-with-nasty-key :no-key))
1376 (let ((fun (checked-compile `(lambda () (cmacro-with-nasty-key)))))
1377 (assert (string= "cmacro=NIL" (funcall fun)))))
1379 (with-test (:name (:cmacro-with-nasty-key :constant-key))
1380 ;; This bogosity is thanks to cmacro lambda lists being /macro/ lambda
1381 ;; lists.
1382 (let ((fun (checked-compile
1383 `(lambda () (cmacro-with-nasty-key 'nasty-key 42)))))
1384 (assert (string= "fun=42" (funcall fun)))))
1386 (with-test (:name (:cmacro-with-nasty-key :variable-key))
1387 (let ((fun (checked-compile
1388 `(lambda (nasty-key) (cmacro-with-nasty-key nasty-key 42)))))
1389 (assert (string= "fun=42" (funcall fun 'nasty-key)))))
1391 (defconstant tricky-key 'tricky-key)
1392 (defun cmacro-with-tricky-key (&key ((tricky-key var)))
1393 (format nil "fun=~A" var))
1394 (define-compiler-macro cmacro-with-tricky-key (&whole form &key ((tricky-key var)))
1395 (if (constantp var)
1396 (format nil "cmacro=~A" (eval var))
1397 form))
1399 (with-test (:name (:cmacro-with-tricky-key :no-key))
1400 (let ((fun (checked-compile
1401 `(lambda () (cmacro-with-tricky-key)))))
1402 (assert (string= "cmacro=NIL" (funcall fun)))))
1404 (with-test (:name (:cmacro-with-tricky-key :constant-quoted-key))
1405 ;; This bogosity is thanks to cmacro lambda lists being /macro/ lambda
1406 ;; lists.
1407 (let ((fun (checked-compile
1408 `(lambda () (cmacro-with-tricky-key 'tricky-key 42)))))
1409 (assert (string= "fun=42" (funcall fun)))))
1411 (with-test (:name (:cmacro-with-tricky-key :constant-unquoted-key))
1412 (let ((fun (checked-compile
1413 `(lambda () (cmacro-with-tricky-key tricky-key 42)))))
1414 (assert (string= "cmacro=42" (funcall fun)))))
1416 (with-test (:name (:cmacro-with-tricky-key :variable-key))
1417 (let ((fun (checked-compile
1418 `(lambda (x) (cmacro-with-tricky-key x 42)))))
1419 (assert (string= "fun=42" (funcall fun 'tricky-key)))))
1421 (defun test-function-983 (x) x)
1422 (define-compiler-macro test-function-983 (x) x)
1424 (with-test (:name :funcall-compiler-macro)
1425 (assert
1426 (handler-case
1427 (and (compile nil
1428 `(lambda ()
1429 (funcall (function test-function-983 junk) 1)))
1430 nil)
1431 (sb-c:compiler-error () t))))
1433 (defsetf test-984 %test-984)
1435 (with-test (:name :setf-function-with-setf-expander)
1436 (assert
1437 (handler-case
1438 (and (defun (setf test-984) ())
1439 nil)
1440 (style-warning () t)))
1441 (checked-compile `(lambda () #'(setf test-984))
1442 :allow-style-warnings t))
1444 (with-test (:name :compile-setf-function)
1445 (defun (setf compile-setf) ())
1446 (assert (equal (compile '(setf compile-setf))
1447 '(setf compile-setf))))
1449 (declaim (inline cut-test))
1450 (defun cut-test (b)
1451 (cond ((integerp b) b)
1452 (b 469)
1453 (t 2)))
1455 (with-test (:name :cut-to-width-bad-constant)
1456 (assert (= (funcall (checked-compile
1457 `(lambda ()
1458 (multiple-value-bind (a b) (values t t)
1459 (declare (ignore b))
1460 (mask-field (byte 10 0) (cut-test a))))))
1461 469)))
1463 ;; META: there's a test in compiler.pure.lisp that also tests
1464 ;; interaction of PROGV with (debug 3). These tests should be together.
1465 (with-test (:name :progv-debug-3)
1466 (unwind-protect
1467 (sb-ext:restrict-compiler-policy 'debug 3)
1468 (assert (funcall (checked-compile
1469 `(lambda (*v*)
1470 (declare (special *v*))
1471 (progv '(*v*) '())
1472 (boundp '*v*)))
1474 (sb-ext:restrict-compiler-policy 'debug 0)))
1476 (with-test (:name :restrict-compiler-policy-result)
1477 (let ((sb-c::*policy-restrictions* sb-c::*policy-restrictions*))
1478 (sb-ext:restrict-compiler-policy 'safety 2)
1479 (checked-compile '(lambda () (declare (optimize (safety 0))))))
1480 (let ((sb-c::*policy-restrictions* sb-c::*policy-restrictions*))
1481 ;; Passing no arguments returns the current quality/value pairs.
1482 (assert (null (sb-ext:restrict-compiler-policy)))
1483 (let ((res (sb-ext:restrict-compiler-policy 'safety 2)))
1484 ;; returns an alist
1485 (assert (equal res '((safety . 2)))))
1486 (let ((res (sb-ext:restrict-compiler-policy 'debug 3)))
1487 ;; returns an alist, indeterminate order
1488 (assert (or (equal res '((safety . 2) (debug . 3)))
1489 (equal res '((debug . 3) (safety . 2))))))
1490 ;; remove the SAFETY restriction
1491 (let ((res (sb-ext:restrict-compiler-policy 'safety 0)))
1492 (assert (equal res '((debug . 3)))))
1493 ;; remove the DEBUG restriction
1494 (let ((res (sb-ext:restrict-compiler-policy 'debug 0)))
1495 (assert (null res)))))
1497 ;;;; tests not in the problem domain, but of the consistency of the
1498 ;;;; compiler machinery itself
1500 (in-package "SB-C")
1502 ;;; Hunt for wrong-looking things in fundamental compiler definitions,
1503 ;;; and gripe about them.
1505 ;;; FIXME: It should be possible to (1) repair the things that this
1506 ;;; code gripes about, and then (2) make the code signal errors
1507 ;;; instead of just printing complaints to standard output, in order
1508 ;;; to prevent the code from later falling back into disrepair.
1509 (defun grovel-results (function)
1510 (dolist (template (fun-info-templates (info :function :info function)))
1511 (when (template-more-results-type template)
1512 (format t "~&Template ~A has :MORE results, and translates ~A.~%"
1513 (template-name template)
1514 function)
1515 (return nil))
1516 (when (eq (template-result-types template) :conditional)
1517 ;; dunno.
1518 (return t))
1519 (let ((types (template-result-types template))
1520 (result-type (fun-type-returns (proclaimed-ftype function))))
1521 (cond
1522 ((values-type-p result-type)
1523 (do ((ltypes (append (args-type-required result-type)
1524 (args-type-optional result-type))
1525 (rest ltypes))
1526 (types types (rest types)))
1527 ((null ltypes)
1528 (unless (null types)
1529 (format t "~&More types than ltypes in ~A, translating ~A.~%"
1530 (template-name template)
1531 function)
1532 (return nil)))
1533 (when (null types)
1534 (unless (null ltypes)
1535 (format t "~&More ltypes than types in ~A, translating ~A.~%"
1536 (template-name template)
1537 function)
1538 (return nil)))))
1539 ((eq result-type (specifier-type nil))
1540 (unless (null types)
1541 (format t "~&Template ~A returns values for function ~A with RESULT-TYPE NIL.~%"
1542 (template-name template)
1543 function)
1544 (return nil)))
1545 ((/= (length types) 1)
1546 (format t "~&Template ~A isn't returning 1 value for ~A.~%"
1547 (template-name template)
1548 function)
1549 (return nil))
1550 (t t)))))
1551 (test-util:with-test (:name :identify-suspect-vops)
1552 (sb-c::call-with-each-globaldb-name
1553 (lambda (name)
1554 ;; LEGAL-FUN-NAME-P test is necessary, since (INFO :FUNCTION :TYPE)
1555 ;; has a defaulting expression that involves calling FDEFINITION.
1556 (when (and (legal-fun-name-p name) (proclaimed-ftype name))
1557 ;; OK, so we have an entry in the INFO database. Now, if ...
1558 (binding* ((info (info :function :info name) :exit-if-null)
1559 (nil (fun-info-templates info) :exit-if-null))
1560 ;; ... it has translators
1561 (grovel-results name))))))
1563 ;;;; bug 305: INLINE/NOTINLINE causing local ftype to be lost
1565 (test-util:with-test (:name (compile inline notinline))
1566 (labels ((compile-lambda (type sense allow-notes)
1567 (nth-value
1568 4 (test-util:checked-compile
1569 `(lambda ()
1570 (declare
1571 ,@(when type '((ftype (function () (integer 0 10)) bug-305)))
1572 (,sense bug-305)
1573 (optimize speed))
1574 (1+ (bug-305)))
1575 :allow-style-warnings t :allow-notes allow-notes))))
1576 (assert (compile-lambda nil 'inline t))
1577 (assert (compile-lambda nil 'notinline t))
1578 (compile-lambda t 'inline nil)
1579 (compile-lambda t 'notinline nil)))
1581 ;;; bug 211e: bogus style warning from duplicated keyword argument to
1582 ;;; a local function.
1583 (test-util:with-test (:name (compile flet :duplicate &key :bug-211e))
1584 (let ((f (test-util:checked-compile
1585 '(lambda ()
1586 (flet ((foo (&key y) (list y)))
1587 (list (foo :y 1 :y 2)))))))
1588 (assert (equal '((1)) (funcall f)))))
1590 ;;; check that EQL is optimized when other argument is (OR SYMBOL FIXNUM).
1591 (test-util:with-test (:name (compile :optimize eql))
1592 (let ((f1 (test-util:checked-compile
1593 '(lambda (x1 y1)
1594 (declare (type (or symbol fixnum) x1)
1595 (optimize speed))
1596 (eql x1 y1))
1597 :allow-notes nil))
1598 (f2 (test-util:checked-compile
1599 '(lambda (x2 y2)
1600 (declare (type (or symbol fixnum) y2)
1601 (optimize speed))
1602 (eql x2 y2))
1603 :allow-notes nil)))
1604 (let ((fix (random most-positive-fixnum))
1605 (sym (gensym))
1606 (e-count 0))
1607 (assert (funcall f1 fix fix))
1608 (assert (funcall f2 fix fix))
1609 (assert (funcall f1 sym sym))
1610 (assert (funcall f2 sym sym))
1611 (handler-bind ((type-error (lambda (c)
1612 (incf e-count)
1613 (continue c))))
1614 (flet ((test (f x y)
1615 (with-simple-restart (continue "continue with next test")
1616 (funcall f x y)
1617 (error "fell through with (~S ~S ~S)" f x y))))
1618 (test f1 "oops" 42)
1619 (test f1 (1+ most-positive-fixnum) 42)
1620 (test f2 42 "oops")
1621 (test f2 42 (1+ most-positive-fixnum))))
1622 (assert (= e-count 4)))))
1624 ;;; bug #389 (Rick Taube sbcl-devel)
1625 (defun bes-jn (unn ux)
1626 (let ((nn unn) (x ux))
1627 (let* ((n (floor (abs nn)))
1628 (besn
1629 (if (= n 0)
1630 (bes-j0 x)
1631 (if (= n 1)
1632 (bes-j1 x)
1633 (if (zerop x)
1635 (let ((iacc 40)
1636 (ans 0.0)
1637 (bigno 1.0e+10)
1638 (bigni 1.0e-10))
1639 (if (> (abs x) n)
1640 (do ((tox (/ 2.0 (abs x)))
1641 (bjm (bes-j0 (abs x)))
1642 (bj (bes-j1 (abs x)))
1643 (j 1 (+ j 1))
1644 (bjp 0.0))
1645 ((= j n) (setf ans bj))
1646 (setf bjp (- (* j tox bj) bjm))
1647 (setf bjm bj)
1648 (setf bj bjp))
1649 (let ((tox (/ 2.0 (abs x)))
1651 (* 2
1652 (floor
1653 (/ (+ n (sqrt (* iacc n)))
1654 2))))
1655 (jsum 0.0)
1656 (bjm 0.0)
1657 (sum 0.0)
1658 (bjp 0.0)
1659 (bj 1.0))
1660 (do ((j m (- j 1)))
1661 ((= j 0))
1662 (setf bjm (- (* j tox bj) bjp))
1663 (setf bjp bj)
1664 (setf bj bjm)
1665 (when (> (abs bj) bigno)
1666 (setf bj (* bj bigni))
1667 (setf bjp (* bjp bigni))
1668 (setf ans (* ans bigni))
1669 (setf sum (* sum bigni)))
1670 (if (not (= 0 jsum)) (incf sum bj))
1671 (setf jsum (- 1 jsum))
1672 (if (= j n) (setf ans bjp)))
1673 (setf sum (- (* 2.0 sum) bj))
1674 (setf ans (/ ans sum))))
1675 (if (and (minusp x) (oddp n))
1676 (- ans)
1677 ans)))))))
1678 (if (and (minusp nn) (oddp nn)) (- besn) besn))))
1681 ;;; bug 233b: lvar lambda-var equality in constraint propagation
1683 ;; Put this in a separate function.
1684 (defun test-constraint-propagation/ref ()
1685 (let ((x nil))
1686 (if (multiple-value-prog1 x (setq x t))
1688 x)))
1690 (test-util:with-test (:name (:compiler :constraint-propagation :ref))
1691 (assert (eq t (test-constraint-propagation/ref))))
1693 ;; Put this in a separate function.
1694 (defun test-constraint-propagation/typep (x y)
1695 (if (typep (multiple-value-prog1 x (setq x y))
1696 'double-float)
1697 (+ x 1d0)
1698 (+ x 2)))
1700 (test-util:with-test (:name (:compiler :constraint-propagation :typep))
1701 (assert (= 6.0d0 (test-constraint-propagation/typep 1d0 5))))
1703 (test-util:with-test (:name (:compiler :constraint-propagation :eq/eql))
1704 (assert (eq :right (let ((c :wrong))
1705 (if (eq (let ((x c))
1706 (setq c :right)
1708 :wrong)
1710 0)))))
1712 ;;; Put this in a separate function.
1713 (defun test-constraint-propagation/cast (x)
1714 (when (the double-float (multiple-value-prog1
1716 (setq x (1+ x))))
1719 (test-util:with-test (:name (:compiler :constraint-propagation :cast))
1720 (assertoid:assert-error
1721 (test-constraint-propagation/cast 1) type-error))
1723 ;;; bug #399
1724 (let ((result (make-array 50000 :fill-pointer 0 :adjustable t)))
1725 (defun string->html (string &optional (max-length nil))
1726 (when (and (numberp max-length)
1727 (> max-length (array-dimension result 0)))
1728 (setf result (make-array max-length :fill-pointer 0 :adjustable t)))
1729 (let ((index 0)
1730 (left-quote? t))
1731 (labels ((add-char (it)
1732 (setf (aref result index) it)
1733 (incf index))
1734 (add-string (it)
1735 (loop for ch across it do
1736 (add-char ch))))
1737 (loop for char across string do
1738 (cond ((char= char #\<)
1739 (add-string "&lt;"))
1740 ((char= char #\>)
1741 (add-string "&gt;"))
1742 ((char= char #\&)
1743 (add-string "&amp;"))
1744 ((char= char #\')
1745 (add-string "&#39;"))
1746 ((char= char #\newline)
1747 (add-string "<br>"))
1748 ((char= char #\")
1749 (if left-quote? (add-string "&#147;") (add-string "&#148;"))
1750 (setf left-quote? (not left-quote?)))
1752 (add-char char))))
1753 (setf (fill-pointer result) index)
1754 (coerce result 'string)))))
1756 ;;; Calling thru constant symbols
1757 (require :sb-introspect)
1759 (declaim (inline target-fun))
1760 (defun target-fun (arg0 arg1)
1761 (+ arg0 arg1))
1762 (declaim (notinline target-fun))
1764 ;; FIXME: should use compiler-test-util, not sb-introspect here.
1765 ;; That issue aside, neither sb-introspect nor ctu:find-named-callees
1766 ;; can examine an interpreted function for its callees,
1767 ;; so we can't actually use this function.
1768 (defun test-target-fun-called (fun res)
1769 (assert (member #'target-fun
1770 (sb-introspect:find-function-callees #'caller-fun-1)))
1771 (assert (equal (funcall fun) res)))
1773 (defun caller-fun-1 ()
1774 (funcall 'target-fun 1 2))
1775 #-interpreter(test-target-fun-called #'caller-fun-1 3)
1777 (defun caller-fun-2 ()
1778 (declare (inline target-fun))
1779 (apply 'target-fun 1 '(3)))
1780 #-interpreter(test-target-fun-called #'caller-fun-2 4)
1782 (defun caller-fun-3 ()
1783 (flet ((target-fun (a b)
1784 (- a b)))
1785 (list (funcall #'target-fun 1 4) (funcall 'target-fun 1 4))))
1786 #-interpreter(test-target-fun-called #'caller-fun-3 (list -3 5))
1788 ;;; Reported by NIIMI Satoshi
1789 ;;; Subject: [Sbcl-devel] compilation error with optimization
1790 ;;; Date: Sun, 09 Apr 2006 17:36:05 +0900
1791 (defun test-minimal-debug-info-for-unstored-but-used-parameter (n a)
1792 (declare (optimize (speed 3)
1793 (debug 1)))
1794 (if (= n 0)
1796 (test-minimal-debug-info-for-unstored-but-used-parameter (1- n) a)))
1798 ;;; &KEY arguments with non-constant defaults.
1799 (declaim (notinline opaque-identity))
1800 (defun opaque-identity (x) x)
1801 (defstruct tricky-defaults
1802 (fun #'identity :type function)
1803 (num (opaque-identity 3) :type fixnum))
1804 (macrolet ((frob (form expected-expected-type)
1805 `(handler-case ,form
1806 (type-error (c) (assert (eq (type-error-expected-type c)
1807 ',expected-expected-type)))
1808 (:no-error (&rest vals) (error "~S returned values: ~S" ',form vals)))))
1809 (frob (make-tricky-defaults :fun 3) function)
1810 (frob (make-tricky-defaults :num #'identity) fixnum))
1812 (test-util:with-test (:name (compile &key :non-constant :default))
1813 (let ((fun (test-util:checked-compile
1814 '(lambda (&key (key (opaque-identity 3)))
1815 (declare (optimize safety) (type integer key))
1816 key))))
1817 (assert (= (funcall fun) 3))
1818 (assert (= (funcall fun :key 17) 17))
1819 (handler-case (funcall fun :key t)
1820 (type-error (c) (assert (eq (type-error-expected-type c) 'integer)))
1821 (:no-error (&rest vals) (declare (ignore vals)) (error "no error")))))
1823 ;;; Basic compiler-macro expansion
1824 (define-compiler-macro test-cmacro-0 () ''expanded)
1826 ;; The interpreter is not required to expand compiler-macros.
1827 ;; (Actually neither is the compiler!)
1828 #-interpreter(assert (eq 'expanded (funcall (lambda () (test-cmacro-0)))))
1830 ;;; FUNCALL forms in compiler macros, lambda-list parsing
1831 (define-compiler-macro test-cmacro-1
1832 (&whole whole a (a2) &optional b &rest c &key d)
1833 (list whole a a2 b c d))
1835 (macrolet ((test (form a a2 b c d)
1836 `(let ((form ',form))
1837 (destructuring-bind (whole a a2 b c d)
1838 (funcall (compiler-macro-function 'test-cmacro-1) form nil)
1839 (assert (equal whole form))
1840 (assert (eql a ,a))
1841 (assert (eql a2 ,a2))
1842 (assert (eql b ,b))
1843 (assert (equal c ,c))
1844 (assert (eql d ,d))))) )
1845 (test (funcall 'test-cmacro-1 1 (x) 2 :d 3) 1 'x 2 '(:d 3) 3)
1846 (test (test-cmacro-1 11 (y) 12 :d 13) 11 'y 12 '(:d 13) 13))
1848 ;;; FUNCALL forms in compiler macros, expansions
1849 (define-compiler-macro test-cmacro-2 () ''ok)
1851 #-interpreter(assert (eq 'ok (funcall (lambda () (funcall 'test-cmacro-2)))))
1852 #-interpreter(assert (eq 'ok (funcall (lambda () (funcall #'test-cmacro-2)))))
1854 ;;; Shadowing of compiler-macros by local functions
1855 (define-compiler-macro test-cmacro-3 () ''global)
1857 (defmacro find-cmacro-3 (&environment env)
1858 (compiler-macro-function 'test-cmacro-3 env))
1860 (assert (funcall (lambda () (find-cmacro-3))))
1861 (assert (not (funcall (lambda () (flet ((test-cmacro-3 ()))
1862 (find-cmacro-3))))))
1863 (assert (eq 'local (funcall (lambda () (flet ((test-cmacro-3 () 'local))
1864 (test-cmacro-3))))))
1865 (assert (eq 'local (funcall (lambda () (flet ((test-cmacro-3 () 'local))
1866 (funcall #'test-cmacro-3))))))
1867 #-interpreter
1868 (assert (eq 'global (funcall (lambda () (flet ((test-cmacro-3 () 'local))
1869 (funcall 'test-cmacro-3))))))
1871 ;;; Local NOTINLINE & INLINE
1872 (defun test-cmacro-4 () 'fun)
1873 (define-compiler-macro test-cmacro-4 () ''macro)
1875 (assert (eq 'fun (funcall (lambda ()
1876 (declare (notinline test-cmacro-4))
1877 (test-cmacro-4)))))
1879 #-interpreter
1880 (assert (eq 'macro (funcall (lambda ()
1881 (declare (inline test-cmacro-4))
1882 (test-cmacro-4)))))
1884 ;;; SETF function compiler macros
1885 (define-compiler-macro (setf test-cmacro-4) (&whole form value) ''ok)
1887 #-interpreter
1888 (assert (eq 'ok (funcall (lambda () (setf (test-cmacro-4) 'zot)))))
1889 #-interpreter
1890 (assert (eq 'ok (funcall (lambda () (funcall #'(setf test-cmacro-4) 'zot)))))
1892 ;;; Step instrumentation breaking type-inference
1893 (test-util:with-test (:name (compile :insert-step-conditions :type-inference))
1894 (assert (= 42 (funcall (test-util:checked-compile
1895 '(lambda (v x)
1896 (declare (optimize sb-c:insert-step-conditions))
1897 (if (typep (the function x) 'fixnum)
1898 (svref v (the function x))
1899 (funcall x))))
1900 nil (constantly 42)))))
1902 ;;; bug 368: array type intersections in the compiler
1903 (defstruct e368)
1904 (defstruct i368)
1905 (defstruct g368
1906 (i368s (make-array 0 :fill-pointer t) :type (or (vector i368) null)))
1907 (defstruct s368
1908 (g368 (error "missing :G368") :type g368 :read-only t))
1909 (declaim (ftype (function (fixnum (vector i368) e368) t) r368))
1910 (declaim (ftype (function (fixnum (vector e368)) t) h368))
1911 (defparameter *h368-was-called-p* nil)
1912 (defun nsu (vertices e368)
1913 (let ((i368s (g368-i368s (make-g368))))
1914 (let ((fuis (r368 0 i368s e368)))
1915 (format t "~&FUIS=~S~%" fuis)
1916 (or fuis (h368 0 i368s)))))
1917 (defun r368 (w x y)
1918 (declare (ignore w x y))
1919 nil)
1920 (defun h368 (w x)
1921 (declare (ignore w x))
1922 (setf *h368-was-called-p* t)
1923 (make-s368 :g368 (make-g368)))
1924 (let ((nsu (nsu #() (make-e368))))
1925 (format t "~&NSU returned ~S~%" nsu)
1926 (format t "~&*H368-WAS-CALLED-P*=~S~%" *h368-was-called-p*)
1927 (assert (s368-p nsu))
1928 (assert *h368-was-called-p*))
1930 ;;; bug 367: array type intersections in the compiler
1931 (defstruct e367)
1932 (defstruct i367)
1933 (defstruct g367
1934 (i367s (make-array 0 :fill-pointer t) :type (or (vector i367) null)))
1935 (defstruct s367
1936 (g367 (error "missing :G367") :type g367 :read-only t))
1937 (declaim (ftype (function ((vector i367) e367) (or s367 null)) r367))
1938 (declaim (ftype (function ((vector e367)) (values)) h367))
1939 (defun frob-367 (v w)
1940 (let ((x (g367-i367s (make-g367))))
1941 (let* ((y (or (r367 x w)
1942 (h367 x)))
1943 (z (s367-g367 y)))
1944 (format t "~&Y=~S Z=~S~%" y z)
1945 (g367-i367s z))))
1946 (defun r367 (x y) (declare (ignore x y)) nil)
1947 (defun h367 (x) (declare (ignore x)) (values))
1948 (assertoid:assert-error (frob-367 0 (make-e367)) type-error)
1950 (handler-case
1951 (delete-file (compile-file "circ-tree-test.lisp"))
1952 (storage-condition (e)
1953 (error e)))
1955 ;;; warnings due to step-instrumentation
1956 (defclass debug-test-class () ())
1957 (test-util:checked-compile
1958 '(lambda ()
1959 (declare (optimize (debug 3)))
1960 (defmethod print-object ((x debug-test-class) s)
1961 (call-next-method))))
1963 ;;; program-error from bad lambda-list keyword
1964 (test-util:with-test (:name (compile :bad lambda-list keyword program-error))
1965 (multiple-value-bind (fun failure-p)
1966 (test-util:checked-compile '(lambda (&whole x)
1967 (list &whole x))
1968 :allow-failure t)
1969 (assert failure-p)
1970 (assertoid:assert-error (funcall fun) program-error)))
1972 (test-util:with-test (:name (eval :bad lambda-list keyword program-error)
1973 :skipped-on (not :sb-eval))
1974 (assertoid:assert-error (let ((*evaluator-mode* :interpret))
1975 (funcall (eval '(lambda (&whole x)
1976 (list &whole x)))))
1977 program-error))
1979 ;;; ignore &environment
1980 (test-util:with-test (:name (defmacro ignore &environment))
1981 (test-util:checked-compile
1982 '(lambda ()
1983 (defmacro macro-ignore-env (&environment env)
1984 (declare (ignore env))
1985 :foo))))
1987 (test-util:with-test (:name (defmacro :no &environment))
1988 (test-util:checked-compile
1989 '(lambda ()
1990 (defmacro macro-no-env ()
1991 :foo))))
1993 (test-util:with-test (:name (disassemble :source-form))
1994 (dolist (*evaluator-mode* '(#+sb-eval :interpret :compile))
1995 (disassemble (eval '(defun disassemble-source-form-bug (x y z)
1996 (declare (optimize debug))
1997 (list x y z)))
1998 :stream (make-broadcast-stream))))
2000 ;;; long-standing bug in defaulting unknown values on the x86-64,
2001 ;;; since changing the calling convention (test case by Christopher
2002 ;;; Laux sbcl-help 30-06-2007)
2004 (defun default-values-bug-demo-sub ()
2005 (format t "test")
2006 nil)
2007 (compile 'default-values-bug-demo-sub)
2009 (defun default-values-bug-demo-main ()
2010 (multiple-value-bind (a b c d e f g h)
2011 (default-values-bug-demo-sub)
2012 (if a (+ a b c d e f g h) t)))
2013 (compile 'default-values-bug-demo-main)
2015 (test-util:with-test (:name (multiple-value-bind :default-values))
2016 (assert (default-values-bug-demo-main)))
2018 ;;; copy propagation bug reported by Paul Khuong
2020 (defun local-copy-prop-bug-with-move-arg (x)
2021 (labels ((inner ()
2022 (values 1 0)))
2023 (if x
2024 (inner)
2025 (multiple-value-bind (a b)
2026 (inner)
2027 (values b a)))))
2029 (test-util:with-test (:name (labels multiple-value-bind :copy-propagation))
2030 (assert (equal '(0 1) (multiple-value-list
2031 (local-copy-prop-bug-with-move-arg nil))))
2032 (assert (equal '(1 0) (multiple-value-list
2033 (local-copy-prop-bug-with-move-arg t)))))
2035 ;;;; with-pinned-objects & unwind-protect, using all non-tail conventions
2037 (defun wpo-quux () (list 1 2 3))
2038 (defvar *wpo-quux* #'wpo-quux)
2040 (defun wpo-call ()
2041 (unwind-protect
2042 (sb-sys:with-pinned-objects (*wpo-quux*)
2043 (values (funcall *wpo-quux*)))))
2044 (assert (equal '(1 2 3) (wpo-call)))
2046 (defun wpo-multiple-call ()
2047 (unwind-protect
2048 (sb-sys:with-pinned-objects (*wpo-quux*)
2049 (funcall *wpo-quux*))))
2050 (assert (equal '(1 2 3) (wpo-multiple-call)))
2052 (defun wpo-call-named ()
2053 (unwind-protect
2054 (sb-sys:with-pinned-objects (*wpo-quux*)
2055 (values (wpo-quux)))))
2056 (assert (equal '(1 2 3) (wpo-call-named)))
2058 (defun wpo-multiple-call-named ()
2059 (unwind-protect
2060 (sb-sys:with-pinned-objects (*wpo-quux*)
2061 (wpo-quux))))
2062 (assert (equal '(1 2 3) (wpo-multiple-call-named)))
2064 (defun wpo-call-variable (&rest args)
2065 (unwind-protect
2066 (sb-sys:with-pinned-objects (*wpo-quux*)
2067 (values (apply *wpo-quux* args)))))
2068 (assert (equal '(1 2 3) (wpo-call-variable)))
2070 (defun wpo-multiple-call-variable (&rest args)
2071 (unwind-protect
2072 (sb-sys:with-pinned-objects (*wpo-quux*)
2073 (apply #'wpo-quux args))))
2074 (assert (equal '(1 2 3) (wpo-multiple-call-named)))
2076 (defun wpo-multiple-call-local ()
2077 (flet ((quux ()
2078 (wpo-quux)))
2079 (unwind-protect
2080 (sb-sys:with-pinned-objects (*wpo-quux*)
2081 (quux)))))
2082 (assert (equal '(1 2 3) (wpo-multiple-call-local)))
2084 ;;; bug 417: toplevel NIL confusing source path logic
2085 (handler-case
2086 (delete-file (compile-file "bug-417.lisp"))
2087 (sb-ext:code-deletion-note (e)
2088 (error e)))
2090 ;;; unknown values return convention getting disproportionate
2091 ;;; amounts of values.
2092 (declaim (notinline one-value two-values))
2093 (defun one-value (x)
2094 (not x))
2095 (defun two-values (x y)
2096 (values y x))
2097 (defun wants-many-values (x y)
2098 (multiple-value-bind (a b c d e f)
2099 (one-value y)
2100 (assert (and (eql (not y) a)
2101 (not (or b c d e f)))))
2102 (multiple-value-bind (a b c d e f)
2103 (two-values y x)
2104 (assert (and (eql a x) (eql b y)
2105 (not (or c d e f)))))
2106 (multiple-value-bind (a b c d e f g h i)
2107 (one-value y)
2108 (assert (and (eql (not y) a)
2109 (not (or b c d e f g h i)))))
2110 (multiple-value-bind (a b c d e f g h i)
2111 (two-values y x)
2112 (assert (and (eql a x) (eql b y)
2113 (not (or c d e f g h i)))))
2114 (multiple-value-bind (a b c d e f g h i j k l m n o p q r s)
2115 (one-value y)
2116 (assert (and (eql (not y) a)
2117 (not (or b c d e f g h i j k l m n o p q r s)))))
2118 (multiple-value-bind (a b c d e f g h i j k l m n o p q r s)
2119 (two-values y x)
2120 (assert (and (eql a x) (eql b y)
2121 (not (or c d e f g h i j k l m n o p q r s))))))
2122 (wants-many-values 1 42)
2124 ;;; constant coalescing
2126 (defun count-code-constants (x f)
2127 (let ((code (sb-kernel:fun-code-header f))
2128 (n 0))
2129 (loop for i from sb-vm:code-constants-offset below (sb-kernel:code-header-words code)
2130 do (when (equal x (sb-kernel:code-header-ref code i))
2131 (incf n)))
2134 (defvar *lambda*)
2136 (defun compile2 (lambda)
2137 (let* ((lisp "compiler-impure-tmp.lisp")
2138 (fasl (compile-file-pathname lisp)))
2139 (unwind-protect
2140 (progn
2141 (with-open-file (f lisp :direction :output)
2142 (prin1 `(setf *lambda* ,lambda) f))
2143 (multiple-value-bind (fasl warn fail) (compile-file lisp)
2144 (declare (ignore warn))
2145 (when fail
2146 (error "File-compiling ~S failed." lambda))
2147 (let ((*lambda* nil))
2148 (load fasl)
2149 (values *lambda* (test-util:checked-compile lambda)))))
2150 (ignore-errors (delete-file lisp))
2151 (ignore-errors (delete-file fasl)))))
2153 ;; named and unnamed
2154 (defconstant +born-to-coalesce+ '.born-to-coalesce.)
2155 (multiple-value-bind (file-fun core-fun)
2156 (compile2 '(lambda ()
2157 (let ((x (cons +born-to-coalesce+ nil))
2158 (y (cons '.born-to-coalesce. nil)))
2159 (list x y))))
2160 (assert (= 1 (count-code-constants '.born-to-coalesce. file-fun)))
2161 (assert (= 1 (count-code-constants '.born-to-coalesce. core-fun))))
2163 ;; some things must retain identity under COMPILE, but we want to coalesce them under COMPILE-FILE
2164 (defun assert-coalescing (constant)
2165 (let ((value (copy-seq (symbol-value constant))))
2166 (multiple-value-bind (file-fun core-fun)
2167 (compile2 `(lambda ()
2168 (let ((x (cons ,constant nil))
2169 (y (cons ',value nil)))
2170 (list x y))))
2171 (assert (= 1 (count-code-constants value file-fun)))
2172 (assert (= 2 (count-code-constants value core-fun)))
2173 (let* ((l (funcall file-fun))
2174 (a (car (first l)))
2175 (b (car (second l))))
2176 (assert (and (equal value a)
2177 (equal a b)
2178 (eq a b))))
2179 (let* ((l (funcall core-fun))
2180 (a (car (first l)))
2181 (b (car (second l))))
2182 (assert (and (equal value a)
2183 (equal a b)
2184 (not (eq a b))))))))
2186 (defconstant +born-to-coalesce2+ "maybe coalesce me!")
2187 (assert-coalescing '+born-to-coalesce2+)
2189 (defconstant +born-to-coalesce3+ #*01101001011101110100011)
2190 (assert-coalescing '+born-to-coalesce3+)
2192 (defconstant +born-to-coalesce4+ '(foo bar "zot" 123 (nested "quux") #*0101110010))
2193 (assert-coalescing '+born-to-coalesce4+)
2195 (defclass some-constant-thing () ())
2197 ;;; correct handling of nested things loaded via SYMBOL-VALUE
2198 (defvar *sneaky-nested-thing* (list (make-instance 'some-constant-thing)))
2199 (defconstant +sneaky-nested-thing+ *sneaky-nested-thing*)
2200 (multiple-value-bind (file-fun core-fun) (compile2 '(lambda () +sneaky-nested-thing+))
2201 (assert (equal *sneaky-nested-thing* (funcall file-fun)))
2202 (assert (equal *sneaky-nested-thing* (funcall core-fun))))
2204 ;;; catch constant modifications thru undefined variables
2205 (defun sneak-set-dont-set-me (x)
2206 (ignore-errors (setq dont-set-me x)))
2207 (defconstant dont-set-me 42)
2208 (assert (not (sneak-set-dont-set-me 13)))
2209 (assert (= 42 dont-set-me))
2210 (defun sneak-set-dont-set-me2 (x)
2211 (ignore-errors (setq dont-set-me2 x)))
2212 (defconstant dont-set-me2 (make-instance 'some-constant-thing))
2213 (assert (not (sneak-set-dont-set-me2 13)))
2214 (assert (typep dont-set-me2 'some-constant-thing))
2216 ;;; check that non-trivial constants are EQ across different files: this is
2217 ;;; not something ANSI either guarantees or requires, but we want to do it
2218 ;;; anyways.
2219 (defconstant +share-me-1+ #-inline-constants 123.456d0 #+inline-constants nil)
2220 (defconstant +share-me-2+ "a string to share")
2221 (defconstant +share-me-3+ (vector 1 2 3))
2222 (defconstant +share-me-4+ (* 2 most-positive-fixnum))
2223 (multiple-value-bind (f1 c1) (compile2 '(lambda () (values +share-me-1+
2224 +share-me-2+
2225 +share-me-3+
2226 +share-me-4+
2227 #-inline-constants pi)))
2228 (multiple-value-bind (f2 c2) (compile2 '(lambda () (values +share-me-1+
2229 +share-me-2+
2230 +share-me-3+
2231 +share-me-4+
2232 #-inline-constants pi)))
2233 (flet ((test (fa fb)
2234 (mapc (lambda (a b)
2235 (assert (eq a b)))
2236 (multiple-value-list (funcall fa))
2237 (multiple-value-list (funcall fb)))))
2238 (test f1 c1)
2239 (test f1 f2)
2240 (test f1 c2))))
2242 ;;; user-defined satisfies-types cannot be folded
2243 (deftype mystery () '(satisfies mysteryp))
2244 (defvar *mystery* nil)
2245 (defun mysteryp (x) (eq x *mystery*))
2246 (defstruct thing (slot (error "missing") :type mystery))
2247 (defun test-mystery (m) (when (eq :mystery (thing-slot m)) :ok))
2248 (setf *mystery* :mystery)
2249 (assert (eq :ok (test-mystery (make-thing :slot :mystery))))
2251 ;;; Singleton types can also be constant.
2252 (test-util:with-test (:name :propagate-singleton-types-to-eql)
2253 (macrolet ((test (type value &aux (fun (gensym "FUN")))
2254 `(progn
2255 (declaim (ftype (function () (values ,type &optional)) ,fun))
2256 (defun ,fun ()
2257 ',value)
2258 (lambda (x)
2259 (if (eql x (,fun))
2261 (eql x (,fun)))))))
2262 (values
2263 (test (eql foo) foo)
2264 (test (integer 0 0) 0)
2265 (test (double-float 0d0 0d0) 0d0)
2266 (test (eql #\c) #\c))))
2268 (declaim (ftype (function () (integer 42 42)) bug-655581))
2269 (defun bug-655581 ()
2271 (declaim (notinline bug-655581))
2272 (test-util:with-test (:name :bug-655581)
2273 (multiple-value-bind (type derived)
2274 (funcall (test-util:checked-compile
2275 `(lambda ()
2276 (ctu:compiler-derived-type (bug-655581)))))
2277 (assert derived)
2278 (assert (equal '(integer 42 42) type))))
2280 (test-util:with-test (:name :clear-derived-types-on-set-fdefn)
2281 (let ((*evaluator-mode* :compile)
2282 (*derive-function-types* t))
2283 (eval `(progn
2284 (defun clear-derived-types-on-set-fdefn-1 ()
2285 "foo")
2286 (setf (symbol-function 'clear-derived-types-on-set-fdefn-1)
2287 (constantly "foobar"))
2288 (defun clear-derived-types-on-set-fdefn-2 ()
2289 (length (clear-derived-types-on-set-fdefn-1)))))
2290 (assert (= 6 (clear-derived-types-on-set-fdefn-2)))))
2292 (test-util:with-test (:name (:bug-655126 :derive-function-types t))
2293 (let ((*evaluator-mode* :compile)
2294 (*derive-function-types* t))
2295 (eval `(defun bug-655126 (x) x))
2296 ;; Full warnings are ok due to *derive-function-types* = T.
2297 (assert (eq :full-warning
2298 (handler-case
2299 (eval `(defun bug-655126-2 ()
2300 (bug-655126)))
2301 ((and warning (not style-warning)) ()
2302 :full-warning))))
2303 (assert (eq 'bug-655126
2304 (handler-case
2305 (eval `(defun bug-655126 (x y)
2306 (cons x y)))
2307 ((and warning (not sb-kernel:redefinition-warning)) ()
2308 :oops))))
2309 (assert (eq :full-warning
2310 (handler-case
2311 (eval `(defun bug-655126 (x)
2312 (bug-655126 x y)))
2313 ((and warning
2314 (not style-warning)
2315 (not sb-kernel:redefinition-warning)) ()
2316 :full-warning))))))
2318 (test-util:with-test (:name (:bug-655126 :derive-function-types nil))
2319 (let ((*evaluator-mode* :compile))
2320 (eval `(defun bug-655126/b (x) x))
2321 ;; Just style-warning here.
2322 (assert (eq :style-warning
2323 (handler-case
2324 (eval `(defun bug-655126-2/b ()
2325 (bug-655126/b)))
2326 (style-warning ()
2327 :style-warning))))
2328 (assert (eq 'bug-655126/b
2329 (handler-case
2330 (eval `(defun bug-655126/b (x y)
2331 (cons x y)))
2332 ((and warning (not sb-kernel:redefinition-warning)) ()
2333 :oops))))
2334 ;; Bogus self-call is always worth a full one.
2335 (assert (eq :full-warning
2336 (handler-case
2337 (eval `(defun bug-655126/b (x)
2338 (bug-655126/b x y)))
2339 ((and warning
2340 (not style-warning)
2341 (not sb-kernel:redefinition-warning)) ()
2342 :full-warning))))))
2344 (test-util:with-test (:name :bug-657499)
2345 ;; Don't trust derived types within the compilation unit.
2346 (ctu:file-compile
2347 `((declaim (optimize safety))
2348 (defun bug-657499-foo ()
2349 (cons t t))
2350 (defun bug-657499-bar ()
2351 (let ((cons (bug-657499-foo)))
2352 (setf (car cons) 3)
2353 cons)))
2354 :load t)
2355 (locally (declare (optimize safety))
2356 (setf (symbol-function 'bug-657499-foo) (constantly "foobar"))
2357 (assert (eq :type-error
2358 (handler-case
2359 (funcall 'bug-657499-bar)
2360 (type-error (e)
2361 (assert (eq 'cons (type-error-expected-type e)))
2362 (assert (equal "foobar" (type-error-datum e)))
2363 :type-error))))))
2365 (declaim (unsigned-byte *symbol-value-test-var*))
2366 (defvar *symbol-value-test-var*)
2368 (declaim (unsigned-byte **global-symbol-value-test-var**))
2369 (defglobal **global-symbol-value-test-var** 0)
2371 (test-util:with-test (:name :symbol-value-type-derivation)
2372 (let ((fun (compile
2374 `(lambda ()
2375 *symbol-value-test-var*))))
2376 (assert (equal '(function () (values unsigned-byte &optional))
2377 (%simple-fun-type fun))))
2378 (let ((fun (compile
2380 `(lambda ()
2381 **global-symbol-value-test-var**))))
2382 (assert (equal '(function () (values unsigned-byte &optional))
2383 (%simple-fun-type fun))))
2384 (let ((fun (compile
2386 `(lambda (*symbol-value-test-var*)
2387 (declare (fixnum *symbol-value-test-var*))
2388 (symbol-value '*symbol-value-test-var*))))
2389 (ufix (type-specifier (specifier-type `(and unsigned-byte fixnum)))))
2390 (assert (equal `(function (,ufix) (values ,ufix &optional))
2391 (%simple-fun-type fun))))
2392 (let ((fun (compile
2394 `(lambda ()
2395 (declare (fixnum **global-symbol-value-test-var**))
2396 (symbol-global-value '**global-symbol-value-test-var**))))
2397 (ufix (type-specifier (specifier-type `(and unsigned-byte fixnum)))))
2398 (assert (equal `(function () (values ,ufix &optional))
2399 (%simple-fun-type fun)))))
2401 (test-util:with-test (:name :mv-bind-to-let-type-propagation)
2402 (let ((f (test-util:checked-compile
2403 `(lambda (x)
2404 (declare (optimize speed)
2405 (type (integer 20 50) x))
2406 (< (truncate x 10) 1))))
2407 (g (test-util:checked-compile
2408 `(lambda (x)
2409 (declare (optimize speed)
2410 (type (integer 20 50) x))
2411 (< (nth-value 1 (truncate x 10)) 10))))
2412 (h (test-util:checked-compile
2413 `(lambda (x)
2414 (declare (optimize speed)
2415 (type (integer 20 50) x))
2416 (multiple-value-bind (q r)
2417 (truncate x 10)
2418 (declare (ignore r))
2419 (< q 1)))))
2420 (type0 '(function ((integer 20 50)) (values null &optional)))
2421 (type1 '(function ((integer 20 50)) (values (member t) &optional))))
2422 (assert (equal type0 (sb-kernel:%simple-fun-type f)))
2423 (assert (equal type1 (sb-kernel:%simple-fun-type g)))
2424 (assert (equal type0 (sb-kernel:%simple-fun-type h)))))
2426 (test-util:with-test (:name :bug-308921)
2427 (let ((*check-consistency* t))
2428 (ctu:file-compile
2429 `((let ((exported-symbols-alist
2430 (loop for symbol being the external-symbols of :cl
2431 collect (cons symbol
2432 (concatenate 'string
2434 (string-downcase symbol))))))
2435 (defun hyperdoc-lookup (symbol)
2436 (cdr (assoc symbol exported-symbols-alist)))))
2437 :load nil)))
2439 (test-util:with-test (:name :bug-308941)
2440 (multiple-value-bind (warn fail)
2441 (let ((*check-consistency* t))
2442 (ctu:file-compile
2443 "(eval-when (:compile-toplevel :load-toplevel :execute)
2444 (defstruct foo3))
2445 (defstruct bar
2446 (foo #.(make-foo3)))"
2447 :load nil))
2448 ;; ...but the compiler should not break.
2449 (assert (and warn fail))))
2451 (test-util:with-test (:name :bug-903821)
2452 (let* ((fun (test-util:checked-compile
2453 '(lambda (x n)
2454 (declare (sb-ext:word x)
2455 (type (integer 0 #.(1- sb-vm:n-machine-word-bits)) n)
2456 (optimize speed))
2457 (logandc2 x (ash -1 n)))))
2458 (thing-not-to-call
2459 (intern (format nil "ASH-LEFT-MOD~D" sb-vm::n-machine-word-bits) "SB-VM")))
2460 (assert (not (member (symbol-function thing-not-to-call)
2461 (ctu:find-named-callees fun))))
2462 (assert (= 7 (funcall fun 15 3)))))
2464 (test-util:with-test (:name :bug-997528)
2465 (let ((fun (test-util:checked-compile
2466 '(lambda (x)
2467 (declare (optimize (speed 0) (space 0))
2468 (type (integer -228645653448155482 -228645653447928749) x))
2469 (floor 1.0 (the (integer -228645653448151677 -228645653448150900) x))))))
2470 (multiple-value-bind (quo rem)
2471 (funcall fun -228645653448151381)
2472 (assert (= quo -1))
2473 (assert (= rem (float -228645653448151381))))))
2475 (defmacro def-many-code-constants ()
2476 `(defun many-code-constants ()
2477 ,@(loop for i from 0 below 1000
2478 collect `(print ,(format nil "hi-~d" i)))))
2480 (test-util:with-test (:name :many-code-constants)
2481 (def-many-code-constants)
2482 (assert (search "hi-999"
2483 (with-output-to-string (*standard-output*)
2484 (many-code-constants)))))
2486 (test-util:with-test (:name :bug-943953)
2487 ;; we sometimes splice compiler structures like clambda in
2488 ;; source, and our error reporting would happily use that
2489 ;; as source forms.
2490 (let* ((src "bug-943953.lisp")
2491 (obj (compile-file-pathname src)))
2492 (unwind-protect (compile-file src)
2493 (ignore-errors (delete-file obj)))))
2495 (declaim (inline vec-1177703))
2496 (defstruct (vec-1177703 (:constructor vec-1177703 (&optional x)))
2497 (x 0.0d0 :type double-float))
2499 (declaim (inline norm-1177703))
2500 (defun norm-1177703 (v)
2501 (vec-1177703 (sqrt (vec-1177703-x v))))
2503 (test-util:with-test (:name :bug-1177703)
2504 (test-util:checked-compile `(lambda (x)
2505 (norm-1177703 (vec-1177703 x)))))
2507 (declaim (inline call-1035721))
2508 (defun call-1035721 (function)
2509 (lambda (x)
2510 (funcall function x)))
2512 (declaim (inline identity-1035721))
2513 (defun identity-1035721 (x)
2516 (test-util:with-test (:name :bug-1035721)
2517 (test-util:checked-compile
2518 `(lambda ()
2519 (list
2520 (call-1035721 #'identity-1035721)
2521 (lambda (x)
2522 (identity-1035721 x))))))
2524 (test-util:with-test (:name :expt-type-derivation-and-method-redefinition)
2525 (defmethod expt-type-derivation ((x list) &optional (y 0.0))
2526 (declare (type float y))
2527 (expt 2 y))
2528 ;; the redefinition triggers a type lookup of the old
2529 ;; fast-method-function's type, which had a bogus type specifier of
2530 ;; the form (double-float 0) from EXPT type derivation
2531 (defmethod expt-type-derivation ((x list) &optional (y 0.0))
2532 (declare (type float y))
2533 (expt 2 y)))
2535 ;; Lp# 1066451 - declarations were either misplaced or dropped
2536 ;; depending on whether the :policy argument was used in DEFTRANSFORM.
2537 ;; That was a bit random. :policy doesn't affect whether decls
2538 ;; are accepted now.
2539 (defun foo (blah)
2540 (declare (special randomness-factor))
2541 (if (constant-lvar-p randomness-factor)
2542 (format nil "Weird transform answer is ~D"
2543 (+ (lvar-value randomness-factor) blah))))
2544 (defknown weird-fn (integer symbol &key (:magic real)) t)
2545 (deftransform weird-fn ((x s &key ((:magic randomness-factor)))
2546 (fixnum t &key (:magic fixnum)))
2547 ;; I can't see much use for declarations other than SPECIAL here,
2548 ;; but we shouldn't supposedly allow them and then not handle them right.
2549 (declare (special fred) (special randomness-factor) (lvar x s))
2550 (foo fred))
2551 (test-util:with-test (:name :deftransform-bug-1066451)
2552 (let ((f (let ((fred 3))
2553 (declare (special fred))
2554 (test-util:checked-compile
2555 '(lambda () (weird-fn 2 'foo :magic 11))))))
2556 (assert (string= (funcall f)
2557 "Weird transform answer is 14"))))
2559 (defun skip-1-passthrough (a b sb-int:&more context count)
2560 (declare (ignore a b))
2561 (multiple-value-call 'list
2562 'start
2563 (sb-c::%more-arg-values context 1 (1- (truly-the fixnum count)))
2564 'end))
2565 (defun skip-2-passthrough (a b sb-int:&more context count)
2566 (declare (ignore a b))
2567 (multiple-value-call 'list
2568 'start
2569 (sb-c::%more-arg-values context 2 (- (truly-the fixnum count) 2))
2570 'end))
2571 (defun skip-n-passthrough (n-skip n-copy sb-int:&more context count)
2572 (assert (>= count (+ n-copy n-skip))) ; prevent crashes
2573 (multiple-value-call 'list
2574 'start
2575 (sb-c::%more-arg-values context n-skip n-copy)
2576 'end))
2578 ;; %MORE-ARG-VALUES was wrong on x86 and x86-64 with nonzero 'skip'.
2579 ;; It's entirely possible that other backends are also not working.
2580 (test-util:with-test (:name :more-arg-fancy
2581 :skipped-on :interpreter)
2582 (assert (equal (skip-1-passthrough 0 0 'a 'b 'c 'd 'e 'f)
2583 '(start b c d e f end)))
2584 (assert (equal (skip-2-passthrough 0 0 'a 'b 'c 'd 'e 'f)
2585 '(start c d e f end)))
2586 (assert (equal (skip-n-passthrough 1 5 'a 'b 'c 'd 'e 'f)
2587 '(start b c d e f end)))
2588 (assert (equal (skip-n-passthrough 1 5 'a 'b 'c 'd 'e 'f 'g)
2589 '(start b c d e f end)))
2590 (assert (equal (skip-n-passthrough 2 5 'a 'b 'c 'd 'e 'f 'g)
2591 '(start c d e f g end)))
2592 (assert (equal (skip-n-passthrough 2 5 'a 'b 'c 'd 'e 'f 'g 'h)
2593 '(start c d e f g end))))
2595 (test-util:with-test (:name :macro-policy)
2596 (flet ((count-notes ()
2597 (let ((count 0))
2598 (handler-bind ((compiler-note
2599 (lambda (c)
2601 (incf count)
2602 (muffle-warning))))
2603 (multiple-value-bind (fasl warnings errors)
2604 (compile-file "macro-policy-test.lisp"
2605 :print nil :verbose nil)
2606 (ignore-errors (delete-file fasl))
2607 (assert (and (not warnings) (not errors)))
2608 count)))))
2609 (let* ((baseline (count-notes))
2610 (test (progv '(*frob-macro-policy*) '(t) (count-notes)))
2611 (baseline-again (count-notes)))
2612 (assert (/= 0 baseline))
2613 (assert (= 0 test))
2614 ;; macro-policy is rebound inside compile-file
2615 (assert (= baseline-again baseline)))))
2617 (in-package :cl-user)
2619 (with-test (:name :merge-lambdas-dead-return)
2620 (let ((fasl (compile-file "merge-lambdas.lisp"
2621 :print nil :verbose nil)))
2622 (ignore-errors (delete-file fasl))))
2624 (declaim (inline ensure-a-fun))
2625 (defun ensure-a-fun (f) (coerce f 'function))
2626 (defmacro compose2 (f g)
2627 `(let ((f (ensure-a-fun ,f))
2628 (g (ensure-a-fun ,g)))
2629 (lambda (arg) (funcall f (funcall g arg)))))
2631 (with-test (:name :coerce-to-function-smarter)
2632 (let ((f (checked-compile
2633 '(lambda (x)
2634 (funcall (compose2 #'integerp #'car) x)))))
2635 ;; should be completely inlined
2636 (assert (null (ctu:find-named-callees f)))))
2638 (with-test (:name :derived-function-type-casts)
2639 (let ((fasl (compile-file "derived-function-type-casts.lisp"
2640 :print nil :verbose nil)))
2641 (load fasl)
2642 (ignore-errors (delete-file fasl))
2643 (assert (null (funcall 'derived-function-type-casts)))))
2645 (with-test (:name (compile function :invalid-function-name))
2646 (flet ((test-case (nameoid)
2647 (assert (nth-value
2648 1 (checked-compile `(lambda () (function ,nameoid))
2649 :allow-failure t)))))
2650 (test-case 1)
2651 (test-case "foo")
2652 (test-case '(foo bar))))
2654 (defun catch-compiled-program-error (form &rest args)
2655 (multiple-value-bind (function failure-p)
2656 (checked-compile form :allow-failure t :allow-warnings t)
2657 (assert failure-p)
2658 (assert-error (apply function args))))
2660 (with-test (:name :duplicate-&key-no-error)
2661 (catch-compiled-program-error
2662 '(lambda () (defun duplicate-&key-no-error (&key a a) a))))
2664 (with-test (:name :bad-type-specifiers)
2665 (catch-compiled-program-error
2666 '(lambda (x) (typep x '(values 10)))
2668 (catch-compiled-program-error
2669 '(lambda () (declare (sb-ext:muffle-conditions 10)))))
2671 (with-test (:name :coverage-and-errors)
2672 (ctu:file-compile
2673 '((locally (declare (optimize sb-c:store-coverage-data))
2674 (1)))))
2676 (symbol-macrolet ((x 30))
2677 (macrolet ((foo (y) (+ x y)))
2678 (declaim (inline environment-around-inline))
2679 (defun environment-around-inline (z)
2680 (* z (foo 4)))))
2682 (with-test (:name :environment-around-inline)
2683 (defun environment-around-inline.2 (z)
2684 (environment-around-inline z))
2685 (assert (= (environment-around-inline.2 10) 340)))
2687 (with-test (:name :defun-setf-return-value)
2688 (let ((name `(setf ,(gensym))))
2689 (assert (equal (eval `(defun ,name ()))
2690 name))))
2692 (with-test (:name :make-sequence-unknown)
2693 (let ((fun (checked-compile
2694 `(lambda (x)
2695 (let ((vector (make-sequence '(simple-array make-sequence-unknown (*)) 10)))
2696 (setf (aref vector 0) x)
2697 vector))
2698 :allow-style-warnings t)))
2699 (deftype make-sequence-unknown () 'fixnum)
2700 (assert-error (funcall fun 'abc) type-error)))
2702 (with-test (:name (:compiler-messages function :type-specifier))
2703 ;; Previously, function types were often printed confusingly, e.g.:
2705 ;; (function ()) => #'NIL
2706 ;; (function *) => #'*
2707 ;; (function (function a)) => #'#'A
2709 (flet ((test-case (spec)
2710 (destructuring-bind (type expected) spec
2711 (unwind-protect
2712 (let ((report))
2713 (proclaim '(ftype (function () boolean) my-function))
2714 (handler-bind ((warning
2715 (lambda (condition)
2716 (setf report (princ-to-string condition))
2717 (muffle-warning))))
2718 (proclaim `(ftype ,type my-function)))
2719 (assert (search expected report)))
2720 (fmakunbound 'my-function)))))
2721 (mapc
2722 #'test-case
2723 `(((function ()) "(FUNCTION NIL)")
2724 ((function *) "(FUNCTION *)")
2725 ((function (function *)) "(FUNCTION (FUNCTION *))")
2726 ((function (function (eql 1))) "(FUNCTION (FUNCTION (EQL 1))")))))
2728 #+this-crashes-the-compiler-and-launchpad-is-flaking-out
2729 (defun foo-node (stuff &optional (pos 0) rule)
2730 (loop for child in stuff
2731 do (setf pos (multiple-value-call #'foo-node child pos 42))
2732 finally (set '*some-var* pos)))
2734 * (compile 'foo-node)
2736 debugger invoked on a SB-INT:BUG in thread
2737 #<THREAD "main thread" RUNNING {10029A6C93}>:
2738 failed AVER:
2739 (OR (NOT SB-C::ENTRY) (EQ (SB-C::FUNCTIONAL-KIND SB-C::ENTRY) :DELETED))