0.7.12.8:
[sbcl.git] / tests / compiler-1.impure-cload.lisp
1 ;;;; miscellaneous compiler tests with side effects (e.g. DEFUN
2 ;;;; changing FDEFINITIONs and globaldb stuff)
3
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.
14
15 (cl:in-package :cl-user)
16
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18   (load "assertoid")
19   (use-package "ASSERTOID"))
20
21 (declaim (optimize (debug 3) (speed 2) (space 1)))
22
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))
38
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))
49
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))
58
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.")
67
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))
90
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))
99           (t
100            (error "Invalid pixarray: ~S." pixarray)))))
101 (assert (eql 1 (pixarray-element-size #*110)))
102
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))
112
113 ;;; bug 220: type check inserted after all arguments in MV-CALL caused
114 ;;; failure of stack analysis
115 (defun bug220-helper ()
116   13)
117 (assert (equal (multiple-value-call #'list
118                  (the integer (bug220-helper))
119                  nil)
120                '(13 nil)))
121
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))
127
128 ;;; bug 166: compiler failure
129 (defstruct bug166s)
130 (defmethod permanentize ((uustk bug166s))
131   (flet ((frob (hash-table test-for-deletion)
132            )
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))
144
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))))
153
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))))
163
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)
169                                     t))
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))
176          (t
177           (safe-format t "~&baz ~S (~A) ~S" condition condition result)))))))
178
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))
184
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)))
194
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))
204
205 (sb-ext:quit :unix-status 104) ; success