0.pre8.81:
[sbcl/lichteblau.git] / tests / compiler-1.impure-cload.lisp
blob6d61de0afc2c16b5dbbceb65a663fbe95b42add6
1 ;;;; miscellaneous compiler tests with side effects (e.g. DEFUN
2 ;;;; changing FDEFINITIONs and globaldb stuff)
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; While most of SBCL is derived from the CMU CL system, the test
8 ;;;; files (like this one) were written from scratch after the fork
9 ;;;; from CMU CL.
10 ;;;;
11 ;;;; This software is in the public domain and is provided with
12 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
13 ;;;; more information.
15 (cl:in-package :cl-user)
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18 (load "assertoid")
19 (use-package "ASSERTOID"))
21 (declaim (optimize (debug 3) (speed 2) (space 1)))
23 ;;; Until version 0.6.9 or so, SBCL's version of Python couldn't do
24 ;;; this correctly, due to the bug patched by Rob MacLachlan on the
25 ;;; cmucl-imp list 2000-06-21, and applied to SBCL by Martin Atzmueller.
26 ;;; (The effectiveness of the test also depends on the implicit
27 ;;; function typing of Python (where DEFUN is like DECLAIM FTYPE),
28 ;;; which violates the ANSI spec, and should be fixed. Once that
29 ;;; unrelated bug is fixed, this code will no longer test the type
30 ;;; inference behavior it's intended to test.)
31 (defun emptyvalues (&rest rest) (declare (ignore rest)) (values))
32 (defstruct foo x y)
33 (defun bar ()
34 (let ((res (emptyvalues)))
35 (unless (typep res 'foo)
36 'expected-value)))
37 (assert (eq (bar) 'expected-value))
39 (declaim (ftype (function (real) (values integer single-float)) valuesify))
40 (defun valuesify (x)
41 (values (round x)
42 (coerce x 'single-float)))
43 (defun exercise-valuesify (x)
44 (multiple-value-bind (i f) (valuesify x)
45 (declare (type integer i))
46 (declare (type single-float f))
47 (+ i f)))
48 (assert (= (exercise-valuesify 1.25) 2.25))
50 ;;; An early version (sbcl-0.6.11.33) of code to check FTYPEs from DEFUN
51 ;;; against DECLAIMed FTYPEs blew up when an FTYPE was DECLAIMed
52 ;;; to be pure FUNCTION, because the internal representation of
53 ;;; FUNCTION itself (as opposed to subtypes of FUNCTION, such as
54 ;;; (FUNCTION () T)) is a BUILT-IN-CLASS object, not a FUN-TYPE
55 ;;; object.
56 (declaim (ftype function i-am-just-a-function))
57 (defun i-am-just-a-function (x y) (+ x y 1))
59 ;;; Stig E Sandoe reported in cclan-Bugs-431263 that SBCL couldn't
60 ;;; compile this. sbcl-0.6.12.26 died in CIRCULAR-LIST-P with "The
61 ;;; value \"EST\" is not of type LIST." Dan Barlow fixed it.
62 (defvar +time-zones+
63 '((5 "EDT" . "EST") (6 "CDT" . "CST") (7 "MDT" .
64 "MST") (8 "PDT" . "PST")
65 (0 "GMT" . "GDT") (-2 "MET" . "MET DST"))
66 "*The string representations of the time zones.")
68 ;;; The old CMU CL Python compiler assumed that it was safe to infer
69 ;;; function types (including return types) from function definitions
70 ;;; and then use them to optimize code later. This is of course bad
71 ;;; when functions are redefined. The problem was fixed in
72 ;;; sbcl-0.6.12.57.
73 (defun foo (x)
74 (if (plusp x)
75 1.0
76 0))
77 (defun bar (x)
78 (typecase (foo x)
79 (fixnum :fixnum)
80 (real :real)
81 (string :string)
82 (t :t)))
83 (assert (eql (bar 11) :real))
84 (assert (eql (bar -11) :fixnum))
85 (setf (symbol-function 'foo) #'identity)
86 (assert (eql (bar 11) :fixnum))
87 (assert (eql (bar -11.0) :real))
88 (assert (eql (bar "this is a test") :string))
89 (assert (eql (bar (make-hash-table)) :t))
91 ;;; bug reported by Brian Spilsbury sbcl-devel 2001-09-30, fixed by
92 ;;; Alexey Dejneka patch sbcl-devel 2001-10-02
93 (defun pixarray-element-size (pixarray)
94 (let ((eltype (array-element-type pixarray)))
95 (cond ((eq eltype 'bit) 1)
96 ((and (listp eltype)
97 (eq (first eltype) 'unsigned-byte))
98 (second eltype))
100 (error "Invalid pixarray: ~S." pixarray)))))
101 (assert (eql 1 (pixarray-element-size #*110)))
103 ;;; bug 31 turned out to be a manifestation of non-ANSI array type
104 ;;; handling, fixed by CSR in sbcl-0.7.3.8.
105 (defun array-element-type-handling (x)
106 (declare (type (vector cons) x))
107 (when (consp (aref x 0))
108 (aref x 0)))
109 (assert (eq (array-element-type-handling
110 (make-array 3 :element-type t :initial-element 0))
111 nil))
113 ;;; bug 220: type check inserted after all arguments in MV-CALL caused
114 ;;; failure of stack analysis
115 (defun bug220-helper ()
117 (assert (equal (multiple-value-call #'list
118 (the integer (bug220-helper))
119 nil)
120 '(13 nil)))
122 ;;; bug 221: sbcl 0.7.9.13 failed to compile the following function
123 (declaim (ftype (function (fixnum) (values package boolean)) bug221-f1))
124 (declaim (ftype (function (t) (values package boolean)) bug221-f2))
125 (defun bug221 (b x)
126 (funcall (if b #'bug221-f1 #'bug221-f2) x))
128 ;;; bug 166: compiler failure
129 (defstruct bug166s)
130 (defmethod permanentize ((uustk bug166s))
131 (flet ((frob (hash-table test-for-deletion)
133 (obj-entry.stale? (oe)
134 (destructuring-bind (key . datum) oe
135 (declare (type simple-vector key))
136 (deny0 (void? datum))
137 (some #'stale? key))))
138 (declare (inline frob obj-entry.stale?))
139 (frob (uustk.args-hash->obj-alist uustk)
140 #'obj-entry.stale?)
141 (frob (uustk.hash->memoized-objs-list uustk)
142 #'objs.stale?))
143 (call-next-method))
145 ;;; bugs 115, 226: compiler failure in lifetime analysis
146 (defun bug115-1 ()
147 (declare (optimize (speed 2) (debug 3)))
148 (flet ((m1 ()
149 (unwind-protect nil)))
150 (if (catch nil)
151 (m1)
152 (m1))))
154 (defun bug115-2 ()
155 (declare (optimize (speed 2) (debug 3)))
156 (flet ((m1 ()
157 (bar (if (foo) 1 2))
158 (let ((x (foo)))
159 (bar x (list x)))))
160 (if (catch nil)
161 (m1)
162 (m1))))
164 (defun bug226 ()
165 (declare (optimize (speed 0) (safety 3) (debug 3)))
166 (flet ((safe-format (stream string &rest r)
167 (unless (ignore-errors (progn
168 (apply #'format stream string r)
170 (format stream "~&foo ~S" string))))
171 (cond
172 ((eq my-result :ERROR)
173 (cond
174 ((ignore-errors (typep condition result))
175 (safe-format t "~&bar ~S" result))
177 (safe-format t "~&baz ~S (~A) ~S" condition condition result)))))))
179 ;;; bug 231: SETQ did not check the type of the variable being set
180 (defun bug231a-1 (x)
181 (declare (optimize safety) (type (integer 0 8) x))
182 (incf x))
183 (assert (raises-error? (bug231a-1 8) type-error))
185 (defun bug231a-2 (x)
186 (declare (optimize safety) (type (integer 0 8) x))
187 (list (lambda (y) (setq x y))
188 (lambda () x)))
189 (destructuring-bind (set get) (bug231a-2 0)
190 (funcall set 8)
191 (assert (eql (funcall get) 8))
192 (assert (raises-error? (funcall set 9) type-error))
193 (assert (eql (funcall get) 8)))
195 (defun bug231b (x z)
196 (declare (optimize safety) (type integer x))
197 (locally
198 (declare (type (real 1) x))
199 (setq x z))
200 (list x z))
201 (assert (raises-error? (bug231b nil 1) type-error))
202 (assert (raises-error? (bug231b 0 1.5) type-error))
203 (assert (raises-error? (bug231b 0 0) type-error))
205 ;;; A bug appeared in flaky7_branch. Python got lost in unconverting
206 ;;; embedded tail calls during let-convertion.
207 (defun bug239 (bit-array-2 &optional result-bit-array)
208 (declare (type (array bit) bit-array-2)
209 (type (or (array bit) (member t nil)) result-bit-array))
210 (unless (simple-bit-vector-p bit-array-2)
211 (multiple-value-call
212 (lambda (data1 start1)
213 (multiple-value-call
214 (lambda (data2 start2)
215 (multiple-value-call
216 (lambda (data3 start3)
217 (declare (ignore start3))
218 (print (list data1 data2)))
219 (values 0 0)))
220 (values bit-array-2 0)))
221 (values 444 0))))
222 (assert (equal (bug239 (make-array 4 :element-type 'bit
223 :adjustable t
224 :initial-element 0)
225 nil)
226 '(444 #*0000)))
228 (defstruct some-structure a)
229 (eval-when (:compile-toplevel)
230 ;; in the big CLASS reorganization in pre8, this would fail with
231 ;; SOME-STRUCTURE-A is not FBOUNDP. Fixed in 0.pre8.64
232 (find-class 'some-structure nil))
233 (eval-when (:load-toplevel)
234 (assert (typep (find-class 'some-structure) 'class)))
236 (sb-ext:quit :unix-status 104) ; success