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 assert-foo-not-checked (fun)
21 (let* ((marker (sb-kernel:make-unbound-marker
))
23 (assert (eq marker
(funcall fun
)))))
25 (defun assert-foo-checked (fun)
26 (let* ((marker (sb-kernel:make-unbound-marker
))
32 (assert (eq '*foo
* (cell-error-name e
)))
35 (with-test (:name
:unbound-cannot-be-always-bound
)
36 (assert-error (proclaim '(sb-ext:always-bound
*foo
*))))
39 (proclaim '(sb-ext:always-bound
*foo
*))
42 (declare (optimize (safety 3)))
44 ;; When run interpreted, FOO-SAFE cannot help but check BOUNDP on *foo*
45 ;; so the assertion would fail.
48 (with-test (:name
:always-bound-elides-boundness-checking
)
49 (assert-foo-not-checked #'foo-safe
))
51 (with-test (:name
:cannot-unbind-always-bound
)
57 (defun can-globalize-p (x)
59 (progn (proclaim `(sb-ext:global
,x
)) t
)
62 (with-test (:name
:cannot-proclaim-special-global
)
63 (assert (not (can-globalize-p '*foo
*))))
65 (define-symbol-macro sm
42)
66 (with-test (:name
:cannot-proclaim-symbol-macro-global
)
67 (assert (not (can-globalize-p 'sm
))))
70 (with-test (:name
:cannot-proclaim-constant-global
)
71 (assert (not (can-globalize-p 'con
))))
73 (with-test (:name
:proclaim-global
)
74 (assert (can-globalize-p '.bar.
)))
77 (with-test (:name
:global-does-not-imply-always-bound
)
82 (cell-error-name e
))))))
84 (with-test (:name
:set-global
)
88 (assert (= 123 (bar1))))
90 (with-test (:name
:cannot-bind-globals
)
91 (assert-error (eval* '(let ((.bar.
6)) .bar.
)))
92 (multiple-value-bind (fun failure-p
)
93 (checked-compile `(lambda ()
94 (let ((.bar.
5)) .bar.
))
97 (assert-error (funcall fun
))))
99 (with-test (:name
:cannot-define-globals-as-symmacs
)
100 (assert-error (eval* '(define-symbol-macro .bar.
0)))
101 (assert-error (eval* `(symbol-macrolet ((.bar.
11)) .bar.
)))
102 (multiple-value-bind (fun failure-p
)
103 (checked-compile `(lambda ()
104 (symbol-macrolet ((.bar.
11)) .bar.
))
107 (assert-error (funcall fun
))))
109 ;;; Cannot proclaim or declare a global as special
110 (with-test (:name
:cannot-declare-global-special
)
111 (assert-error (proclaim '(special .bar.
666)))
112 (assert-error (eval `(locally (declare (special .bar.
)) .bar.
)))
113 (multiple-value-bind (fun failure-p
)
114 (checked-compile `(lambda ()
115 (declare (special .bar.
))
119 (assert-error (funcall fun
))))
121 ;;; Dead globals get bound checks
122 (declaim (global this-is-unbound
))
123 (with-test (:name
:dead-unbound-global
)
124 (let ((fun (checked-compile '(lambda ()
127 (assert-error (funcall fun
) unbound-variable
)))
129 (defun compile-form (form)
130 (let* ((lisp "defglobal-impure-tmp.lisp"))
133 (with-open-file (f lisp
:direction
:output
)
135 (multiple-value-bind (fasl warn fail
) (compile-file lisp
)
136 (declare (ignore warn
))
138 (error "compiling ~S failed" form
))
140 (ignore-errors (delete-file lisp
)))))
143 (with-test (:name
:defconstant-evals
)
145 (fasl (compile-form `(defglobal .counter-1.
(incf *counter
*)))))
146 (assert (= 1 *counter
*))
147 (assert (= 1 (symbol-value '.counter-1.
)))
148 (assert (eq :global
(sb-int:info
:variable
:kind
'.counter-1.
)))
151 (ignore-errors (delete-file fasl
)))
152 (assert (= 1 *counter
*))
153 (assert (= 1 (symbol-value '.counter-1.
))))
155 (set '.counter-2.
:bound
)
157 (fasl (compile-form `(defglobal .counter-2.
(incf *counter
*)))))
158 (assert (= 0 *counter
*))
159 (assert (eq :bound
(symbol-value '.counter-2.
)))
160 (assert (eq :global
(sb-int:info
:variable
:kind
'.counter-2.
)))
163 (ignore-errors (delete-file fasl
)))
164 (assert (= 0 *counter
*))
165 (assert (eq :bound
(symbol-value '.counter-2.
))))
167 ;; This is a *really* dirty trick...
169 (fasl (let ((.counter-3.
:nasty
))
170 (declare (special .counter-3.
))
171 (compile-form `(defglobal .counter-3.
(incf *counter
*))))))
172 (assert (= 0 *counter
*))
173 (assert (not (boundp '.counter-3.
)))
174 (assert (eq :global
(sb-int:info
:variable
:kind
'.counter-3.
)))
177 (ignore-errors (delete-file fasl
)))
178 (assert (= 1 *counter
*))
179 (assert (= 1 (symbol-value '.counter-3.
)))))
181 (with-test (:name
:defglobal-refers-to-defglobal
)
182 (let ((fasl (compile-form `(progn
183 (defglobal **global-1
** :fii
)
184 (defglobal **global-2
** **global-1
**)))))
186 (assert (eq (symbol-value '**global-1
**) (symbol-value '**global-2
**)))
187 (assert (eq :fii
(symbol-value '**global-1
**)))))