Use immobile_space_p() in gc_managed_addr_p()
[sbcl.git] / tests / compiler-1.impure-cload.lisp
blob68c1e0b9b15b20007c742d5b81c65b0bcfca429d
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 (declaim (optimize (debug 1) (speed 1) (space 1)))
70 ;;; The old CMU CL Python compiler assumed that it was safe to infer
71 ;;; function types (including return types) from function definitions
72 ;;; and then use them to optimize code later [and it was almost
73 ;;; right!]. This is of course bad when functions are redefined. The
74 ;;; problem was fixed in sbcl-0.6.12.57.
75 (defun foo (x)
76 (if (plusp x)
77 1.0
78 0))
79 (eval '(locally
80 (defun bar (x)
81 (typecase (foo x)
82 (fixnum :fixnum)
83 (real :real)
84 (string :string)
85 (t :t)))
86 (compile 'bar)))
87 (assert (eql (bar 11) :real))
88 (assert (eql (bar -11) :fixnum))
89 (setf (symbol-function 'foo) #'identity)
90 (assert (eql (bar 11) :fixnum))
91 (assert (eql (bar -11.0) :real))
92 (assert (eql (bar "this is a test") :string))
93 (assert (eql (bar (make-hash-table)) :t))
95 ;;; bug reported by Brian Spilsbury sbcl-devel 2001-09-30, fixed by
96 ;;; Alexey Dejneka patch sbcl-devel 2001-10-02
97 (defun pixarray-element-size (pixarray)
98 (let ((eltype (array-element-type pixarray)))
99 (cond ((eq eltype 'bit) 1)
100 ((and (listp eltype)
101 (eq (first eltype) 'unsigned-byte))
102 (second eltype))
104 (error "Invalid pixarray: ~S." pixarray)))))
105 (assert (eql 1 (pixarray-element-size #*110)))
107 ;;; bug 31 turned out to be a manifestation of non-ANSI array type
108 ;;; handling, fixed by CSR in sbcl-0.7.3.8.
109 (defun array-element-type-handling (x)
110 (declare (optimize safety))
111 (declare (type (vector cons) x))
112 (when (consp (aref x 0))
113 (aref x 0)))
114 (assert-error
115 (array-element-type-handling
116 (make-array 3 :element-type t :initial-element 0))
117 type-error)
119 ;;; bug 220: type check inserted after all arguments in MV-CALL caused
120 ;;; failure of stack analysis
121 (defun bug220-helper ()
123 (assert (equal (multiple-value-call #'list
124 (the integer (bug220-helper))
125 nil)
126 '(13 nil)))
128 ;;; bug 221: sbcl 0.7.9.13 failed to compile the following function
129 (declaim (ftype (function (fixnum) (values package boolean)) bug221-f1))
130 (declaim (ftype (function (t) (values package boolean)) bug221-f2))
131 (defun bug221 (b x)
132 (funcall (if b #'bug221-f1 #'bug221-f2) x))
134 ;;; bug 166: compiler failure
135 (defstruct bug166s)
136 (defmethod permanentize ((uustk bug166s))
137 (flet ((frob (hash-table test-for-deletion)
139 (obj-entry.stale? (oe)
140 (destructuring-bind (key . datum) oe
141 (declare (type simple-vector key))
142 (deny0 (void? datum))
143 (some #'stale? key))))
144 (declare (inline frob obj-entry.stale?))
145 (frob (uustk.args-hash->obj-alist uustk)
146 #'obj-entry.stale?)
147 (frob (uustk.hash->memoized-objs-list uustk)
148 #'objs.stale?))
149 (call-next-method))
151 ;;; bugs 115, 226: compiler failure in lifetime analysis
152 (defun bug115-1 ()
153 (declare (optimize (speed 2) (debug 3)))
154 (flet ((m1 ()
155 (unwind-protect nil)))
156 (if (catch nil)
157 (m1)
158 (m1))))
160 (defun bug115-2 ()
161 (declare (optimize (speed 2) (debug 3)))
162 (flet ((m1 ()
163 (bar (if (foo) 1 2))
164 (let ((x (foo)))
165 (bar x (list x)))))
166 (if (catch nil)
167 (m1)
168 (m1))))
170 (defun bug226 ()
171 (declare (optimize (speed 0) (safety 3) (debug 3)))
172 (flet ((safe-format (stream string &rest r)
173 (unless (ignore-errors (progn
174 (apply #'format stream string r)
176 (format stream "~&foo ~S" string))))
177 (cond
178 ((eq my-result :ERROR)
179 (cond
180 ((ignore-errors (typep condition result))
181 (safe-format t "~&bar ~S" result))
183 (safe-format t "~&baz ~S (~A) ~S" condition condition result)))))))
185 ;;; bug 231: SETQ did not check the type of the variable being set
186 (defun bug231a-1 (x)
187 (declare (optimize safety) (type (integer 0 8) x))
188 (incf x))
189 (assert-error (bug231a-1 8) type-error)
191 (defun bug231a-2 (x)
192 (declare (optimize safety) (type (integer 0 8) x))
193 (list (lambda (y) (setq x y))
194 (lambda () x)))
195 (destructuring-bind (set get) (bug231a-2 0)
196 (funcall set 8)
197 (assert (eql (funcall get) 8))
198 (assert-error (funcall set 9) type-error)
199 (assert (eql (funcall get) 8)))
201 (defun bug231b (x z)
202 (declare (optimize safety) (type integer x))
203 (locally
204 (declare (type (real 1) x))
205 (setq x z))
206 (list x z))
207 (assert-error (bug231b nil 1) type-error)
208 (assert-error (bug231b 0 1.5) type-error)
209 (assert-error (bug231b 0 0) type-error)
211 ;;; A bug appeared in flaky7_branch. Python got lost in unconverting
212 ;;; embedded tail calls during let-conversion.
213 (defun bug239 (bit-array-2 &optional result-bit-array)
214 (declare (type (array bit) bit-array-2)
215 (type (or (array bit) (member t nil)) result-bit-array))
216 (unless (simple-bit-vector-p bit-array-2)
217 (multiple-value-call
218 (lambda (data1 start1)
219 (multiple-value-call
220 (lambda (data2 start2)
221 (multiple-value-call
222 (lambda (data3 start3)
223 (declare (ignore start3))
224 (print (list data1 data2)))
225 (values 0 0)))
226 (values bit-array-2 0)))
227 (values 444 0))))
228 (assert (equal (bug239 (make-array 4 :element-type 'bit
229 :adjustable t
230 :initial-element 0)
231 nil)
232 '(444 #*0000)))
234 (defstruct some-structure a)
235 (eval-when (:compile-toplevel)
236 ;; in the big CLASS reorganization in pre8, this would fail with
237 ;; SOME-STRUCTURE-A is not FBOUNDP. Fixed in 0.pre8.64
238 (find-class 'some-structure nil))
239 (eval-when (:load-toplevel)
240 (assert (typep (find-class 'some-structure) 'class)))
242 ;; When loading from fasl, if a form in that file tried referencing
243 ;; compiled-debug-info for a function provided earlier in that same file,
244 ;; it wouldn't work because the earlier function's #<code> component is
245 ;; present but without its debug-info-source, which is dumped later.
246 (defun f1 (x) (+ x 3))
247 ;; There's nothing to assert other than that this works.
248 (print (multiple-value-list (function-lambda-expression #'f1)))
250 ;; It's possible for an instance to refer to a LAYOUT that has no classoid.
251 ;; This can arise from one file compiling a defstruct and another file compiling
252 ;; a function that refers to an instance of it as a literal either from
253 ;; LOAD-TIME-VALUE or MAKE-LOAD-FORM. Then you restart lisp and load the second
254 ;; but not the first file. A function to recreate the instance was correctly
255 ;; dumped, but everything including the disassembler would crash trying to view
256 ;; the resultant object.
258 (eval-when (:compile-toplevel)
259 (declaim (inline make-my-awesome-struct))
260 (defstruct (my-awesome-struct (:predicate nil) (:copier nil)) a b c)
261 (defmethod make-load-form ((self my-awesome-struct) &optional env)
262 (declare (ignore env))
263 ;; Can't use MAKE-LOAD-FORM-SAVING-SLOTS
264 ;; because that goes (ALLOCATE-INSTANCE (FIND-CLASS 'MY-AWESOME-STRUCT).
265 ;; Our named constructor is inlined and doesn't need the class.
266 (with-slots (a b c) self `(make-my-awesome-struct :a ',a :b ',b :c ',c)))
267 (defvar *my-awesome-instance*
268 (make-my-awesome-struct :a 1 :b '(foo . #*101) :c #(4 5 3))))
270 (defun trythis (x)
271 ;; use a macro to return the object itself, not the symbol naming it.
272 (macrolet ((it () *my-awesome-instance*))
273 (list x (it))))
275 (defun get-foo-val (x) (my-awesome-struct-b x))
277 ;; This approximates the test case - no class's proper name is MY-AWESOME-STRUCT.
278 ;; The old metaobjects are detritus in the object hierarchy but don't affect
279 ;; corectness of the test.
280 (eval-when (:compile-toplevel)
281 (dolist (sym '("MY-AWESOME-STRUCT" "MY-AWESOME-STRUCT-A"
282 "MY-AWESOME-STRUCT-B" "MY-AWESOME-STRUCT-C"))
283 (unintern (find-symbol sym))))
285 (assert (not (find-class 'my-awesome-struct nil)))
287 (let ((x (trythis 9))
288 (*print-pretty* nil))
289 (format t "~&Hi! ~S~%" x)
290 (assert (search "UNPRINTABLE" (write-to-string x)))
291 (assert (equal (get-foo-val (second x)) '(foo . #*101))))