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