0.7.9.54:
[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 (declaim (optimize (debug 3) (speed 2) (space 1)))
18
19 ;;; Until version 0.6.9 or so, SBCL's version of Python couldn't do
20 ;;; this correctly, due to the bug patched by Rob MacLachlan on the
21 ;;; cmucl-imp list 2000-06-21, and applied to SBCL by Martin Atzmueller.
22 ;;; (The effectiveness of the test also depends on the implicit
23 ;;; function typing of Python (where DEFUN is like DECLAIM FTYPE),
24 ;;; which violates the ANSI spec, and should be fixed. Once that
25 ;;; unrelated bug is fixed, this code will no longer test the type
26 ;;; inference behavior it's intended to test.)
27 (defun emptyvalues (&rest rest) (declare (ignore rest)) (values))
28 (defstruct foo x y)
29 (defun bar ()
30   (let ((res (emptyvalues)))
31     (unless (typep res 'foo)
32       'expected-value)))
33 (assert (eq (bar) 'expected-value))
34
35 (declaim (ftype (function (real) (values integer single-float)) valuesify))
36 (defun valuesify (x)
37   (values (round x)
38           (coerce x 'single-float)))
39 (defun exercise-valuesify (x)
40   (multiple-value-bind (i f) (valuesify x)
41     (declare (type integer i))
42     (declare (type single-float f))
43     (+ i f)))
44 (assert (= (exercise-valuesify 1.25) 2.25))
45
46 ;;; An early version (sbcl-0.6.11.33) of code to check FTYPEs from DEFUN
47 ;;; against DECLAIMed FTYPEs blew up when an FTYPE was DECLAIMed
48 ;;; to be pure FUNCTION, because the internal representation of
49 ;;; FUNCTION itself (as opposed to subtypes of FUNCTION, such as
50 ;;; (FUNCTION () T)) is a BUILT-IN-CLASS object, not a FUN-TYPE
51 ;;; object.
52 (declaim (ftype function i-am-just-a-function))
53 (defun i-am-just-a-function (x y) (+ x y 1))
54
55 ;;; Stig E Sandoe reported in cclan-Bugs-431263 that SBCL couldn't
56 ;;; compile this. sbcl-0.6.12.26 died in CIRCULAR-LIST-P with "The
57 ;;; value \"EST\" is not of type LIST." Dan Barlow fixed it.
58 (defvar +time-zones+
59   '((5 "EDT" . "EST") (6 "CDT" . "CST") (7 "MDT" .
60 "MST") (8 "PDT" . "PST")
61     (0 "GMT" . "GDT") (-2 "MET" . "MET DST"))
62   "*The string representations of the time zones.")
63
64 ;;; The old CMU CL Python compiler assumed that it was safe to infer
65 ;;; function types (including return types) from function definitions
66 ;;; and then use them to optimize code later. This is of course bad
67 ;;; when functions are redefined. The problem was fixed in
68 ;;; sbcl-0.6.12.57.
69 (defun foo (x)
70   (if (plusp x)
71       1.0
72       0))
73 (defun bar (x)
74   (typecase (foo x)
75     (fixnum :fixnum)
76     (real :real)
77     (string :string)
78     (t :t)))
79 (assert (eql (bar 11) :real))
80 (assert (eql (bar -11) :fixnum))
81 (setf (symbol-function 'foo) #'identity)
82 (assert (eql (bar 11) :fixnum))
83 (assert (eql (bar -11.0) :real))
84 (assert (eql (bar "this is a test") :string))
85 (assert (eql (bar (make-hash-table)) :t))
86
87 ;;; bug reported by Brian Spilsbury sbcl-devel 2001-09-30, fixed by
88 ;;; Alexey Dejneka patch sbcl-devel 2001-10-02
89 (defun pixarray-element-size (pixarray)
90   (let ((eltype (array-element-type pixarray)))
91     (cond ((eq eltype 'bit) 1)
92           ((and (listp eltype)
93                 (eq (first eltype) 'unsigned-byte))
94            (second eltype))
95           (t
96            (error "Invalid pixarray: ~S." pixarray)))))
97 (assert (eql 1 (pixarray-element-size #*110)))
98
99 ;;; bug 31 turned out to be a manifestation of non-ANSI array type
100 ;;; handling, fixed by CSR in sbcl-0.7.3.8.
101 (defun array-element-type-handling (x)
102   (declare (type (vector cons) x))
103   (when (consp (aref x 0))
104     (aref x 0)))
105 (assert (eq (array-element-type-handling
106              (make-array 3 :element-type t :initial-element 0))
107             nil))
108
109 ;;; bug 220: type check inserted after all arguments in MV-CALL caused
110 ;;; failure of stack analysis
111 (defun bug220-helper ()
112   13)
113 (assert (equal (multiple-value-call #'list
114                  (the integer (bug220-helper))
115                  nil)
116                '(13 nil)))
117
118 ;;; bug 221: sbcl 0.7.9.13 failed to compile the following function
119 (declaim (ftype (function (fixnum) (values package boolean)) bug221-f1))
120 (declaim (ftype (function (t) (values package boolean)) bug221-f2))
121 (defun bug221 (b x)
122   (funcall (if b #'bug221-f1 #'bug221-f2) x))
123
124 ;;; bug 166: compiler failure
125 (defstruct bug166s)
126 (defmethod permanentize ((uustk bug166s))
127   (flet ((frob (hash-table test-for-deletion)
128            )
129          (obj-entry.stale? (oe)
130            (destructuring-bind (key . datum) oe
131              (declare (type simple-vector key))
132              (deny0 (void? datum))
133              (some #'stale? key))))
134     (declare (inline frob obj-entry.stale?))
135     (frob (uustk.args-hash->obj-alist uustk)
136           #'obj-entry.stale?)
137     (frob (uustk.hash->memoized-objs-list uustk)
138           #'objs.stale?))
139   (call-next-method))
140
141 ;;; bugs 115, 226: compiler failure in lifetime analysis
142 (defun bug115-1 ()
143   (declare (optimize (speed 2) (debug 3)))
144   (flet ((m1 ()
145            (unwind-protect nil)))
146     (if (catch nil)
147         (m1)
148         (m1))))
149
150 (defun bug115-2 ()
151   (declare (optimize (speed 2) (debug 3)))
152   (flet ((m1 ()
153            (bar (if (foo) 1 2))
154            (let ((x (foo)))
155              (bar x (list x)))))
156     (if (catch nil)
157         (m1)
158         (m1))))
159
160 (defun bug226 ()
161   (declare (optimize (speed 0) (safety 3) (debug 3)))
162   (flet ((safe-format (stream string &rest r)
163            (unless (ignore-errors (progn
164                                     (apply #'format stream string r)
165                                     t))
166              (format stream "~&foo ~S" string))))
167     (cond
168       ((eq my-result :ERROR)
169        (cond
170          ((ignore-errors (typep condition result))
171           (safe-format t "~&bar ~S" result))
172          (t
173           (safe-format t "~&baz ~S (~A) ~S" condition condition result)))))))
174
175
176 (sb-ext:quit :unix-status 104) ; success