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