1 ;;;; DEFGLOBAL and related tests
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (proclaim '(special *foo
*))
17 (let ((*evaluator-mode
* :interpret
))
20 (defun unbound-marker ()
21 (sb-c::%primitive sb-c
:make-unbound-marker
))
22 (compile 'unbound-marker
)
24 (defun assert-foo-not-checked (fun)
25 (let* ((marker (unbound-marker))
27 (assert (eq marker
(funcall fun
)))))
29 (defun assert-foo-checked (fun)
30 (let* ((marker (unbound-marker))
36 (assert (eq '*foo
* (cell-error-name e
)))
39 (with-test (:name
:unbound-cannot-be-always-bound
)
42 (proclaim '(sb-ext:always-bound
*foo
*))
46 (proclaim '(sb-ext:always-bound
*foo
*))
49 (declare (optimize (safety 3)))
51 ;; When run interpreted, FOO-SAFE cannot help but check BOUNDP on *foo*
52 ;; so the assertion would fail.
55 (with-test (:name
:always-bound-elides-boundness-checking
)
56 (assert-foo-not-checked #'foo-safe
))
58 (with-test (:name
:cannot-unbind-always-bound
)
64 (defun can-globalize-p (x)
66 (progn (proclaim `(sb-ext:global
,x
)) t
)
69 (with-test (:name
:cannot-proclaim-special-global
)
70 (assert (not (can-globalize-p '*foo
*))))
72 (define-symbol-macro sm
42)
73 (with-test (:name
:cannot-proclaim-symbol-macro-global
)
74 (assert (not (can-globalize-p 'sm
))))
77 (with-test (:name
:cannot-proclaim-constant-global
)
78 (assert (not (can-globalize-p 'con
))))
80 (with-test (:name
:proclaim-global
)
81 (assert (can-globalize-p '.bar.
)))
84 (with-test (:name
:global-does-not-imply-always-bound
)
89 (cell-error-name e
))))))
91 (with-test (:name
:set-global
)
95 (assert (= 123 (bar1))))
97 (with-test (:name
:cannot-bind-globals
)
100 (eval* '(let ((.bar.
6)) .bar.
))
104 (funcall (compile nil
`(lambda ()
105 (let ((.bar.
5)) .bar.
))))
108 (with-test (:name
:cannot-define-globals-as-symmacs
)
111 (eval* '(define-symbol-macro .bar.
0))
115 (eval* `(symbol-macrolet ((.bar.
11)) .bar.
))
119 (funcall (compile nil
`(lambda ()
120 (symbol-macrolet ((.bar.
11)) .bar.
))))
123 ;;; Cannot proclaim or declare a global as special
124 (with-test (:name
:cannot-declare-global-special
)
126 (handler-case (proclaim '(special .bar.
666))
130 (funcall (compile nil
`(lambda ()
131 (declare (special .bar.
))
135 (handler-case (eval `(locally (declare (special .bar.
)) .bar.
))
138 ;;; Dead globals get bound checks
139 (declaim (global this-is-unbound
))
140 (with-test (:name
:dead-unbound-global
)
143 (funcall (compile nil
150 (defun compile-form (form)
151 (let* ((lisp "defglobal-impure-tmp.lisp"))
154 (with-open-file (f lisp
:direction
:output
)
156 (multiple-value-bind (fasl warn fail
) (compile-file lisp
)
157 (declare (ignore warn
))
159 (error "compiling ~S failed" form
))
161 (ignore-errors (delete-file lisp
)))))
164 (with-test (:name
:defconstant-evals
)
166 (fasl (compile-form `(defglobal .counter-1.
(incf *counter
*)))))
167 (assert (= 1 *counter
*))
168 (assert (= 1 (symbol-value '.counter-1.
)))
169 (assert (eq :global
(sb-int:info
:variable
:kind
'.counter-1.
)))
172 (ignore-errors (delete-file fasl
)))
173 (assert (= 1 *counter
*))
174 (assert (= 1 (symbol-value '.counter-1.
))))
176 (set '.counter-2.
:bound
)
178 (fasl (compile-form `(defglobal .counter-2.
(incf *counter
*)))))
179 (assert (= 0 *counter
*))
180 (assert (eq :bound
(symbol-value '.counter-2.
)))
181 (assert (eq :global
(sb-int:info
:variable
:kind
'.counter-2.
)))
184 (ignore-errors (delete-file fasl
)))
185 (assert (= 0 *counter
*))
186 (assert (eq :bound
(symbol-value '.counter-2.
))))
188 ;; This is a *really* dirty trick...
190 (fasl (let ((.counter-3.
:nasty
))
191 (declare (special .counter-3.
))
192 (compile-form `(defglobal .counter-3.
(incf *counter
*))))))
193 (assert (= 0 *counter
*))
194 (assert (not (boundp '.counter-3.
)))
195 (assert (eq :global
(sb-int:info
:variable
:kind
'.counter-3.
)))
198 (ignore-errors (delete-file fasl
)))
199 (assert (= 1 *counter
*))
200 (assert (= 1 (symbol-value '.counter-3.
)))))
202 (with-test (:name
:defglobal-refers-to-defglobal
)
203 (let ((fasl (compile-form `(progn
204 (defglobal **global-1
** :fii
)
205 (defglobal **global-2
** **global-1
**)))))
207 (assert (eq (symbol-value '**global-1
**) (symbol-value '**global-2
**)))
208 (assert (eq :fii
(symbol-value '**global-1
**)))))