-
Notifications
You must be signed in to change notification settings - Fork 0
/
defglobal.impure.lisp
187 lines (161 loc) · 6.12 KB
/
defglobal.impure.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
;;;; DEFGLOBAL and related tests
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.
(proclaim '(special *foo*))
(defun eval* (form)
(let ((*evaluator-mode* :interpret))
(eval form)))
(defun assert-foo-not-checked (fun)
(let* ((marker (sb-kernel:make-unbound-marker))
(*foo* marker))
(assert (eq marker (funcall fun)))))
(defun assert-foo-checked (fun)
(let* ((marker (sb-kernel:make-unbound-marker))
(*foo* marker))
(assert (eq :error
(handler-case
(funcall fun)
(unbound-variable (e)
(assert (eq '*foo* (cell-error-name e)))
:error))))))
(with-test (:name :unbound-cannot-be-always-bound)
(assert-error (proclaim '(sb-ext:always-bound *foo*))))
(set '*foo* t)
(proclaim '(sb-ext:always-bound *foo*))
(defun foo-safe ()
(declare (optimize (safety 3)))
*foo*)
;; When run interpreted, FOO-SAFE cannot help but check BOUNDP on *foo*
;; so the assertion would fail.
(compile 'foo-safe)
(with-test (:name :always-bound-elides-boundness-checking)
(assert-foo-not-checked #'foo-safe))
(with-test (:name :cannot-unbind-always-bound)
(assert (eq :oops
(handler-case
(makunbound '*foo*)
(error () :oops)))))
(defun can-globalize-p (x)
(handler-case
(progn (proclaim `(sb-ext:global ,x)) t)
(error () nil)))
(with-test (:name :cannot-proclaim-special-global)
(assert (not (can-globalize-p '*foo*))))
(define-symbol-macro sm 42)
(with-test (:name :cannot-proclaim-symbol-macro-global)
(assert (not (can-globalize-p 'sm))))
(defconstant con 13)
(with-test (:name :cannot-proclaim-constant-global)
(assert (not (can-globalize-p 'con))))
(with-test (:name :proclaim-global)
(assert (can-globalize-p '.bar.)))
(defun bar1 () .bar.)
(with-test (:name :global-does-not-imply-always-bound)
(assert (eq '.bar.
(handler-case
(bar1)
(unbound-variable (e)
(cell-error-name e))))))
(with-test (:name :set-global)
(setf .bar. 7)
(assert (= 7 (bar1)))
(setf .bar. 123)
(assert (= 123 (bar1))))
(with-test (:name :cannot-bind-globals)
(assert-error (eval* '(let ((.bar. 6)) .bar.)))
(multiple-value-bind (fun failure-p)
(checked-compile `(lambda ()
(let ((.bar. 5)) .bar.))
:allow-failure t)
(assert failure-p)
(assert-error (funcall fun))))
(with-test (:name :cannot-define-globals-as-symmacs)
(assert-error (eval* '(define-symbol-macro .bar. 0)))
(assert-error (eval* `(symbol-macrolet ((.bar. 11)) .bar.)))
(multiple-value-bind (fun failure-p)
(checked-compile `(lambda ()
(symbol-macrolet ((.bar. 11)) .bar.))
:allow-failure t)
(assert failure-p)
(assert-error (funcall fun))))
;;; Cannot proclaim or declare a global as special
(with-test (:name :cannot-declare-global-special)
(assert-error (proclaim '(special .bar. 666)))
(assert-error (eval `(locally (declare (special .bar.)) .bar.)))
(multiple-value-bind (fun failure-p)
(checked-compile `(lambda ()
(declare (special .bar.))
.bar.)
:allow-failure t)
(assert failure-p)
(assert-error (funcall fun))))
;;; Dead globals get bound checks
(declaim (global this-is-unbound))
(with-test (:name :dead-unbound-global)
(let ((fun (checked-compile '(lambda ()
this-is-unbound
42))))
(assert-error (funcall fun) unbound-variable)))
(defun compile-form (form)
(let* ((lisp "defglobal-impure-tmp.lisp"))
(unwind-protect
(progn
(with-open-file (f lisp :direction :output)
(prin1 form f))
(multiple-value-bind (fasl warn fail) (compile-file lisp)
(declare (ignore warn))
(when fail
(error "compiling ~S failed" form))
fasl))
(ignore-errors (delete-file lisp)))))
(defvar *counter*)
(with-test (:name :defconstant-evals)
(let* ((*counter* 0)
(fasl (compile-form `(defglobal .counter-1. (incf *counter*)))))
(assert (= 1 *counter*))
(assert (= 1 (symbol-value '.counter-1.)))
(assert (eq :global (sb-int:info :variable :kind '.counter-1.)))
(unwind-protect
(load fasl)
(ignore-errors (delete-file fasl)))
(assert (= 1 *counter*))
(assert (= 1 (symbol-value '.counter-1.))))
(set '.counter-2. :bound)
(let* ((*counter* 0)
(fasl (compile-form `(defglobal .counter-2. (incf *counter*)))))
(assert (= 0 *counter*))
(assert (eq :bound (symbol-value '.counter-2.)))
(assert (eq :global (sb-int:info :variable :kind '.counter-2.)))
(unwind-protect
(load fasl)
(ignore-errors (delete-file fasl)))
(assert (= 0 *counter*))
(assert (eq :bound (symbol-value '.counter-2.))))
;; This is a *really* dirty trick...
(let* ((*counter* 0)
(fasl (let ((.counter-3. :nasty))
(declare (special .counter-3.))
(compile-form `(defglobal .counter-3. (incf *counter*))))))
(assert (= 0 *counter*))
(assert (not (boundp '.counter-3.)))
(assert (eq :global (sb-int:info :variable :kind '.counter-3.)))
(unwind-protect
(load fasl)
(ignore-errors (delete-file fasl)))
(assert (= 1 *counter*))
(assert (= 1 (symbol-value '.counter-3.)))))
(with-test (:name :defglobal-refers-to-defglobal)
(let ((fasl (compile-form `(progn
(defglobal **global-1** :fii)
(defglobal **global-2** **global-1**)))))
(load fasl)
(assert (eq (symbol-value '**global-1**) (symbol-value '**global-2**)))
(assert (eq :fii (symbol-value '**global-1**)))))