Decentralize per-thread initial special bindings.
[sbcl.git] / src / code / target-error.lisp
blobeca83be46773d287516148576e80da512bb74cec
1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!KERNEL")
15 (defun muffle-warning-p (warning)
16 (declare (special *muffled-warnings*))
17 (typep warning *muffled-warnings*))
19 ;; Host lisp does not need a value for this, so start it out as NIL.
20 (defglobal **initial-handler-clusters** nil)
22 ;;; Each cluster is an alist of the form
23 ;;;
24 ;;; ((TYPE-TEST1 . HANDLER1) (TYPE-TEST2 . HANDLER2) ...)
25 ;;;
26 ;;; where TYPE-TESTN are functions of one argument which test a given
27 ;;; condition instance for the type required by the corresponding
28 ;;; HANDLERN. HANDLERN are function designators.
29 ;;;
30 ;;; Newly established handlers are added at the beginning of the
31 ;;; list. Elements to the left of the alist take precedence over
32 ;;; elements to the right.
33 ;;;
34 ;;; Lists to which *HANDLER-CLUSTERS* is bound generally have dynamic
35 ;;; extent.
36 #!+sb-thread (!define-thread-local *handler-clusters* **initial-handler-clusters**)
37 #!-sb-thread (defvar *handler-clusters* **initial-handler-clusters**)
39 ;;; a list of lists of currently active RESTART instances. maintained
40 ;;; by RESTART-BIND.
41 (!define-thread-local *restart-clusters* nil)
43 (defun !target-error-cold-init ()
44 (setq **initial-handler-clusters**
45 `(((,(find-classoid-cell 'warning :create t)
47 ,(named-lambda "MAYBE-MUFFLE" (warning)
48 (when (muffle-warning-p warning)
49 (muffle-warning warning)))))))
50 ;;; If multithreaded, *HANDLER-CLUSTERS* is #<unbound> at this point.
51 ;;; This SETQ assigns to TLS since the value is not no-tls-value-marker.
52 (setq *handler-clusters* **initial-handler-clusters**))
54 (defmethod print-object ((restart restart) stream)
55 (if *print-escape*
56 (print-unreadable-object (restart stream :type t :identity t)
57 (prin1 (restart-name restart) stream))
58 (restart-report restart stream)))
60 (setf (fdocumentation 'restart-name 'function)
61 "Return the name of the given restart object.")
63 (defun restart-report (restart stream)
64 (if (restart-report-function restart)
65 (funcall (truly-the function (restart-report-function restart))
66 stream)
67 (prin1 (or (restart-name restart)
68 restart)
69 stream)))
71 (defvar *restart-test-stack* nil)
73 ;; Call FUNCTION with all restarts in the current dynamic environment,
74 ;; 1) that are associated to CONDITION (when CONDITION is NIL, all
75 ;; restarts are processed)
76 ;; 2) and for which the restart test returns non-NIL for CONDITION.
77 ;; When CALL-TEST-P is non-NIL, all restarts are processed.
78 (defun map-restarts (function &optional condition (call-test-p t))
79 (declare (function function))
80 (let ((stack *restart-test-stack*))
81 (dolist (restart-cluster *restart-clusters*)
82 (dolist (restart restart-cluster)
83 (when (and (or (not condition)
84 (null (restart-associated-conditions restart))
85 (memq condition (restart-associated-conditions restart)))
86 ;; A call to COMPUTE-RESTARTS -- from an error,
87 ;; from user code, whatever -- inside the test
88 ;; function would cause infinite recursion here, so
89 ;; we disable each restart using
90 ;; *restart-test-stack* for the duration of the
91 ;; test call.
92 (not (memq restart stack))
93 (or (not call-test-p)
94 (let ((*restart-test-stack* (cons restart stack)))
95 (declare (truly-dynamic-extent *restart-test-stack*))
96 (funcall (restart-test-function restart) condition))))
97 (funcall function restart))))))
99 (defun compute-restarts (&optional condition)
100 "Return a list of all the currently active restarts ordered from most recently
101 established to less recently established. If CONDITION is specified, then only
102 restarts associated with CONDITION (or with no condition) will be returned."
103 (collect ((result))
104 (map-restarts (lambda (restart) (result restart)) condition)
105 (result)))
107 (defun %find-restart (identifier condition &optional (call-test-p t))
108 (flet ((eq-restart-p (restart)
109 (when (eq identifier restart)
110 (return-from %find-restart restart)))
111 (named-restart-p (restart)
112 (when (eq identifier (restart-name restart))
113 (return-from %find-restart restart))))
114 ;; KLUDGE: can the compiler infer this dx automatically?
115 (declare (truly-dynamic-extent #'eq-restart-p #'named-restart-p))
116 (if (typep identifier 'restart)
117 ;; The code under #+previous-... below breaks the abstraction
118 ;; introduced by MAP-RESTARTS, but is about twice as
119 ;; fast as #+equivalent-... . Also, it is a common case due to
121 ;; (INVOKE-RESTART RESTART)
122 ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
123 ;; -> (FIND-RESTART RESTART)
125 ;; However, both #+previous-... and #+equivalent-... may be
126 ;; wrong altogether because of
127 ;; https://bugs.launchpad.net/sbcl/+bug/774410:
128 ;; The behavior expected in that report can be achieved by the
129 ;; following line (which is, of course, the slowest of all
130 ;; possibilities):
131 (map-restarts #'eq-restart-p condition call-test-p)
133 #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
134 (map-restarts #'eq-restart-p nil nil)
136 #+previous-behavior--fastest-but-see-bug-774410
137 (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
138 identifier)
140 (map-restarts #'named-restart-p condition call-test-p))))
142 (defun find-restart (identifier &optional condition)
143 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
144 then the innermost applicable restart with that name is returned. If IDENTIFIER
145 is a restart, it is returned if it is currently active. Otherwise NIL is
146 returned. If CONDITION is specified and not NIL, then only restarts associated
147 with that condition (or with no condition) will be returned."
148 ;; Calls MAP-RESTARTS such that restart test functions are
149 ;; respected.
150 (%find-restart identifier condition))
152 ;;; helper for the various functions which are ANSI-spec'ed to do
153 ;;; something with a restart or signal CONTROL-ERROR if there is none
154 (defun find-restart-or-control-error (identifier &optional condition (call-test-p t))
155 (declare (optimize allow-non-returning-tail-call))
156 (or (%find-restart identifier condition call-test-p)
157 (error 'simple-control-error
158 :format-control "No restart ~S is active~@[ for ~S~]."
159 :format-arguments (list identifier condition))))
161 (defun invoke-restart (restart &rest values)
162 "Calls the function associated with the given restart, passing any given
163 arguments. If the argument restart is not a restart or a currently active
164 non-nil restart name, then a CONTROL-ERROR is signalled."
165 (/show "entering INVOKE-RESTART" restart)
166 ;; The following code calls MAP-RESTARTS (through
167 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
168 ;; test functions are respected when RESTART is a symbol, but not
169 ;; when RESTART is a RESTART instance.
171 ;; Without disabling test functions for the RESTART instance case,
172 ;; the following problem would arise:
174 ;; (restart-case
175 ;; (handler-bind
176 ;; ((some-condition (lambda (c)
177 ;; (invoke-restart (find-restart 'foo c)) ; a)
178 ;; (invoke-restart 'foo) ; b)
179 ;; )))
180 ;; (signal 'some-condition))
181 ;; (foo ()
182 ;; :test (lambda (c) (typep c 'some-condition))))
184 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
185 ;; cannot supply the condition instance needed by the test. In case
186 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
187 ;; condition instance. As a result, the restart would be impossible
188 ;; the invoke.
189 (let ((real-restart (find-restart-or-control-error
190 restart nil (symbolp restart))))
191 (apply (restart-function real-restart) values)))
193 (defun interactive-restart-arguments (real-restart)
194 (let ((interactive-function (restart-interactive-function real-restart)))
195 (if interactive-function
196 (funcall interactive-function)
197 '())))
199 (defun invoke-restart-interactively (restart)
200 "Calls the function associated with the given restart, prompting for any
201 necessary arguments. If the argument restart is not a restart or a
202 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
203 ;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
204 ;; see comment in INVOKE-RESTART.
205 (let* ((real-restart (find-restart-or-control-error
206 restart nil (symbolp restart)))
207 (args (interactive-restart-arguments real-restart)))
208 (apply (restart-function real-restart) args)))
210 ;;; To reduce expansion size of RESTART-CASE
211 (defun with-simple-condition-restarts (function cerror-arg datum &rest arguments)
212 (let ((sb!debug:*stack-top-hint* (or sb!debug:*stack-top-hint*
213 'with-simple-condition-restarts))
214 (condition (apply #'coerce-to-condition datum
215 (case function
216 (warn 'simple-warning)
217 (signal 'simple-condition)
218 (t 'simple-error))
219 function
220 arguments)))
221 (with-condition-restarts condition (car *restart-clusters*)
222 (if (eq function 'cerror)
223 (cerror cerror-arg condition)
224 (funcall function condition)))))
227 (defun assert-error (assertion &optional args-and-values places datum &rest arguments)
228 (let ((cond (if datum
229 (apply #'coerce-to-condition
230 datum 'simple-error 'error arguments)
231 (make-condition
232 'simple-error
233 :format-control "~@<The assertion ~S failed~:[.~:; ~
234 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
235 :format-arguments (list assertion args-and-values)))))
236 (restart-case
237 (error cond)
238 (continue ()
239 :report (lambda (stream)
240 (format stream "Retry assertion")
241 (if places
242 (format stream " with new value~P for ~{~S~^, ~}."
243 (length places) places)
244 (format stream ".")))
245 nil))))
247 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
248 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
249 ;;; and by CHECK-TYPE.
250 (defun read-evaluated-form (&optional (prompt-control nil promptp)
251 &rest prompt-args)
252 (apply #'format *query-io*
253 (if promptp prompt-control "~&Type a form to be evaluated: ")
254 prompt-args)
255 (finish-output *query-io*)
256 (list (eval (read *query-io*))))
258 ;;; Same as above but returns multiple values
259 (defun mv-read-evaluated-form (&optional (prompt-control nil promptp)
260 &rest prompt-args)
261 (apply #'format *query-io*
262 (if promptp prompt-control "~&Type a form to be evaluated: ")
263 prompt-args)
264 (finish-output *query-io*)
265 (multiple-value-list (eval (read *query-io*))))
267 (defun check-type-error (place place-value type &optional type-string)
268 (let ((condition
269 (make-condition
270 'simple-type-error
271 :datum place-value
272 :expected-type type
273 :format-control
274 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
275 :format-arguments (list place place-value type-string type))))
276 (restart-case (error condition)
277 (store-value (value)
278 :report (lambda (stream)
279 (format stream "Supply a new value for ~S." place))
280 :interactive read-evaluated-form
281 value))))
283 (defun etypecase-failure (value keys)
284 (declare (optimize allow-non-returning-tail-call))
285 (error 'case-failure
286 :name 'etypecase
287 :datum value
288 :expected-type `(or ,@keys)
289 :possibilities keys))
291 (defun ecase-failure (value keys)
292 (declare (optimize allow-non-returning-tail-call))
293 (error 'case-failure
294 :name 'ecase
295 :datum value
296 :expected-type `(member ,@keys)
297 :possibilities keys))
299 (defun case-body-error (name keyform keyform-value expected-type keys)
300 (restart-case
301 (error 'case-failure
302 :name name
303 :datum keyform-value
304 :expected-type expected-type
305 :possibilities keys)
306 (store-value (value)
307 :report (lambda (stream)
308 (format stream "Supply a new value for ~S." keyform))
309 :interactive read-evaluated-form
310 value)))